In this section, your job will be to look at the following sections outlined in this lab and identify knowledge gaps (if any) in your understanding of Java syntax and the programming techniques covered so far. Each section has a corresponding code segment that lives in one of the two files provided for this lab {ClassExamplesChap4.java, Rectangle.java}, and you should review the code sample first before attempting the exercises described below. Just like Classes themselves, the sections are split into two main groups: methods and data.
Data Section
Build a class that is composed of both primitives and objects. Try a Car Class that has primitives for things like odometers, etc., and Strings for make and model.
Build a simple class, and inside, declare two variables: one local and one with class-level scope. Prove to yourself that you can access the class-level variable throughout the class in which it’s defined. Then, try to access the local variable from a method other than the one in which it’s defined.
Take a class you’ve already built (or build the Car Class described in the “Data in Java” section) and build an object from that class. Observe the address of that object in main by using println and toString(). Next, from within a method inside the class, print out the address of the “this” object using println. Call that method on the object you’ve just built – are they the same or different. Why? What is This a reference to?
Build a simple class (say, vehicle). Then, create another class that is to be the “driver” for this example. In your driver, build an object of the Vehicle class and try to access a method declared as public. Now, on the same vehicle object, try to call a method declared as private. What message does Java print out? Next, declare some class-level data item as public (an int, say), and declare another class-level data item as private. In your driver’s “main”, try again to access these two data items – what message does the Java compiler display now?
Methods Section
Construct a simple class used to store the time or date. Add a field for minute, second, and hour, but make these class-level variables private. So our class can be used by external clients, we need to declare some public methods for use with our private data. Build two methods for each data item: one to get the value of the data item, and one to set the value of the data item. See the getters and setters defined in the Rectangle class for more examples.
Build a simple class to represent a Vehicle. Build an object of that class, and print out the object using System.out.println(). Notice that this simply reports the memory address of the object in question, and we’d like to do something more useful. To replace (or override) the toString (or equals) function, first see the examples defined in the ClassExamples file for both toString and equals. Now, build a toString function that prints out the make, model, and odometer reading for a vehicle object.
First, check out the set of constructors provided for you in the Rectangle class. Notice how they are overloaded (meaning many methods with the same name, but different with regards to input) to provide flexibility for users of this class. Build a simple car class that has two constructors – one to take a string make, the other to take two strings (make, model).
Check out the Rectangle class constructors to see an example of overloading – defining multiple methods with the same name. Now build a SquareSomething class, with all static functions, whose purpose is to take an int, a double, or a float, and report back the square of the number (returning the correct type). This will mean your square class has three functions ( all named “square”) that each take a different type of data as input (int, double, float). Your square function that takes a double should return a double as well. This is how the println() method accomplishes such flexibility – you can hand it an int, a string, a double, a float, etc. and it simply prints the data. How it does so is by overloading the println() method to provide a different function for each possible type of input.