Need help with classes and constructors!!
Hello everyone, I am new to java and method calls from outside a class is still a little confusing to me, What I am trying to do is take a square object defined in one class and calculate the area and perimeter of that square object inside a different class( probably an easy task for someone with a decent grasp on this topic). I will attach my code that I have so far to make the area so that you can see where I am heading. I'm looking for feedback as to why I can't call the area function from the printSquareMeasurements method.
Code :
class Square extends SquareCalc {
private double side;
public double area;
public Square (Square sq){
side = s;
}
public double Area (Square sq){
area = s * s;
return area;
}
}
public class SquareCalc {
static void printSquareMeasures(Square sq){
System.out.println("area = " + sq.Area());
}
public static void main(String[] args) {
Square sq = new Square(4);
printSquareMeasures(sq); // call to function that displays results
}
}
Re: Need help with classes and constructors!!
What you want to do is define a getArea() method in your Square class. It doesn't take a Square parameter, it IS the Square.
Then from your SquareCalc class, you simply create an instance of Square, then call its getArea() method. You shouldn't be using calls to the Square.area variable directly (in fact I don't think you should have an area variable in your Square class at all), but just call that method instead.
Re: Need help with classes and constructors!!
Quote:
Originally Posted by
KevinWorkman
What you want to do is define a getArea() method in your Square class. It doesn't take a Square parameter, it IS the Square.
Then from your SquareCalc class, you simply create an instance of Square, then call its getArea() method. You shouldn't be using calls to the Square.area variable directly (in fact I don't think you should have an area variable in your Square class at all), but just call that method instead.
Thanks, what you are sayin seems right but I just cant grasp it. I made
Code :
public double getArea(double a){
area = a * a;
return area;
}
inside class Square, but how can I run that from printSquareMeasures? I tried
Code :
System.out.println("This is drivin me insane"+area);
but no luck
Re: Need help with classes and constructors!!
You have to call the method, which means you have to have an instance of Square.
Hint: System.out is a variable. When you do System.out.println(), you are really calling the println() method of the System.out variable.
Re: Need help with classes and constructors!!
Quote:
Originally Posted by
KevinWorkman
You have to call the method, which means you have to have an instance of Square.
Hint: System.out is a variable. When you do System.out.println(), you are really calling the println() method of the System.out variable.
But I already have an instance of Square which is sq right? So it should work if I do sq.getArea(); All I need to do is display the calculation made in getArea...
Re: Need help with classes and constructors!!
Quote:
Originally Posted by
rayp4993
But I already have an instance of Square which is sq right? So it should work if I do sq.getArea(); All I need to do is display the calculation made in getArea...
To quote a professor of mine in college, "Now you're cookin' with gas."
Re: Need help with classes and constructors!!
I ended up revising my code and decided to go with
Code :
class Square {
public double side;
public double area;
public double perim;
public Square (double s){
side = s;
}
public double getPerim(double p){
perim = p * 4;
return perim;
}
public double getArea(double a){
area = a * a;
return area;
}
}
public class SquareCalc {
static void printSquareMeasures(Square sq){
System.out.println("Area = "+sq.getArea(4));
System.out.println("Perimeter = "+sq.getPerim(4));
}
public static void main(String[] args) {
Square sq = new Square(4);
printSquareMeasures(sq); // call to function that displays results
}
}
Re: Need help with classes and constructors!!
Quote:
Originally Posted by
KevinWorkman
To quote a professor of mine in college, "Now you're cookin' with gas."
Thank you for all your help tonight I really appreciate it!
Re: Need help with classes and constructors!!
Just a few words of advice:
The only time you should pass the side length into the Square class is to the constructor. Then you should save that number in the Square class (which you do) for use in other functions. You shouldn't have to pass that value into the other methods, since the Square instance will already know its side length.
Also, there is no need to have area or perimeter be class variables, since you recalculate them each time the method is called.