Results 1 to 7 of 7
  1. #1
    lyes_djaml is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default problem with method

    Hello

    I am a beginne rin Java and OOP

    please help and tell me why this pgmm dn't return the Result Resting this

    conception :

    Class Main :

    package Calc;


    import java.io.OutputStreamWriter;

    public class Main {



    /**
    * @param args
    */



    public static void main(String[] args) {
    // TODO Auto-generated method stub
    {

    Rectangl R = new Rectangl() ;

    R.SetLength(2);
    R.Setwidth(2);
    int S = R.Surface ;
    System.out.println(S);
    }



    }
    }





    Class Rectangl :

    package Calc;

    import java.math.*;

    public class Rectangl {

    public int Width;
    public int Length;
    public int Surface;



    public int Surface (int Setlength , int Setwidth ,int Surf )
    {
    return ((Setwidth) * (SetLength)) ;
    }

    public void Setwidth(int wid)
    {

    wid = Width ;
    }

    public void Setlength(int len)
    {
    len = Length ;
    }
    }








    Thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Please use code tags (see the link in Erangas post).

    Also try and use the naming standards.
    Class names start with a capital.
    Methods and variables/attributes start with a lower case.

    And this code doesn't even compile does it?

  4. #4
    lyes_djaml is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    The rsult must be normaly 4 (2 * 2 )

    but he give me 0 as Result ?

    why ?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by lyes_djaml View Post
    The rsult must be normaly 4 (2 * 2 )

    but he give me 0 as Result ?

    why ?
    Please show us the code that produced that result; the code you've shown us doesn't even compile so we can't check it. Also put your code between code tags for readability.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by lyes_djaml View Post
    The rsult must be normaly 4 (2 * 2 )

    but he give me 0 as Result ?

    why ?

    watch out: Java is case-sensitive, if a method is called Setlength you can't call this method with SetLength. the other big bug is in the class Rectangle:

    Java Code:
    	public void Setwidth(int wid) {
    
    		wid = Width;
    	}
    
    	public void SetLength(int len) {
    		len = Length;
    	}


    you should assign wid to Width and Length to len and not the contrary! and the very last bug is the assignment int S = R.Surface ; change this to int S = R.Surface(R.Length, R.Width); i hope after this corrections you get your result 4. good luck!

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    the code is a bit weird to read without code tags, and you have some typos that need to be corrected but it could be this:
    Java Code:
    public int Surface (int Setlength , int Setwidth ,int Surf )
    {
    return ((Setwidth) * (SetLength)) ;
    }
    Surface expects 3 arguments, setLength, setWidth, Surf. when you make the call to surface you supply it with no arguments.
    Java Code:
    int S = R.Surface ;
    that seems incorrect, try removing the parameter list from Surface so it looks like:
    Java Code:
    public int Surface()
    {
      return ((width) * (length));
    }

Similar Threads

  1. Calling method problem
    By jmoutia in forum New To Java
    Replies: 7
    Last Post: 10-24-2010, 09:06 AM
  2. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  3. Problem with int method ..!!
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 19
    Last Post: 01-17-2010, 09:35 PM
  4. Method problem
    By Jeff6461 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 06:48 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 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
  •