Question

In Java. Write a program in a single file that: Main: Creates 10 random doubles, all...

In Java. Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls a class that writes 10 random doubles to a text file, one number per line. Calls a class that reads the text file and displays all the doubles and their sum accurate to two decimal places. There has to be three classes, main for create random numbers, one to write , one to read all in the same file

SAMPLE OUTPUT

10.6269119604172
2.737790338909455
5.427925738865128
1.3742058065472509
1.1858700262498836
4.180391276485228
4.910969998930675
5.710858234343958
7.790857007373052
3.1806714736219543

The total is 47.13

The program runs, the main question I have is. what does my professor mean by "in a single file"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the below program, all the classes (Writer, Reader and Main) are written in the same file, "Main.java" . You can run the program by running Main class.

CODE :

import java.io.*;
import java.util.Random;
import java.util.Scanner;

class Writer{
    public void write(double arr[]) throws IOException {
        //Writer to write to file
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("doubles.txt")));
        //Iterate through double array
        for(int i=0;i<arr.length;i++){
            //Write to file
            writer.write(String.valueOf(arr[i]));
            //Write to next line
            if(i!=arr.length-1){
                writer.write("\n");
            }
        }
        writer.flush();
        writer.close();
    }
}

class Reader{
    public double getSum() throws IOException {
        //Reader to read file
        BufferedReader br = new BufferedReader(new FileReader(new File("doubles.txt")));
        String str;
        double ans = 0;
        //Read all lines
        while((str=br.readLine())!=null){
            //Find sum
            ans = ans + Double.parseDouble(str);
        }
        return ans;
    }
}

class Main{
    public static void main(String[] args) throws IOException {
        Random r = new Random();
        double arr[] = new double[10];
        //Generate 10 randoms from 1 to 11
        for(int i=0;i<10;i++){
            arr[i] = 10*r.nextDouble()+1;
            System.out.println(arr[i]);
        }
        Writer w = new Writer();
        w.write(arr);
        Reader rd = new Reader();
        System.out.println("Total is "+rd.getSum());
    }
}

OUTPUT :

Add a comment
Know the answer?
Add Answer to:
In Java. Write a program in a single file that: Main: Creates 10 random doubles, all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please help me with, Write a program in java that generates 10 random doubles, all between...

    please help me with, Write a program in java 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. SAMPLE OUTPUT 10.6269119604172 2.737790338909455 5.427925738865128 1.3742058065472509 1.1858700262498836 4.180391276485228 4.910969998930675 5.710858234343958 7.790857007373052 3.1806714736219543 The total is 47.13

  • Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat...

    Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat and prints out the sum of the positive values from the file. Assume that the file contains only numeric values. You must output an error if the file can't be opened. You must write the complete program and the output when the program runs successfully must conform exactly to the sample output. Bonus ( 5 pts). Output an error if the file contains non-numeric...

  • Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write...

    Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write the odd numbers 1 to 99 into the file. Close the file. Using Scanner, open the numbers.txt file and read in the numbers. Add them all up and print the total. Close the file.

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • 1.Write a python program that writes a series of random numbers to a file named random.txt....

    1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold. 2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data: I. The total of the numbers II. The number of random numbers read from the file

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • How to write a Java program that reads the file "input.txt" and writes all even values...

    How to write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt." Sample input 10 12 1 3 5 34 2 5 7 9 44

  • PYTHON 1- Write a program that creates a dictionary of the English words for all single...

    PYTHON 1- Write a program that creates a dictionary of the English words for all single digit numbers as follows: 1: 'one', 2: 'two', 3: 'three', etc. 2- Write a function called numConvert that receives the above dictionary and an integer number as parameters, and prints the English words for all the number's digits. For example, if the function receives 2311, it will print: two three one one 3- Test your program by adding a main function, which calls numConvert....

  • please put the comment each line, make sure i will have output too. write a program,...

    please put the comment each line, make sure i will have output too. write a program, Summarize (Summarize.java), containing the main() method, that first writes 10,000 random positive double type numbers, to the full accuracy of the number (15/16 decimal places), to a text file named DataValues.txt, one number per line. The program must then close that file, and reopen it for reading. Read back the values from the file and write the following information to a file named Summary.txt:...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT