In: Computer Science
Write a program in Java and run it in BlueJ according to the following specifications:
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:
/* 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
/****************************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 :)