FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

java help pls!

 


logicfail182
Please understand that I don't expect you to do my work; I was just hoping that after 2 days of work someone could point me in the right direction.

This is my code.
any help would be much appreciated.
Here's the assignment:
http://pavao.org/compsci/apcs/WebLessons2/LessonA4/Lab-A4-1.html

Here's the code:
public class Car{

int start;
int end;
double gasUsed;
public void car(int odometerReading){
}


public void fillUp(int odometerReading, double gallons){

}

public double calculateMPG(){
calculateMPG = odometerReading - start / gallons;
}

public double resetMPG(){
start = odometerReading;
}

public void calculate(){
Car auto = new Car(15);
System.out.println("New car odometer reading: ");
auto.fillUp(150, Cool;
System.out.println("Miles per gallon" + auto.calculateMPG());
System.out.println("Miles per gallon" + auto.calculateMPG());
auto.resetMPG();
auto.fillUp(350, 10);
auto.fillUp(450, 20);
System.out.println("Miles per gallon" + auto.calculateMPG());
auto.resetMPG();
auto.fillUp(603, 25.5);
System.out.println("Miles per gallon" + auto.calculateMPG());
}
}

[/code]
Peterssidan
I wrote it so that it gives the same output as the "Sample Output". resetMPG returns a double but what value is that? I don't know.
A tricky thing is the fillUp method. Note that the myEndMiles is set to the new value but the myGallonsUsed is added on top of the old value.

Code:
public class Car {

   int myStartMiles;
   int myEndMiles;
   double myGallonsUsed;

   public Car(int odometerReading) {
      myStartMiles = odometerReading;
      myEndMiles = 0;
      myGallonsUsed = 0;
   }

   public void fillUp(int odometerReading, double gallons) {
      myEndMiles = odometerReading;
      myGallonsUsed += gallons;
   }

   public double calculateMPG() {
      return (double)(myEndMiles - myStartMiles) / myGallonsUsed;
   }

   public double resetMPG() {
      myStartMiles = myEndMiles;
      myGallonsUsed = 0;
      
      return 0.0; // what should be returned?
   }

   public static void main(String[] args) {
      Car auto = new Car(15);
      System.out.println("New car odometer reading: ");
      auto.fillUp(150, 8);
      System.out.println("Miles per gallon: " + auto.calculateMPG());
      System.out.println("Miles per gallon: " + auto.calculateMPG());
      auto.resetMPG();
      auto.fillUp(350, 10);
      auto.fillUp(450, 20);
      System.out.println("Miles per gallon: " + auto.calculateMPG());
      auto.resetMPG();
      auto.fillUp(603, 25.5);
      System.out.println("Miles per gallon: " + auto.calculateMPG());
   }
}
raneez_n
This coding is better than the first one..
And i think the reset method does not need to return any values..it just reset the previous values.
Peterssidan wrote:
I wrote it so that it gives the same output as the "Sample Output". resetMPG returns a double but what value is that? I don't know.
A tricky thing is the fillUp method. Note that the myEndMiles is set to the new value but the myGallonsUsed is added on top of the old value.

Code:
public class Car {

   int myStartMiles;
   int myEndMiles;
   double myGallonsUsed;

   public Car(int odometerReading) {
      myStartMiles = odometerReading;
      myEndMiles = 0;
      myGallonsUsed = 0;
   }

   public void fillUp(int odometerReading, double gallons) {
      myEndMiles = odometerReading;
      myGallonsUsed += gallons;
   }

   public double calculateMPG() {
      return (double)(myEndMiles - myStartMiles) / myGallonsUsed;
   }

   public double resetMPG() {
      myStartMiles = myEndMiles;
      myGallonsUsed = 0;
      
      return 0.0; // what should be returned?
   }

   public static void main(String[] args) {
      Car auto = new Car(15);
      System.out.println("New car odometer reading: ");
      auto.fillUp(150, 8);
      System.out.println("Miles per gallon: " + auto.calculateMPG());
      System.out.println("Miles per gallon: " + auto.calculateMPG());
      auto.resetMPG();
      auto.fillUp(350, 10);
      auto.fillUp(450, 20);
      System.out.println("Miles per gallon: " + auto.calculateMPG());
      auto.resetMPG();
      auto.fillUp(603, 25.5);
      System.out.println("Miles per gallon: " + auto.calculateMPG());
   }
}
blueray
This is my version but added a bit other stuffs.
implemented it c# first and then converted back to java.
Cause I have no java installed at all.

Code:

public class Car
{
   private int myStartMiles;
   private int myEndMiles;
   private double myGallonsUsed;

   public Car(int odometerReading)
   {
      this.myStartMiles = odometerReading;
      this.myEndMiles = 0;
      this.myGallonsUsed = 0;
   }

   public void fillUp(int odometerReading, double gallons)
   {
      this.myEndMiles = odometerReading;
      this.myGallonsUsed += gallons;
   }

   public double calculateMPG()
   {
      if (this.myEndMiles > this.myStartMiles)
      {
         return (double)(this.myEndMiles - this.myStartMiles) / this.myGallonsUsed;
      }
      return 0;
   }

   public double resetMPG()
   {
      this.myStartMiles = this.myEndMiles;
      this.myGallonsUsed = 0;
      return this.myStartMiles;
   }
}

public class test
{
   //public static void Main(string[] argv) // C# header
   public static void main(String[] argv) // Java header
   {
      Car auto = new Car(15);
      Console.WriteLine("New car odometer reading:");
      auto.fillUp(150, 8);
      Console.WriteLine("Miles per gallon: " + auto.calculateMPG());
      auto.resetMPG();
      auto.fillUp(350, 10);
      auto.fillUp(450, 20);
      Console.WriteLine("Miles per gallon: " + auto.calculateMPG());
      auto.resetMPG();
      auto.fillUp(603, 25.5);
      Console.WriteLine("Miles per gallon: " + auto.calculateMPG());
      
   }
}
The-Nisk
What a chunky & awkward looking code Shocked
Related topics

Java Help
java help
JAva HELP
Nc6230 probs..need sum help pls!
Favourite game? (OFFICIAL THREAD)

Constructors in Java, help
Starting my WEBSITE.. HELP PLS??
psychostats
SMF Forum is not working
Hi

Java Help
New Problem with Viewing letters Þ Ý not i and s
Help pls
Public Speaking URGENT
java help:how does dynamic instantiation of classess happen?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.