For this java project, you will create a Java program for a school. The purpose
ID: 3746565 • Letter: F
Question
For this java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain:
The room number of the classroom.
The teacher and the subject assigned to the classroom.
A list of students assigned to the classroom including their student id and final grade.
Application Structure
Project should be organized in a directory. Create a directory called “Project01_School”
The following shows the directory and files you should have:
Project01_School Directory
Displayable.java
Create Displayable interface. The interface should declare one method as follows:
public abstract String display()
Person.java
Create Person class. Make it an abstract class. Declare the following instance variables:
String firstName
String lastName
Include the getter and setter methods for each variable. Use the camelcase naming convention for methods and variables. Include a method named getFullName() that returns both names concatenated into a String with space between the first and last name.
Teacher.java
Create the Teacher class. It inherits the Person abstract class and implements the Displayable interface. It defines only one variable as follows:
String subject
Include the getter and setter methods for the variable. Use the camelcase naming convention. Provide a constructor that uses the following parameters to initialize the variables:
String firstName
String lastName
String subject
Override the display() method. It should return a String containing the teacher’s name using the getFullName() method defined in Person and the subject taught as follows:
John Smith teaches Computer Science
Student.java
Create the Student class. It inherits the Person abstract class and implements the Displayable interface. It defines two variables:
int studentId
int finalGrade
Include the getter and setter methods for the variables. Use the camelcase naming convention. Provide a constructor that uses the following parameters to initialize the variables:
String firstName
String lastName
int studentId
IntfinalGrade
Override the display() method. It should return a String containing the student’s id, the student’s name using the getFullName() method defined in Person, and the student’s final grade as follows:
Student ID: 1 John Doe Final Grade: 90
Classroom.java
Create the Classroom class. It inherits the Person abstract class and implements the Displayable interface. It defines three variables:
int roomNumber
Displayable teacher (note that the Teacher instance is downcast to the Displayable interface)
ArrayList<Displayable> students (note that the Student instances is downcast to the Displayable interface)
Provide a no argument constructor. Provide another constructor that uses the following parameters to initialize the variables:
int roomNumber
Displayable teacher
ArrayList<Displayable> students
In your project directory create the PrintReports class.
PrintReports should define the following methods using the listed method signatures:
public PrintReports()
public Displayable enterClassroom()
public Displayable enterTeacher()
public Displayable enterStudents()
void report(ArrayList<Displayable>)
Note that the methods are non static. One way to escape the static requirement main() imposes is to use this approach:
public class School
{
public static void main(String[] args) {
New PrintReports();
}
The PrintReports class
The public PrintReport() constructor
In a do..while loop collect the data need to create a Classroom object using the enterClassroom() method. You should be able
to create any number of Classroom objects. Prompt the user so he or she can enter another Classroom or quit the loop. Store the Classroom objects in an ArrayList<Displayable> collection.
The public Displayable enterClassroom() Method
Prompt the user for a room number. Save it as an int. The room number must be 100 or greater. If the user enters a lower number, he or she should be prompted again until an acceptable number is entered.
Call enterTeacher() to obtain an instance of a teacher and store it as a Displayable object.
In a do..while loop, call enterStudent() to obtain a Student as a Displayable object and store it in an ArrayList<Displayable> collection. Prompt the user so he or she can enter another student or quit the loop.
The public Displayable enterTeacher() Method
The method should prompt the user for their first and last name as well as the subject they teach. Create an instance of Teacher using that data and return the object as instance of Displayable.
The public Displayable enterStudent() Method
Prompt the user for student id, first and last names, and their final grade. Using the data, create a Student instance. A student’s id must be greater than 0. A student’s final grade must be between 0 and 100. Return the student object as a Displayable object.
The void report(ArrayLIst<Displayable>) Method
I a for loop, iterate through the ArrayList<Displayable> collection containing the downcast Classroom objects.
Call the display() method defined in Classroom. It should report the room number.
It should call the display() method in the teacher variable to report the teacher assigned to the classroom.
In a for loop it should iterate through the ArrayList<Displayable> collection of Student objects calling the display() method for each one.
Explanation / Answer
/* KeyboardReader.java */
package course.software.reports.util;
import java.util.Scanner;
public class KeyboardReader {
private final Scanner scanner = new Scanner(System.in);
public void printStatement(String statement) {
System.out.println(statement);
}
public String readAnswer(String question) {
System.out.print(question);
String answer = scanner.nextLine();
return answer;
}
}
/* Displayable.java */
package course.software.reports.util;
public interface Displayable {
String display();
}
/* Person.java */
package course.software.reports.school;
public abstract class Person {
private String firstName;
private String lastName;
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 getFullName() {
return this.firstName + " " + this.lastName;
}
}
/* Teacher.java */
package course.software.reports.school;
import course.software.reports.util.Displayable;
public class Teacher extends Person implements Displayable {
private String subject;
public Teacher() {
}
public Teacher(String firstName, String lastName, String subject) {
this.setFirstName(firstName);
this.setLastName(lastName);
this.setSubject(subject);
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Override
public String display() {
return this.getFullName() + " teaches " + this.getSubject();
}
}
/* Student.java */
package course.software.reports.school;
import course.software.reports.util.Displayable;
public class Student extends Person implements Displayable {
private int studentId;
private int finalGrade;
public Student() {
}
public Student(String firstName, String lastName, int studentId, int finalGrade) {
this.setFirstName(firstName);
this.setLastName(lastName);
this.setStudentId(studentId);
this.setFinalGrade(finalGrade);
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(int finalGrade) {
this.finalGrade = finalGrade;
}
@Override
public String display() {
return "Student ID: " + this.getStudentId() + " " + this.getFullName() + " Final Grade: " + this.getFinalGrade();
}
}
/* Classroom.java */
package course.software.reports.school;
import course.software.reports.util.Displayable;
import java.util.ArrayList;
public class Classroom implements Displayable {
private int roomNumber;
private Displayable teacher;
private ArrayList<Displayable> students;
public Classroom() {
}
public Classroom(int roomNumber, Displayable teacher, ArrayList<Displayable> students) {
this.setRoomNumber(roomNumber);
this.setTeacher(teacher);
this.setStudents(students);
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public Displayable getTeacher() {
return teacher;
}
public void setTeacher(Displayable teacher) {
this.teacher = teacher;
}
public ArrayList<Displayable> getStudents() {
return students;
}
public void setStudents(ArrayList<Displayable> students) {
this.students = students;
}
@Override
public String display() {
String output = "Room Number: " + this.getRoomNumber() + " ";
output += this.getTeacher().display() + " ";
ArrayList<Displayable> studentList = this.getStudents();
for (int i=0; i<studentList.size(); i++) {
output += studentList.get(i).display();
if (i < (studentList.size()-1)) {
output += " ";
}
}
return output;
}
}
/* PrintReports.java */
package course.software.reports;
import course.software.reports.school.Classroom;
import course.software.reports.school.Student;
import course.software.reports.school.Teacher;
import course.software.reports.util.Displayable;
import course.software.reports.util.KeyboardReader;
import java.util.ArrayList;
public class PrintReports {
ArrayList<Displayable> classroomList = new ArrayList<>();
public PrintReports() {
KeyboardReader reader = new KeyboardReader();
Displayable classroom;
String addClassroom;
reader.printStatement("First You Need To Create A Classroom");
do {
classroom = enterClassroom();
if (classroom != null) {
classroomList.add(classroom);
}
boolean validInput = true;
do {
addClassroom = reader.readAnswer("Enter Another Classroom? (Y/N): ");
if ((!addClassroom.equalsIgnoreCase("Y")) && (!addClassroom.equalsIgnoreCase("N"))) {
validInput = false;
}
} while (!validInput);
} while (addClassroom.equalsIgnoreCase("Y"));
reader.printStatement("");
}
public Displayable enterClassroom() {
KeyboardReader reader = new KeyboardReader();
boolean validInput = true;
int roomNumber = 0;
Displayable teacher = null;
ArrayList<Displayable> studentList = new ArrayList<>();
do {
String roomNo = reader.readAnswer("Enter Room Number: ");
try {
roomNumber = Integer.parseInt(roomNo);
if (roomNumber < 100) {
validInput = false;
}
} catch (NumberFormatException ne) {
validInput = false;
}
} while (!validInput);
reader.printStatement("");
reader.printStatement("Now You Need To Enter A Teacher For The Classroom.");
teacher = enterTeacher();
reader.printStatement("");
reader.printStatement("Now You Need To Add Students For The Classroom");
Displayable student;
validInput = true;
String addStudent;
do {
student = enterStudent();
if (student != null) {
studentList.add(student);
}
do {
addStudent = reader.readAnswer("Enter Another Student? (Y/N): ");
if ((!addStudent.equalsIgnoreCase("Y")) && (!addStudent.equalsIgnoreCase("N"))) {
validInput = false;
}
} while (!validInput);
} while (addStudent.equalsIgnoreCase("Y"));
reader.printStatement("");
Classroom classroom = new Classroom(roomNumber, teacher, studentList);
return classroom;
}
public Displayable enterTeacher() {
KeyboardReader reader = new KeyboardReader();
String firstName = reader.readAnswer("Enter Teacher First Name: ");
String lastName = reader.readAnswer("Enter Teacher Last Name: ");
String subject = reader.readAnswer("Enter Subject Taught: ");
Teacher teacher = new Teacher(firstName,lastName,subject);
return teacher;
}
public Displayable enterStudent() {
KeyboardReader reader = new KeyboardReader();
String firstName = reader.readAnswer("Enter Student First Name: ");
String lastName = reader.readAnswer("Enter Student Last Name: ");
int studentId = 0;
boolean validInput = true;
do {
String id = reader.readAnswer("Enter Student ID: ");
try {
studentId = Integer.parseInt(id);
if (studentId < 0) {
validInput = false;
}
} catch (NumberFormatException ne) {
validInput = false;
}
} while (!validInput);
int studentGrade = 0;
validInput = true;
do {
String grade = reader.readAnswer("Enter Student Final Grade: ");
try {
studentGrade = Integer.parseInt(grade);
if ((studentGrade < 0) || (studentGrade > 100)) {
validInput = false;
}
} catch (NumberFormatException ne) {
validInput = false;
}
} while (!validInput);
reader.printStatement("");
Student student = new Student(firstName, lastName, studentId, studentGrade);
return student;
}
public ArrayList<Displayable> getClassroomList() {
return classroomList;
}
void report(ArrayList<Displayable> classroomList) {
KeyboardReader reader = new KeyboardReader();
for (int i=0; i<classroomList.size(); i++) {
reader.printStatement("----------------------------------------------------------");
Displayable classroom = classroomList.get(i);
reader.printStatement(classroom.display());
reader.printStatement("----------------------------------------------------------");
}
}
public static void main (String[] args) {
PrintReports printReports = new PrintReports();
printReports.report(printReports.getClassroomList());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.