Write a program in Java and run it in BlueJ according to the following specifica
ID: 3837118 • Letter: W
Question
Write a program in Java and run it in BlueJ according to the following specifications:
**YOU CANNOT USE ARRAYLISTS****
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.
extend the program to accept the "sort" command that will print the array of students sorted by grade. (bubble sort)
HERE ARE THE CLASSES TO USE:
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 toString()
{
return fname + " " + lname + " " + grade;
}
}
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, total=0, count=0;
double average;
Scanner fileInput = new Scanner(new File("students.txt"));
while (fileInput.hasNext())
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
Student st = new Student(first_name, last_name, grade);
System.out.println(st);
total = total + grade;
count++;
}
average = (double)total/count;
System.out.println("There are " + count + " students with average grade " + average);
}
}
Explanation / Answer
Hi, I have implemented every thing except sort(extra) command.
Please repost new requirement in separate post.
Please let me know in case of any issue in answered part.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
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 array
Student[] studentArrr = new Student[50];
int n = 0;
// 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);
studentArrr[n++] = 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(studentArrr, n); // 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(studentArrr, n, 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(studentArrr, n, arr[1]);
}
// if there are three word in command and first word is 'interval
else if(arr.length == 3 && arr[0].equalsIgnoreCase("interval")){
int low = Integer.parseInt(arr[1].trim()); // parsing second word of command as integer
int high = Integer.parseInt(arr[2].trim());// parsing third word of command as integer
printGrade(studentArrr, n, low, high); // 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(Student[] arr, int size,String name)
{
for (int i=0; i<size; i++)
{
if (arr[i].getFname().indexOf(name.trim()) == 0)
{
System.out.println(arr[i]);
}
}
}
static void printLname(Student[] arr, int size,String name)
{
for (int i=0; i<size; i++)
{
if (arr[i].getLname().indexOf(name.trim()) == 0)
{
System.out.println(arr[i]);
}
}
}
static void printGrade(Student[] arr, int size, int m,int n)
{
for (int i=0; i<size; i++)
{
if (arr[i].getGrade()>=m && arr[i].getGrade()<=n)
{
System.out.println(arr[i]);
}
}
}
static void printStudent(Student[] arr, int size)
{
for (int i=0; i<size; i++)
{
System.out.println(arr[i]);
}
}
}
/*
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.