• Post Reply Bookmark Topic Watch Topic
  • New Topic

My program shows no output in Eclipse in NetBeans  RSS feed

 
Connor Palazzo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has no errors in it, but when it runs, it just doesn't show any output at all. Can someone explain why?
Here's my code.

Fuel Class:

public class FuelGauge {
// Setting the gallons and the total amount of gallons
private int gallons;
final static int TotalGallons=15;


public FuelGauge(){
gallons=0;
}

// Initiate the number of gallons
public FuelGauge(int gallons){
if (gallons < TotalGallons){
this.gallons=gallons;
} else{
gallons= TotalGallons;
}
}

// Return the number of gallons
public int findGallons(){
return gallons;
}

//Adds one gallon until it is full and displays a message
public void increaseFuel(){
if (gallons < TotalGallons) {
gallons++;
} else {
System.out.println("Tank is full");
}
}

// Reduce the number of gallons by one until there is none left and displays a message.
public void loseFuel() {
if (gallons > 0) {
gallons--;
} else {
System.out.println("Tank has ran out of fuel.");
}
}
}

Odometer Class:

public class Odometer {

// Constants
public final int MPG = 24;
public final int TotalMileage = 999999;

private int StartingMileage;
private int mileage;

// Reference to FuelGauge class
private FuelGauge fuelgauge;


// Constructor and reference.
public Odometer(int mileage, FuelGauge fuelGauge) {
this.StartingMileage=mileage;
this.mileage = mileage;
this.fuelgauge = fuelGauge;
}

// Return mileage
public int findMileage() {
return mileage;
}

// Increases the mileage, if it reaches 999999, it resets to 0.
public void addMileage() {

if (mileage < TotalMileage) {
mileage++;
} else {
mileage = 0;
}
int driven = StartingMileage - mileage;
if (driven % MPG == 0) {
fuelgauge.loseFuel();
}

}

}

FuelOdometerCarDemo:

public class FuelOdometerCarDemo {


public static void main(String[] args) {

//Constants and references to other classes
FuelOdometerCarDemo FO= new FuelOdometerCarDemo ();
FuelGauge fuel = new FuelGauge();
Odometer odometer;
odometer = new Odometer (0,fuel);

//Adds fuel
for (int x = 0; x < FuelGauge.TotalGallons; x++) {
fuel.findGallons();
}


// Infinite loop
while (fuel.findGallons() > 0) {

odometer.addMileage();

// Shows the mileage.
System.out.println("Miles: " + odometer.findMileage());


// Shows the amount of fuel.
System.out.println("Fuel left: " + fuel.findGallons() + " gallons");
}

}

}

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


and that constructor is:


So you have 0 gallons, which is what findGallons returns.
So it never goes into the while loop.
 
Connor Palazzo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:

and that constructor is:


So you have 0 gallons, which is what findGallons returns.
So it never goes into the while loop.


Thanks, I was wondering if it was one of the class codes.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've no idea if the rest of your logic is correct...

By the way, there's a Code button at the top of the Reply editor that'll stick in code tags, in between which you can post code so the formatting is retained.
Helps people follow your code.
 
Connor Palazzo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Well, I've no idea if the rest of your logic is correct...

By the way, there's a Code button at the top of the Reply editor that'll stick in code tags, in between which you can post code so the formatting is retained.
Helps people follow your code.


Thanks and the rest of it does work the way it's suppose to.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a page that talks about how to UseCodeTags
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please repost your code with code tags and proper indentation, as people have told you.

Please give all your methods the correct names. It should be getGallons not findGallons.
Why have you got a static variable? Do you really want it to be the same for all fuel tank instances? Why have you got those public fields?
In the loop where you fill the tank, what are you doing?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I confess that I have not read any of this thread, but the phrase "in Eclipse in NetBeans" caught my attention - if nothing else it indicates some confusion, IMO. :-)
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then,

why didn't you read OP's topic?

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!