Results 1 to 17 of 17

Thread: code help

  1. #1
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Unhappy code help

    I'm back again (just like i said i would!) haha. anyways, this program is something i had so complete, and based a lot of the code off another example, which works perfectly, however, i'm getting a "recursive constructor invocation" error, that i don't understand, and 6 other errors which all stem from the same basic concept that i don't understand. "cannot find symbol" when calling on Rectangle1.java from the Rectangle1Test.java i'm in way over my head here, so help is greatly appreciated.OH, my instructions as to what i'm supposed to accomplish are in the comments at the beginning of the program. this chapter i "learned" the concepts of "this" and overloaded constructors, so im supposed to utilize those.....(i think)

    2 programs

    Java Code:
    // ex8.4 Rectangle1.java
    // Create a class Rectangle with attributes "length", "width", each of which
    //defaults to 1. Provide methods that calculate the rectangle's perimeter and area
    //It has SET and GET methods for both length and width. The SET methods should verify
    //that the length and width are each floating-point numbers larger than 0.0 and less 20.0
    
    public class Rectangle1
    {
    	private float length; // initialize length
    	private float width; // initialize width
    	
    	//Rectangle1 no-argument constructor, initializes each variable to one
    	public Rectangle1 ()
    	{
    		this ( 1,1 ); // invoke Rectangle1 constructor with 2 arguments
    	} //end Rectangle1 no-argument constructor
    	
    	//Rectangle1 constructor: length supplied, width defaulted to 0
    	public Rectangle1 ( float l )
    	{
    		this ( l, 1 ); // invoke rectangle1 constructor
    	} // end Rectangle1 one-argument constructor
    	
    	// Rectangle1 constructor: length and width supplied
    	public Rectangle1 ( float l, float w )
    	{
    		this ( l, w ); // invoke rectangle1  constructor 
    	} //end Rectangle1 two-argument constructor
    	
    	//SET METHODS
    	public void setRectangle1 ( float l, float w )
    	{
    		setLength ( l ); // set the length
    		setWidth ( w ); // set the width
    	}// end method setRectangle1
    	
    	// validate and setlength
    	public void setLength ( float l )
    	{
    		length = ( ( l >= 0 && l < 20 ) ? l : 0 );
    	}// end method setlength
    	
    	// validate and setWidth
    	public void setWidth ( float w )
    	{
    		width = ( ( w >= 0 && w < 20 ) ? w : 0 );
    	}//end method setwidth
    	
    	// GET METHODS
    	
    	//get the length
    	public float getLength ()
    	{
    		return length;
    	}// end method getLength
    	
    	//get the width
    	public float getWidth ()
    	{
    		return width;
    	}// end method getWidth
    	
    	// calculate area
    	public float calculateArea ()
    	{
    		float area;
    	
    		area = ( getLength () * getWidth () );
    		
    	} // end calculate area
    	
    	public float calculatePerimeter ()
    	{
    		float perimeter;
    		
    		perimeter = ( (getLength() * 2) + (getWidth() * 2) );
    	} // end calculater perimeter
    	 
    } // end class Rectangle1.java
    program 2
    Java Code:
    //// ex8.4 Rectangle1Test.java
    // Create a class Rectangle with attributes "length", "width", each of which
    //defaults to 1. Provide methods that calculate the rectangle's perimeter and area
    //It has SET and GET methods for both length and width. The SET methods should verify
    //that the length and width are each floating-point numbers larger than 0.0 and less 20.0
    
    public class Rectangle1Test
    {
    	public static void main ( String [] args )
    	{
    		Rectangle1 r1 = new Rectangle1 (); // default "no argument"
    		Rectangle1 r2 = new Rectangle1 ( 8 ); // one argument defined
    		Rectangle1 r3 = new Rectangle1 ( 6, 12 ); // both arguments defined
    		
    		System.out.println ( "Constructed with:" );
    		System.out.println ( "r1: all arguments defaulted" );
    		System.out.println ( "Area is:  %d\n", r1.calculateArea() );
    		System.out.println ( "Perimeter is: %d\n\n", r1.calculatePerimter() );
    		
    		System.out.println ( "Constructed with:" );
    		System.out.println ( "r2: one argument defined" );
    		System.out.println ( "Area is:  %d\n", r2.calculateArea() );
    		System.out.println ( "Perimeter is: %d\n\n", r2.calculatePerimter() );
    		
    		System.out.println ( "Constructed with:" );
    		System.out.println ( "r3: both arguments defined" );
    		System.out.println ( "Area is:  %d\n", r3.calculateArea() );
    		System.out.println ( "Perimeter is: %d\n\n", r3.calculatePerimter() );
    		
    	}//end main
    }// end class
    here are my errors:

    Java Code:
    c:\SimplyJava\Rectangle1>javac *.java
    Rectangle1.java:25: recursive constructor invocation
            public Rectangle1 ( float l, float w )
                   ^
    Rectangle1Test.java:17: cannot find symbol
    symbol  : method println(java.lang.String,float)
    location: class java.io.PrintStream
                    System.out.println ( "Area is:  %d\n", r1.calculateArea() );
                              ^
    Rectangle1Test.java:18: cannot find symbol
    symbol  : method calculatePerimter()
    location: class Rectangle1
                    System.out.println ( "Perimeter is: %d\n\n", r1.calculatePerimte
    r() );
                                                                   ^
    Rectangle1Test.java:22: cannot find symbol
    symbol  : method println(java.lang.String,float)
    location: class java.io.PrintStream
                    System.out.println ( "Area is:  %d\n", r2.calculateArea() );
                              ^
    Rectangle1Test.java:23: cannot find symbol
    symbol  : method calculatePerimter()
    location: class Rectangle1
                    System.out.println ( "Perimeter is: %d\n\n", r2.calculatePerimte
    r() );
                                                                   ^
    Rectangle1Test.java:27: cannot find symbol
    symbol  : method println(java.lang.String,float)
    location: class java.io.PrintStream
                    System.out.println ( "Area is:  %d\n", r3.calculateArea() );
                              ^
    Rectangle1Test.java:28: cannot find symbol
    symbol  : method calculatePerimter()
    location: class Rectangle1
                    System.out.println ( "Perimeter is: %d\n\n", r3.calculatePerimte
    r() );
                                                                   ^
    7 errors
    most of what is in this code, i just learned TODAY, so please be kind/detailed if you decide to offer help! thanks in advance

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    The first error occurs because

    Java Code:
    	public Rectangle1 ( float l, float w )
    	{
    		this ( l, w ); // invoke rectangle1  constructor 
    	} //end Rectangle1 two-argument constructor
    is a little strange. What does this(l, w) do do you think?

    The errors about println occur because println takes only one argument. You provide two. Have a look at Printstream (the type of System.out) to find the correct method.

    The other errors occur because of a typo.

    Success.
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by venerik View Post
    The first error occurs because

    Java Code:
    	public Rectangle1 ( float l, float w )
    	{
    		this ( l, w ); // invoke rectangle1  constructor 
    	} //end Rectangle1 two-argument constructor
    is a little strange. What does this(l, w) do do you think?



    The errors about println occur because println takes only one argument. You provide two. Have a look at Printstream (the type of System.out) to find the correct method.

    The other errors occur because of a typo.

    Success.
    honestly, like i said, im not entirely familiar with the new "this" keyword, and im ASSUMING that this sets the l (length) and w (width) to float numbers, as opposed to using the initial value? not sure though.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    No, you would set those variables with

    Java Code:
    length = l;
    width = w;

    If you think about it, how is Java to know that l goes with length and w with width unless you tell it.

    Perhaps you should consult your textbooks etc in an attempt to answer Erik's question. Or read Using the this Keyword in Oracle's Tutorial. Because you ought to wonder why this(l,w) failed while the other this() calls did not.
    Last edited by pbrockway2; 11-14-2010 at 12:16 AM.

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Did you write and document the code yourself... :(

    It says

    Java Code:
    // invoke rectangle1  constructor
    Which constructor is being invoked do you think?
    I'm new to Java but I like to help where ever I can. :)

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    The this keyword references the current object.
    Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Here's a couple of examples to help explain.
    Java Code:
    public class MySize {
         int width;
         int height;
    
         public MySize(int width, int height) {
              this.width = width;
              this.height = height;
         }
    
    }
    Inside the constructor, the this.width and this.width statements refer to instance variables. This is needed as just writing width would instead refer to the width variable passed into the constructor.

    The this keyword can also be used to call the different constructors of the class.
    Java Code:
    public class MySize {
         
         int width;
    
         int height;
    
         public MySize() {
              super();
         }
    
         public MySize(int height) {
              this();
              this.height = height;
         }
    
         public MySize(int height, int width) {
              this(height);
              this.width = width;
         }
    }
    In the above example, the this keyword is being used to call the other constructors in the class. The MySize(int) constructor contains the call "this()" which calls the MySize() constructor, but since this isn't really doing anything its not noticeable. The MySize(int, int) constructor contains the call "this(height)" which calls the MySize(int) constructor which sets the height of the class.

    I've used the above to consolidate the logic/code when a class has a lot of different constructors.

  7. #7
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    i figured out the mistakes about the print output based on what you said, oversight on my part with using println instead of printf, and misspelling perimeter (3x).

    as i said, this is all still over my head, and im trying to comprehend it by reading a book, and its an online course, so help from a instructor is days away....trying to learn it on my own.

    i ASSUME that when i set up a this with

    ( 1, 1) it sets the sides to the default of 1 and 1?
    ( L, 1) capital l used for clarity at the moment uses...Length as a floating varible and 1?
    ( L, W) i though would use both use floating variables. i guess i don't really understand the concept.

  8. #8
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    instead of using "this" should that line read?:
    Java Code:
    setRectangle1 ( l,w );

  9. #9
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    sorry for the now (triple post) but im just updating on progress, and seeking addntl help...

    i updated the code in question to:
    Java Code:
    // Rectangle1 constructor: length and width supplied
    	public Rectangle1 ( float l, float w )
    	{
    		setLength (l);
    		setWidth (w);
    	} //end Rectangle1 two-argument constructor
    and that seemed to eliminate the error, but now i have a new pair of errors, which i was expecting, but need help with....

    c:\SimplyJava\Rectangle1>javac *.java
    Rectangle1.java:71: missing return statement
    } // end calculate area
    ^
    Rectangle1.java:79: missing return statement
    } // end calculater perimeter
    ^
    2 errors

    i am reviewing my book, but not sure at all how to write the return statement. any help would be greatly appreciated, sorry for a million questions, but im only 2 weeks into this Java class, so im a newbie.

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    post the methods at line 71 and 79, im guessing they are calculate Perimeter and calculate Area methods since they do not return floats

  11. #11
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    this is what i have so far, but its way off base. i know i need a "return" value somehow.

    Java Code:
    // calculate area
    	public float calculateArea ()
    	{
    		float area;
    	
    		area = ( getLength () * getWidth () );
    		
    	} // end calculate area
    	
    	public float calculatePerimeter ()
    	{
    		float perimeter;
    		
    		perimeter = ( (getLength() * 2) + (getWidth() * 2) );

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    yup calculateArea and calculatePerimeter methods are expecting a return type of float....you have float variables inside those methods namely area and perimeter, return them.

    the reason you need a return type is in your method declaration

    [code]
    public float calculateArea(){}
    [/code[

    the float keyword in the method declaration means you want this method to return a type of float...

  13. #13
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    how do i appropriately return them? the only example i have in the book is the following, with no explanations:

    Java Code:
    public String toString()
    {
       return String.format ( "%d%d%d", month, day, year );
    }
    but that example doesn't need a calculation, like i need to calculate area from the length and width.

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    the example you gave
    Java Code:
    public String toString()
    {}
    this method expects a String as a return type, again look at the type after the access modifier (public) in this case.

    Not sure what you mean by needing calculations since it looks like you have already, see your methods...

    Java Code:
    // calculate area
    	public float calculateArea ()
    	{
    		float area;
    	
    		area = ( getLength () * getWidth () ); // calculation
    		
    	} // end calculate area
    	
    	public float calculatePerimeter ()
    	{
    		float perimeter;
    		
    		perimeter = ( (getLength() * 2) + (getWidth() * 2) ); //calculation
    literally you need to return the float variables area and perimeter. Please, please understand the reason for this however, and the clue is in your method declarations after the access modifier(public) in your case, comes the return type (float) in your case, this means this method must return a type of float. If your variables area and perimeter are floats, what are you missing from these methods? see you String method for an example of returning a type

  15. #15
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    i understand the reason WHY i needed to return a value. tried all these complicated ways to return a value, when all i had to do was add

    return area;

    to the end of my statement.....wow, i should have realized i was tired last night, it clicked first thing this morning. thank you.


    now in continuing on, it completes the compiling, but during runtime i get an exception error....i will troubleshoot that awhile, and post details up if i can't get through it.

  16. #16
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Java Code:
    //// ex8.4 Rectangle1Test.java
    // Create a class Rectangle with attributes "length", "width", each of which
    //defaults to 1. Provide methods that calculate the rectangle's perimeter and area
    //It has SET and GET methods for both length and width. The SET methods should verify
    //that the length and width are each floating-point numbers larger than 0.0 and less 20.0
    
    public class Rectangle1Test
    {
    	public static void main ( String [] args )
    	{
    		Rectangle1 r1 = new Rectangle1 (); // default "no argument"
    		Rectangle1 r2 = new Rectangle1 ( 8 ); // one argument defined
    		Rectangle1 r3 = new Rectangle1 ( 6, 12 ); // both arguments defined
    		
    		System.out.println ( "Constructed with:" );
    		System.out.println ( "r1: all arguments defaulted" );
    		System.out.println ( "Area is:  %d\n", r1.calculateArea() );
    		System.out.println ( "Perimeter is: %d\n\n", r1.calculatePerimter() );
    		
    		System.out.println ( "Constructed with:" );
    		System.out.println ( "r2: one argument defined" );
    		System.out.println ( "Area is:  %d\n", r2.calculateArea() );
    		System.out.println ( "Perimeter is: %d\n\n", r2.calculatePerimter() );
    		
    		System.out.println ( "Constructed with:" );
    		System.out.println ( "r3: both arguments defined" );
    		System.out.println ( "Area is:  %d\n", r3.calculateArea() );
    		System.out.println ( "Perimeter is: %d\n\n", r3.calculatePerimter() );
    		
    	}//end main
    }// end class
    got it working! had to change the %d to %s, which raises a question. i thought %s was for inserting a string into a printf? not inserting a float? is there an online list that shows what each one does, so i can keep it for reference, until i memorize it better? im always forgetting. just something basic like

    %s = string
    %f = float
    %d = double
    %.2f =

    etc etc i know the above ones arent correct, just example.

  17. #17
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Similar Threads

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •