[go: up one dir, main page]

0% found this document useful (0 votes)
8 views26 pages

7th Open Book

The document describes a school management program that includes classes for Teacher, Student, and SchoolClass, implementing encapsulation, constructors, and methods for managing students and teachers. It outlines the creation of default, parameter-based, and copy constructors for the Teacher and Student classes, as well as a static method to find the youngest student in a class. Additionally, it includes a main method for initializing a SchoolClass object with user input.

Uploaded by

martinmar1317
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views26 pages

7th Open Book

The document describes a school management program that includes classes for Teacher, Student, and SchoolClass, implementing encapsulation, constructors, and methods for managing students and teachers. It outlines the creation of default, parameter-based, and copy constructors for the Teacher and Student classes, as well as a static method to find the youngest student in a class. Additionally, it includes a main method for initializing a SchoolClass object with user input.

Uploaded by

martinmar1317
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Consider a school where courses are taught by teachers to many students.

A Teacher is
described by his name, ID and salary. A Student is described by his name, ID and GPA.
Each course has a name, an array of 30 students and one teacher.

In the scope of this description do the following:

1. Write java code which implement the problem. (5 marks)

2. Apply encapsulation concept to Teacher class. (2 mark)

3. Define a default constructor for Teacher and Course classes. Use any values for
initialization, for references create objects. (3 marks)

4. Define a parameter based constructor for Teacher and Course classes. (3 marks)

5. Define a copy constructors for Teacher and Course classes. (3 marks)

6. In class Course, define a static method to calculate average GPA of students in a Course.
(2 mark)

7. In main create an array of 3 Teachers objects and initialize them from user. (2 marks)

// Base class Person

