Question: Part A: Java: Primitives and Objects In this section, well build a class that is composed of both primitives and objects. Java has classes to

Part A: 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());.

Part B. 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.

Part C. 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.

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!