Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All part of one question just several parts Lets start by building a small class used to represent a single concept such as a Car

All part of one question just several parts

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions