Question

PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...

PLEASE DO IN JAVA


3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed.


4) Add a method storeCollege(String filename) to College. This method saves all the college information in a file (name specified in variable filename). Use ObjectOutputStream and write one single object. Add method loadStoreCollege(String filename) which loads the information written by storeCollege.


5) Add the program’s main method to College. The main method instantiates a new College, adds some faculty and students in random order (populated with their name and ID), sorts them using the code in Part 3, and stores them into a file. It then instantiates a new college that loads the data previously written to file, and verifies that the first and second college contain identical data. The main method does not ask for any input from the terminal.


PLEASE DO IN JAVA... BASED ON PREVIOUS QUESTION WHERE PARTS 1 AND 2 WERE ANSWERED

3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed.


4) Add a method storeCollege(String filename) to College. This method saves all the college information in a file (name specified in variable filename). Use ObjectOutputStream and write one single object. Add method loadStoreCollege(String filename) which loads the information written by storeCollege.


5) Add the program’s main method to College. The main method instantiates a new College, adds some faculty and students in random order (populated with their name and ID), sorts them using the code in Part 3, and stores them into a file. It then instantiates a new college that loads the data previously written to file, and verifies that the first and second college contain identical data. The main method does not ask for any input from the terminal.


// Part 1

public interface Person {
  
    public void setName(String name);
    public String getName();

    public void setID(String id);
    public String getID();
}

public class Student implements Person {

    private String name;

    private String studentID;

    private double GPA;

    public Student() {

        name = "";

        studentID = "";

        GPA = 0;

    }

    public Student(String name, String studentID) {

        this.name = name;

        this.studentID = studentID;

    }

    public Student(String name, String studentID, double GPA) {

        this.name = name;

        this.studentID = studentID;

        this.GPA = GPA;

    }

    public double getGPA() {

        return GPA;

    }

    public void setGPA(double GPA) {

        this.GPA = GPA;

    }

    @Override

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String getName() {

        return name;

    }

    @Override

    public void setID(String id) {

        this.studentID = id;

    }

    @Override

    public String getID() {

        return studentID;

    }

}
public class Faculty implements Person {

    private String name;

    private String facultyID;

    private String department;

    public Faculty() {

        name = "";

        facultyID = "";

        department = "";

    }

    public Faculty(String name, String studentID) {

        this.name = name;

        this.facultyID = studentID;

    }

    public Faculty(String name, String studentID, String department) {

        this.name = name;

        this.facultyID = studentID;

        this.department = department;

    }

    public String getDepartment() {

        return department;

    }

    public void setDepartment(String department) {

        this.department = department;

    }

    @Override

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String getName() {

        return name;

    }

    @Override

    public void setID(String id) {

        this.facultyID = id;

    }

    @Override

    public String getID() {

        return facultyID;

    }

}
// Part 2

public class College {

    private String collegeName;

    private Person persons[];

    private int count;

    public College(String collegeName) {

        this.collegeName = collegeName;

        persons = new Person[100];

        count = 0;

    }

    public void addPerson(Person aPerson) {

        if (count < persons.length) {

            persons[count++] = aPerson;

        }

    }

    public String getName() {

        return collegeName;

    }

    public void setName(String collegeName) {

        this.collegeName = collegeName;

    }

}

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

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

Student.java
=====
import java.io.Serializable;

// Part 1

public class Student implements Person, Serializable {


private String name;

private String studentID;

private double GPA;


public Student() {

name = "";

studentID = "";

GPA = 0;

}


public Student(String name, String studentID) {

this.name = name;

this.studentID = studentID;

}


public Student(String name, String studentID, double GPA) {

this.name = name;

this.studentID = studentID;

this.GPA = GPA;

}


public double getGPA() {

return GPA;

}


public void setGPA(double GPA) {

this.GPA = GPA;

}


@Override

public void setName(String name) {

this.name = name;

}


@Override

public String getName() {

return name;

}


@Override

public void setID(String id) {

this.studentID = id;

}


@Override

public String getID() {

return studentID;

}
  
@Override
public String toString() {
   return "Student id = " + getID() + ", name = " + getName() + ", gpa = " + getGPA();
}

}

Faculty.java
=====
import java.io.Serializable;

public class Faculty implements Person, Serializable{


private String name;

private String facultyID;

private String department;


public Faculty() {

name = "";

facultyID = "";

department = "";

}


public Faculty(String name, String studentID) {

this.name = name;

this.facultyID = studentID;

}


public Faculty(String name, String studentID, String department) {

this.name = name;

this.facultyID = studentID;

this.department = department;

}


public String getDepartment() {

return department;

}


public void setDepartment(String department) {

this.department = department;

}


@Override

public void setName(String name) {

this.name = name;

}


@Override

public String getName() {

return name;

}


@Override

public void setID(String id) {

this.facultyID = id;

}


@Override

public String getID() {

return facultyID;

}
  
@Override
public String toString() {
   return "Faculty id = " + getID() + ", name = " + getName() + ", department = " + getDepartment();
}

}


