You\'ve just received a crash course in Java. You may be completely new to this,
ID: 3865668 • Letter: Y
Question
You've just received a crash course in Java. You may be completely new to this, or you may have only learned one or two new things. In any case, it's key that you cement your newfound knowledge with practice. Now we're going to take some time to practice writing some simple Java programs. In doing so, you will become familiar with Eclipse and how to run your applications. 1. Create a Java project in eclipse, called "PracticeCar" 2. Create a class called "Simulator", with a main[] method. 3. Create a "Car" class, with a run() method. The main[] method of Simulator will create an instance of a car object, and call that object's run() method. Now, you will create additional classes that represent the different components of a car - the engine, the fuel tank, the wheels, etc. These classes should have methods related to their behaviors, and properties representing their various states (an engine is running or not, tires have levels of wear, etc). 1. Every class representing a car part should inherit from a superclass called "CarPar". The CarPart class will have all of the states and behaviors that are shared by all types of car parts (like condition and the ability to print a diagnostic report). 2. Make sure that your Car class creates an instance of each CarPart subclass, as part of its constructor. Parts that a car has multiples of [like wheels] should be stored in a collection. Make sure that the run() method of the Car class does something with each instance of a CarPart subclass. Many of the design details for this task will be up to you-this is an exercise to help you get into the habit of thinking in an object-oriented way, so get creative! There are no additional requirements for the functionality of the program, save that it actually works when executed. When you are done, ZIP it and upload it.Explanation / Answer
step
1.open eclipse editor.
2.click File -> New -> Java project
3. give name PacticeCar of that project
4. now right click on the PacticeCar and create class
YOUR ANSWER STEP BY STEP
2. ANS
SIMULATOR CLASS
public class Simulator {
public static void main(String[] args) {
Car car=new Car(); // create instance of Car class
car.run(); // call the run method
}
}
CAR CLASS
3. ANS
public class Car {
boolean running;
public Car() // declare a Car class constructor
{
Engine engine=new Engine(); // create instance of Engine Class
FuelTank fuelTank=new FuelTank(); // create instance of FuelTank Class
Wheels wheels=new Wheels(); // create instance of Wheels Class
engine.EngineStart(); // now call the EngineStart method using engine instance
this.running=engine.EngineRunnig(); // update the running variable value which return from EngineRunnig method
fuelTank.fuel=100; // stock fuel in FuelTank class
fuelTank.FuelQuantity(); // call FuelQuantity method using fuelTank instance
wheels.NoOfWheels.add(4); // add NoOfWheels in a collection class
}
public void run() // declare the run method
{
if(running)
System.out.println("car is running");
else
System.out.println("car is not able to run");
}
}
CARPARTS CLASS
2ND PART
public class CarParts {
public boolean FuelTankIsEmpty=false;
public boolean FuelTankIsFull=false;
public boolean engine;
public int TireLevelsOfWear=2;
}
ENGINE CLASS
/* Engine class inherit Carparts class */
public class Engine extends CarParts{
boolean running=false;
boolean StartEngine=false;
public void EngineStart() /* check engine should start or not */
{
if(FuelTankIsEmpty) /* check FuelTankIsEmpty which is inherit from CarParts */
{
StartEngine=false;
}
else if(FuelTankIsFull)
{
StartEngine=true;
}
}
public boolean EngineRunnig() /* check engine running position */
{
if(StartEngine==true)
{
running=true;
}
else if(StartEngine==false)
{
running=false;
}
return running;
}
}
FUELTANK CLASS
/*FuelTank class inherit Carparts class */
public class FuelTank extends CarParts{
public float fuel;
public void FuelQuantity() /* check FuelQuantity*/
{
if(fuel==0) /* fuel variable inherit from CarParts */
{
FuelTankIsEmpty=true;
System.out.println("Please insert fuel");
}
else if(fuel==100)
{
FuelTankIsFull=true;
}
}
}
WHEELS CLASS
import java.util.ArrayList;
public class Wheels extends CarParts { /* Wheels class inherit Carparts class */
ArrayList<Integer> NoOfWheels=new ArrayList<Integer>(); /* declare a collection for no of wheels */
public void TyreLevels()
{
TireLevelsOfWear=3; /* update the TireLevelsOfWear which declare in CarParts class */
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.