Question

Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

Introduction

In this lab, you will be working with three classes: Person, Student, and Roster.

Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number.

The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList.

The Person class has been completed for you with class variables, a constructor, and a toString() method. You will be responsible for finishing the Roster class, and writing the Student class from scratch.

Step 1: Student Class

Define the Student class to inherit from the Person class.

Declare an int class variable to represent the ID number.

Write the constructor to initialize all class variables.

Also, define the toString() method to resemble the toString() method from the Person class, except with the ID following the name.

The format should resemble: Student: FirstName LastName IDnumber

Step 2: Roster.initializeListFromFile()

Now, complete the unfinished initializeListFromFile() in the Roster class. This method takes a filename as a parameter and fills the ArrayList of objects of type Person or Student.

Each line in the file to read from represents a single object with the following possible formats: For a Student object: firstName lastName idNumber

For a Person object: firstName lastName

We recommend using the String method .split() which returns an array of strings, to parse the line.

Your method should loop through every line in the file, and add a new Person or Student object to the people ArrayList by calling the appropriate constructor with the appropriate arguments.

Test Your Methods

As usual, you will also be able to test your code within the Main class. You may run your program in Develop mode as many times as you would like, but you may only submit your program once.

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

Student.java CODE:

// Step 1: Student Class

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

Roster.java CODE:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Roster {

    ArrayList<Person> people;

    public Roster(String filename) {
        people = new ArrayList<Person>();
        initializeListFromFile(filename);
    }

    public void initializeListFromFile(String filename) {
        try {
            // Step 2
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String [] args) {
        Roster r = new Roster("test");
        System.out.println(r.people);
    }

}

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

Person.java CODE:

public class Person {
    String firstName;
    String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String toString() {
        return "Person: " + firstName + " " + lastName;
    }



}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Roster.java

//Roster.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Roster {

    ArrayList<Person> people;
    ArrayList<Student> students;


    public Roster(String filename ) {
        people = new ArrayList<Person>();
        students = new ArrayList<Student>();
        initializeListFromFile(filename);
    }

    public void initializeListFromFile(String filename) {
        try {
            Scanner scnr = new Scanner(new File(filename));
            while (scnr.hasNextLine()) 
            {
                String tmp = scnr.nextLine();
                String[] li = tmp.split(" ");
                if(li.length == 3)
                {
                    // Student Object
                    students.add(new Student(li[0], li[1], Integer.parseInt(li[2])));
                
                }
                else if(li.length == 2)
                {
                    // Person Object
                    people.add(new Person(li[0],li[1]));
                }
                else
                {
                    System.out.println("Invalid entry in file: " + filename);
                    System.exit(1);
                }
            }
            scnr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String [] args) {
        Roster r = new Roster("test.txt");
        System.out.println(r.people);
        System.out.println(r.students);
    }

}

Student.java

// Student.java


public class Student extends Person {
    int id;

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

    public String toString() {
        return "Student: " + firstName + " " + lastName + " " + id;
    }



}

Person.java

// Person.java

public class Person {
    String firstName;
    String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String toString() {
        return "Person: " + firstName + " " + lastName;
    }



}

Test input file , name : test.txt

Alexander Supertramp
Alaska Green 12
Adam Abraham 2
Camila Black 19
Alice Thomas
Christopher Nolan
David Fincher 123
James Hall 
John Franklin 122

Keeping Roster.java , Student.java , Person.java in the same folder ( also copy test.txt to to the same directory for verification)

Compile :

javac Roster.java

run :

java Roster

Output Sample :

[Person: Alexander Supertramp, Person: Alice Thomas, Person: Christopher Nolan, Person: James Hall] [Student: Alaska Green 12

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

Please Give like if you find the program helpful

Add a comment
Know the answer?
Add Answer to:
Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...
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
  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

  • Write a class called Course_xxx that represents a course taken at a school. Represent each student...

    Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class...

  • For this lab assignment, you will be writing a few classes that can be used by...

    For this lab assignment, you will be writing a few classes that can be used by an educator to grade multiple choice exams. It will give you experience using some Standard Java Classes (Strings, Lists, Maps), which you will need for future projects. The following are the required classes: Student – a Student object has three private instance variables: lastName, a String; firstName, a String; and average, a double. It has accessor methods for lastName and firstName, and an accessor...

  • Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the...

    Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the TA class to represent the course the student is TA'ing. 1.3) Provide a constructor for the class so the code in the PeopleFactory will work as provided. 1.4) Provide a toString() method for the TA  class so that TA's will output as shown in the sample code. Provide a class Staff so that you can un-comment the lines of code in the PeopleFactory class that...

  • 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,...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

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