College.java
======
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class College implements Serializable{


private String collegeName;

private Person persons[];

private int count;


public College(String collegeName) {

this.collegeName = collegeName;

persons = new Person[100];

count = 0;

}


public void addPerson(Person aPerson) {

if (count < persons.length) {

persons[count++] = aPerson;

}

}


public String getName() {

return collegeName;

}


public void setName(String collegeName) {

this.collegeName = collegeName;

}
  
public void sort() {
   int min;
   Person temp;
   for(int i = 0 ;i < count; i++) {
       min = i;
       for(int j = i+1; j < count; j++) {
           if(compare(persons[j], persons[min]) < 0)
               min = j;
       }
      
       temp = persons[i];
       persons[i] = persons[min];
       persons[min] = temp;
   }
}

private int compare(Person p1, Person p2) {
   if(p1 instanceof Student && p2 instanceof Student) {
       return ((Student)p1).getName().compareTo(((Student)p2).getName());
   }
   else if(p1 instanceof Faculty && p2 instanceof Faculty) {
       return ((Faculty)p1).getName().compareTo(((Faculty)p2).getName());
   }
   else if(p1 instanceof Student)
       return -1;
   else
       return 1;
}
  
  
public void storeCollege(String filename) {
   try {
           ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
           oos.writeObject(this);
           oos.close();
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
}
  
  
public void loadCollege(String filename) {
   try {
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
           College c = (College)   ois.readObject();
           ois.close();
           this.collegeName = c.collegeName;
           this.count = c.count;
           this.persons = c.persons;
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
}
  
  
@Override
public String toString() {
   String s = "College: " + getName() + "\n" + "Count = " + count + "\n";
          
   for(int i = 0; i < count; i++)
       s += persons[i] + "\n";
   return s;
  
}
  
public static void main(String[] args) {
       Student s1 = new Student("John", "111", 3.5);
       Student s2 = new Student("Bill", "222", 3.1);
       Student s3 = new Student("Christina", "333", 3.9);

       Faculty f1 = new Faculty("Josh", "567", "CSC");
       Faculty f2 = new Faculty("Bob", "345", "Math");
       Faculty f3 = new Faculty("Thomas" , "234", "Physics");
      
       College c1 = new College("ABC College");
       c1.addPerson(s1);
       c1.addPerson(f1);
       c1.addPerson(f2);
       c1.addPerson(s2);
       c1.addPerson(f3);
       c1.addPerson(s3);
      
       System.out.println("Before sorting");
       System.out.println(c1);
       c1.sort();
       System.out.println("After sorting");
       System.out.println(c1);
      
      
       System.out.println("Storing college object c1 into file college.bin");
       c1.storeCollege("college.bin");
      
      
       College c2 = new College("");
       System.out.println("Reading college object from file college.bin into c2");
       c2.loadCollege("college.bin");
      
       System.out.println("college object loaded from file is \n" + c2);
}
      
}


output
===
Before sorting
College: ABC College
Count = 6
Student id = 111, name = John, gpa = 3.5
Faculty id = 567, name = Josh, department = CSC
Faculty id = 345, name = Bob, department = Math
Student id = 222, name = Bill, gpa = 3.1
Faculty id = 234, name = Thomas, department = Physics
Student id = 333, name = Christina, gpa = 3.9

After sorting
College: ABC College
Count = 6
Student id = 222, name = Bill, gpa = 3.1
Student id = 333, name = Christina, gpa = 3.9
Student id = 111, name = John, gpa = 3.5
Faculty id = 345, name = Bob, department = Math
Faculty id = 567, name = Josh, department = CSC
Faculty id = 234, name = Thomas, department = Physics

Storing college object c1 into file college.bin
Reading college object from file college.bin into c2
college object loaded from file is
College: ABC College
Count = 6
Student id = 222, name = Bill, gpa = 3.1
Student id = 333, name = Christina, gpa = 3.9
Student id = 111, name = John, gpa = 3.5
Faculty id = 345, name = Bob, department = Math
Faculty id = 567, name = Josh, department = CSC
Faculty id = 234, name = Thomas, department = Physics

Add a comment
Know the answer?
Add Answer to:
PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...
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
  • PLEASE DO IN JAVA 1) Create interface named “Person” which exposes getter/setter methods for the person’s...

    PLEASE DO IN JAVA 1) Create interface named “Person” which exposes getter/setter methods for the person’s name and college ID: 1. getName 2. setName 3. getID 4. setID. The college ID is represented as “int”. Define two classes, Student and Faculty, which implement Person. Add field GPA to Student and department to faculty. Make them private and create corresponding getters (getGPA, getDepartment) and setters (setGPA, setDepartment). Use appropriate types. 2) Write a class College, which contains the name of the...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • JAVA How to add array to develop a contact list application for the person class objects...

    JAVA How to add array to develop a contact list application for the person class objects developed in this code? The application will include functionality to add, remove, sort and search the contact list. You should also include a method to output the contents of a contact searched for, and also to output the entire list. The code: package BankProg; public class personal {    private String facebook;    public personal() { }    public personal(String facebook) {    this.facebook...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

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