0% found this document useful (0 votes)
14 views2 pages

Objectives:: Task

The document describes two programming problems. Problem C involves creating interfaces and classes for vehicles. It defines a Vehicle interface with start and stop methods, an AbstractVehicle class that implements these and adds an accelerate method, and Car and Bicycle classes that extend AbstractVehicle and add specific acceleration implementations. Problem D involves creating a Book class that implements Comparable to allow sorting by publication year, author, then title.

Uploaded by

230107069
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)
14 views2 pages

Objectives:: Task

The document describes two programming problems. Problem C involves creating interfaces and classes for vehicles. It defines a Vehicle interface with start and stop methods, an AbstractVehicle class that implements these and adds an accelerate method, and Car and Bicycle classes that extend AbstractVehicle and add specific acceleration implementations. Problem D involves creating a Book class that implements Comparable to allow sorting by publication year, author, then title.

Uploaded by

230107069
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/ 2

Problem C

Objectives:
To define interfaces and define classes that implement interfaces (§13.5).

Task: Create an interface named Vehicle with the following method declarations:
● void start()
● void stop()

Create an abstract class named AbstractVehicle that implements the Vehicle interface.
The AbstractVehicle class should have:

● Common functionality for starting and stopping vehicles


● An abstract method accelerate().

Create a class Car that extends AbstractVehicle. The Car class should have:

● Specific implementation for accelerating a car


● Additional methods/properties specific to a car

Create a class Bicycle that extends AbstractVehicle. The Bicycle class should have:

● Specific implementation for accelerating a bicycle


● Additional methods/properties specific to a bicycle

Create a class Main with a main method to test the classes. Create instances of Car
and Bicycle, call their methods to start, stop, and accelerate.

Example Output:
Vehicle started
Car is accelerating
Vehicle stopped
Honk honk!

Vehicle started
Bicycle is pedaling faster
Vehicle stopped
Ring ring!
Problem D(bonus)
Objectives:
To define a natural order using the Comparable interface (§13.6).

Task: Create a class named Book with the following attributes:


● title (String)
● author (String)
● publicationYear (int)
​ Implement Comparable:
● Make the Book class implement the Comparable<Book> interface.
● Implement the compareTo method to compare books based on the
following criteria:
● First, compare based on publicationYear in descending order.
● If the publication years are the same, compare based on author in
ascending order.
● If the authors are the same, compare based on title in ascending
order.
​ Test the Implementation:
● Create a main method to test your Book class.
● Create an ArrayList of Book objects and add at least 5 books with different
attributes.
● Use Collections.sort() to sort the list of books.
​ Print Results:
● Print the original list of books and the sorted list after using
Collections.sort().

Example Output:

Original list of books:


Book{title='The Great Gatsby', author='F. Scott Fitzgerald', publicationYear=1925}
Book{title='To Kill a Mockingbird', author='Harper Lee', publicationYear=1960}
Book{title='1984', author='George Orwell', publicationYear=1949}
Book{title='Brave New World', author='Aldous Huxley', publicationYear=1932}
Book{title='The Catcher in the Rye', author='J.D. Salinger', publicationYear=1951}

Sorted list of books:


Book{title='To Kill a Mockingbird', author='Harper Lee', publicationYear=1960}
Book{title='The Catcher in the Rye', author='J.D. Salinger', publicationYear=1951}
Book{title='1984', author='George Orwell', publicationYear=1949}
Book{title='Brave New World', author='Aldous Huxley', publicationYear=1932}
Book{title='The Great Gatsby', author='F. Scott Fitzgerald', publicationYear=1925}

In this example, the books are sorted first by publicationYear in descending order, then
by author in ascending order, and finally by title in ascending order. You can see how
the compareTo method defined in the Book class is used to achieve this custom sorting
order.

You might also like