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

  • 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 *****************************************************/...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Last picture is the tester program! In this Assignment, you will create a Student class and...

    Last picture is the tester program! In this Assignment, you will create a Student class and a Faculty class, and assign them as subclasses of the superclass UHPerson. The details of these classes are in the UML diagram below: UHPerson - name : String - id : int + setName(String) : void + getName(): String + setID(int) : void + getID(): int + toString(): String Faculty Student - facultyList : ArrayList<Faculty - rank: String -office Hours : String - studentList...

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
ADVERTISEMENT