Ask Question
2 February, 23:24

Write a program that generates 10 random doubles, all between 1 and 11, and writes them to a text file, one number per line. Then write another program that reads the text file and displays all the doubles and their sum accurate to two decimal places.

+2
Answers (1)
  1. 3 February, 00:09
    0
    WriteDoubleValuesFile. java

    import java. io. File;

    import java. io. FileNotFoundException;

    import java. io. PrintWriter;

    import java. util. Random;

    public class WriteDoubleValuesFile {

    public static void main (String[] args) throws FileNotFoundException {

    File file = new File ("doubleFile");

    PrintWriter pw = new PrintWriter (file);

    double start = 1;

    double end = 11;

    Random random = new Random ();

    for (int i=0; i<10; i++) {

    double randNum = start + (random. nextDouble () * (end - start));

    pw. write (randNum+"/n");

    }

    System. out. println ("File has been geenrated");

    pw. flush ();

    pw. close ();

    }

    }

    ReadDoubleValuesFile. java

    import java. io. File;

    import java. io. FileNotFoundException;

    import java. util. Random;

    import java. util. Scanner;

    public class ReadDoubleValuesFile {

    public static void main (String[] args) throws FileNotFoundException {

    File file = new File ("doubleFile");

    Scanner scan = new Scanner (file);

    double sum = 0;

    while (scan. hasNextDouble ()) {

    double value = scan. nextDouble ();

    System. out. println (value);

    sum = sum + value;

    }

    System. out. println ("The total is "+sum);

    scan. close ();

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that generates 10 random doubles, all between 1 and 11, and writes them to a text file, one number per line. Then write ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers