Question: This lab both reviews old materials and introduce new materials that correspond with the readings in the schedule for weeks one and two. In this

This lab both reviews old materials and introduce new materials that correspond with the readings in the schedule for weeks one and two. In this section, well use a driver provided for you to explore each of the sections below. In BlueJ or Eclipse, make a new Java project and add Rectangle.java and it's driver NewAndReviewExamples.java to get started. Run the NewAndReviewExamples.java driver for each case of the switch statement and observe what happens. After you run each case, take a look at the code used to produce that result. Use these two files as examples while working on the remaining sections.

You may complete this section in lab if time permits, but this section is designed to be completed outside of lab and before lecture next week. In this section, you will review some old topics and explore some new ones as well. Well start with topics related to data (primitives and objects), and then look at topics that focus on methods.

1. Data in Java: Primitives and Objects

In this section, well build a class that is composed of both primitives and objects. Java has classes to help in converting from primitives to objects (for example converting an int to an Integer) and vice-versa. Lets start by building a small class used to represent a single concept such as a Car or Vehicle.

  • Make a new Java project and and create a new class called Car.
  • Cars should define primitives for things like odometers, etc., and Strings for make and model. Inside your Car class but outside of any method, define three (instance) variables for the odometer, make, and model.
  • Write a main that builds 2 cars and prints them out. (Hint: Car c1 = new Car(); and System.out.println(c1.toString());.

2. Variable Scope in Java: Local and Class-Level

Lets practice building classes again and defining 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 its defined. Then, try to access the local variable from a method other than the one in which its defined. Finally, look inside of Rectangle.java and identify at least 4 instance variables (class-level) and at least 2 local variables. Indicate these using comments.

3. This (the Implicit Parameter)

Take a class youve 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 with toString(). Next, from a method inside the class, print out the address of the this object using println. Call that method on the object youve just built, and explain why the two addresses are the same. If you get stuck take a look at Case 1 in NewAndReviewExamples.java.

4. Access Modifiers: Public and Private

Build yet another simple class (say, a Point or Pair). Then, create another (separate, distinct) class that is to be the driver for this example (just like the driver for Rectangle.java above). In Your Point class, create two methods. One should be public and another private. In your driver, build an object of the Point class and try to access a method declared as public. Now, on the same Point object, try to call a method declared as private. What message does Java print out? Next, declare a class-level data item as public (say, an int), and declare another class-level data item as private. In your drivers main, try again to access these two data items what message does the Java compiler display now?

5. Accessors and Mutators (or, Getters and Setters)

Each of these sections encourages you to practice building small classes, and well continue that pattern here. 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 that 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 examples of these getters & setters.

6. Overriding toString() (and equals())

Build a simple class to represent a Vehicle (or reuse your Car class above). In a main, 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 wed like to do something more useful. To replace (or override) the toString (or equals) function, first see the examples defined in the NewAndReviewExamples.java file for both toString and equals. Now, build a toString function that prints out the make, model, and odometer reading for a vehicle object.

7. Overloading Methods

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.

8. Constructors as Methods

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. Add to your Car class two constructors one to take a string make, the other to take two strings: a make and a model. Test these constructors by building multiple Cars in your main() driver, calling each constructor in turn.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!