7th Open Book
7th Open Book
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.
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)
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)
class Person {
// Constructor
this.name = name;
this.id = id;
return name;
}
this.name = name;
return id;
this.id = id;
// Default constructor
public Teacher() {
// Parameter-based constructor
this.salary = salary;
// Copy constructor
super(other.getName(), other.getId());
this.salary = other.salary;
return salary;
this.salary = salary;
@Override
return "Teacher{name='" + getName() + "', id='" + getId() + "', salary=" + salary + "}";
// Default constructor
public Student() {
// Parameter-based constructor
super(name, id);
this.gpa = gpa;
// Copy constructor
super(other.getName(), other.getId());
this.gpa = other.gpa;
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
@Override
return "Student{name='" + getName() + "', id='" + getId() + "', gpa=" + gpa + "}";
// Course class
class Course {
// Default constructor
public Course() {
}
// Parameter-based constructor
this.name = name;
this.students = students;
this.teacher = teacher;
// Copy constructor
this.name = other.name;
int count = 0;
totalGPA += student.getGpa();
count++;
return name;
this.name = name;
return students;
this.students = students;
return teacher;
this.teacher = teacher;
}
@Override
// Main class
import java.util.Scanner;
System.out.print("Name: ");
System.out.print("ID: ");
String id = scanner.nextLine();
System.out.print("Salary: ");
System.out.println(teacher);
students[i] = new Student("Student " + (i + 1), "S" + (i + 1), 3.0 + (Math.random() * 1));
// Random GPA between 3.0 and 4.0
}
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.
3. Define a default constructor for Panel class. Lias non default valers for initialication, (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)
class Person {
System.out.println("Person clicked");
}
// Button class extending Person
private int x;
private int y;
this.x = x;
this.y = y;
this.label = label;
return x;
this.x = x;
return y;
}
public void setY(int y) {
this.y = y;
return label;
this.label = label;
@Override
System.out.println("Button clicked");
private int x;
private int y;
this.x = x;
this.y = y;
this.maxLength = maxLength;
return x;
this.x = x;
return y;
this.y = y;
return maxLength;
}
this.maxLength = maxLength;
@Override
System.out.println("TextField clicked");
class Panel {
private int x;
private int y;
// Default constructor
public Panel() {
this.x = 0;
this.y = 0;
// Argument-based constructor
this.x = x;
this.y = y;
this.textFields = textFields;
this.x = other.x;
this.y = other.y;
int totalLength = 0;
totalLength += textField.getMaxLength();
}
// Getter methods
return x;
return y;
return textFields;
// Main class
// Let's print the average number of characters for the first panel
+ Panel.calculateAverageTextLength(panels[0]));
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:
Define default constructors for student class. Use any values for initialization except
default values. (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)
import java.util.Scanner;
class Person {
// Default constructor
public Person() {
this.name = "Unknown";
this.address = "Unknown";
// Parameterized constructor
this.name = name;
this.address = address;
return name;
}
public void setName(String name) {
this.name = name;
return address;
this.address = address;
// Default constructor
public Teacher() {
this.salary = 50000.0;
this.subject = "Math";
}
// Parameterized constructor
this.salary = salary;
this.subject = subject;
return salary;
this.salary = salary;
return subject;
this.subject = subject;
// Default constructor
public Student() {
this.id = 0;
// Parameterized constructor
this.id = id;
this.age = age;
// Copy constructor
this.id = other.id;
this.age = other.age;
}
// Getter and Setter methods
return id;
this.id = id;
return age;
this.age = age;
// SchoolClass class
class SchoolClass {
public static final int MAX_STUDENTS = 20; // Constant for max students in a class
this.className = className;
this.teacher = teacher;
youngestAge = schoolClass.students[i].getAge();
youngestStudentName = schoolClass.students[i].getName();
students[index] = student;
}
// Main class
schoolClass.addStudent(student, i);