Write a program in Java and run it in BlueJ according to the following specifica
ID: 3835397 • 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:
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.
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();
String fname = "A";
printFname(st,count,fname);
String lname = "Miller";
printLname(st,count,lname);
}
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 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;
}
}
this is waht i haveso far
Explanation / Answer
package chegg.datastructure;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import chegg.RoomManagement;
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];
List<Student> list = new ArrayList<Student>();
Scanner fileInput = new Scanner(new File("d://students.txt"));
while (fileInput.hasNext()) {
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
if (first_name != null && last_name != null) {
list.add(new Student(first_name, last_name, grade));
count++;
}
}
displayMenu(list);
}
public static void displayMenu(List<Student> strList) {
char continueProcess = 'Y';
while (continueProcess == 'Y' || continueProcess == 'y') {
System.out
.println("Enter one of the following commands printall firstname <name> lastname <name> interval m n ");
Scanner scanner = new Scanner(System.in);
String command = scanner.nextLine();
if ("printall".equalsIgnoreCase(command)) {
printStudents(strList);
} else if ("firstname".equalsIgnoreCase(command.substring(0, 9))) {
Collections.sort(strList, new FirstNameComparator());
char firstchar = command.charAt(10);
for (Student student : strList) {
if ((student.getFname().charAt(0) == firstchar)) {
System.out.println(student.toString());
}
}
} else if ("lastname".equalsIgnoreCase(command.substring(0, 8))) {
Collections.sort(strList, new LastNameComparator());
String[] strings = command.split(" ");
for (Student student : strList) {
if ((student.getLname().charAt(0) == strings[1].charAt(0))) {
System.out.println(student.toString());
}
}
} else if ("interval".equalsIgnoreCase(command.substring(0, 8))) {
String[] strings = command.split(" ");
int m = Integer.parseInt(strings[1]);
int n = Integer.parseInt(strings[2]);
Collections.sort(strList, new GradeComparator());
for (Student student : strList) {
if ((student.getGrade() >= m && student.getGrade() <= n)) {
System.out.println(student.toString());
}
}
} else if ("end".equalsIgnoreCase(command)) {
continueProcess = 'N';
}
System.out.println("Do You want to continue ? 'Y' or 'N'");
continueProcess = scanner.next().charAt(0);
}
}
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 printStudents(List<Student> arr) {
for (Student student : arr) {
System.out.println(student + " ");
}
}
int binarySearch(int arr[], int low, int high, int key) {
if (high < low)
return -1;
int mid = (low + high) / 2; /* low + (high - low)/2; */
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
}
class Student implements Comparable<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 int getGrade() {
return grade;
}
public String toString() {
return fname + " " + lname + " " + grade;
}
@Override
public int compareTo(Student o) {
if (o != null)
return -1;
return this.getFname().compareTo(o.getFname());
}
}
class FirstNameComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1 == null || o2 == null)
return -1;
return o1.getFname().compareTo(o2.getFname());
}
}
class LastNameComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1 == null || o2 == null)
return -1;
return o1.getLname().compareTo(o2.getLname());
}
}
class GradeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.getGrade() == o1.getGrade()) {
return 0;
} else if (o1.getGrade() < o1.getGrade()) {
return -1;
} else {
return 1;
}
}
}
Output
------------
Enter one of the following commands
printall
firstname <name>
lastname <name>
interval m n
printall
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
Do You want to continue ? 'Y' or 'N'
y
Enter one of the following commands
printall
firstname <name>
lastname <name>
interval m n
firstname a
Do You want to continue ? 'Y' or 'N'
y
Enter one of the following commands
printall
firstname <name>
lastname <name>
interval m n
firstname A
Al Clark 80
Ann Miller 75
Do You want to continue ? 'Y' or 'N'
y
Enter one of the following commands
printall
firstname <name>
lastname <name>
interval m n
lastname M
Do You want to continue ? 'Y' or 'N'
y
Enter one of the following commands
printall
firstname <name>
lastname <name>
interval m n
lastname B
Do You want to continue ? 'Y' or 'N'
Y
Enter one of the following commands
printall
firstname <name>
lastname <name>
interval m n
interval 65 88
Al Clark 80
Ann Miller 75
John Miller 65
Do You want to continue ? 'Y' or 'N'
Y
Enter one of the following commands
printall
firstname <name>
lastname <name>
interval m n
Description :
To execute above program please run the main method of students class.
please let me know if face any dificulties to make understanding on above code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.