Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    2
    ReputationRep:
    Hi guys, in an attempt to understand classes/methods/objects I've written a small application with a simple example. Could someone give feedback? I've added comments to exploani what I (think!) I'm doing :P The big line of #'s is to indicate that the next bit of writing would be in a seperate Java file.


    package MethodsExample {

    //Make a public class caledMethods
    public class Methods {

    //Create an integer method called addMethod with arguments int x and int y
    int addMethod(int x, int y) {
    //The code for the method adds x and y and then returns this value
    int add = x + y ;
    return add;
    }

    }

    }

    ################################ ################################ #########
    package MethodExample {

    public static void main(String[] args) {

    //Make a new Methods object called number
    Methods number = new Methods();
    /*creates an int number_1 which uses the addMethod with values 3 and 4 on number */
    int number_1 = number.addMethod(3,4);

    System.out.print("number_1 is " + number_1 );

    }
    }


    Thanks for any feedback/advice!
    Offline

    0
    ReputationRep:
    Well the best way would be to run this using either the command line or an IDE and see what it outputs, it will also give you an indication of any errors.
    At first glance, a package name should end with ; it does not need to enclose everything in brackets {}
    Start your method with the word public. If you want to return an int, string, array etc then you would say public int add() like you have, but not all methods need to return something so instead you would use public void.

    So make the changes and run it to see, reply back with any errors.
    • Thread Starter
    Offline

    2
    ReputationRep:
    (Original post by wales321)
    Well the best way would be to run this using either the command line or an IDE and see what it outputs, it will also give you an indication of any errors.
    At first glance, a package name should end with ; it does not need to enclose everything in brackets {}
    Start your method with the word public. If you want to return an int, string, array etc then you would say public int add() like you have, but not all methods need to return something so instead you would use public void.

    So make the changes and run it to see, reply back with any errors.
    I'm at work so no NetBeans IDE...how do you run stuff through the commanf line? [If it makes a difference I'm just writing stuff on notepad]

    Thanks
    Offline

    0
    ReputationRep:
    (Original post by Chemhistorian)
    I'm at work so no NetBeans IDE...how do you run stuff through the commanf line? [If it makes a difference I'm just writing stuff on notepad]

    Thanks
    To compile:
    javac myFile.java

    To run:
    java myFile.java
    Offline

    2
    ReputationRep:
    (Original post by Chemhistorian)
    I'm at work so no NetBeans IDE...how do you run stuff through the commanf line? [If it makes a difference I'm just writing stuff on notepad]

    Thanks
    You need to open command prompt/terminal and then navigate to the location of the file (google this if you cant do it). Then:
    javac filename.java
    java filename

    Been a whole long time since i have done that, IIRC above is needed.

    On the whole code front:
    - Be careful how you define the package, it is not enclosed with {}, it just has package Name;
    - All files need to be enclosed in a class. Your MethodExample was not.
    - look up the difference between Private, Protected, and Public. For me, this should use a protected method to call the add.

    Below is a quick fix of your code (hardly needed any adjustments):

    Code:
    package MethodExample;
    
    //Make a public class call edMethods
    public class Methods2
    {
    	//Create an integer method called addMethod with arguments int x and int y	
    	protected int addMethod(int x, int y) 
    	{
    		//The code for the method adds x and y and then returns this value
    		return x + y ;
    	}
    }
    Code:
    package MethodExample; //The package name does not need curly brackets, its an assignment.
    
    public class Methods
    {
    public static void main(String[] args) {
    
    	//Make a new Methods object called number
    	Methods2 number = new Methods2();
    	/*creates an int number_1 which uses the addMethod with values 3 and 4 on number */
    	int number_1 = number.addMethod(3,4);
    
    	System.out.print("number_1 is " + number_1 );
    
    	}
    }
    One other thing I would advise you to look at is the issue that many people face of having a 'Cannot make a static reference to the non-static method nonStaticCall() from the type Methods' error when they try and do things like this:

    Code:
    public class Methods
    {
    	public static void main(String[] args) 
    	{
    		nonStaticCall();
    	}
    	
    	public void nonStaticCall()
    	{
    		System.out.println("Hellowww!");
    	}
    }
    This problem can be fixed by creating a new instance of the initial class, then calling the non static method from there, see below:

    Code:
    public class Methods
    {
    	public static void main(String[] args) 
    	{
    		new Methods();
    	}
    	
    	public Methods()
    	{
    		nonStaticCall();
    	}
    	
    	public void nonStaticCall()
    	{
    		System.out.println("Hellowww!");
    	}
    }
    Offline

    2
    ReputationRep:
    Like others have said, you don't use parentheses with namespace (package) declarations.

    Other than that the code is very basic and seems fine, though you can shed down some parts of your code. E.g. in your first method, there is no need to declare another variable to store the sum of the numbers in. You can just do return x+y
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    What newspaper do you read/prefer?
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.