Question

In: Computer Science

Write a program in Java and run it in BlueJ according to the following specifications: The...

Write a program in Java and run it in BlueJ according to the following specifications:

  • The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok).
  • Then it prompts the user to enter a command, executes the command and loops. The commands are the following:
    • "all" - prints all student records (first name, last name, grade, type).
    • "excellent" - prints students with grade > 89.
    • "ok" - prints students with grade <= 89.
    • "end" - exits the loop the terminates the program.

For example, if the input text file is students.txt and the user enters "all" the program prints the following:

John Smith 90 excellent

Barack Obama 95 excellent

Al Clark 80 ok

Sue Taylor 55 ok

Ann Miller 75 ok

George Bush 58 ok

John Miller 65 ok

If the user enters "excellent" the program prints the following:

John Smith 90 excellent

Barack Obama 95 excellent

If the user enters "ok" the program prints the following:

Al Clark 80 ok

Sue Taylor 55 ok

Ann Miller 75 ok

George Bush 58 ok

John Miller 65 ok

Requirements and restrictions:

  • Use the Students.java, Excellent.java, and Ok.java classes from the course website and make the following modifications/additions:
    • Create an interface Student and implement it with classes Excellent and Ok to represent the excellent and ok students correspondingly.
    • Create an ArrayList and fill it with objects of classes Excellent and Ok to store all students from the file.
    • Use the ArrayList to print all, excellent and ok students. Use the instanceof operator to distinguish between excellent and ok objects.

/* ArrayList, Interfaces, Class Object and instanceof operator
*
Suggested exercises:
- Create an ArrayList and fill it with Excellent and Ok objects.
Use the instanceof operator and casting to print the array (calling the info() method).
- Create interface Student (with info() method) and modify classes Excellent and Ok to implement Student.
Define the ArrayList of type Student. Print the array. Is casting needed?
*/
import java.util.Scanner;
import java.io.*;

public class Students
{
public static void main (String[] args) throws IOException
{ String first_name, last_name;
int grade, count=0;
Scanner fileInput = new Scanner(new File("students.txt"));
Object st;
while (fileInput.hasNext())
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();

if (grade>89)
st = new Excellent(first_name, last_name, grade);
else
st = new Ok(first_name, last_name, grade);

if (st instanceof Excellent)
((Excellent)st).info();
else
((Ok)st).info();

count++;
}
System.out.println("There are " + count + " students");
}
}

public class Excellent
{
private String fname, lname;
private int grade;

public Excellent(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}

public void info()
{
System.out.println(fname + " " + lname + "\t" + "excellent");
}
}

public class Ok
{
private String fname, lname;
private int grade;
  
public Ok(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}

public void info()
{
System.out.println(fname + " " + lname + "\t" + "ok");
}
}


public interface Student
{
void info();
}

students.txt

John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65

Solutions

Expert Solution

/****************************Student.java************************/

public interface Student {
   public void info();

   public int getGrade();
}

/******************************Excellent.java******************************/

package student3;

public class Excellent implements Student {
   private String fname, lname;
   private int grade;

   public Excellent(String fname, String lname, int grade) {
       this.fname = fname;
       this.lname = lname;
       this.grade = grade;
   }

   public String getFname() {
       return fname;
   }

   public void setFname(String fname) {
       this.fname = fname;
   }

   public String getLname() {
       return lname;
   }

   public void setLname(String lname) {
       this.lname = lname;
   }

   public int getGrade() {
       return grade;
   }

   public void setGrade(int grade) {
       this.grade = grade;
   }
  
   @Override
   public void info() {
       System.out.println(fname + " " + lname + "\t" + "excellent");
   }
}

/******************************Ok.java*********************/

package student3;

public class Ok implements Student {
   private String fname, lname;
   private int grade;

   public Ok(String fname, String lname, int grade) {
       this.fname = fname;
       this.lname = lname;
       this.grade = grade;
   }

  
   public String getFname() {
       return fname;
   }


   public void setFname(String fname) {
       this.fname = fname;
   }


   public String getLname() {
       return lname;
   }


   public void setLname(String lname) {
       this.lname = lname;
   }


   public int getGrade() {
       return grade;
   }


   public void setGrade(int grade) {
       this.grade = grade;
   }

   @Override
   public void info() {
       System.out.println(fname + " " + lname + "\t" + "ok");
   }
}

/****************************Students.java*********************/

package student3;

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

public class Students {
   public static void main(String[] args) throws IOException {
       String first_name, last_name;
       int grade, count = 0;
       ArrayList<Student> students = new ArrayList<>();
       Scanner scan = new Scanner(System.in);
       Scanner fileInput = new Scanner(new File("students.txt"));
       Object st;
       while (fileInput.hasNext()) {
           first_name = fileInput.next();
           last_name = fileInput.next();
           grade = fileInput.nextInt();

           if (grade > 89)
               students.add(new Excellent(first_name, last_name, grade));
           else
               students.add(new Ok(first_name, last_name, grade));

           count++;
       }
       String command = "";
       do {
           command();
           System.out.print("Please enter command: ");
           command = scan.next().toLowerCase();
           switch (command) {
           case "all":
               for (Student student : students) {

                   student.info();
               }
               break;
           case "excellent":
               for (Student student : students) {

                   if (student.getGrade() > 89) {

                       student.info();
                   }
               }
               break;
           case "ok":
               for (Student student : students) {

                   if (student.getGrade() < 89) {

                       student.info();
                   }
               }
               break;
           case "end":
               System.out.println("Thank you!");
               break;
           default:
               break;
           }
       } while (!command.equalsIgnoreCase("end"));
   }

   private static void command() {

       System.out
               .println("\"all\" - prints all student records\n" + "\"excellent\" - prints students with grade > 89\n"
                       + "\"ok\" - prints students with grade <= 89\n" + "\"end\" - exits.");
   }
}

/****************output**************/

"all" - prints all student records
"excellent" - prints students with grade > 89
"ok" - prints students with grade <= 89
"end" - exits.
Please enter command: all
John Smith   excellent
Barack Obama   excellent
Al Clark   ok
Sue Taylor   ok
Ann Miller   ok
George Bush   ok
John Miller   ok
"all" - prints all student records
"excellent" - prints students with grade > 89
"ok" - prints students with grade <= 89
"end" - exits.
Please enter command: excellent
John Smith   excellent
Barack Obama   excellent
"all" - prints all student records
"excellent" - prints students with grade > 89
"ok" - prints students with grade <= 89
"end" - exits.
Please enter command: ok
Al Clark   ok
Sue Taylor   ok
Ann Miller   ok
George Bush   ok
John Miller   ok
"all" - prints all student records
"excellent" - prints students with grade > 89
"ok" - prints students with grade <= 89
"end" - exits.
Please enter command: end
Thank you!

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a...
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a title Ask the user to enter the following values: The type of plane(Boeing 747 or Airbus A300) The number of passengers on board the Plane The number of crew aboard the plane The maximum number of people that can be carried by one liferaft assuming all the liferafts on the plane are the same size The actual number of liferafts that are available on...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called "DoTheMath" that accepts as input three floating-point numbers x, y, and z (define them as double) and outputs several calculations: x to the power of y x to the power of (y to the power of z) The absolute value of x The square root of (x*y to the power of z). Sample Run: Enter the values for x, y, z: -3.7 -3 5...
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read data back from the file and display the data in increasing order. After writing the file to disk, the input file should be read into an array, sorted using the static Arrays.sort() method from the Java API and then displayed in the...
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...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister does...
IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods...
IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister...
IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods...
IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT