• Post Reply Bookmark Topic Watch Topic
  • New Topic

random floating point and truncate rrors in java  RSS feed

 
atem frank
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write, compile, and run a Java program that: (1) uses a single line comment to
document your source code with your name and section number. This should be the
very first line of your program, (2) uses a println statement to print your name and
section number as the first line of your output, (3) 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. (4) adjust the
random number to round at the 3rd, 4th, 5th, and 6th decimal place. (5) use an
accumulator to capture each number after it has been calculated. (6) use an
accumulator to capture the actual sum of the random numbers. (7) repeat steps 3
through 6 for 10, 100, 1000, and 10000 repetitions. (8) upon completion of each
loop, print the real sum, each of the 8 calculated/truncated.
import java.util.Random;
import java.text.DecimalFormat;
import java.text.NumberFormat;

// Generate 10 random integers in the range 0..9. //
public class Lab3
{

public static void main(String[] args)
{
Random generator = new Random();
double num1, num2, num3, num4, num5;
DecimalFormat df2 = new DecimalFormat("0.###");
DecimalFormat df3 = new DecimalFormat("0.####");
DecimalFormat df4 = new DecimalFormat("0.#####");
DecimalFormat df5 = new DecimalFormat("0.#######");

num1 = generator.nextDouble();
num2 = generator.nextDouble();
num3 = generator.nextDouble();
num4 = generator.nextDouble();
NumberFormat cutoff3= NumberFormat.getInstance();
cutoff3.setMaximumFractionDigits(3);
String count1=cutoff3.format(num1);
System.out.println("number cutoff at 3rd: "+count1);

NumberFormat cutoff4 = NumberFormat.getInstance();
cutoff4.setMaximumFractionDigits(4);
String count2=cutoff4.format(num2);
System.out.println("number cutoff at 4th: "+count2);

NumberFormat cutoff5 = NumberFormat.getInstance();
cutoff5.setMaximumFractionDigits(5);
String count3=cutoff5.format(num3);
System.out.println("number cutoff at 5th: "+count3);

NumberFormat cutoff6 = NumberFormat.getInstance();
cutoff6.setMaximumFractionDigits(6);
String count4=cutoff6.format(num4);
System.out.println("number cutoff at 6th: "+count4);
System.out.println("round off at 3rd: " + df2.format(num1));
System.out.println("round off at 4th: " + df3.format(num2));
System.out.println("round off at 5th: " + df4.format(num3));
System.out.println("round off at 6th: " + df5.format(num4));
double counti = 0.0;
for (double i=1; i<=10; i++)
{
counti = counti + 1;
}
System.out.println(counti);




}

}
i have this so far, nut i dont know how to do step5,6 and 7.I am having a hard time trying to accumulate to capture each number, i know i have to initialize and then a for loop but i dont know how to go ahead with the question. i need help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!