Question

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

Registration

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 that will store up to students (for now) in an array roster of Student objects.
After the user has entered the information for the 5 students, use the InsertionSort code written in class, and posted on Blackboard, to sort the array roster.

Step 4:
Write a method called storeRosterthat will save the data in the array roster to a text file Roster1.txt.

Step 5:
Write a method called loadRoster that will read the data in Roster1.txt into an array roster. (Note: this data will already be sorted!)

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

thanks for the question, Please post only one question at a time, the whole assignment was too big to be completed in the given time. Please break your questions into sub questions and post. Also, you have not shared the insertSort() code written in in your class. I had to do that also.

Nevertheless, I have implemented the complete assignment and commented the section each part/Step as asked in the question.

Here is the full code.

===========================================================================

public class Student implements Comparable<Student> {

    private String firstName;
    private String lastName;
    private String studentId;
    private int totalCredits;
    private double gpa;

   public Student() {
        this.firstName = "";
        this.lastName = "";
        this.studentId = "";
        this.totalCredits = 0;
        this.gpa = 0;
    }

    public Student(String firstName, String lastName, String studentId, int totalCredits, double gpa) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.studentId = studentId;
        this.totalCredits = totalCredits;
        this.gpa = gpa;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

   public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public int getTotalCredits() {
        return totalCredits;
    }

    public void setTotalCredits(int totalCredits) {
        this.totalCredits = totalCredits;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    @Override
    public boolean equals(Object o) {
       if (o == null || !(o instanceof Student)) return false;
        Student aStudent = (Student) o;
        return aStudent.getFirstName().equals(getFirstName()) &&
                aStudent.getLastName().equals(getLastName()) &&
                aStudent.getStudentId().equals(getStudentId());
    }


    @Override
    public String toString() {
        return
               
"First name: " + firstName +
                        "\nLast Name: " + lastName +
                        "\nStudent Id: " + studentId +
                        "\nTotal credits: " + totalCredits +
                        "\ngpa:" + gpa;

    }

    @Override
    public int compareTo(Student student) {
        if (getFirstName().compareTo(student.getFirstName()) == 0) {
            if (getLastName().compareTo(student.getLastName()) == 0) {
                return getStudentId().compareTo(student.getStudentId());

            } else {
                return getLastName().compareTo(student.getLastName());
            }
        } else {
            return getFirstName().compareTo(student.getFirstName());
        }
    }
}

===========================================================================

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Arrays;

import java.util.Scanner;



public class Registration {



    public static void main(String[] args) {



        // Below code is for Step 2

        Student students[] = new Student[3];

        students[0] = new Student("John", "Doe", "123-45", 40, 4.6);

        students[1] = new Student("John", "Smith", "124-45", 50, 4.7);

        students[2] = new Student("Alan", "Donald", "127-65", 30, 2.7);



        // printing the students

        for (Student student : students) {

            System.out.println(student);

        }



        Arrays.sort(students);

        //insertionSort(students);

        System.out.println("====================");

        System.out.println("Sorting the students");

        System.out.println("====================");



        // printing the students again

        for (Student student : students) {

            System.out.println(student);

        }



        // Step 3



        Student fiveStudents[] = {new Student(), new Student(), new Student(), new Student(), new Student()};

        Scanner scanner = new Scanner(System.in);

        for (int i = 0; i < fiveStudents.length; i++) {

            System.out.println("Student " + (i + 1));

            System.out.print("Enter First Name: ");

            fiveStudents[i].setFirstName(scanner.nextLine());

            System.out.print("Enter Last Name: ");

            fiveStudents[i].setLastName(scanner.nextLine());

            System.out.print("Enter Student ID: ");

            fiveStudents[i].setStudentId(scanner.nextLine());

            System.out.print("Enter total credits (whole number) : ");

            fiveStudents[i].setTotalCredits(scanner.nextInt());

            System.out.print("Enter GPA score: ");

            fiveStudents[i].setGpa(scanner.nextDouble());

            scanner.nextLine();

        }



        insertionSort(fiveStudents);



        // printing the students entered by the user

        for (Student student : fiveStudents) {

            System.out.println(student);

        }



        storeRoster(fiveStudents);

        loadRoster();



    }



    // Step 5

    public static Student[] loadRoster() {



        Student students[] = new Student[5];

        int index = 0;

        String fileName = "Roster1.txt";

        try {

            Scanner fileReader = new Scanner(new File(fileName));

            while (fileReader.hasNextLine()) {

                String tokens[] = fileReader.nextLine().split("\\s+");

                if (tokens.length == 5) {



                    int credits = Integer.parseInt(tokens[3]);

                    double gpa = Double.parseDouble(tokens[4]);



                    students[index++] = new Student(tokens[0], tokens[1], tokens[2], credits, gpa);

                }

            }

        } catch (FileNotFoundException e) {

            System.out.println("Error: Unable to read/open file: " + fileName);

        }



        // printing the students after reading from the file

        System.out.println("=================================================");

        System.out.println("printing the students after reading from the file");

        System.out.println("=================================================");

        for (Student student : students) {

            System.out.println(student);

        }

        return students;

    }



    // Part Step 4

    public static void storeRoster(Student[] students) {

        String fileName = "Roster1.txt";

        try {

            FileWriter writer = new FileWriter(new File(fileName));

            for (Student student : students) {

                writer.write(student.getFirstName() + " ");

                writer.write(student.getLastName() + " ");

                writer.write(student.getStudentId() + " ");

                writer.write(student.getTotalCredits() + " ");

                writer.write(student.getGpa() + "\r\n");

                writer.flush();

            }

            writer.close();

            System.out.println(fileName + " updated successfully");

        } catch (IOException e) {

            System.out.println("Error: Unable to write/open file: " + fileName);

        }

    }



    public static void insertionSort(Student[] students) {



        int n = students.length;

        for (int i = 1; i < n; i++) {

            Student key = students[i];

            int j = i - 1;

            while (j >= 0 && students[j].compareTo(key) > 0) {

                students[j + 1] = students[j];

                j = j - 1;

            }

            students[j + 1] = key;

        }

    }

}

===========================================================================

Add a comment
Know the answer?
Add Answer to:
Registration Language Java Step 1: Design a class called Student. The Student class should contain 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
  • 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...

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

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

  • JAVA program Note: you can't change anything is already written Student.java public class Student {   ...

    JAVA program Note: you can't change anything is already written Student.java public class Student {    private String name;    private String major;    private double gpa;    public Student(String name, String major, double gpa) {        // TO-DO: Assign the given parameters to the data fields. Use the this keyword.    }    public double getGPA() {        // TO-DO: return this.gpa    }    public String getName() {        // TO-DO: return this.name    }...

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

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

  • Design a class named Student. The Student class should have a Firstname, a Lastname, an Address,...

    Design a class named Student. The Student class should have a Firstname, a Lastname, an Address, an email address, a major and a GPA. Determine the data types for each property, then create the class diagram and write the pseudo-code that defines the class. You can use java language for pseudo-code

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • The object class should be a Student with the following attributes: id: integer first name: String...

    The object class should be a Student with the following attributes: id: integer first name: String last name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. Write a sort method that must be your original work and included in the main class. The sort method will sort based on student...

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