Description
Constructorsandmethods
1. Write a program to create a class circle with centre and the radius as instance variables. Initialize and display its variables.
• Modify the exercise to have a constructor in class circle to initialize its variables.
• Modify the exercise to define the instance method calculateArea() to calculate the area and a static method compareArea() to compare the area of the circle and declare the result as smaller than or larger than or equal
2. Write a program to display the use of
• this keyword.
• Default constructor
• Parameterized constructor
• Pass Object as an argument
• Return object
3. Write a program to count the number of instances created for the classusing static variable and the non static variable not visible to all the instances.
4. Write a program that implements method overloading(multiple methods in the same class can have the same name but different parameter lists) based on the following conditions
• By changing number of arguments
• By changing the data type of the arguments
• Can we overload java main method?
• Show that the method overloading is not possible by just changing the return type
• Passing object as parameter.
5. Write a program that show the differences of
a. Instancevariables
b. Instancemethods
c. staticvariable
d. staticmethods
6. Write a program to create an immutable class Person(state cannot be changed)
• Define private final fields of name and age
• Define a constructor to set the fields and a getter method to display the values.
• Show that the state cannot be changed and enhances robustness
7. Write a Java class Clock for dealing with the day time represented by hours, minutes, and seconds. Your class must have the following features:
• Three instance variables for the hours (range 0- 23), minutes(range 059),and seconds(range 0-59).
• Three constructors: o default(with no parameters passed; initialize the represented time to12:0:0)
o a constructor with three parameters: hours, minutes, and seconds.
o a constructor with one parameter: the value of time in seconds since midnight (it should be converted into the time value in hours, minutes,and seconds)
• Instance methods: o A set-method method setClock() with one parameter seconds since midnight (to be converted into the time value in hours, minutes, and seconds as above).
o get-methods : getHours(), getMinutes(), getSeconds() with no parameters that return the corresponding values. o set-methods : setHours(), setMinutes(), setSeconds() with one parameter each that setup the corresponding instance variables. o method tick() with no parameters that increments the time stored in a Clock object by one second. o method addClock() accepting an object of type Clock as a parameter.The method should add the time represented by the parameter class to the time represented in the current class.
o Add an instance method tickDown() which decrements the time stored in a Clock object by one second.
o Add an instance method subtractClock() that takes one Clock parameter and returns the difference between the time represented in the current Clock object and the one represented by the Clock parameter. Difference of time should be returned as an clock object.
Write a separate class Clock Demo with a main() method. The program should:
• Instantiate a Clock object first Clock using one integer seconds since midnight obtained from the keyboard.
• Print both clock object
Create a reference thirdClock that should reference to object of difference of
first Clock and second Clock by calling the method subtractClock().




Reviews
There are no reviews yet.