5 Replies - 1774 Views - Last Post: 14 August 2008 - 06:04 AM Rate Topic: -----

#1 java.abc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 08

Java assignment! Help me quick!

Post icon  Posted 14 August 2008 - 03:22 AM

Hey i have this assignment for next tuesday, and I really don't know
where to begin with this. Can someone please help me. I'm not saying to
give me the whole code, just a few to have some ideas to begin with...
------------------------------------------------------------------------------------------

The task is to write a class that simulates a car. When an oject of
this class is created, one must supply

- The capacity of the fuel tank in gallons.
- The fuel efficiency in miles per gallon;

Newly manufactured cars have empty fuel tanks.

The car will include the following displays (functions):

-a fuel guage, giving a number between 0.0 and 1.0 indicating how much
fuel is left in the tank.
-on odometer, giving the total number of miles the car has travelled.

The car should also support the following operations (procedures)

-fill the fuel tank
-drive a certaion mr of miles, consuming fuel. If there is insufficient
fuel to drive the whole distance, drive until the fuel is exhausted.
(You may wixh to use Math.min or Math.max to implement this part)

Is This A Good Question/Topic? 0
  • +

Replies To: Java assignment! Help me quick!

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Java assignment! Help me quick!

Posted 14 August 2008 - 04:06 AM

Look, this is a very simple assignment for you to learn the concept of object oriented programming. To understand the concept of a class and its methods.

What you're looking to create is a class called Car. Let's say the car has a few features, for example a gastank and an odometer. You can represent these in your class using apropriate variables. The fuel tank can be something between empty (0.0) or full (1.0) or anything in between like 0.5 = 50% full. Decimal values in java are represented by the type double so you should have a:

private double tank = 0.0;

To manipulate this value or to read it, you can simply have setters and getters. This is just a name for methods that set or get the values of a class private variable. For instance:

public void Filltank(double t) { // A setter that increases the fuel by t, returns void = nothing
tank = tank + t;
}

public boolean emptyTank() { // Not a getter or setter
return (tank == 0);
}

The later method returns a boolean (true or false) indicating if the tank is empty.
The first will fill up the tank, but you have to make sure you don't fill it up too much. Either by adding an if-statement inside the method or in your main method where you create the Car object.
Now the Car object doesn't only contain a gastank but also an odometer and possibly many other features:

private int odometer = 0;
private int numberOfDoors = 4;
private boolean trunk = true; //Does the car have a trunk? (boolean = true or false)
private String brand = "Porsche"; //Is it a Ferrari, Porsche or a Dodge?

public int getOdometer() { //A getter that returns how many miles the car has travelled, returns an integer value
return odometer;
}

This is not a solution for you but more an introduction to how OOP works. You'll have to put it together yourself and write more methods and also a main class or method. In the main method you will have to create a Car-object on which you use your methods.
To create an object in Java, you write:
Car myCar = new Car();
This will call the class constructor which you probably haven't yet written and in fact you don't always have to write.
On the other hand, you can also have many different constructors.
A constructor could look like:

public Car() {
}

or

public Car(double t, int o) {
tank = t;
odometer = o;
}

The first would only create an instance of the car object while the later allows you to set some of it's values from the start. In java, you can have both constructors in the same class and java will choose which one to use depending on what you send as input to the constructor. For example:

Car myCar = new Car(); //will call the first constructor while
Car someCar = new Car(1.0, 100); // Will call the second and set the values tank = 1.0 and odometer = 100;
Car yourCar = new Car(0.1, 40000)

Now let's say you want to fill the gastank, you simply invoke your Filltank-method on your Car object:

myCar.Filltank(0.8);

or check the odometer:

int tmp = yourCar.getOdometer();

or even if a car is out of gas:

boolean tmp2 = yourCar.emptyTank();

myCar, someCar and yourCar are all instances of the Car-class. All cars have the same structure (doors, gastank, odometer) but can have different features (1,2,3,4 or 5 doors, a trunk or no trunk).

I hope this helps you get started and that you'll come to enjoy programming.

This post has been edited by Gloin: 14 August 2008 - 04:54 AM

Was This Post Helpful? 1
  • +
  • -

#3 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Re: Java assignment! Help me quick!

Posted 14 August 2008 - 04:28 AM

I learned more from this post than I learned in the last nine weeks of Java class.....Thanks Gloin!

This post has been edited by StaceyE: 14 August 2008 - 04:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Java assignment! Help me quick!

Posted 14 August 2008 - 04:31 AM

Haha.. Wow! Maybe I ought to try being a teacher then.. Anyways, you're welcome!
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java assignment! Help me quick!

Posted 14 August 2008 - 04:44 AM

View PostStaceyE, on 14 Aug, 2008 - 04:28 AM, said:

I learned more from this post than I learned in the last nine weeks of Java class.....Thanks Gloin!

So for your next topic, instead of qualifying it as "Intermediate" you would be able to give it the "Advanced" tab :)
Happy coding.
Was This Post Helpful? 0
  • +
  • -

#6 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Re: Java assignment! Help me quick!

Posted 14 August 2008 - 06:04 AM

View Postpbl, on 14 Aug, 2008 - 04:44 AM, said:

View PostStaceyE, on 14 Aug, 2008 - 04:28 AM, said:

I learned more from this post than I learned in the last nine weeks of Java class.....Thanks Gloin!

So for your next topic, instead of qualifying it as "Intermediate" you would be able to give it the "Advanced" tab :)
Happy coding.

Ha Ha ...I wouldn't go that far....I couldn't code my way out of a wet paper bag so to speak...LOL :blink:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1