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. February 18 is a special date...
Write a complete Java Program to solve the following problem. February 18 is a special date as this is the date that can be divisible by both 9 and 18 Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18. If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the...
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...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT