Question

FOR JAVA: Summary: Create a program that adds students to the class list (list below). The...

FOR JAVA:

Summary: Create a program that adds students to the class list (list below).

The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program.

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

List of student names and IDs for class (this will be your separate text file):

  • Jones, Jim,45
  • Hicks, Josh,56
  • Perry, Andy,82
  • Rush, Ashely,19
  • Salenga, Aaron,55
  • Matos, Pamela,22
  • Jeffries, Rick,87
  • Bush, Fred,40
  • Ashford, Kate,52
  • Naamah, Ester,68
  • Lachlan, Bae,35
  • Eachann, Steven,41
  • Ubon, Peter,16
  • Bacon, Gabe,59
  • Rockson, Kurt,77
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

Student,java class

public class Student {
private String lastName, firstName;
private int ID;
  
public Student()
{
this.lastName = this.firstName = "";
this.ID = 0;
}

public Student(String lastName, String firstName, int ID) {
this.lastName = lastName;
this.firstName = firstName;
this.ID = ID;
}

public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

public int getID() {
return ID;
}
  
@Override
public String toString()
{
return(this.ID + " - " + this.firstName + " " + this.lastName);
}
}

Roster.java class

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;

public class Roster {
private LinkedList<Student> students;
  
public Roster()
{
this.students = new LinkedList<>();
}
  
public void readStudents(String fileName)
{
Scanner fileReader;
int i=0;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(",");
String lastName = data[0];
String firstName = data[1];
int ID = Integer.parseInt(data[2]);
i++;
this.students.add(new Student(lastName, firstName, ID));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Couldn't find " + fileName);
System.exit(1);
}
}
  
public void sortByLastName()
{
Collections.sort(this.students, new CompareByLastName());
}
  
public void sortByID()
{
Collections.sort(this.students, new CompareByID());
}
  
public void displayList()
{
for(Student student : this.students)
{
System.out.println(student);
}
}
private class CompareByLastName implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
if(o1.getLastName().compareTo(o2.getLastName()) < 0)
return -1;
else if(o1.getLastName().compareTo(o2.getLastName()) == 0)
return 0;
else
return 1;
}
}
  
class CompareByID implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
if(o1.getID() < o2.getID())
return -1;
else if(o1.getID() == o2.getID())
return 0;
else
return 1;
}
}
}

Roster402_v2.java


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Roster402_v2 {
  
private static final String FILENAME = "students.txt";

public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
Roster roster = new Roster();
String fname,lname,choice,str;
int id;
File file = new File(FILENAME);
   FileWriter fr = null;
   fr = new FileWriter(file, true);
do
{
//ask user for student information
System.out.println("Enter Last name of the student; ");
lname=sc.next();
System.out.println("Enter First name of the student; ");
fname=sc.next();
System.out.println("Enter Student ID: ");
id=sc.nextInt();
str="\n"+lname+", "+fname+","+id;
fr.write(str);//append data to the file
System.out.println("Student added to the roster.");
System.out.print("Do you want to add another student?(y/n): ");
choice=sc.next();
}while(choice.equalsIgnoreCase("y"));
fr.close();
roster.readStudents(FILENAME);
System.out.print("Do you want to see student roster?(y/n): ");
choice=sc.next();
System.out.println("\n");
if(choice.equalsIgnoreCase("y"))
roster.displayList();
}
  
}

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
FOR JAVA: Summary: Create a program that adds students to the class list (list below). The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • team, create a Java program that adds or removes entries from a linked list based on...

    team, create a Java program that adds or removes entries from a linked list based on user input. You can use the index of the entry as the key to identify the record for addition or deletion. The program can be as simple as you like but must add an entry to the list based on the user input and also must delete the proper item in the list based on the index entered by the user Display your list...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • create a java prgram that have UniversityDriver Class The UniversityDriver contains the main method. The following...

    create a java prgram that have UniversityDriver Class The UniversityDriver contains the main method. The following commands can be entered: (a) "hire" //to hire a new faculty member (b) "admit" //to admit a new student (c) "find student" //to display information about a specific student: name, date of birth, and major (d)"find faculty" //display information about a specific faculty: name, date of birth, and courses; (e) “list students” // list the first and last names of all students (f) “list...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • 1. Create a new multi-class Java program which implements a vending machine simulator which contains the...

    1. Create a new multi-class Java program which implements a vending machine simulator which contains the following functionality: A) At program startup, the vending machine is loaded with a variety of products in a variety of packaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Also included is the cost of each item. The program should be designed to easily load a different set of products easily (for example, from a file). Also at program startup,...

  • JAVA Create a Governor class with the following attributes: name : String party - char (the...

    JAVA Create a Governor class with the following attributes: name : String party - char (the character will be either D, R or I) ageWhenElected : int Create a State class with the following attributes: name : String abbreviation : String population : long governor : Governor Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy). You will be turning...

  • C++ Create an application that searches a file of male and female first names. A link...

    C++ Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration. Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT