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

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

ID: 3836505 • Letter: W

Question

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:

John Smith 90

Barack Obama 95

Al Clark 80

Sue Taylor 55

Ann Miller 75

George Bush 58

John Miller 65

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

Al Clark 80

Ann Miller 75

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

Ann Miller 75

John Miller 65

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

John Smith 90

Al Clark 80

Ann Miller 75

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.

Extra credit (up to 2 points): The extra credit will be given for extending the program to accept the "sort" command that will print the array of students sorted by grade. Use the bubble sort algorithm as implemented in array.java.

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

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

printStudent(st,count);
  
System.out.println();
  
Stringpall = "Print All";
printPall(st,count,pall);
  
String fname = "A";
printFname(st,count,fname);
  
String lname = "Miller";
printLname(st,count,lname);
  
String grade = "interval 75 90";
printGrade(st,count,grade);

}

static void printPall(Student[] arr, int n,String name)
{
for (int i=0; i<n; i++)
if ( arr[i].getFname().indexOf(name) == 0);//string the points to name of the student
System.out.println(arr[i]);
}
  
static void printFname(Student[] arr, int n,String name)
{
for (int i=0; i<n; i++)
if ( arr[i].getFname().indexOf(name) == 0);//string the points to name of the student
System.out.println(arr[i]);
}   
  
static void printLname(Student[] arr, int n,String name)
{
for (int i=0; i<n; i++)
if ( arr[i].getLname().indexOf(name) == 0);//string the points to name of the student
System.out.println(arr[i]);
}
  
static void printGrade(Student[] arr, int n,String name)
{
for (int i=0; i<n; i++)
if ( arrlength == 3 && arr[0].getGrade().indexOf(name) == 0);//string the points to name of the student
System.out.println(arr[i]);
}   

static void printStudent(Student[] arr, int n)
{
for (int i=0; i<n; i++)
System.out.println(arr[i]);

}   
}

----------

public class Student
{
private String fname, lname;
private int grade;
  
public Student(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}
  
  
public String getFname() {return fname;}
  
public String getLname() {return lname;}
  
public String toString()
{
return fname + " " + lname + " " + grade;
}
}

Explanation / Answer

Please find my implementation.

###########

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;

   }

}

###############

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

//Create a main class, StudentGradeDemo

public class Students

{

   // 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);

       }

      

       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")){

               printStudent(list, list.size());

           }

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

               printFname(list, list.size(), arr[1]);

           }

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

               printLname(list, list.size(), arr[1]);

           }

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

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

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

               printGrade(list, m, n);

           }else{

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

           }

           System.out.println(" ");

       }

      

       fileInput.close();

       scn.close();

   }

   static void printFname(ArrayList<Student> arr, int n,String name)

   {

       for (Student s : arr)

       {

           if (s.getFname().startsWith(name.trim()))

           {

               System.out.println(s);

           }

       }

   }

   static void printLname(ArrayList<Student> arr, int n,String name)

   {

       for (Student s : arr)

       {

           if (s.getLname().startsWith(name.trim()))

           {

               System.out.println(s);

           }

       }

   }

   static void printGrade(ArrayList<Student> arr, int m,int n)

   {

       for (Student s : arr)

       {

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

           {

               System.out.println(s);

           }

       }

   }

   static void printStudent(ArrayList<Student> arr, int n)

   {

       for (Student s : arr)

       {

           System.out.println(s);

       }

   }

}

/*

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

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote