CSE1116 Lab04
CSE1116 Lab04
Lab Objective
1. Familiarize students with the implementation of classes
2. Instantiation of objects accordingly.
Lab Activities
● Modify your Icecream class definition accordingly by including setters and getters.
● Remove constructors for ‘Icecream’ class in previous example and define new constructor that set price of
the Icecream class to zero.
D. Passing Objects to Methods
● In Java we can pass objects to methods
● When we pass a primitive type to a method, it is passed by value.
● But when we pass an object to a method, the situation changes dramatically, because objects are passed by
what is effectively call-by-reference.
● Add equals (Icecream I) method to your Icecream class and return true if price of caller object and
callee object are same; false, otherwise.
● Add compareTo(Icecream I): returns 1 if price of caller object is higher, 0 if both prices are the same,
-1 otherwise
E. Array of Objects
● An array of objects is actually an array of reference variables.
● For the Icecream class we can create array of objects :
Icecream[] IcecreamArray = new Icecream [10];
F. Introducing Class Diagram
● Specifications of a class can be also expressed using a class diagram.
● Class diagram is a standard way to represent a class.
● The above-mentioned Icecream class can be represented using the following diagram.
Lab Problems
01: Based on the ‘Icecream’ class as defined before, now create an array of Icecream type objects and get the input from
the user to initialize those objects. The size of array must be at least 5.
Now, write a static method searchByCompany in the Main class of the previous implementation, which takes a String
parameter representing company name and then prints all icecream’s information manufactured by that company.
02: Write a program in Java that follows the specifications as given below.
● Define a class Book as shown in the following figure.
● Create your Main class and the main() method. Define an array of Book type objects of size 5.
● Instantiate these Book objects by taking the inputs from the user.
● Print all Book objects’ data using a static method displayAll().
● Invoke compareTo() method to compare any two Book objects based on their pages. If caller object’s number of
pages is greater than callee object’s number of pages, compareTo() returns 1; if both pages are same, the method
returns 0 and -1 otherwise. Make sure to print the returned value.
● Define a static method isHeavier() within the Main class that takes a Book object as input parameter and returns
true if the Book’s number of pages is greater than 500; false, otherwise. Add appropriate code into your main()
method to demonstrate its functionalities.
03: Implement the following class ‘Fraction’ and test its methods.
String toString()
Returns the value of the fraction in 1 / 2 format where 1 is numerator and 2 is denominator.
Now write a test program, take two Fraction objects. Print both of them. Test add, sub, multiplication and division
methods. Print calling object after each method call.