[go: up one dir, main page]

0% found this document useful (0 votes)
3 views1 page

Sheet 5.1

B

Uploaded by

shroukmistafaa
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 views1 page

Sheet 5.1

B

Uploaded by

shroukmistafaa
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/ 1

Fayoum University Computer & Systems Eng. Section.

Faculty of Engineering Computer Eng. I (CCE108)


Electrical Engineering Dept. Dr. Sayed Taha

Sheet 5 (Abstract Classes, Interfaces, and Generics)


1- Create an abstract class called "Shape" with an abstract method called "getArea()". Create two
subclasses of Shape called "Rectangle" and "Circle". Implement the getArea() method in both
subclasses to calculate the area of the shape.
2- Create an abstract class called Animal that has a concrete move() method that prints "Animal is
moving", an abstract eat() method that takes no parameters, and a static sleep() method that prints
"Animal is sleeping". Create a subclass called Dog that implements the eat() method to print "Dog is
eating". Create a main() method that creates a Dog object and calls its move(), eat(), and sleep()
methods.
3- Create an interface called "Movable" with two methods called "moveUp()" and "moveDown()".
Create a class called "Car" that implements the "Movable" interface. Implement the "moveUp()" and
"moveDown()" methods in the "Car" class to control the movement of the car.
4- Create an interface called "Drawable" with one method called "draw()". Create two classes called
"Square" and "Circle" that implement the "Drawable" interface. Implement the "draw()" method in
both classes to draw a square and a circle respectively.
5- Create a generic class called Pair that has two fields of any type named first and second. Implement
a constructor that takes two parameters of the corresponding types, and methods getFirst() and
getSecond() that return the values of the fields. Create an ArrayList of Pair objects and add some
elements to it. Print the values of the first and second fields for each element in the list.
6- Create a generic class called "ListWrapper" that wraps a list of objects of any type and provides
methods to add, remove, and get items from the list. The class should also have a generic method
called "filter" that takes a predicate as a parameter and returns a new "ListWrapper" instance
containing only the items that match the predicate.

Note: In Java, Predicate is a functional interface from the java.util.function package that represents
a function that takes an input parameter of a certain type and returns a boolean value. The
Predicate interface has one abstract method called test() that accepts an object of the specified type
and returns a boolean value based on some criteria.

The test() method is typically used to implement a test or filter condition. For example, a Predicate
can be used to test whether an object meets a certain condition, such as being greater than a certain
value, or having a specific property.

You might also like