Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.ArrayList;

public class OOP {
    public static void main ( Static [] args){
        Car MercedesW13 = new Car1();
        MercedesW13.liters = 375;
        MercedesW13.consumeGas();
        System.out.println(MercedesW13.liters);
        MercedesW13.moveUntilNoMoreGas();
        System.out.println(MercedesW13.km);
        System.out.println();

        Car RedBullRB16 = new Car1();
        RedBullRB16.liters = 375;
        RedBullRB16.consumeGas();
        System.out.println(RedBullRB16.liters);
        RedBullRB16.moveUntilNoMoreGas();
        System.out.println(RedBullRB16.km);
        System.out.println();

        Car FerrariF175 = new Car1();
        FerrariF175.liters = 375;
        FerrariF175.consumeGas();
        System.out.println(FerrariF175.liters);
        FerrariF175.moveUntilNoMoreGas();
        System.out.println(FerrariF175.km);
        System.out.println();

        Car McLarenMCL36 = new Car1();
        McLarenMCL36.liters = 375;
        McLarenMCL36.consumeGas();
        System.out.println(McLarenMCL36.liters);
        McLarenMCL36.moveUntilNoMoreGas();
        System.out.println(McLarenMCL36.km);
        System.out.println();

        Car AstonMartinAMR22 = new Car1();
        AstonMartinAMR22.liters = 375;
        AstonMartinAMR22.consumeGas();
        System.out.println(AstonMartinAMR22.liters);
        AstonMartinAMR22.moveUntilNoMoreGas();
        System.out.println(AstonMartinAMR22.km);
        System.out.println();

        RacingSimulator.carsToRace()

    class Vehicle
    {
     public double currentLitersOfGasInTank;
     public double kilometersPerLiter;
     public double totalKilometersTravelled;
     public double speedInMph;

    }

    class Car extends Vehicle
    {
        public void speedInMph()
        {
            this.speedInMph = 120 + (Math.random()*(60-120));
        }

        public void consumeGas()
        {
            this.liters -= 7.5;
        }

        public void moveUntilNoMoreGas()
        {
            this.km += 10;
            if (consumeGas < 500);
            System.out.println();
            
            if (consumeGas >= 500);
            System.out.println("You");
                        break;

                    }
                    else
                    {
                        System.out.println("Your car will need more gas to return.");
                        break;
                    }

        }


    }
    class Racecar extends Car
    {
        public ArrayList<Car> car;


    }
    class RacingSimulator 
    {
        public static void carsToRace(Car c, Racecar r)
        {
           r.car.add(c);
        }

        public static void race(ArrayList<Car> car)
        {

        }    
    
    }
    }
    
}



This was the instruction:
A class hierarchy or inheritance tree in computer science is a classification of object types, denoting objects as the instantiations of classes inter-relating the various classes by relationships such as "inherits", "extends", "is an abstraction of", "an interface definition". Write a java program with a class hierarchy using the following classes:
Vehicle
Car
Racecar

In the Vehicle class, create the fields called currentLitersOfGasInTank, kilometersPerLiter, and totalKilometersTravelled , all of type double. 
In the Car class, create the methods consumeGas(), and moveUntilNoMoreGas(), both having a return type of void.
consumeGas() - this method decrements the currentLitersOfGasInTank field by 1 every time it is called.
moveUntilNoMoreGas() - this method calls consumeGas() and then increments totalKilometersTravelled by whatever the value of kilometersPerLiter is.  When currentLitersOfGasInTank is 0, it will print "Out of gas".

In the Racecar class, create a string field called raceCarName.


Create another class, RacingSimulator with a static field named carsToRace, which is an ArrayList of Racecars. In this RacingSimulator class, create a static method called race(), which will call each of the racecar's moveUntilNoMoreGas() method.
If two or more cars have the same values for currentLitersOfGasInTank and totalKilometersTravelled, then those cars will obviously have the same totalKilometersTravelled. In this case, just pick any of those cars as the winner.


In your main method, create 5 instances of a Racecar. Initialize the values of raceCarName, currentLitersOfGasInTank and totalKilometersTravelled to be whatever you want for each race car. Add the 5 Racecar objects to the carsToRace field of RacingSimulator, and call RacingSimulator.race(); Your program should print the raceCarName of the car that travelled the most kilometers. Obviously, it should be the car that has the highest value for totalKilometersTravelled .


What I have tried:

None because I'm clueless on this activity as always.
Posted
Updated 22-Feb-22 21:28pm

Quote:
None because I'm clueless on this activity as always.
Then you have two simple choices:

  1. Go to Java Tutorials Learning Paths[^] and study the language.
  2. Forget programming and find a course that you are
    a) interested to learn, or
    b) have some ability in.
 
Share this answer
 
Quote:
What I have tried:

None because I'm clueless on this activity as always.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Strat by reading the task carefully: it tells you pretty much exactly what to do!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900