-3

I've read through all the duplicates that are around. And my brain somehow doesn't understand this. Basically, I'm trying to call a method from a different class and then using the method's value in a calculation. However this problem seems to keep arising for me. I really need help, I've been at it for 2+ hours now.

Here's the Code I'm trying to import the method into.

/* Alex Pedersen V 0.1 13 - 02 - 2015 Forest Aflevering */
  public class Forest {

  /* Strings */
  private String name;
  private String location;

  /* Integers */
  private int squareMeters;
  private int price;
  private int totalcost;

  /* Reference types */
  private Owner forestOwner;
  private Tree newtree;
  private Tree sumcost;
  private Tree costPerSquareKilometer;

    /* Constructor med parametere */
    public Forest(String nameInput, String locationInput, int sizeOfForest, int pricePerSquare) {
      /* Initialisering af instans variabler */
      name = nameInput;
      location = locationInput;
      squareMeters = sizeOfForest;
      price = pricePerSquare;
      totalcost = 0;
    }

    /* Constructor uden parametere */
    public Forest() {
      /* Initialisering af instans variabler */
      name = "Rold Skov";
      location = "Nordjylland";
      squareMeters = 4000;
      price = 100;
      totalcost = 0;
    }

      /* ## ACCESORS ## */

      /* String Returns */
      public String getName() {
        return name;
      }

      public String getLocation() {
        return location;
      }

      public Owner getForestOwner() {
        return forestOwner;
      }

      /* Int Returns */
      public int getSquareMeters() {
        return squareMeters;
      }

      public int getPrice() {
        return price;
      }

      public Tree getCostPerSquareKilometer() {
        return costPerSquareKilometer;
      }

      /* ## MUTATORS ## */

      /* String Mutators */
      public void setName(String newNameInput) {
        name = newNameInput;
      }

      public void setLocation(String newLocation) {
        location = newLocation;
      }

      /* Int Mutators */
      public void setSquareMeters(int newSquareMeters) {
        squareMeters = newSquareMeters;
      }

      public void setPrice(int newPrice) {
        price = newPrice;
      }

      /* Reference String Mutators */
      public void setForestOwner(Owner newForestOwner) {
        forestOwner = newForestOwner;
      }

      public void setTree(Tree newTree) {
        newtree = newTree;
      }

      public void setCostPerSquareKilometer() {
        Tree sumcost = new Tree();

        costPerSquareKilometer = Tree.getCostPerSquareKilometer();

      }

      /* ## PRINTS ## */
      public void printForest() {
        System.out.println("Forest Owner: " + forestOwner + "\nName: " + name + "\nLocation: " + location + "\n Square Meters: " + squareMeters + "\n Price: " + price);
      }

      public void printForest2() {
        if (newtree != null) {
          System.out.println("Tree: " + newtree + "\nForest Owner: " + forestOwner + "\nName: " + name + "\nLocation: " + location + "\n Square Meters: " + squareMeters + "\n Price:" + price);
        }
        else {
          System.out.println("No trees in this program :(!");
        }

      }
  }

And here's the code that I'm trying to import the getCostPerSquareKilometer from.

public class Tree {

/* Strings */
private String sort;

/* Integers */
private int lifespan;
p int costPerSquareKilometer;
private int revenuePerSquareKilometer;

    public Tree() {
        /* Initialisering af variabler */
        sort = "Aske";
        lifespan = 100;
        costPerSquareKilometer = 1000;
        revenuePerSquareKilometer = 2000;
    }

    public Tree(String sortInput, int lifepanInput, int costPerSquareKilometerInput, int revenuePerSquareKilometerInput, int squareMetersInput) {
        sort = sortInput;
        lifespan = lifepanInput;
        costPerSquareKilometer = costPerSquareKilometerInput;
        revenuePerSquareKilometer = revenuePerSquareKilometerInput;
    }

    /* ## ACCESORS ## */

    /* String Returns */
    public String getSort() {
        return sort;
    }

    /* Int Returns */
    public int getLifespan() {
        return lifespan;
    }

    public int getCostPerSquareKilometer() {
        return costPerSquareKilometer;
    }

    public int getRevenuePerSquareKilometer() {
        return revenuePerSquareKilometer;
    }

