Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: The car/vehicle application will be responsible for providing a car simulation.

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: The car/vehicle application will be responsible for providing a car simulation.

    help me with the following program .....

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: The car/vehicle application will be responsible for providing a car simulation.

    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.
    FuelStation audi = new FuelStation();
    Hmmm, audi is a car and not a gas station.
    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).

  6. The Following User Says Thank You to Junky For This Useful Post:

    vikasreddy556 (June 19th, 2011)

  7. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  8. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: The car/vehicle application will be responsible for providing a car simulation.

    OK now do you have a question?

  10. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: The car/vehicle application will be responsible for providing a car simulation.

    could u change the code according to the requirement ....

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default 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.

  12. #11
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  13. #12
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  14. #13
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default 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:
    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.

  15. The Following User Says Thank You to Junky For This Useful Post:

    vikasreddy556 (June 20th, 2011)

  16. #14
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: The car/vehicle application will be responsible for providing a car simulation.

    i will try my best

Similar Threads

  1. Airline Management Simulation Game
    By aussiemcgr in forum The Cafe
    Replies: 23
    Last Post: July 8th, 2011, 03:59 PM
  2. Blackjack simulation program help
    By senorfletch in forum Java Theory & Questions
    Replies: 2
    Last Post: April 5th, 2011, 09:22 AM
  3. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 05:51 PM
  4. Monty Hall Problem Simulation
    By caseyd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 12:31 AM
  5. Keypress simulation problems
    By ummix in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2010, 06:31 AM