The program reads a text file with student records (first name, last name and gr
ID: 3837198 • Letter: T
Question
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.
Explanation / Answer
Please find my implementation.
########## Student.java ##########
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;
}
}
########### Students.java ##########
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);
}
// now processing using command
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();
// split the command by space
String arr[] = command.split("\s+");
// if command contains one word and that is end
if(arr.length == 1 && arr[0].equalsIgnoreCase("end"))
break;
// if command contains one word and that is 'printall'
else if(arr.length == 1 && arr[0].equalsIgnoreCase("printall")){
printStudent(list, list.size()); // calling method to print all student
}
// if command contains two word and first word is 'firstname'
else if(arr.length == 2 && arr[0].equalsIgnoreCase("firstname")){
// print all students whose first name starts with second word of the command
printFname(list, list.size(), arr[1]);
}
// if command contains two word and first word is 'lastname'
else if(arr.length == 2 && arr[0].equalsIgnoreCase("lastname")){
// print all students whose last name starts with second word of the command
printLname(list, list.size(), arr[1]);
}
// if there are three word in command and first word is 'interval
else if(arr.length == 3 && arr[0].equalsIgnoreCase("interval")){
int m = Integer.parseInt(arr[1].trim()); // parsing second word of command as integer
int n = Integer.parseInt(arr[2].trim());// parsing third word of command as integer
printGrade(list, m, n); // printing all students whose grade lies in given interval
}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
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.