Question

In: Computer Science

Java Complete Example.java to fulfill the following problem statement: Write a program to accept a list...

Java

Complete Example.java to fulfill the following problem statement:

Write a program to accept a list of numbers, one per line. Input is provided from a file if one is provided as the first command line argument. If a file is not provided, input should be read from the terminal.

The numbers can either be whole numbers or floating point numbers. The program should continue to accept input until a number equal to 00 is input. Of course, if the input in a file ends before a 00, that can be treated as the end of the input.

Once 00 is received or the input ends, the program should print the largest and smallest number provided as input (not including the 00 to end input). Sample outputs are shown below.

The program should catch and handle all common exceptions. When an exception is caught, the program should print the name of the exception, followed by the message included in the exception. It should then continue to accept input (if possible). Specifically, if it is unable to open the file provided, input should be read from the terminal.

Thanks!

Solutions

Expert Solution

I have implemented the MinMaxFinder to read input either from file or standard input and find Smallest,largest numbers per the given description.

Please find the following Code Screenshot, Output, and Code.

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT

1.CODE SCREENSHOT :

2.OUTPUT :

3.CODE :

import java.io.*; 
public class MinMaxFinder 
{ 
  public static void main(String[] args)throws Exception 
  { 
    /*To Store smallest,largest and temp(t)*/
        double smallest=0,largest=0,t=0;
        //flag to check the file exits
        //'i' to initilize the first smallest and largest
        int i=0,flag=0;
        //Create a BufferedReader class Object to read from standard input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //if the number of arguments is '1'
  if(args.length==1){
          //open the file
         File file = new File("input.txt"); 
         //if the file exists 
         if (file.exists()) {
                 //create a buffered reader object to the file
         br = new BufferedReader(new FileReader(file)); 
         flag=1;
         }
  }
  //if flag=0 then we read data from input 
  if(flag==0){
        System.out.println("Enter Numbers One per Line Ending with '00'");
  }
     String st; 
         //loop until 'EOF' or '00' is reached 
     while ((st = br.readLine()) != null &&!st.equals("00")) 
                try{
                        //Convert the number from string
                        t=Double.parseDouble(st);
                        //if the element is the fisrt element then it is both smallest and largest
                        if(i==0){
                        smallest=t;
                        largest=t;
                        i++;
                }
                //otherwise we compare with previous values
                if(smallest>t)
                        smallest=t;
                if(largest<t)
                        largest=t;
                }catch(NumberFormatException e){
                        //if there is any exception we print the exception
                        System.out.println(e);
                }
        //print the result
    System.out.println("The Smallest Number : "+smallest);
    System.out.println("The Largest Number : "+largest);
        
  }
 
} 

input.txt:

1
29
38
34
35
23
4
3
2
1
-321
31
3

00

Related Solutions

Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
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 complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT