Write a java program, (1)using a random number generator to produce floating poi
ID: 3543194 • Letter: W
Question
Write a java program, (1)using a random number generator to produce floating point(double) number, use this number to create 4 floating point numbers: the number cutoff at the 3rd,4th,5th, and 6th decimal place.(2)Adjust the random number to round at the 3rd,4th,5th , and 6th decimal place.(3) Use an accumulator to capture each number after it has been calculated.(4)use an accumulator to capture the actual sum of the random numbers.(5) repeat steps 1 through 4 for 10,100,1000, and 10000 repitions. upon completion of each loop, print the real sum, each of the 8 calculated/truncated.
Explanation / Answer
please rate - thanks
any questions - ask
public class main
{public static void main(String args[])
{double n,temp,sum,roundSum3,roundSum4,roundSum5,roundSum6,cutSum3,cutSum4,cutSum5,cutSum6;
int i,j=10;
for(j=10;j<=10000;j*=j)
{System.out.println(j +" iterations");
sum=0;
roundSum3=0;
cutSum3=0;
roundSum4=0;
cutSum4=0;
roundSum5=0;
cutSum5=0;
roundSum6=0;
cutSum6=0;
for(i=0;i<j;i++)
{n=Math.random();
//if(j==10)
// System.out.println(n);
sum+=n;
cutSum3=cutSum3+(int)(n*1000.)/1000.;
cutSum4=cutSum4+(int)(n*10000.)/10000.;
cutSum5=cutSum5+(int)(n*10000.)/10000.;
cutSum6=cutSum6+(int)(n*1000000.)/1000000.;
roundSum3=roundSum3+(int)((n+.0005)*1000.)/1000.;
roundSum4=roundSum4+(int)((n+.00005)*10000.)/10000.;
roundSum5=roundSum5+(int)((n+.000005)*10000.)/10000.;
roundSum6=roundSum6+(int)((n+.0000005)*1000000.)/1000000.;
}
System.out.printf("sum of the numbers as generated: %f ",sum);
System.out.printf("sum of the numbers rounded 3 places: %.3f ",roundSum3);
System.out.printf("sum of the numbers rounded 4 places: %.4f ",roundSum4);
System.out.printf("sum of the numbers rounded 5 places: %.5f ",roundSum5);
System.out.printf("sum of the numbers rounded 6 places: %.6f ",roundSum6);
System.out.printf("sum of the numbers truncated after 3 places :%.3f ",cutSum3);
System.out.printf("sum of the numbers truncated after 4 places : %.4f ",cutSum4);
System.out.printf("sum of the numbers truncated after 5 places : %.5f ",cutSum5);
System.out.printf("sum of the numbers truncated after 6 places : %.6f ",cutSum6);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.