6 Replies - 621 Views - Last Post: 16 July 2012 - 06:21 PM Rate Topic: -----

#1 jillmasonmarquez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-June 12

I'm having a problem with my Java methods

Posted 16 July 2012 - 01:28 PM

My program does not crash when ran but I am having trouble getting the correct output values. The assignment is as follows:
1. Add a named constant to the Car class to represent a default MPG of 23.0. This should be used in the ocnstructors
described below.
2.Add the following methods to the Car class:
 Car() | a standard no-args constructor. Set mileage to 0 and tank level to 0.
 Car(double miles, double fuel) | a constructor that creates a car that has had some miles put on it and
was bought with fuel in the tank. Call the other constructor to automatically set the MPG to the default
MPG.
double fillTank(double amount) | Same behavior as the existing fillTank method, except that the
argument is a number of gallons to add to the tank. If the resulting tank level would be greater than 15
gallons, print a message to this e ect and don't change the tank level. This is an example of overloading.
3. Create a Java class called Fleet. Implement a main mathod in this class. As before, this is for your own testing; I
will use my own main method to grade your program. This Fleet class should have two instance variables of type
Car, primaryCar and secondaryCar. Implement accessors and mutatord for these properties.
4. Implement an instance method in the Fleet class called size that returns the number of cars (0, 1, or 2) in the
fleet.
5. Also implement a static method in the Fleet class called getDescription that takes a Fleet as an argument and
returns a string containing the size of the
eet and the mileage and fuel level of any cars that are in the
eet.
6. In your main method, create a
eet, get (and print) its description, add a primary and secondary car, and again
get (and print) the description. Drive each car 30 miles, then 60 miles. Fuel up the primary car and then drive
both 300 miles. Print out the
eet's description after each step. Then add a new primary car and get (and print)
the description.
7. When constructing cars, use the new constructor to create cars, giving them rendom mileages between 20,000 and
100,000 miles and full tanks of gas.

the code i have so far is this:
public class Car
{
  private double odometer;
  private double numberOfGallons;
  private double averageMPG;
  private double lastOilChange;
  private int serialNumber;
  
    public double getMileage()
    {
      return odometer;
    }
    public double getFuelLevel()
    {
      return numberOfGallons;
    }
    public double getAverageMPG()
    {
      return averageMPG;
    }
    public void setAverageMPG(double newAverage)
    {
      if (newAverage >= 0)
        averageMPG = newAverage;
    }
    public double getLastOilChange(){
      return lastOilChange;
    }
    public double getSerialNumber()
    {
      return serialNumber;
    }
    public void setSerialNumber(int newSerialNumber)
    {
      if (newSerialNumber >= 0)
        serialNumber = newSerialNumber;
    }
    public void drive(double miles){
    miles += 0;
    odometer = miles;
    numberOfGallons = 15;
    averageMPG = miles/numberOfGallons;
    if (miles > odometer){
      System.out.println("Not enough fuel");
    }
    if (miles >= 3000){
      System.out.println("Car in need of oil change");
    }
    if (miles >= 4500){
      System.out.println("Urgent: Oil change needed");
    }
   }
    public double fillTank(){
    numberOfGallons = 15;
    return (averageMPG - odometer);
    }
    public void changeOil(){
      lastOilChange = odometer;
    }
    public void Car(){
      odometer=0;
      numberOfGallons=0;
    }  
    public void Car(double miles, double fuel){
      averageMPG=23.0;
    }    
    public double fillTank(double amount){
      amount += 0;      
      if (amount > 15){
      System.out.println("Already enough fuel");
      }
      return (amount - numberOfGallons);
      }
    public String toString(){
      return "<Car(" + odometer + "," + numberOfGallons + "," + averageMPG + "," + lastOilChange + "," + serialNumber +")>";
    }
    public static void main(String[] args){
      Car myCar = new Car();
      myCar.drive(75);
      System.out.print(myCar);
      myCar.fillTank();
      System.out.print(myCar);
      myCar.changeOil();
      System.out.print(myCar);

  }
}



that piece is my car class and the next piece will be my fleet class:
public class Fleet
{  
    private Car primaryCar;
    private Car secondaryCar;
    private double size;
    private String description;
        
    public Car getPrimaryCar(){
      return primaryCar;
    }
    public Car getSecondaryCar(){
      return secondaryCar;
    }
    public double getSize(double size){
      if (size == 0)
        System.out.println("There are no cars in the fleet");
      if (size == 1)
        System.out.println("There is one car in the fleet");
      if (size == 2)
        System.out.println("There are two cars in the fleet");
      return size;
    }
    public static String getDescription(String Fleet){
      return Fleet;
    }
    public String toString(){
      return "<Fleet: " + primaryCar +","+ secondaryCar +","+ size +","+ description +">";
    }
    public static void main(String[] args){
      Fleet myFleet = new Fleet();
      System.out.println(myFleet);

      }    
    }


There are not any current error's in my program however i am a bit lost on where to go from here.

Is This A Good Question/Topic? 0
  • +

Replies To: I'm having a problem with my Java methods

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: I'm having a problem with my Java methods

Posted 16 July 2012 - 01:30 PM

Quote

My program does not crash when ran but I am having trouble getting the correct output values.

It would be helpful if you could be more specific about this.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

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

Re: I'm having a problem with my Java methods

Posted 16 July 2012 - 01:33 PM

So what is the actual output and the expected one ?
Was This Post Helpful? 0
  • +
  • -

#4 jillmasonmarquez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-June 12

Re: I'm having a problem with my Java methods

Posted 16 July 2012 - 02:00 PM

I am receiving a null output where i should be receiving outputs such as the number of cars in the fleet, the mileage off the car, the amount of fuel. I also need to be able to drive a car 30 then 60 miles, refill the tank and drive another 300 miles. Sorry if that I was unclear before hopefully this explains thing a bit better.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: I'm having a problem with my Java methods

Posted 16 July 2012 - 02:09 PM

And which main() do you run ? The one in Car or the one in Fleet ?

The one in Fleet does not do much, it creates a Fleet object and prints it with a size of 0.

BTW your size should not be a double but a int. Do you expect to have 3.14159 cars in your Fleet ? Comparaison of double with == are dangerous

if(size == 2.0) might not be true if 2.0 was calculated and is effectively 2.000000000001 or 1.99999999999999
Was This Post Helpful? 0
  • +
  • -

#6 jillmasonmarquez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-June 12

Re: I'm having a problem with my Java methods

Posted 16 July 2012 - 02:24 PM

i was trying to run the main() in fleet, however when i tried to add different commands to my main method in fleet that is where i was encountering some error messages that read something like variable is not defined. That error message came up when i was trying to add "size" into my main method. That is why my main() in the fleet class doesnt do anything. Here is my statement that i received the error message from when i added it to my main().
myFleet.size()
System.out.println(myFleet);


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: I'm having a problem with my Java methods

Posted 16 July 2012 - 06:21 PM

Fleet has a getSize() method but not a size() method so

myFleet.size();

will generate an error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1