0

I have not been able to get my methods working, as when I import classes from the same package it gives me an error. When I put them in the same package it gives me an error of "cannot find symbol", referring to the class/method I am trying to use in the second one. Like here for example, I can use variables from the other classes but it throws an error whenever I use a method. I have seen similar problems but none of them have helped me so far.

1st class:

package main;

/**
*
* @author Darias
*/
public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        ball p;
        p = new ball();
        System.out.println("the ball weighs" +p.getlength);
    }

}

2nd class:

package main;

public class ball {

    float length;
    float weight;
    public ball()
    {
        length = 100;
        weight = 250;
    }
    public ball(float length, float peso)
    {
        this.length = length;
        this.weight = weight;
    }
    public float getlength()
    {
        return length;
    }
    public float getweight()
    {
        return weight;
    }
    public void kickball()
    {
        System.out.println("you kicked the ball");
    }
    public void atraparPelota()
    {
        System.out.println("you caught the ball");
    }

}

Note: it's properly indented in the program, here I was just having trouble passing it to text

user207421
  • 305,947
  • 44
  • 307
  • 483
  • `p.getlength` should be `p.getlength()` in `System.out.println("the ball weighs" +p.getlength);` – Atri Feb 03 '16 at 23:13
  • 'When I import classes from the same package it gives me an error'. You don't have to import classes from the same package. Netbeans has really nothing to do with this problem. – user207421 Feb 03 '16 at 23:37

2 Answers2

0

You're missing the parentheses after p.getlength in Main.

System.out.println("the ball weighs" +p.getlength());
No Name
  • 131
  • 1
  • 10
0

The getlength is a function so use this way: "the ball weighs" +p.getlength() and please use visibility syntax and be object oriented: private, protected etc.

public class Ball {

   private float length;
   private float weight;
   public Ball()
   {
     this.length = 100;
     this.weight = 250;
   }
   public Ball(float length, float peso)
   {
      this.length = length;
      this.weight = weight;
   }
   public float getLength()
   {
      return length;
   }
   public float getWeight()
   {
     return weight;
   }
   public void kickBall()
   {
     System.out.println("you kicked the ball");
   }
   public void atraparPelota()
   {
     System.out.println("you caught the ball");
   }
ktom
  • 228
  • 1
  • 3
  • 11