Unit 2 - Tutorial
1. Create a class Rectangle with data members length and breadth. Use a constructor to initialize
values. Add a method to calculate area. Use this to refer to instance variables.
2. Create a class Calculator with overloaded methods add() for (a) two integers, (b) three integers,
and (c) two double values
[Link] a class Distance with feet and inches. Write a method addDistance(Distance d2) that adds
two Distance objects and returns the result as a new object.
4. Create a class Employee that keeps track of the number of employee objects created. Use a static
variable count and display it each time a new object is created.
5. Create a class Car with a final variable MAX_SPEED = 180. Write a method showSpeed() to display
it. Try to change the value and observe the compiler error.
6. Create an outer class University and an inner class Department. The inner class should display the
university name along with department name. Create a base class Person with attributes name and
age.
7. Derive a class Teacher with an additional attribute subject. Use super() to call the base class
constructor. Create an abstract class Shape with abstract method area(). Derive Circle and Rectangle
classes to implement area(). Use a reference of Shape to call area for both subclasses.
8. Create two interfaces A and B with one method each. Implement both interfaces in a class C and
call both methods to demonstrate multiple inheritance.
9. Create two interfaces Sports and Academics. Each interface should have one method to display
marks. Implement both interfaces in a class Result and display the total marks of a student.
10. Create an interface Playable with a method play(). Define two classes Football and Cricket that
implement the interface. Write a test class to call their respective play() methods using interface
reference variables.