In this exercise, we will be refactoring a working program. Code refactoring is a process to improve an existing code in term of its internal structure without changing its external behavior.
In this particular example, we have three classes Course, Student, Instructor in addition to Main. All classes are well documented. Read through them to understand their structure. In brief, Course models a course that has a title, max capacity an instructor and students. Instructor models a course instructor who has a name, id and salary. Student models a student who has a name, id, and credits. The Main class create hypothetical instructor, students and course and calls the method print in each object to format the output (see below).
Hani Javadi, instructor ID: 888777666, salary: 11111.0 --- --- --- Joe Loop, student ID: 810123456, credits: 10 --- --- --- Mary Collection, student ID: 811987321, credits: 25 --- --- --- Ke Method, student ID: 810010101, credits: 0 --- --- --- MIST 4700 MWF 6am-7am Instructor: Hani Javadi Room: Z101 Class list: Joe Loop, student ID: 810123456, credits: 10 Mary Collection, student ID: 811987321, credits: 25 Ke Method, student ID: 810010101, credits: 0 Number of students: 3
This program is working and fully functional. However, there is a substantial code duplication between Student and Instructor. Your job is to eliminate this redundancy and improve the program structure by following these steps:
Effectively the super class Person will take care of printing name, then the subclasses will provide the added functionality of displaying ID and credits (Student) and salary (Instructor)
The Main and Course classes should compile and work as usual at this point producing exactly same output as above. Happy refactoring!
The code that is given and needs to be worked on follows (There are 5 separate classes. they are Main.java, Person.java, Student.java, Course.java, Instructor.java) :
//Main.java Class
public class Main {
public static void main(String[] args) {
Instructor instructor = new
Instructor();
instructor.setName("Hani
Javadi");
instructor.setID("888777666");
instructor.setSalary(11111);
instructor.print();
System.out.println("--- ---
---");
Student student1 = new
Student();
student1.setName("Joe Loop");
student1.setID("810123456");
student1.addCredits(10);
student1.print();
System.out.println("--- ---
---");
Student student2 = new
Student();
student2.setName("Mary
Collection");
student2.setID("811987321");
student2.addCredits(25);
student2.print();
System.out.println("--- ---
---");
Student student3 = new
Student();
student3.setName("Ke
Method");
student3.setID("810010101");
student3.print();
System.out.println("--- ---
---");
Course course = new Course("MIST
4700", 40);
course.setInstructor(instructor);
course.setRoom("Z101");
course.setTime("MWF
6am-7am");
course.enrollStudent(student1);
course.enrollStudent(student2);
course.enrollStudent(student3);
course.printList();
}
}
//Person.java class
public class Person {
}
//Student.java Class
/**
* The Student class represents a student in a student
administration system. It
* holds the student details relevant in our context.
*/
public class Student {
// the student's full name
private String name;
// the student ID
private String id;
// the amount of credits for study taken so far
private int credits;
public String getName() {
return name;
}
public void setName(String replacementName) {
name = replacementName;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
/**
* Add some credit points to the student's accumulated
credits.
*/
public void addCredits(int additionalPoints) {
credits += additionalPoints;
}
/**
* Return the number of credit points this student has
accumulated.
*/
public int getCredits() {
return credits;
}
/**
* Return the login name of this student. The login
name is a combination of the
* first four characters of the student's name and the
first three characters of
* the student's ID number.
*/
public String getLoginName() {
return name.substring(0, 4) +
id.substring(0, 3);
}
/**
* Print the student's name and ID number to the output
terminal.
*/
public void print() {
System.out.println(name + ",
student ID: " + id + ", credits: " + credits);
}
}
//Course.java Class
import java.util.*;
/**
* The Course class represents an enrollment list for one class. It
stores the
* time, room and participants of the lab, as well as the
instructor's name.
*/
public class Course {
private String name;
private String room;
private String timeAndDay;
private ArrayList<Student> students;
private Instructor instructor;
private int capacity;
/**
* Create a Course with a maximum number of enrolments.
All other details are
* set to default values.
*/
public Course(String name, int maxNumberOfStudents)
{
this.name = name;
instructor = new
Instructor();
room = "unknown";
timeAndDay = "unknown";
students = new
ArrayList<Student>();
capacity =
maxNumberOfStudents;
}
/**
* Add a student to this Course.
*/
public void enrollStudent(Student newStudent) {
boolean atCapacity =
(students.size() == capacity);
if (atCapacity == true) {
System.out.println("The class is full, you cannot enrol.");
} else {
students.add(newStudent);
}
}
/**
* Return the number of students currently enrolled in
this Course.
*/
public int numberOfStudents() {
return students.size();
}
/**
* Set the room number for this Course.
*/
public void setRoom(String roomNumber) {
room = roomNumber;
}
/**
* Set the time for this Course. The parameter should
define the day and the
* time of day, such as "Friday, 10am".
*/
public void setTime(String timeAndDayString) {
timeAndDay =
timeAndDayString;
}
/**
* Set the name of the instructor for this
Course.
*/
public void setInstructor(Instructor instructorN)
{
instructor = instructorN;
}
/**
* Print out a class list with other Course details to
the standard terminal.
*/
public void printList() {
System.out.println(name + " " +
timeAndDay);
System.out.println("Instructor: " +
instructor.getName() + " Room: " + room);
System.out.println("Class
list:");
for (Student student : students)
{
student.print();
}
System.out.println("Number of
students: " + numberOfStudents());
}
}
//instructor.java Class
/**
* Write a description of class Instructor here.
*/
public class Instructor {
// the student's full name
private String name;
// the student ID
private String id;
// the amount of credits for study taken so far
private double salary;
public String getName() {
return name;
}
public void setName(String replacementName) {
name = replacementName;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getLoginName() {
return name.substring(0, 4) +
id.substring(0, 3);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
salary = newSalary;
}
public void print() {
System.out.println(name + ",
instructor ID: " + id + ", salary: " + salary);
}
}
SOLUTION :-
CODE IN JAVA :-
Person.java
public class Person {
private String name, id;
public Person()
{
name = "";
id = "";
}
public Person(String name, String id)
{
this.name = name;
this.id = id;
}
public String getName()
{
return this.name;
}
public String getId()
{
return this.id;
}
public void setName(String name)
{
this.name = name;
}
public void setId(String id)
{
this.id = id;
}
public void print()
{
System.out.print(getName());
}
}
Student.java
public class Student extends Person{
private int credits;
public Student()
{
super();
this.credits = 0;
}
public Student(String name, String id, int credits)
{
super(name, id);
this.credits = credits;
}
public int getCredits() {
return credits;
}
public void setCredits(int credits) {
this.credits = credits;
}
@Override
public void print()
{
super.print();
System.out.println(", Student Id: " + super.getId() + ", Credits: "
+ getCredits() + "");
}
}
Instructor.java
public class Instructor extends Person{
private double salary;
public Instructor()
{
super();
salary = 0.0;
}
public Instructor(String name, String id, double salary)
{
super(name, id);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public void print()
{
super.print();
System.out.println(", Instructor Id: " + getId() + ", Salary: $" +
getSalary());
}
}
Course.java
public class Course {
private String title;
private int maxCapacity;
private Instructor instructor;
private Student[] students;
public Course()
{
this.title = "";
this.maxCapacity = 0;
this.instructor = null;
this.students = new Student[this.maxCapacity];
}
public Course(String title, int maxCapacity, Instructor
instructor, Student[] students) {
this.title = title;
this.maxCapacity = maxCapacity;
this.instructor = instructor;
this.students = students;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getMaxCapacity() {
return maxCapacity;
}
public void setMaxCapacity(int maxCapacity) {
this.maxCapacity = maxCapacity;
}
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public void print()
{
System.out.println("Course Title: " + getTitle() +
"\nMax Capacity: " + getMaxCapacity() +
"\nInstructor: " + getInstructor().getName() +
"\n\nClass list:");
for(int i = 0; i < students.length; i++)
{
System.out.print(i + 1 + ". ");
students[i].print();
}
System.out.println("\nNumber of students: " +
students.length);
}
}
CourseDriver.java (Main class)
public class CourseDriver {
public static void main(String[] args)
{
// creating an array of students
Student[] students = {
new Student("Joe Loop", "810123456", 10),
new Student("Mary Collection", "811987321", 25),
new Student("Ke Method", "810010101", 0)
};
System.out.println(students.length + " students created!");
// creating an instructor
Instructor instructor = new Instructor("Hani Javadi", "888777666",
11111.0);
System.out.print("Instructor created: ");
instructor.print();
// creating an instance of a Course
Course course = new Course("MIST", 4700, instructor,
students);
System.out.println("\n***** COURSE DETAILS
*****\n--------------------------");
course.print();
System.out.println();
}
}
In this exercise, we will be refactoring a working program. Code refactoring is a process to...
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 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; } ...
Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...
java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...
JAVA
3files seprate
1.Classroom.java
2.ClassroomTester.java (provided)
3.Student.java(provided)
Use the following files:
ClassroomTester.java
import java.util.ArrayList;
/**
* Write a description of class UniversityTester here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ClassroomTester
{
public static void main(String[] args)
{
ArrayList<Double> grades1 = new ArrayList<>();
grades1.add(82.0);
grades1.add(91.5);
grades1.add(85.0);
Student student1 = new Student("Srivani", grades1);
ArrayList<Double> grades2 = new ArrayList<>();
grades2.add(95.0);
grades2.add(87.0);
grades2.add(99.0);
grades2.add(100.0);
Student student2 = new Student("Carlos", grades2);
ArrayList<Double> grades3 = new...
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 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)...
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....
I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...
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...