Lab Exercise 1
Part 1: Editing, compiling, and running a java program (using CMD)
1. Write a Java class named ‘Rectangle’, which has two methods.
a. The 1st method takes length and height as parameters and returns the area
b. The 2nd method takes length and height as parameters and returns the perimeter
2. Instantiate two Rectangle objects in the Main class.
a. Set the dimensions of the first rectangle to have a length of 8mm and a height of
4mm,
b. Set the dimensions of the second rectangle to have a length of 10mm and a
height of 6mm.
c. Calculate and display the area and perimeter for both rectangles.
3. Using the javac command, compile the Rectangle class
4. Run the compiled Java program using the java command: java ClassName
Part 2: Class declarations and object instantiation
1. Create three Java classes, 'Person' , 'Book,' and a Main class
2. Class name: Person. Attributes: Name (String), Gender (String), Birth Year (int)
a. Implement a method named 'calculateAge' that takes the birth year as a
parameter and returns the age of the person.
b. Implement another method named 'displayPersonInfo' that prints the
person's name, gender, and current age.
3. Class name: Book. Attributes: Title (String), Author (String), NoOfPages (int)
a. Define a method named 'calculatePagesRead' that takes the number of pages
read as a parameter and returns the total pages read.
4. Instantiate one object of the 'Person' class and another object of the 'Book' class in the
main class
5. Calculate and display the age and person information for the 'Person' object
6. Calculate and display the total pages read for the 'Book' object.