Create A Vehicle Class

-1 Ayami Asakura · September 6, 2014
create a class named Vehicle that acts as a superclass for vehicles types.
the vehicle class contains private variables for
the number of wheels and the average number of miles per gallon. 
the vehicle class also contains a constructor with integer arguments 
for the number of wheels and average miles per gallon. 
create two subclasses. car and motorcycle, that extend the vehicle class. 
each subclass contains a constructor 
that accepts the miles-per-gallon value as an argument
and forces the number of wheels to 
appropriate value - 2 for a motorcycle and 4 for a car

this is the question my Prof gave me.

this is how i translate it:

public class Vehicle {
    private int numberOfWheels;
    private int averageMPG;
    
    public Vehicle(int numberOfWheels, int averageMPG) {
            numberOfWheels = numberOfWheels ;
    }
    public void setNumberOfWheels(int numberOfWheels) {
        this.numberOfWheels = numberOfWheels;
    }

    public int getNumberOfWheels() {
        return numberOfWheels;
    }
    public void vehicle(){
        System.out.println(numberOfWheels);
    }
}

public class Car extends Vehicle{
    int milesPerGallon;

    public Car(){
        numberOfWheels = 4;
    }

    @Override
    public void setNumberOfWheels(int numberOfWheels) {
        super.setNumberOfWheels(numberOfWheels);
    }

    @Override
    public int getNumberOfWheels() {
        return super.getNumberOfWheels();
    }

public class Motorcycle extends Vehicle {
    int milesPerGallon;
    
    public Motorcycle(){
          numberOfWheels = 2;
    }

    @Override
    public void setNumberOfWheels(int numberOfWheels) {
        super.setNumberOfWheels(numberOfWheels);
    }

    @Override
    public int getNumberOfWheels() {
        return super.getNumberOfWheels();
    }

 Please can any tell me where did get wrong.
I really can't figure it out.
It's Driving me Crazy ALL NIGHT LONG!!

Post a Reply

Replies

Oldest  Newest  Rating
0 Adam Karrer · September 7, 2014
Let's see... You have multiple issues here. I fixed up some of the code and I'll try to explain what I did here:


public class Vehicle {
    private int numberOfWheels;
    private int averageMPG;
    
    public Vehicle(int wheels, int mpg) {
            numberOfWheels = wheels;
            averageMPG = mpg;
            System.out.println(numberOfWheels);
    }
    
    public void setNumberOfWheels(int numberOfWheels) {
        this.numberOfWheels = numberOfWheels;
    }

    public int getNumberOfWheels() {
        return numberOfWheels;
    }
}

First, you need your constructor. Don't name your arguments the same as your variables you're applying them to. Also, make sure if you're creating two arguments, to do something with both of them. Your constructor always comes first and has the same name as your class. That's where I tossed your println.

I believe the rest of that was fine.

Now "Car":


public class Car extends Vehicle
{
    public Car(int wheels, int averageMPG) {
super(wheels, averageMPG);
}

int milesPerGallon;

    @Override
    public void setNumberOfWheels(int numberOfWheels) {
        super.setNumberOfWheels(numberOfWheels); 
    }

    @Override
    public int getNumberOfWheels() {
        return super.getNumberOfWheels(); 
    }
}

Pretty much the same idea. Create your constructor first. That's what the object is going to do the second it's instantiated. So if I create a test:



public class test
{
public static void main(String[] args)
{
Vehicle vehicle = new Vehicle(6, 12);
Car car = new Car(4, 21);
}
}

Because your constructor prints the number of wheels out for me automatically, I get:




6
4



Still got some work to do, but I hope this helps. =)
0 Abdirasaq Ali · September 7, 2014
Where is your main??
  • 1

Java / Android Development

107,173 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator