0% found this document useful (0 votes)
63 views4 pages

Lab 03 Sample Solutions

This document contains solutions to a CSI141 lab assignment with multiple parts. Part A calculates mathematical expressions. Part B demonstrates using Math methods like pow and nextDouble. Part C sums the digits of a number by extracting them using modulo and division. Part D shows a Java program to calculate gravitational force between two bodies. Part E displays the value of an investment over 5 years by calculating it with compound interest.

Uploaded by

Offie Mphale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views4 pages

Lab 03 Sample Solutions

This document contains solutions to a CSI141 lab assignment with multiple parts. Part A calculates mathematical expressions. Part B demonstrates using Math methods like pow and nextDouble. Part C sums the digits of a number by extracting them using modulo and division. Part D shows a Java program to calculate gravitational force between two bodies. Part E displays the value of an investment over 5 years by calculating it with compound interest.

Uploaded by

Offie Mphale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSI141_Lab_03_9A.

M Solution

PART A

a). ((3/4)*(4*y))- (y*z)

b). (a*Math.pow(x,3)) + 5

c). ((3*x)+1)/(x+8)

d). Math.cbrt(n) * Math.pow(e,Math.PI)

PART B

a). double pi_pi = Math.pow(Math.PI,2);


b). double number = kBoard.nextDouble();
PART C

N
23456

a 23456%10 = 6

b 23456/10 = 2345%10 = 5

c
23456/100 = 234%10 = 4

d
23456/1000 = 23%10 = 3

e
23456/10000 = 2

sum
6+5+4+3+2 = 20

The program sums up the values of a,b,c,d and e to 20.


PART D

/****************************************************************
* Filename: GravitationalForce.java
* @author Tallman Nkgau
* @version 1.0
* Lab Exercises: 3
*
* Program to read the masses of two bodies, the distance between them and display the gravitational
force between
* them.
* Compile: javac GravitationalForce.java
* Execute: java GravitationalForce
*/
import java.util.Scanner; // For keyboard input

public class GravitationalForce


{
public static void main(String[] args)
{
double m1; // mass of body 1
double m2; // mass of body 2
double r; // distance between body 1 and body 2
final double K; // Gravitational constant
K = (6.674*(Math.pow(10,-11)));
double F; // Gravitational force
Scanner in = new Scanner(System.in);//creating a Scanner object
System.out.printf("Enter mass of body 1: ");
m2 = (in.nextDouble());
System.out.printf("Enter mass of body 2: ");
m1 = in.nextDouble();
System.out.printf("Enter distance between bodies: ");
r = in.nextDouble();
F = K*((m1*m2)/(r*r));
System.out.printf("F = %2.2e\n", F);
}
}
PART E

/****************************************************************
* Filename: Investment.java
* @author Tallman Nkgau
* @version 1.0
* Lab Exercises: 3
*
* Program displays the value of your investment for the first five years
* Compile: javac Investment.java
* Execute: java Investment
*/
import java.util.Scanner;
public class Investment
{
public static void main (String[] args)
{
Scanner kBoard = new Scanner(System.in);
System.out.print("Please enter investment in pulas: ");
double P = kBoard.nextDouble(); //reading pulas from the keyboard
System.out.print("Please enter the interest rate: ");
double r = kBoard.nextDouble(); //reading percent interest from the keyboard
double firstYear = P*Math.pow((1 + r/100),1);
double secondYear = P*Math.pow((1 + r/100),2);
double thirdYear = P*Math.pow((1 + r/100),3);
double fourthYear = P*Math.pow((1 + r/100),4);
double fifthYear = P*Math.pow((1 + r/100),5);

System.out.printf("Investment: P%1.2f\n",P);
System.out.printf("Interest rate: %1.2f%1s\n",r,"%");
System.out.println("N(years) Total");
System.out.printf("1 %24.2f\n",firstYear);
System.out.printf("2 %24.2f\n",secondYear);
System.out.printf("3 %24.2f\n",thirdYear);
System.out.printf("4 %24.2f\n",fourthYear);
System.out.printf("5 %24.2f\n",fifthYear);

}
}

You might also like