0% found this document useful (0 votes)
3 views

QuestionSet4

The document outlines a series of programming tasks involving class creation, inheritance, and method implementation in Java. It includes examples of hierarchical inheritance, method overriding, and method overloading across various classes such as Number, Document, Vehicle, and others. Additionally, it covers array manipulation and ArrayList operations, providing a comprehensive set of exercises for practicing object-oriented programming concepts.

Uploaded by

peachypaimon
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)
3 views

QuestionSet4

The document outlines a series of programming tasks involving class creation, inheritance, and method implementation in Java. It includes examples of hierarchical inheritance, method overriding, and method overloading across various classes such as Number, Document, Vehicle, and others. Additionally, it covers array manipulation and ArrayList operations, providing a comprehensive set of exercises for practicing object-oriented programming concepts.

Uploaded by

peachypaimon
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/ 7

Question 1 and 2 are connected.

1. (Inheritance)
Create a class Number that contains an int attribute and a method printNumber() to display the
number. Then, create a subclass PrimeChecker that inherits from Number and adds a method
isPrime() to check if the number stored in Number is prime or not. The isPrime() method should
return a boolean value indicating if the number is prime. The main is given and output should
look like below.

2. (Hierarchical Inheritance)
Extend the PrimeChecker class to create a class PrimeFactorizer that adds a method
printFactors() to print all prime factors of the number. Override the isPrime() method to check if
the number has any prime factors. For example, number 15 has the factors as 3 and 5. The main
is given and output should look like below.
Question 3 and 4 are connected.
3. (Hierarchical Inheritance)
Create a base class called Document with attributes for title and content. Implement a method
printSummary() that prints a brief summary of the document. Create two subclasses:
TextDocument and PdfDocument. The TextDocument class should override the printSummary()
method to print the first 100 characters of the content, while the PdfDocument class should
override it to print the first line of the content.
The main is given and output should look like below.

4. (Inheritance)
Enhance the Document class to include a method getDocumentType() that returns a string
representing the type of document. Override this method in TextDocument to return "Text
Document" and in PdfDocument to return "PDF Document".
The main is given and output should look like below.
5. (Inheritance)
Create a base class named Vehicle with two attributes: type (String) initialized to "4W" and
maxSpeed (int) initialized to 100. Include a parameterized constructor to initialize these
attributes. Next, create a subclass of Vehicle called Car. This class should add an additional
attribute trans (String) for the transmission type and implement a parameterized constructor to
initialize trans. The constructor of Car should call the base class constructor with "4W" and 150
as arguments. The main is given and output should look like below.

6. (Hierarchical Inheritance)
Create a class Person with a method speak() that displays a message “The person is speaking”.
Create two subclasses “Student” and “Teacher” that extend “Person” and implement the
speak() method to display “The student is asking a question” and “The teacher is giving a
lecture” respectively. The main is given and output should look like below.

7. (Inheritance)
Create a class called Airplane with attributes for flight number, destination, and departure time.
Include methods to check the flight status and delay. Additionally, create a subclass called
InternationalFlight that inherits from Airplane and adds an attribute for the country of origin.
Override the flight status method in InternationalFlight to include information about international
flights.
8. (Linked list)
Create a class named LinkedList that holds an int[] array (data) and initializes it via a constructor.
Implement a display method in this class to print all node data. Also, define a passMessage
method to send and print a message for each node. The main is given and output should look like
below.

9. (Pointers)
You will write a twoSum method inside the TwoSum class. This method takes an integer array
(input) and a target value (targetValue). It should check if there are two numbers in the array that
sum up to the target value. If such a pair exists, return true; otherwise, return false. Assume the
array is sorted. (This method is designed to work with sorted arrays.) For example, with the array
[1, 3, 5, 7, 9], you cannot obtain the number 20 by summing any two numbers, so the result
should be false. However, if the target value were 8, since you can obtain it by summing 7 and 1,
the result would be true. The main is given and output should look like below.
10. (Inheritance and constructor)
Create a superclass named Vehicle with two attributes: model (String) and year (int), and include
a constructor. Then, create a subclass named Car that extends Vehicle and has an additional
attribute numberOfDoors (int). In the Car class, write a constructor that calls the Vehicle class
constructor. In the Main class, create an instance of the Car class, set the model, year, and
number of doors, and print these details to the screen. The main is given and output should look
like below.

11. (Inheritance, overriding and constructor)


Create a superclass named Employee with two attributes: name (String) and salary (double), and
define a method called calculateBonus that returns 0 to calculate the employee's bonus. Then,
create a subclass named Manager that extends Employee and overrides the calculateBonus
method to compute the manager's bonus as 10% of the salary. Also, create another subclass
named Intern that extends Employee and overrides the calculateBonus method to return 5% of
the salary for the intern's bonus. In the Main class, create instances of Manager and Intern,
calculate their bonuses, and print the results to the screen. The main is given and output should
look like below.

12. (Overloading)
You are required to create a Calculator class with the following methods: add(int a, int b) which
adds two integers and returns the result; add(double a, double b) which adds two double values
and returns the result; multiply(double a, int b) which multiplies a double value by an integer and
returns the result. All methods will have 2 attributes as a and b. The main is given and output
should look like below.
13. (Overloading)
Create a class named SimpleConverter with two methods: convert(double value), which
converts the given value from meters to feet, and convert(double value, String unit), which
converts the given value to centimeters based on the specified unit. When the unit is "meters,"
the method should convert the value to feet; when the unit is "inches," it should convert the
value to centimeters. (Hint: Overloading, meters => feet is x3.28084, inches => cm is x2.54) The
main is given and output should look like below.

Question 16 and 17 are connected.


14. (Array)
Create a class named ArrayManipulator. This class should include a method called sumArray
that calculates and returns the sum of all elements in the given integer array with an int[]
numbers attribute. The main is given and output should look like below.

15. (Array)
Create a class named `ArrayManipulator` with an `int[] numbers` attribute. Add a method
named `sumArray(int[] numbers)` to this class. This method should calculate and return the
sum of all elements in the provided integer array. The main is given and output should look like
below.
16. (Constructor)
Write a class to check whether a number is a Duck Number or not. A Duck number is a number
which has zeroes present in it, but there should be no zero present in the beginning of the
number. For example 3210, 7056, 8430709 are all duck numbers whereas 08237, 04309 are not.
isDuckNumber() method should have number attribute as type of String. The main is given and
output should look like below.

17. (ArrayList)
Write a Java program to replace the second element of an ArrayList with the specified element.
You should write replaceSecondElement() method with ArrayList<String> list and String
newElement attributes. In addition, please make sure to add a printList method as well for the
printing operation. The main is given and output should look like below.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy