*p3.5

From GGCWiki
Jump to: navigation, search

Nick's Completed Program


  • Hey guys, I can't for the life of me get java to work on this laptop I've tried to delete the folder and update to the newest version , Every browser has the option checked to enable java , ive tried ggc.wiki It wont load the java applet , Ive tried Eclipse and it aside from taking 5 mins to do anything it dosent work either. I'll keep trying thoughout the day. My laptop broke Tuesday night, I shipped it off to dell thursday afternoon , I'm praying it will get back to me over the weekend I really need it for all my school files, Luckily we kept adding revisions to the eclipse file for poker on this wiki so I will keep trying but at this point all I can do is comment on code till I actually get a working machine. -Sorry about the inconvience I can comment on anyones code and suggest new ways to do it or try to point out mistakes . - Chris Hull


Chris's suggestion for this : The car needs get a input saying how many miles per gallon it can drive . In the book they say 50 mles per gallon

Next it says what is the tank size. In the book it suggests 20 meaning the total miles this care can now drive is 400 x= new Car(50) * Gas(20)

Finally it says you drive 100 miles so it would be x-100 which in this example would be 300 we need to make a tester class for this we have been using a few example of those let me link up to one. http://ggc.javawide.org/index.php/intermediate/RationalTest this has an example of a test which can add numbers it looks like it would benefit us well.


Revision

  • I made some changes that allow user input to test the program and it will also output the information better.
package assignments;

import java.util.Scanner;

public class Car {

	/**
	 * @param args
	 */
	double fuel;
	int eff; //I felt more comfortable declaring this rather than having to use the this.something
	
	public Car (int efficiency, int gas) // added the gas parameter so the user can choose how much gas is in the tank
	{
		fuel = 0;
		eff = efficiency;
		addGas(gas);
		driveCar(100);
	}
	
	public static void main(String[] args) 
	{
                //allows user input
		Scanner in = new Scanner(System.in);
		int input;
		int gas;
		System.out.println("Input Miles per Gallon : ");
		input=in.nextInt();
		System.out.println("How many gallons are in the tank? ");
		gas=in.nextInt();
		Car car = new Car(input, gas);
	}

	public void driveCar(int distance)
	{
		fuel -= distance / eff;
		System.out.println(getGasInTank());
	}
	
	public String getGasInTank()
	{
                // makes the output more clear if its a string instead of just an int.
		return "After driving 100 miles there are " + fuel + " gallons of gas remaining";
	}
	
	public void addGas(double gas)
	{
		fuel+=gas;
	}
}
Personal tools