Question

Can you write with comments please. Thank you. Write a Java Program that asks the user...

Can you write with comments please. Thank you.

Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

1)

// FoodRating.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FoodRating {

   public static void main(String[] args) throws IOException {
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       int students[] = new int[10];

       for (int i = 0; i < students.length; ) {
           while(true)
           {
               System.out.print("\nEnter Student#" + (i + 1) + " rating :");
               students[i] = sc.nextInt();
               if(students[i]<1 ||students[i]>5)
               {
                   System.out.println("** Invalid.Must be between 1-5 **");
                   continue;
               }
               else
               {
                   i++;
                   break;
               }
           }

       }

       FileWriter fw = new FileWriter(new File("responses.txt"));
       for (int i = 0; i < students.length; i++) {
           fw.write(students[i] + " ");
       }
       fw.close();

   }

}
_______________________

Output:


Enter Student#1 rating :5

Enter Student#2 rating :4

Enter Student#3 rating :3

Enter Student#4 rating :4

Enter Student#5 rating :4

Enter Student#6 rating :2

Enter Student#7 rating :1

Enter Student#8 rating :3

Enter Student#9 rating :4

Enter Student#10 rating :5

_____________________

// responses.txt (Output file)

5 4 3 4 4 2 1 3 4 5

_____________________

2)

// CalculateRatingFrequency.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CalculateRatingFrequency {

   public static void main(String[] args) throws FileNotFoundException {
       //Creating one-Dimensional array which hold frequencies
       int freq[]=new int[5];
       //Opening the input file
       Scanner sc=new Scanner(new File("responses.txt"));
      
       //reading the data from the file
       while(sc.hasNext())
       {
           freq[sc.nextInt()-1]++;
       }
      
       //Displaying the frequencies
       System.out.println("Number\tFrequency");
       System.out.println("------\t--------");
       for(int i=0;i<freq.length;i++)
       {
           System.out.println((i+1)+"\t\t"+freq[i]);
       }
      

   }

}
__________________________

Output:

Number   Frequency
------   --------
1       1
2       1
3       2
4       4
5       2

_______________Could you plz rate me well.Thank You

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Can you write with comments please. Thank you. Write a Java Program that asks the user...
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
  • 1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...

    1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods. 2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory. 3) Write a Java program to...

  • This assignment involves 2 Java programs 1) Write a Java program to read a CSV (Comma...

    This assignment involves 2 Java programs 1) Write a Java program to read a CSV (Comma separated values) text file. The text file will contain comma separated values in each line. Each line contains data in this format: String(20), String(5),String(10), double. There will be multiple lines with each line containing the same number of values. Ask the user to enter the path for the CSV file for reading the file. After reading the data from the file output the data...

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • 1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...

    1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods. 2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory. 3) Write a Java program to...

  • ***** JAVA ONLY ***** Write a program that asks the user to enter the name of...

    ***** JAVA ONLY ***** Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad to create a simple file that can be used to test the program. ***** JAVA ONLY *****

  • Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the...

    Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use...

  • write a program in java that asks the user for a sentence, and then returns that...

    write a program in java that asks the user for a sentence, and then returns that string with the words backwards. For example: Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards. demonstrate program by...

  • In Java, write a program that asks the user for the name of a file. The...

    In Java, write a program that asks the user for the name of a file. The program should read all the numbers from the given file and display the total and average of all numbers in the following format (three decimal digits): Total: nnnnn.nnn Average: nnnnn.nnn Class name: FileTotalAndAverage

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

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