Question

In: Computer Science

Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...

Write a Java program (single file/class) whose filename must be Numbers.java.

The program reads a list of integers from use input and has the following methods:

1. min - Finds the smallest integer in the list.

2. max- Finds the largest integer in the list.

3. average - Computes the average of all the numbers.

4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire list of entries on one line and then the minimum, maximum, and average. values on separate lines with nice readable labels.

5. Make sure your code runs without error and handles bad inputs and 0 and negative numbers are valid.

6. Followed instructions and include to get full credit file must include your name, date, class and Javadoc code comments for each and every method as well as logic comments.

Solutions

Expert Solution

JAVA CODE:(Numbers.java)

// Your name here
// date of assignment
// class

import java.util.*;

public class Numbers
{      
       /** main method gets user input and calls various methods
       */
       public static void main(String args[])
       {
           //scanner object to get user input
           Scanner ob=new Scanner(System.in);
          
           // variable to store the user input
           String input="";
          
           //list to store the user input integers
            ArrayList<Integer> list = new ArrayList<Integer>();
          
           //infinite loop
            while(true)
           {
               //user input
               System.out.print("Enter a integer: ");
               input=ob.next();
              
               // checking for breaking the loop
               if (input.equalsIgnoreCase("Q"))
                    break;
               try
               {
                   //converting string into int and stored in the list
                   list.add(Integer.valueOf(input));
               }
               catch(NumberFormatException e)
               {
                   //error message when non integer is input from the user
                   System.out.println("\nError: Please enter a valid integer\n");
               }                              
              
           }
          
           //displaying the list of integers
           System.out.println("\nIntegers in the list: ");
          
           for(Integer i: list)
           {
               System.out.println(i);
           }
          
           //calling the method to find the largest integer
           max(list);
          
           //calling the method to find the minimum integer
           min(list);
          
           //calling the method to find the average
           average(list);
       }
      
       /** method to find the largest number in the list
           @param list a list of integers
       */
       static void max(ArrayList<Integer> list)
       {
           //variable to store the maximum integer
           int max=Integer.MIN_VALUE;
          
           //iterating over a list of integers
           for(Integer i: list)
           {
               //checking for maximum integer
               if (i> max)
                  
               //storing the maximum integer
                   max=i;              
           }
          
           //printing the maximum integer
           System.out.println("\nLargest integer in the list: "+max);
       }


       /** method to find the smallest number in the list
           @param list a list of integers
       */
       static void min(ArrayList<Integer> list)
       {
           //variable to store the minimum integer
           int min=Integer.MAX_VALUE;
          
           //iterating over a list of integers
           for(Integer i: list)
           {
               //checking for minimum integer
               if (i< min)
                  
               //storing the minimum integer
                   min=i;              
           }
           //printing the maximum integer
           System.out.println("\nSmallest integer in the list: "+min);
       }
      
       /** method to find the average of integers in the list
           @param list a list of integers
       */
       static void average(ArrayList<Integer> list)
       {
           //variable to store total and counter
           int total=0;
           int count=0;
          
           //looping over a list           
           for(Integer i: list)
           {
               //calculating the total
               total=total+i;
              
               //incrementing the counter
               count++;
              
           }
          
           //calculating the average
           double average=(double)total/(double)count;
          
           //printing the average
           System.out.println("\nAverage: "+String.format("%.2f", average));
       }
}

SCREENSHOT FOR OUTPUT:


Related Solutions

1. Write a program that prompts the user for a filename, then reads that file in...
1. Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is: abcdef ghijkl the output will be: lkjihg fedcba Need Help with this be done in only PERL. Please use "reverse"
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT