user1879127
user1879127

Reputation: 1

Java 7 Cannot retrieve information from an object

This is my first post so sorry if it's not done right. This program is for my AP Computer Science course (I do not want a sample of code, just an explanation of why this is happening). Whenever I try to get a variable from an object, I get an error that says the variable does not exist(NetBeans IDE). Thanks in advance!

This is the code:

public class AnnualFuelTester 
{   
    public static void main(String [] args)
    {
        AnnualFuelUse Fill1 = new AnnualFuelUse(1, 1,  45023, 45231, 10.00, 2.95);
        AnnualFuelUse Fill2 = new AnnualFuelUse(2, 4,  45231, 45480, 11.70, 2.99);
        AnnualFuelUse Fill3 = new AnnualFuelUse(3, 8,  45480, 45659,  9.30, 3.03);
        AnnualFuelUse [] FillArray = {Fill1, Fill2, Fill3};

        System.out.println("Fillup    Days    Start Miles    Distance    Gallons        Consumed    MpG    Fuel Price    Cost");
        **int temp = Fill1.distance;**
        System.out.printf ("%4.1f     %4.1f   %4.1f          %4.1f       %4.1f                   %4.1f  %4.1f         %4.1f", );
    }
}

(edit:) AnnualFuelUse:

public class AnnualFuelUse 
{
    private int dist = 0;
    private double MpG = 0;
    private double CoT = 0;
    private double totals = 0;
    private double projection = 0;
    private double min = 0;
    private double max = 0;

    Annual_Fuel_Use(int fillTemp, int daysTemp, int startTemp, int stopTemp, double gallTemp, double priceTemp){
        int fillNumber = fillTemp;                                       
        int daysSinceLastFill = daysTemp;                                       
        int startMiles = starttemp;                                     
        int stopMiles = stoptemp;                                       
        int distance = Annual_FuelUse.travelDistance(startMiles, stopMiles);
        double fuelConsumed = galltemp;
        double mPG = AnnualFuelUse.milesPerGal(dist, fuelConsumed);
        double pricePerGallon = pricetemp;
        double tripCost = AnnualFuelUse.costOfTrip(pricePerGallon, fuelConsumed);
    }

    public double getMilesPerGal(){
        return MpG;        
    }

    public int getTravelDistance(){
        return dist;        
    }

    public double getCostOfTrip(){
        return CoT;        
    }

    public double getTotals(){
        return totals;
    }

    public double getProjection(){
        return projection;
    }



    public int travelDistance(int distance1, int distance2){   
        dist = distance2 - distance1;
        return dist;
    }

    public double milesPerGal(int travelDistance, double fuelConsumed){        
        MpG = travelDistance / fuelConsumed;
        return MpG;
    }

    public double costOfTrip(double pricePerGal, double fuelConsumed){        
    CoT = pricePerGal * fuelConsumed;
        return CoT;
    }

    public double totals(double dat1, double dat2, double dat3){
        totals = dat1 + dat2 + dat3;
        return totals;
    }

    public double projection(int days,double projected){
        projection = (projected / days) * 365;
        return projection;
    }

    public double dataMin(int dat1, int dat2, int dat3){
        if (dat1 <= dat2){
            min = dat1;
        }
        if (min <= dat3){
            return min;
        } else {
            return dat3;
        }
    }

    public double dataMax(int dat1, int dat2, int dat3){
        if (dat1 >= dat2){
            max = dat1;
        }
        if (max >= dat3){
            return max;
        } else {
            return dat3;
        }
    }
}

Upvotes: 0

Views: 159

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533660

I have just noticed you have incorrectly used static fields. You should avoid setting static fields in a constructor as this leads to a nightmare of confusion.

I suggest you

  • make all your fields non-static, and any methods which use them non-static. If you don't do this all your objects are actually using the same fields, not their own.
  • make you getters match your field names. Try to keep as much consistency as possible or this can lead to confusion as well. For example if you have a travelDistance make the fields the same name as well. If you want to make it clear make the getter method getTravelDistance
  • don't set lots of variables you then discard. Only keep code which is useful and this can be confusing as well.
  • don't use underscores in class names.

In general you need to work of your consistency, e.g. your min/max method are half implemented using Math and half your own code.

I suggest you fix all your code before you continue.

Upvotes: 2

Perception
Perception

Reputation: 80623

You have some basic flaws in the design of your Java (bean) class, and as you can see, this is leading to issues in using it.

  • The most immediate problem is one of scope/visibility. Your class variables are declared private, which means they are not visible outside of the class itself. In order to use them the way you are now (in the main method), you would need to make them public or package-private. But dont! See point below.

  • You are correct to make your class variables private, this is due to the principles of encapsulation. An object should hide as much of its internal implementation as possible, while providing a consistent interface to interact with it. What you are missing is getters/setters that can be used by callers to manipulate the internal state of your object. Add them in!

  • Your class variables are just that - class variables! This is because you have made them static. This means all instances of your class will share the same values - I'm pretty sure thats not what you intended. Fix this by removing the static keyword on those fields. Also remove the static keyword from all the classes methods.

  • Lastly, you are not using good Java Naming Conventions. Doing so makes your code more readable, both to yourself, and others. Trust me, you will be able to identify, diagnose, and fix bugs much faster if you follow these conventions than if you dont.

Your Java (bean) class should look something like this:

public class AnnualFuelUse {
    private int dist = 0;
    private double MpG = 0;
    private double CoT = 0;
    private double totals = 0;
    private double projection = 0;

    // Constructors

    // Non-static getters/setters

    // Other methods
}

And your calling class should look something like this:

public class AnnualFuelTester {   
    public static void main(String [] args) {
        AnnualFuelUse fill1 = new AnnualFuelUse(1, 1,  45023, 45231, 10.00, 2.95);
        int temp = fill1.getDistance();
    }
}

That should get your started in the right direction.

Upvotes: 1

Robin Chander
Robin Chander

Reputation: 7425

First of all I don't see and distance variable but I do see dist and that too is private.

Create getters for your fields as

 public int getDist(){
   return dist;
 }

Then use then in your Annual_Fuel_Tester class as

int temp = FILL_1.getDist();

Upvotes: 1

Related Questions