Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program is done in bluej Write a program in Java and run it in BlueJ accordi

ID: 3830874 • Letter: T

Question

The program is done in bluej

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).

Then it prompts the user to enter a command, executes the command and loops. The commands are the following:

"printall" - prints all student records (first name, last name, grade).

"firstname name" - prints all students with first name that starts with the provided parameter name.

"lastname name" - prints all students with first name that starts with the provided parameter name.

"interval m n" - prints all students with grades in the interval [m, n].

"end" - exits the loop the terminates the program.

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

If the user enters "firstname A" the program prints the following:   

If the user enters "lastname Miller" the program prints the following:

If the user enters "interval 75 90" the program prints the following:

Requirements and restrictions:

Use the Student.java and Students.java classes from the course website to represent and process student records and modify them accordingly:

Define and use an array of objects of class Student to store all student records read from the file.

In class Students define the following methods that accept the array of students and other proper parameters and do the following:

Prints all student records in the array.

Prints all students with first name that starts with the provided parameter. Hint: use the indexOf(String str) method form class String.

Prints all students with last that starts with the provided parameter. Hint: use the indexOf(String str) method form class String.

prints all students with grades in the interval [m, n].

Do NOT use the array of Students directly in the main method, all processing must be done by the methods described above using the array passed to them as a parameter.

Enforce the encapsulation principle.

No need to verify the user input.

When you write your program

use proper names for the variables suggesting their purpose.

format your code accordingly using indentation and spacing.

use multiple line comment in the beginning of the code and write your name, e-mail address, class, and section.

for each line of code add a short comment to explain its meaning.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

//Create a main class, StudentGradeDemo
public class StudentGradeAsk
{
// main method
public static void main(String args[]) throws FileNotFoundException
{
// Declare variables
String first_name, last_name;
int grade;
// Create a Scanner class's object
Scanner scn = new Scanner(System.in);

// Create an ArrayList
ArrayList list = new ArrayList();

// Reads a text file with student records (first name, last name and
// grade on each line).
Scanner fileInput = new Scanner(new File("students.txt"));
Student st;
while (fileInput.hasNext()) {

first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
st = new Student(first_name, last_name, grade);
list.add(st);
}
// prompts the user to enter a command, executes the command
System.out.print("Enter a command (all/excellent/ok/end):");
switch (scn.next()) {
case "all":
for (Student s : list)
System.out.println(s);
break;
case "excellent":
for (Student s : list)
{
if (s.getGrade() > 89)
{
System.out.println(s);
}
}

break;
case "ok":
for (Student s : list)
{
if (s.getGrade() <= 89)
{
System.out.println(s);
}
}

break;
case "end":
System.exit(0);
}
}
}

-------------------------

public class Student
{
String fname;
String lname;
int grade;
public Student(String fn,String ln, int gd )
{
fname=fn;
lname=ln;
grade=gd;
}
//get methods to retrieve the values of instance variables
public String getLname()
{
return lname;
}
public String getFname()
{
return fname;
}
public int getGrade()
{
return grade;
}
public String toString()
{
return fname+" "+lname+" "+grade;
}
}

Explanation / Answer

Hi, Please find my implementation.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

//Create a main class, StudentGradeDemo

public class StudentGradeAsk

{

   // main method

   public static void main(String args[]) throws FileNotFoundException

   {

       // Declare variables

       String first_name, last_name;

       int grade;

       // Create a Scanner class's object

       Scanner scn = new Scanner(System.in);

       // Create an ArrayList

       ArrayList<Student> list = new ArrayList<>();

       // Reads a text file with student records (first name, last name and

       // grade on each line).

       Scanner fileInput = new Scanner(new File("students.txt"));

       Student st;

       while (fileInput.hasNext()) {

           first_name = fileInput.next();

           last_name = fileInput.next();

           grade = fileInput.nextInt();

           st = new Student(first_name, last_name, grade);

           list.add(st);

       }

       /*// prompts the user to enter a command, executes the command

       System.out.print("Enter a command (all/excellent/ok/end):");

       switch (scn.next()) {

       case "all":

           for (Student s : list)

               System.out.println(s);

           break;

       case "excellent":

           for (Student s : list)

           {

               if (s.getGrade() > 89)

               {

                   System.out.println(s);

               }

           }

           break;

       case "ok":

           for (Student s : list)

           {

               if (s.getGrade() <= 89)

               {

                   System.out.println(s);

               }

           }

           break;

       case "end":

           System.exit(0);

       }*/

       while(true){

           System.out.println("printall");

           System.out.println("firstname name");

           System.out.println("lastname name");

           System.out.println("interval m n");

           System.out.println("end");

           System.out.print("Enter a command :");

           String command = scn.nextLine();

           String arr[] = command.split("\s+");

           if(arr.length == 1 && arr[0].equalsIgnoreCase("end"))

               break;

           else if(arr.length == 1 && arr[0].equalsIgnoreCase("printall")){

               for (Student s : list)

               {

                   System.out.println(s);

               }

           }

           else if(arr.length == 2 && arr[0].equalsIgnoreCase("firstname")){

               for (Student s : list)

               {

                   if (s.getFname().startsWith(arr[1].trim()))

                   {

                       System.out.println(s);

                   }

               }

           }

           else if(arr.length == 2 && arr[0].equalsIgnoreCase("lastname")){

               for (Student s : list)

               {

                   if (s.getLname().startsWith(arr[1].trim()))

                   {

                       System.out.println(s);

                   }

               }

           }

           else if(arr.length == 3 && arr[0].equalsIgnoreCase("interval")){

               int m = Integer.parseInt(arr[1].trim());

               int n = Integer.parseInt(arr[2].trim());

               for (Student s : list)

               {

                   if (s.getGrade()>=m && s.getGrade()<=n)

                   {

                       System.out.println(s);

                   }

               }

           }else{

               System.out.println("invalid option!!!");

           }

           System.out.println(" ");

       }

   }

}

/*

Sample run:

printall

firstname name

lastname name

interval m n

end

Enter a command :printall

John Smith 90

Barack Obama 95

Al Clark 80

Sue Taylor 55

Ann Miller 75

George Bush 58

John Miller 65

printall

firstname name

lastname name

interval m n

end

Enter a command :firstname A

Al Clark 80

Ann Miller 75

printall

firstname name

lastname name

interval m n

end

Enter a command :lastname Miller

Ann Miller 75

John Miller 65

printall

firstname name

lastname name

interval m n

end

Enter a command :interval 75 90

John Smith 90

Al Clark 80

Ann Miller 75

printall

firstname name

lastname name

interval m n

end

Enter a command :end

*/