الرجوع
تسجيل الدخول
أنت تتصفح نسخة مؤرشفة من الموقع اضغط للانتقال إلى الموقع الجديد بكامل المزايا

أسئلة تدريبية للفاينل العملي برمجة 2

5/18/2024 5 1451

ملاحظة: اللابات الرسمية هي المرجع الأساسي. السؤالين بالاسفل كتبها 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 name to "No name yet" and age to 0.
    • A parameterized constructor that takes a String for name and an int for age and sets the respective instance variables.
  • Public getter and setter methods for both instance variables.
  • Override the toString method to return a string representation of the object.
  • Implement the equals method to compare two Person objects based on their name and age.

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 studentID to "Unknown" and major to "Undeclared".
    • A parameterized constructor that takes a String for name, an int for age, a String for studentID, and a String for major, and sets the respective instance variables. Use the super keyword to call the constructor of the base class.
  • Public getter and setter methods for the new instance variables.
  • Override the toString method to include the new instance variables.
  • Override the equals method to compare Student objects 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 printStudents that prints all student objects in the array.
  • Static method findStudentByID that searches for a student in the array based on a given studentID and returns the student object.
  • Static method findMinAge that finds the minimum age of students in the array and returns it.
  • Static method findMaxAge that finds the maximum age of students in the array and returns it.
  • Static method calculateAverageAge that calculates and returns the average age of students in the array.
  • Static method filterByMajor that 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 Student objects.
  • Populate the array with at least five Student objects.
  • Use the methods from StudentManager to:
    • 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 make and model to "Unknown" and year to 0.
    • A parameterized constructor that takes a String for make, a String for model, and an int for year and sets the respective instance variables.
  • Public getter and setter methods for all instance variables.
  • Override the toString method to return a string representation of the object.
  • Implement the equals method to compare two Vehicle objects based on their make, model, and year.

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 numberOfDoors to 4 and fuelType to "Petrol".
    • A parameterized constructor that takes a String for make, a String for model, an int for year, an int for numberOfDoors, and a String for fuelType, and sets the respective instance variables. Use the super keyword to call the constructor of the base class.
  • Public getter and setter methods for the new instance variables.
  • Override the toString method to include the new instance variables.
  • Override the equals method to compare Car objects 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 printCars that prints all car objects in the array.
  • Method findCarByModel that searches for a car in the array based on a given model and returns the car object.
  • Method findMinYear that finds the minimum year of cars in the array and returns it.
  • Method findMaxYear that finds the maximum year of cars in the array and returns it.
  • Method calculateAverageYear that calculates and returns the average year of cars in the array.
  • Method filterByFuelType that 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 Car objects.
  • Populate the array with at least five Car objects.
  • Use the methods from CarManager to:
    • 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
...