Java Beginners - Java For Beginners Objects in Java - Introduction Rating: 4.4/5 (5 votes cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Now that we have covered some of the theory of objects, it is time to have a little fun. We'll create an object hierarchy with a Car super class and a Mustang subclass. Then we'll take them both for a test drive with the TestDrive class. Below is the complete code for the Car, Mustang, and TestDrive classes. Create a subdirectory named objintro. Create a file in objintro for each class below. Copy the contents of the classes into the files, and save the files so the class and file name match (remember the .java extension) . So, Car would be saved into a file named Car.java, Mustang into Mustang.java, TestDrive into TestDrive.java. Compile the classes with the javac command. Then run TestDrive with the java command. See First Java Program for more details on compiling and executing Java programs. Car package arunsoft;
/** * Represents a car. * The attributes are speed and color. * The methods are accelerate, * decelerate, getSpeed, getColor, getAcceleration * and getMaxSpeed. * * Add your own attributes and behaviors * for breaking and for shifting gears. * */ public class Car {
protected int speed = 0; protected String color = "black"; private static final int MIN_SPEED = 0; private static final int CAR_MAX_SPEED = 100; private static final int CAR_ACCELERATION = 10;
/** * Default constructor to create a new * Car object. */ public Car() { //creates a new object }
/** * Simulates pressing the accelerator. * @return the new speed */ public int accelerate() { int newSpeed = speed + getAcceleration(); if(newSpeed <= getMaxSpeed()) { speed = newSpeed; } else { speed = getMaxSpeed(); } return speed; }
/** * Simulates releasing the accelerator. * @return the new speed */ public int decelerate() { if(speed > MIN_SPEED) { speed--; } return speed; }
/** * @return the current speed */ public int getSpeed() { return speed; }
/** * @return the max speed */ public int getMaxSpeed() { return CAR_MAX_SPEED; }
/** * @return the max speed */ public String getColor() { return color; }
/** * @return the car's acceleration */ public int getAcceleration() { return CAR_ACCELERATION; } }
Mustang package arunsoft;
/** * Represents a Mustang. * It overrides (changes) the getMaxSpeed method, * which sets an upper limit for the accelerate * method. It also overrides getAcceleration. */ public class Mustang extends Car {
private static final int MUSTANG_MAX_SPEED = 150; private static final int MUSTANG_ACCELERATION = 20;
/** * Constructors are used to initialize * the objects variables. * * The Mustang color is define when the constructor * is called, whereas the Car objects are always black. */ public Mustang(String passedColor) { // creates a new instance of Mustang and // assigns a color color = passedColor; }
/** * This method is implemented in the Car class and * in the Mustang class. The Mustang version overrides * the Car version. * * @return The max speed for a Mustang. */ public int getMaxSpeed() { return MUSTANG_MAX_SPEED; }
/** * This method is implemented in the Car class and * in the Mustang class. The Mustang version overrides * the Car version. * * @return The acceleration for a Mustang. */ public int getAcceleration() { return MUSTANG_ACCELERATION; } }
TestDrive package arunsoft;
/** * TestDrive demonstrates creating and calling * methods on Car and Mustang objects. */ public class TestDrive {
//The Java virtual machine (JVM) always starts //execution with the 'main' method of the class passed //as a argument to the java command public static void main(String []args) { TestDrive td = new TestDrive(); td.start(); //exit TestDrive }
private void start() { //Create a Car Car yugo = new Car(); //Take it for a drive System.out.println("Starting yugo test drive!"); driveCar(yugo);
//Create a Mustang //Remember, myMustang is a Mustang AND a Car Car pony = new Mustang("red"); //Take it for a drive System.out.println("Starting mustang test drive!"); driveCar(pony); }
public static void driveCar(Car c) { System.out.println("Car color is: " + c.getColor()); //press the accelerator 15 "times" for(int i = 0; i < 15; i++) { System.out.println("accelerating: " + c.accelerate()); } //release the accelerator 5 "times" for(int i = 0; i < 5; i++) { ; System.out.println("decelerating: " + c.decelerate()); } System.out.println("final cruising speed: " + c.getSpeed()); } }
When you compile and run the program, you should see something like the following: c:\arun\javac arunsoft/Car.java objintro/Mustang.java objintro/TestDrive.java c:\arun\java objintro.TestDrive Starting yugo test drive! Car color is: black accelerating: 10 accelerating: 20 accelerating: 30 accelerating: 40 accelerating: 50 accelerating: 60 accelerating: 70 accelerating: 80 accelerating: 90 accelerating: 100 accelerating: 100 accelerating: 100 accelerating: 100 accelerating: 100 accelerating: 100 decelerating: 99 decelerating: 98 decelerating: 97 decelerating: 96 decelerating: 95 final cruising speed: 95 Starting mustang test drive! Car color is: red accelerating: 20 accelerating: 40 accelerating: 60 accelerating: 80 accelerating: 100 accelerating: 120 accelerating: 140 accelerating: 150 accelerating: 150 accelerating: 150 accelerating: 150 accelerating: 150 accelerating: 150 accelerating: 150 accelerating: 150 decelerating: 149 decelerating: 148 decelerating: 147 decelerating: 146 decelerating: 145 final cruising speed: 145
< Previous 1 | 2 |
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|