The car/vehicle application will be responsible for providing a car simulation.
behaviour
Expected Behaviour:
User shall be able to run the program with the following command line:
java –jar PROJ1.jar (Begin with a car)
java –jar PROJ1.jar Car (Begin with a car)
java –jar PROJ1.jar Truck (Begin with a Truck)
Commands:
M – Displays this menu
F – Display the current liters of fuel in the Vehicle. Display the total KM driven.
Z (X) – Fill up the vehicle with X liters.
D (X) – Drive the specified X kilometers.
Example of Behaviour:
Begin:
$>F
Your car has 10 liters of Fuel.
$>D 10
You drive the automobile 10KM.
$>D 10
You drive the automobile 10KM.
$>F
Your car has 8 liters of Fuel. You have driven 20 km.
$> D 85
You drive the automobile 80KM.
Your automobile has thrown an OutOfFuelException
Begin:
$>Z 50
You fill the car to the maximum of 50 liters of Fuel.
$>D 10
You drive the automobile 10KM.
$>D 10
You drive the automobile 10KM.
$>F
Your car has 48 liters of Fuel. Your car has driven 20 km
$> D 2000
You drive the automobile 480KM
Your automobile has thrown an OutOfFuelException
Begin:
$>Z 51
The Gas Station has thrown an FuelOverflowException
requirements
Project submission shall include an executable JAR file. (PROJ1.jar)
Project submission shall enable user to enter input as expected
Project submission shall output expected results
Project submission shall include a class named Car
Project submission shall include a class named Vehicle
Class Car shall inherit its properties and functionality from class Vehicle
Project submission shall include a class named FuelStation
FuelStation shall have a method named fillup that takes a Vehicle as an argument
Vehicle shall have a method named drive() that takes a double named km as a parameter
Eg. public void drive(double km)
Vehicle.drive(double km) shall attempt to drive km. If the vehicle runs out of fuel, the vehicle will throw an OutOfFuelException
FuelStation.fillup(Vehicle vehicle) shall throw a FuelOverflowException if the vehicle has too much fuel.
Project submission shall include javadocs
Project submission shall include file called README.txt
Project submission shall seamlessly handle either a Car or a Truck as a startup parameter
Project submission shall handle unexpected input gracefully
is there any body who can solve this problem of java
Re: The car/vehicle application will be responsible for providing a car simulation.
help me with the following program .....
Re: The car/vehicle application will be responsible for providing a car simulation.
Sure.
Post your code and ask a specific question. We are not going to do it for you.
Re: The car/vehicle application will be responsible for providing a car simulation.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication20;
/**
*
* @author MERUGU
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
class FuelStation{
static double fuel;
public void setfuel(double fuel){
FuelStation.fuel=fuel;
}
public static Vehicle fillup(Vehicle c){
c.setfuel(c.getfuel());
return c;
}
}
class FuelOverFlowException extends Exception {
}
class OutOfFuelException extends Exception {
}
class Vehicle {
double fuel=0;
public double getfuel(){
return fuel;
}
public void drive(double km) {
if (fuel < 0)
{
try {
throw new OutOfFuelException();
} catch (OutOfFuelException ex) {
}
}
else if (fuel >= 60)
{
try {
throw new FuelOverFlowException();
} catch (FuelOverFlowException ex) {
}
}
}
public void setfuel(double fuel){
this.fuel+=fuel;
}
}
class Car extends Vehicle{
}
/**
*
* @author Ankur
*/
public class Main {
static double fuel;
/**
* @param args the command line arguments
*/
public static void main(String [] args){
Scanner input = new Scanner (System.in);
// TODO code application logic here
FuelStation audi = new FuelStation();
for (;;){
System.out.println("M for Display this Menu."+"\n"+"F for displaying current litres of fuel"+
"\n"+"X for Filling Up the Vehicle With X litres"+"\n"+"D for Driving the Car"+"\n" +
"Q to quit from the car program");
String ch = input.next();
if (ch.equalsIgnoreCase("X"))
{
fuel+=fuel;
System.out.println("Enter Desired amount of fuel");
fuel = input.nextDouble();
// fuel+=fuel;
audi.setfuel(fuel);
} else if (ch.equalsIgnoreCase("F")){
System.out.println("You have "+fuel+"litres left");
}
if(ch.equalsIgnoreCase("q"))
{
System.exit(0);
}
}
}
}
in this code the first entered fuel is not adding to the second entered fuel plz help
Re: The car/vehicle application will be responsible for providing a car simulation.
Code java:
public class Main {
static double fuel;
Why do you have a static variable for fuel in the Main class? I can understand FuelStation and Vehicle having a fuel variable.
Code java:
FuelStation audi = new FuelStation();
Hmmm, audi is a car and not a gas station.
Code java:
if (ch.equalsIgnoreCase("X"))
{
fuel+=fuel;
System.out.println("Enter Desired amount of fuel");
fuel = input.nextDouble();
// fuel+=fuel;
audi.setfuel(fuel);
Why do you double the value in the fuel variable only to throw that value away and replace it with user input 2 lines later? Clean your code, delete lines don't comment them out. It just adds clutter and makes it harder to visualise your problem(s).
Re: The car/vehicle application will be responsible for providing a car simulation.
i want to sum all the fuels which are entered while running the program .
i will remove that doubling the value of fuel
Re: The car/vehicle application will be responsible for providing a car simulation.
Constraints:
• Car will have fuel tank capacity of 50 liters
• Truck will have fuel tank capacity of 60 liters
• Car will get 10 km per liter of gas mileage.
• Truck will get 6 km per liter of gas mileage
Re: The car/vehicle application will be responsible for providing a car simulation.
OK now do you have a question?
Re: The car/vehicle application will be responsible for providing a car simulation.
could u change the code according to the requirement ....
Re: The car/vehicle application will be responsible for providing a car simulation.
Yes I could but it is not my assignment. It is yours so you need to write the code. If I did it for you then that would be cheating.
Re: The car/vehicle application will be responsible for providing a car simulation.
its my assignment but it would be helpful for me . i will also learn from u. that why i am asking
Re: The car/vehicle application will be responsible for providing a car simulation.
it would be helpful for me and i will be learning from u thats why i am asking
Re: The car/vehicle application will be responsible for providing a car simulation.
What you need to do is break your assingment up into smaller chunks. Then think about placing those smaller chunks into their own method instead of cramming it all into the main method. For example you can have one method for each of the menu choices. For example if user enters 'M' call the printMenu method.
The other thing that will help is to concentrate on a single task, write code for that task, test the code thoroughly before moving onto the next task. Lets concentrate on the very first thing your assignment says:
User shall be able to run the program with the following command line:
java –jar PROJ1.jar
java –jar PROJ1.jar Car
java –jar PROJ1.jar Truck
This means your program needs to deal with command line arguments. I don't usually do this but here is a starter:
Code :
public static void main(String[] args) {
if(args.length > 0) {
System.out.println(args[0]);
} else {
System.out.println("You did not enter a command line argument");
}
}
Now if you use that main method in your class and run those three commands from your instructions you will see either "Car", "Truck" or "You did not enter a command line argument" displayed.
Re: The car/vehicle application will be responsible for providing a car simulation.