class Person {

private String name;

private String id;

// Constructor

public Person(String name, String id) {

this.name = name;

this.id = id;

// Getters and Setters

public String getName() {

return name;
}

public void setName(String name) {

this.name = name;

public String getId() {

return id;

public void setId(String id) {

this.id = id;

// Teacher class extending Person

class Teacher extends Person {

private double salary;

// Default constructor

public Teacher() {

super("Default Teacher", "T000", 50000); // default values

// Parameter-based constructor

public Teacher(String name, String id, double salary) {


super(name, id);

this.salary = salary;

// Copy constructor

public Teacher(Teacher other) {

super(other.getName(), other.getId());

this.salary = other.salary;

// Encapsulation: Getters and Setters

public double getSalary() {

return salary;

public void setSalary(double salary) {

this.salary = salary;

@Override

public String toString() {

return "Teacher{name='" + getName() + "', id='" + getId() + "', salary=" + salary + "}";

// Student class extending Person


class Student extends Person {

private double gpa;

// Default constructor

public Student() {

super("Default Student", "S000");

this.gpa = 0.0; // default GPA

// Parameter-based constructor

public Student(String name, String id, double gpa) {

super(name, id);

this.gpa = gpa;

// Copy constructor

public Student(Student other) {

super(other.getName(), other.getId());

this.gpa = other.gpa;

// Encapsulation: Getters and Setters

public double getGpa() {

return gpa;

}
public void setGpa(double gpa) {

this.gpa = gpa;

@Override

public String toString() {

return "Student{name='" + getName() + "', id='" + getId() + "', gpa=" + gpa + "}";

// Course class

class Course {

private String name;

private Student[] students;

private Teacher teacher;

// Default constructor

public Course() {

this.name = "Default Course";

this.students = new Student[30]; // Array of 30 students

for (int i = 0; i < 30; i++) {

students[i] = new Student(); // Initialize students with default values

this.teacher = new Teacher(); // Default teacher

}
// Parameter-based constructor

public Course(String name, Student[] students, Teacher teacher) {

this.name = name;

this.students = students;

this.teacher = teacher;

// Copy constructor

public Course(Course other) {

this.name = other.name;

this.teacher = new Teacher(other.teacher); // Copy teacher

this.students = new Student[30];

for (int i = 0; i < 30; i++) {

this.students[i] = new Student(other.students[i]); // Copy each student

// Static method to calculate average GPA of students in the Course

public static double calculateAverageGPA(Course course) {

double totalGPA = 0.0;

int count = 0;

for (Student student : course.students) {

totalGPA += student.getGpa();

count++;

return count > 0 ? totalGPA / count : 0.0; // Avoid division by zero


}

// Getters and Setters

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public Student[] getStudents() {

return students;

public void setStudents(Student[] students) {

this.students = students;

public Teacher getTeacher() {

return teacher;

public void setTeacher(Teacher teacher) {

this.teacher = teacher;

}
@Override

public String toString() {

return "Course{name='" + name + "', teacher=" + teacher + "}";

// Main class

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Create an array of 3 Teacher objects

Teacher[] teachers = new Teacher[3];

// Initialize Teachers from user input

for (int i = 0; i < 3; i++) {

System.out.println("Enter details for Teacher " + (i + 1) + ":");

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

String name = scanner.nextLine();

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

String id = scanner.nextLine();

System.out.print("Salary: ");

double salary = scanner.nextDouble();


scanner.nextLine(); // Consume the newline

teachers[i] = new Teacher(name, id, salary);

// Display the Teachers

for (Teacher teacher : teachers) {

System.out.println(teacher);

// Create students for the course and calculate GPA

Student[] students = new Student[30];

for (int i = 0; i < 30; i++) {

students[i] = new Student("Student " + (i + 1), "S" + (i + 1), 3.0 + (Math.random() * 1));
// Random GPA between 3.0 and 4.0

// Create a course and calculate average GPA

Course course = new Course("Math 101", students, teachers[0]);

double avgGPA = Course.calculateAverageGPA(course);

System.out.println("Average GPA of students in " + course.getName() + ": " + avgGPA);

}
Consider building a simple graphical user interface (GUI) library. In the library there exist
three main items. A Button, which is described by its x coordinate, y coordinate, and a label
representing the text written on the button. A Textield, which it described by its x
coordinate, y coordinate and max number of characters that can be written inside it. A
Panel, which is described by its x coordinate, y coordinate, and an array of 3 Textfields.

Buttons and Textfields are Clickable items, A clickable item is an item which can handle
mouse clicks. For simplicity, handic mouse click prints to screen "Button clicked" in class
Button, while prints to screen "TestField clicked" in class Teutfield.

In the scope of this description do the following:

1. Write java code which implements the problem. (5 marks)

2. Apply eocapsulation concept to Button class. (& mark)

3. Define a default constructor for Panel class. Lias non default valers for initialication, (3
marks)

4. Define argument-based constructor for Panel class. Applyahallow come (3 marks)

Define copy constructors for Panel class. Apply dece. copy. (3 marks)

7.

In class Panel, define a static method to calculate average number of characters of all
Textfileds in a Panel. (2 mark) In main, create an array of 3 Panels using the argument-
based constructor defined in part 4, In your work, use the required constructor such that,
sash panel has its own array ofob/ect. (2 marks)

// Base class 'Person', which represents a clickable item

class Person {

// The click method that will be inherited and overridden

public void click() {

// Default click behavior, can be overridden by subclasses

System.out.println("Person clicked");

}
// Button class extending Person

class Button extends Person {

private int x;

private int y;

private String label;

// Constructor for Button

public Button(int x, int y, String label) {

this.x = x;

this.y = y;

this.label = label;

// Encapsulation: Getters and Setters

public int getX() {

return x;

public void setX(int x) {

this.x = x;

public int getY() {

return y;

}
public void setY(int y) {

this.y = y;

public String getLabel() {

return label;

public void setLabel(String label) {

this.label = label;

// Override the click method to provide behavior specific to Button

@Override

public void click() {

System.out.println("Button clicked");

// TextField class extending Person

class TextField extends Person {

private int x;

private int y;

private int maxLength;


// Constructor for TextField

public TextField(int x, int y, int maxLength) {

this.x = x;

this.y = y;

this.maxLength = maxLength;

// Encapsulation: Getters and Setters

public int getX() {

return x;

public void setX(int x) {

this.x = x;

public int getY() {

return y;

public void setY(int y) {

this.y = y;

public int getMaxLength() {

return maxLength;
}

public void setMaxLength(int maxLength) {

this.maxLength = maxLength;

// Override the click method to provide behavior specific to TextField

@Override

public void click() {

System.out.println("TextField clicked");

// Panel class to hold TextField objects

class Panel {

private int x;

private int y;

private TextField[] textFields; // Array of TextFields

// Default constructor

public Panel() {

this.x = 0;

this.y = 0;

this.textFields = new TextField[3];

// Initialize TextFields with some default values

for (int i = 0; i < 3; i++) {


textFields[i] = new TextField(0, 0, 20); // default maxLength = 20

// Argument-based constructor

public Panel(int x, int y, TextField[] textFields) {

this.x = x;

this.y = y;

this.textFields = textFields;

// Copy constructor (shallow copy)

public Panel(Panel other) {

this.x = other.x;

this.y = other.y;

this.textFields = other.textFields; // Shallow copy

// Static method to calculate the average number of characters in the TextFields

public static double calculateAverageTextLength(Panel panel) {

int totalLength = 0;

for (TextField textField : panel.textFields) {

totalLength += textField.getMaxLength();

return totalLength / (double) panel.textFields.length;

}
// Getter methods

public int getX() {

return x;

public int getY() {

return y;

public TextField[] getTextFields() {

return textFields;

// Main class

public class Main {

public static void main(String[] args) {

// Creating TextField objects for Panels

TextField textField1 = new TextField(10, 10, 15);

TextField textField2 = new TextField(20, 20, 25);

TextField textField3 = new TextField(30, 30, 10);

// Create an array of Panels using the argument-based constructor

TextField[] panel1Fields = { textField1, textField2, textField3 };

Panel[] panels = new Panel[3];


panels[0] = new Panel(100, 200, panel1Fields);

// Let's print the average number of characters for the first panel

System.out.println("Average number of characters in Panel 1: "

+ Panel.calculateAverageTextLength(panels[0]));

// Click the Button and TextField

Button button = new Button(50, 50, "Submit");

button.click(); // Output: "Button clicked"

TextField textField = new TextField(100, 100, 30);

textField.click(); // Output: "TextField clicked"

Comsder a simple school management program. The program manages many school-
classes, Each school-class has a co (LA2B, IC etc) and one teacher and 20 students. A
student has a name, address, id and age. A teacher has a name, is salary and the name of
subject he teaches. in the scope of this description do the following:

Write java code which implements the problem. (3 marks)

Apply encapsulation concept to teacher class. (2 mark)

Define default constructors for student class. Use any values for initialization except
default values. (3 marks)

Define parameter based constructor for student class. (3 marks)

Define copy constructors for student class. (3 marks)

In class school-class, define a static method which prints the name of the youngest
student of a given school-class C

mark
An class school-class define a constant representing max number of students in the class
(i.e. the value 20). (2 maris)

In main, create a school-class object and initialize it from user. (2 marks)

import java.util.Scanner;

// Base class Person (common properties for Teacher and Student)

class Person {

private String name;

private String address;

// Default constructor

public Person() {

this.name = "Unknown";

this.address = "Unknown";

// Parameterized constructor

public Person(String name, String address) {

this.name = name;

this.address = address;

// Getter and Setter methods

public String getName() {

return name;

}
public void setName(String name) {

this.name = name;

public String getAddress() {

return address;

public void setAddress(String address) {

this.address = address;

// Teacher class extends Person

class Teacher extends Person {

private double salary;

private String subject;

// Default constructor

public Teacher() {

super("Default Teacher", "Unknown");

this.salary = 50000.0;

this.subject = "Math";

}
// Parameterized constructor

public Teacher(String name, String address, double salary, String subject) {

super(name, address); // Call to parent constructor (Person)

this.salary = salary;

this.subject = subject;

// Getter and Setter methods

public double getSalary() {

return salary;

public void setSalary(double salary) {

this.salary = salary;

public String getSubject() {

return subject;

public void setSubject(String subject) {

this.subject = subject;

// Student class extends Person


class Student extends Person {

private int id;

private int age;

// Default constructor

public Student() {

super("John Doe", "Unknown Address");

this.id = 0;

this.age = 18; // Default age

// Parameterized constructor

public Student(String name, String address, int id, int age) {

super(name, address); // Call to parent constructor (Person)

this.id = id;

this.age = age;

// Copy constructor

public Student(Student other) {

super(other.getName(), other.getAddress()); // Copy name and address from another


Student

this.id = other.id;

this.age = other.age;

}
// Getter and Setter methods

public int getId() {

return id;

public void setId(int id) {

this.id = id;

public int getAge() {

return age;

public void setAge(int age) {

this.age = age;

// SchoolClass class

class SchoolClass {

public static final int MAX_STUDENTS = 20; // Constant for max students in a class

private String className;

private Teacher teacher;

private Student[] students;

// Constructor to initialize SchoolClass with a teacher and an empty array of students


public SchoolClass(String className, Teacher teacher) {

this.className = className;

this.teacher = teacher;

this.students = new Student[MAX_STUDENTS];

// Static method to print the youngest student in the class

public static void printYoungestStudent(SchoolClass schoolClass) {

int youngestAge = Integer.MAX_VALUE;

String youngestStudentName = "";

for (int i = 0; i < MAX_STUDENTS; i++) {

if (schoolClass.students[i] != null && schoolClass.students[i].getAge() <


youngestAge) {

youngestAge = schoolClass.students[i].getAge();

youngestStudentName = schoolClass.students[i].getName();

System.out.println("The youngest student is: " + youngestStudentName);

// Method to add a student to the class

public void addStudent(Student student, int index) {

if (index >= 0 && index < MAX_STUDENTS) {

students[index] = student;
}

// Main class

public class SchoolManagement {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input for Teacher

System.out.print("Enter teacher's name: ");

String teacherName = scanner.nextLine();

System.out.print("Enter teacher's address: ");

String teacherAddress = scanner.nextLine();

System.out.print("Enter teacher's salary: ");

double teacherSalary = scanner.nextDouble();

scanner.nextLine(); // consume the leftover newline

System.out.print("Enter the subject the teacher teaches: ");

String teacherSubject = scanner.nextLine();

// Create teacher object

Teacher teacher = new Teacher(teacherName, teacherAddress, teacherSalary,


teacherSubject);

// Input for School Class


System.out.print("Enter the class name (e.g., LA2B, IC): ");

String className = scanner.nextLine();

// Create school class object

SchoolClass schoolClass = new SchoolClass(className, teacher);

// Add students to the class

for (int i = 0; i < SchoolClass.MAX_STUDENTS; i++) {

System.out.println("Enter details for student " + (i + 1));

System.out.print("Enter student name: ");

String studentName = scanner.nextLine();

System.out.print("Enter student address: ");

String studentAddress = scanner.nextLine();

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

int studentId = scanner.nextInt();

System.out.print("Enter student age: ");

int studentAge = scanner.nextInt();

scanner.nextLine(); // consume the leftover newline

// Create a student object and add to the class

Student student = new Student(studentName, studentAddress, studentId,


studentAge);

schoolClass.addStudent(student, i);

// Print the youngest student in the class


SchoolClass.printYoungestStudent(schoolClass);

You might also like