Java Assignment. The assignment is to create a program to create a program that
ID: 669771 • Letter: J
Question
Java Assignment.
The assignment is to create a program to create a program that adds the numbers 1/2 to itself and 1/3 to itself a large number of times using both types float and double. It is to then compare the values of adding the numbers to multiplying 1/2 and 1/3 times the number of times they were added.
The program must first add 1/2 and 1/3 using type float, and compare to the value obtain using multiplication instead of addition. It is to do this for the following values:
1.000
10,000
100,000
1,000,000
10,000,000
100,000,000
1,000,000,000
The program should do this using two nested loops. The outer loop determines the number of times to add and starts at a 1000 and increments by multiples of 10 up to a 1,000,000,000. The inner loop does the addition of the numbers 1/2 and 1/3. When the inner loop completes, output the two sums and the two products, and the differences between the sums and the products to check for rounding errors. Then continue the outer loop. Remember to clear your variables for the sums before the start of the inner loop.
Next the program repeats the above except that it uses type double instead of type float for the variables. This will require a second set of nested loops and different variables.
Explanation / Answer
class cmp
{
public static void main(String args[])
{
float ad;
float mu;
double ad1,mu1;
int i,j;
for(i=1000;i<1000000000;i=i+10)
{
ad=0;
mu=1;
for(j=1;j<=i;j++)
{
ad=.5+.33;
mu=.5*.33;
}
System.out.println("Sum is " + ad);
System.out.println("Product is " + mu);
}
for(i=1000;i<1000000000;i=i+10)
{
ad1=0;
mu1=1;
for(j=1;j<=i;j++)
{
ad1=.5+.33;
mu1=.5*.33;
}
System.out.println("Sum is " + ad1);
System.out.println("Product is " + mu1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.