Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Need help with classes and constructors!!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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
     
     
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with classes and constructors!!

    Quote Originally Posted by KevinWorkman View Post
    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
    public double getArea(double a){
     
    	area = a * a;
    	return area;
    	}
    inside class Square, but how can I run that from printSquareMeasures? I tried
    System.out.println("This is drivin me insane"+area);
    but no luck

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with classes and constructors!!

    Quote Originally Posted by KevinWorkman View Post
    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...

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help with classes and constructors!!

    Quote Originally Posted by rayp4993 View Post
    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."
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with classes and constructors!!

    I ended up revising my code and decided to go with

     
    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
     
     
        }
    }

  8. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with classes and constructors!!

    Quote Originally Posted by KevinWorkman View Post
    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!

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  2. constructors in servlets
    By the light in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: June 29th, 2011, 10:32 AM
  3. constructors in servlets
    By the light in forum Java Servlet
    Replies: 3
    Last Post: June 27th, 2011, 04:13 AM
  4. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  5. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 04:04 PM