    /* ## MUTATORS ## */

    /* String Mutators */
    public void setSort(String newSort) {
        sort = newSort;
    }

    /* Int Mutators */
    public void setLifespan(int newLifeSpan) {
        lifespan = newLifeSpan;
    }

    /* Double Mutators */
    public void setCostPerSquareKilometer(int newCostPerSquareKilometer) {
        costPerSquareKilometer = newCostPerSquareKilometer;
    }

    public void setRevenuePerSquareKilometer(int newRevenuePerSquareKilometer) {
        revenuePerSquareKilometer = newRevenuePerSquareKilometer;
    }

    /* ## PRINT ## */
    public void printTree() {
        System.out.println("Sort: " + sort + "\nLifespan: " + lifespan + "\nCost per KM^2: " + costPerSquareKilometer + "\nRevenue per KM^2" + revenuePerSquareKilometer);
    }
}

Thank you very much in advance :>

2 Answers2

0

You should change your line:

costPerSquareKilometer = Tree.getCostPerSquareKilometer();

To

 costPerSquareKilometer = sumcost.getCostPerSquareKilometer();

Currently you are calling it like . which means method has to be static while you defined method as non static i.e. no static keyword when you defined the method. So that means you can access method only on object instance of Tree in your case, hence use sumcost instance to access cost.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • I will try that, one moment good sir. – Alex Pedersen Feb 13 '15 at 12:46
  • It lets me know that "incompatible types: int cannot be converted to Tree". – Alex Pedersen Feb 13 '15 at 12:47
  • change `private Tree costPerSquareKilometer;` to `private int costPerSquareKilometer;` why would you need it to be tree type? – SMA Feb 13 '15 at 12:54
  • I changed it to private int costPerSquareKilometer; now, and now I get the error: "non static-method getCostPerSquareKilometer() cannot be referenced from a static context". I just don't understand where I made a context static. Like where have I made anything static? The word static isn't even in my entire code. – Alex Pedersen Feb 13 '15 at 13:00
  • Which line its giving error on and whats the error? – SMA Feb 13 '15 at 13:02
  • It gives me the error in the line: costPerSquareKilometer = Tree.getCostPerSquareKilometer(); and the error is: "non static-method getCostPerSquareKilometer() cannot be referenced from a static context" – Alex Pedersen Feb 13 '15 at 13:05
  • did you read my answer then? I said remove Tree from your method call. – SMA Feb 13 '15 at 14:04
  • Okay, I'll explain it more in debth. – Alex Pedersen Feb 13 '15 at 16:40
  • I have 2 different classes, in 1 class (Tree) I have a value (which is costPerSquareKiloMeter). Now my assignment lets me know, that I have to take the value from the costPerSquareKiloMeter and call it in a new accessor method inside my Forest class. But I don't know how I do this. – Alex Pedersen Feb 13 '15 at 16:41
0

The problem is as described by the error. Static calls can only be made to static methods. The getCostPerSquareKilometer is not static, so cannot be referenced as a static.

  public void setCostPerSquareKilometer() {
    Tree sumcost = new Tree();

    costPerSquareKilometer = Tree.getCostPerSquareKilometer();

  }

Updated:

  public void setCostPerSquareKilometer() {
    Tree sumcost = new Tree();

    costPerSquareKilometer = sumcost.getCostPerSquareKilometer();

  }

This change, means that you would be calling the method on the object and not on the class

Mike Curry
  • 1,609
  • 1
  • 9
  • 12
  • I've tried updating it to this, and it lets me know "incompatible types: int cannot be converted to Tree". Thanks in advance :> – Alex Pedersen Feb 13 '15 at 12:49
  • That is because your Tree method returns an int for getCostPerSquareKilometer(), but in your forest class you have this declared as a Tree and then you try to assign the int to the Tree which the compiler does not know what to do with. – Mike Curry Feb 13 '15 at 12:55
  • I managed to fix that now, but I get the return "non static method getCostPerSquareKilometer() cannot be referenced from a static context". I just don't understand why it's telling me that I have a static context, as far as I understand, I don't even have the word static in my code.. – Alex Pedersen Feb 13 '15 at 13:01