ملاحظة: اللابات الرسمية هي المرجع الأساسي. السؤالين بالاسفل كتبها ChatGPT4 اعتمادا على ملفات اللابات السابقة من 1 الى 6 بحيث تتضمن جميع المفاهيم.
سؤال 1
Step 1: Define the Base Class Person
Create a class called Person with the following features:
- Private instance variables:
name(String)age(int)
- Constructors:
- A default constructor that initializes
nameto "No name yet" andageto 0. - A parameterized constructor that takes a
Stringfornameand anintforageand sets the respective instance variables.
- A default constructor that initializes
- Public getter and setter methods for both instance variables.
- Override the
toStringmethod to return a string representation of the object. - Implement the
equalsmethod to compare twoPersonobjects based on theirnameandage.
Step 2: Define the Derived Class Student
Create a class called Student that extends Person with the following features:
- Additional private instance variables:
studentID(String)major(String)
- Constructors:
- A default constructor that initializes
studentIDto "Unknown" andmajorto "Undeclared". - A parameterized constructor that takes a
Stringforname, anintforage, aStringforstudentID, and aStringformajor, and sets the respective instance variables. Use thesuperkeyword to call the constructor of the base class.
- A default constructor that initializes
- Public getter and setter methods for the new instance variables.
- Override the
toStringmethod to include the new instance variables. - Override the
equalsmethod to compareStudentobjects based on all attributes.
Step 3: Implement Array Operations
Create a class called StudentManager that performs various operations on an array of Student objects:
- Static method
printStudentsthat prints all student objects in the array. - Static method
findStudentByIDthat searches for a student in the array based on a givenstudentIDand returns the student object. - Static method
findMinAgethat finds the minimum age of students in the array and returns it. - Static method
findMaxAgethat finds the maximum age of students in the array and returns it. - Static method
calculateAverageAgethat calculates and returns the average age of students in the array. - Static method
filterByMajorthat filters the array to create a subset of students that have a specific major and returns the new array.
Step 4: Test the Implementation
In a separate class called Lab07Demo, define a main method that performs the following tasks:
- Create an array of
Studentobjects. - Populate the array with at least five
Studentobjects. - Use the methods from
StudentManagerto:- Print all student objects.
- Find and print a student based on a specific
studentID. - Find and print the minimum and maximum ages of the students.
- Calculate and print the average age of the students.
- Filter and print students based on a specific major.
Sample Output:
Student: Name: John Doe, Age: 20, StudentID: S12345, Major: Computer Science
Student: Name: Jane Smith, Age: 22, StudentID: S67890, Major: Mathematics
...
Student with ID S12345: Name: John Doe, Age: 20, StudentID: S12345, Major: Computer Science
Minimum age: 18, Maximum age: 25
Average age: 22.0
Students majoring in Computer Science:
Student: Name: John Doe, Age: 20, StudentID: S12345, Major: Computer Science
...
شرح سؤال 1
سؤال 2
Step 1: Define the Base Class Vehicle
Create a class called Vehicle with the following features:
- Private instance variables:
make(String)model(String)year(int)
- Constructors:
- A default constructor that initializes
makeandmodelto "Unknown" andyearto 0. - A parameterized constructor that takes a
Stringformake, aStringformodel, and anintforyearand sets the respective instance variables.
- A default constructor that initializes
- Public getter and setter methods for all instance variables.
- Override the
toStringmethod to return a string representation of the object. - Implement the
equalsmethod to compare twoVehicleobjects based on theirmake,model, andyear.
Step 2: Define the Derived Class Car
Create a class called Car that extends Vehicle with the following features:
- Additional private instance variables:
numberOfDoors(int)fuelType(String)
- Constructors:
- A default constructor that initializes
numberOfDoorsto 4 andfuelTypeto "Petrol". - A parameterized constructor that takes a
Stringformake, aStringformodel, anintforyear, anintfornumberOfDoors, and aStringforfuelType, and sets the respective instance variables. Use thesuperkeyword to call the constructor of the base class.
- A default constructor that initializes
- Public getter and setter methods for the new instance variables.
- Override the
toStringmethod to include the new instance variables. - Override the
equalsmethod to compareCarobjects based on all attributes.
Step 3: Implement Array Operations
Create a class called CarManager that performs various operations on an array of Car objects:
- Method
printCarsthat prints all car objects in the array. - Method
findCarByModelthat searches for a car in the array based on a givenmodeland returns the car object. - Method
findMinYearthat finds the minimum year of cars in the array and returns it. - Method
findMaxYearthat finds the maximum year of cars in the array and returns it. - Method
calculateAverageYearthat calculates and returns the average year of cars in the array. - Method
filterByFuelTypethat filters the array to create a subset of cars that have a specific fuel type and returns the new array.
Step 4: Test the Implementation
In a separate class called Lab08Demo, define a main method that performs the following tasks:
- Create an array of
Carobjects. - Populate the array with at least five
Carobjects. - Use the methods from
CarManagerto:- Print all car objects.
- Find and print a car based on a specific
model. - Find and print the minimum and maximum years of the cars.
- Calculate and print the average year of the cars.
- Filter and print cars based on a specific fuel type.
Sample Output:
Car: Make: Toyota, Model: Camry, Year: 2018, Number of Doors: 4, Fuel Type: Petrol
Car: Make: Honda, Model: Accord, Year: 2020, Number of Doors: 4, Fuel Type: Hybrid
...
Car with model Accord: Make: Honda, Model: Accord, Year: 2020, Number of Doors: 4, Fuel Type: Hybrid
Minimum year: 2015, Maximum year: 2022
Average year: 2019.0
Cars with fuel type Petrol:
Car: Make: Toyota, Model: Camry, Year: 2018, Number of Doors: 4, Fuel Type: Petrol
...