Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Few questions about classes/objs/methods

  1. #1
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Few questions about classes/objs/methods

    Hello,

    i am totally new to this forum, and i have some questions regarding methods, classes and objects, im really having a hard time understanding how they work.

    To begin with a few of my questions, here's the source code for a simple dice program:


    PHP Code:
    package test;
    
    public class Die {
    
    private final int MAX = 6;
    	
    	private int faceValue;
    	
    	
    	public Die()     //<--- Constructor: Why is the parameter empty ? when do we type stuff in it ? when do we write a constructor ?
    	{
    		faceValue = 1;    
    	}
    			
    	public int roll()
    	{
    		faceValue = (int) (Math.random() * MAX) + 1;
    	}
    	
    	public void setFaceValue(int value)   //<--- Is there any case where the setter is NOT a void method ?
    	{	
    		faceValue = value;
    	}
    	
    	public int getFaceValue()  //<--- Why is the accessor empty ? do we always put an empty parameter for the accessor ?
    	{
    		return faceValue;
    	}
    	
    }
    			
    			
    
    //This is another class, contains the main method
    
    package test;
    
    /* This program demonstrates the use of a user-defined  *
     * class		                                **/
    
    
    public class RollingDice {
    	
    	// Creates two Die objects and rolls them display the values.
    	
    
    	public static void main (String[] args) {
    		
    		Die die1 = new Die();
    		Die die2 = new Die();
    		int sum;
    		
    		
    		die1.roll();
    		die2.roll();
    		
    		System.out.println("Die1: " + die1 + ", Die2: " + die2);
    		
    		die1.roll();
    		die2.setFaceValue(4);
    		System.out.println("Die1: " + die1 + ", Die2: " + die2);
    		
    		sum = die1.getFaceValue() + die2.getFaceValue();
    		
    		System.out.println("Sum: " + sum);
    		
    		sum = die1.roll() + die2.roll();
    		System.out.println("Die1: " + die1 + ", Die2: " + die2);
    		System.out.println("New sum: " + sum);
    		
    		
    		
    		
    		
    		
    		
    	// Let the user choose his own die value (cheating) and display the value	
    
    	
    	
    	
    	
    		
    	}
    }
    
    			
    //OUTPUT: 
    
    /*
    Die1: test.Die@55f33675, Die2: test.Die@527c6768
    Die1: test.Die@55f33675, Die2: test.Die@527c6768
    Sum: 8
    Die1: test.Die@55f33675, Die2: test.Die@527c6768
    New sum: 6
    */


    Could you please provide some similar exercise for me to practice on ? or maybe provide me with a link that has similar programs that are NOT very complicated, i would be glad if anyone knows any exercises as such.

    Thanks in advance,
    Regards,
    Last edited by toiletpaper; 05-01-2014 at 12:31 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Few questions about classes/objs/methods

    You can't learn this stuff by staring at code; do yourself a huge favor and get a good book. You need structured and well-formed knowledge and understanding, not code and the small limited hints and tiny bits of information that a forum can provide.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Few questions about classes/objs/methods

    Quote Originally Posted by gimbal2 View Post
    You can't learn this stuff by staring at code; do yourself a huge favor and get a good book. You need structured and well-formed knowledge and understanding, not code and the small limited hints and tiny bits of information that a forum can provide.
    i've got the java software solutions, but i barely read it, i usually read my java notes. Thanks for the advice though, i'll read more from the book from now on, since we barely pass up on details in class, we have a huge assignment to complete and we haven't practiced anything in class on them yet, lol, hilarious.
    Last edited by toiletpaper; 05-01-2014 at 12:50 PM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Few questions about classes/objs/methods

    Quote Originally Posted by toiletpaper View Post
    i've got the java software solutions, but i read it as much i do read my java notes
    You do realize that programming is a constant battle of keeping your knowledge base up to date right? Reading and research is going to be a large part of your life.
    milovan likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Few questions about classes/objs/methods

    The parameter of the constructor is empty because you don't need a parameter. You only need to put a parameter in there if you need to have a certain parameter in the code that constructs your class (like a size if you were creating a graphical object).
    the setter will usually be void because void just means that it doesn't pass anything back. if you are setting a member variable, you obviously don't need to pass anything back (notmally).
    The accessor is empty because you don't need to pass anything in (just like with the constructor).

    Your main problem is you don't understand how functions work. Basically when you write a function you have to think about what you want to get back from the function, and what you want to bring into that function so it can do it's thing. The info going into the function goes in the parentheses, the type of value that will come out of the function/method goes before the method (like where void is).

    After you have understood this you can start learning how all the other stuff works (like classes...)
    I hope that makes things clearer.

    Good luck and happy coding :)

  6. #6
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Few questions about classes/objs/methods

    Quote Originally Posted by Silvertale View Post
    The parameter of the constructor is empty because you don't need a parameter. You only need to put a parameter in there if you need to have a certain parameter in the code that constructs your class (like a size if you were creating a graphical object).
    the setter will usually be void because void just means that it doesn't pass anything back. if you are setting a member variable, you obviously don't need to pass anything back (notmally).
    The accessor is empty because you don't need to pass anything in (just like with the constructor).

    Your main problem is you don't understand how functions work. Basically when you write a function you have to think about what you want to get back from the function, and what you want to bring into that function so it can do it's thing. The info going into the function goes in the parentheses, the type of value that will come out of the function/method goes before the method (like where void is).

    After you have understood this you can start learning how all the other stuff works (like classes...)
    I hope that makes things clearer.

    Good luck and happy coding :)
    Thanks:D! i kind of understand much more how constructors work now, +1'ed! At least u provided me with some form of help, unlike other viewers who just ignored this >_>

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default Re: Few questions about classes/objs/methods

    Hi,

    I have to confess that I completely missed your questions. They was hidden away in the source code. I would suggest to ask questions in the main text and only use code as example.

    So I was wondering what Silbvwrtale was writing because I just saw a question about exercise....

    Maybe it was just me, but that would be my suggestion that would have helped me to help you.

    Konrad

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. questions on static methods.
    By laxmareddynerelly in forum New To Java
    Replies: 1
    Last Post: 01-10-2012, 11:26 AM
  5. Replies: 9
    Last Post: 12-01-2009, 08:30 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
  •