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 3 of 3

Thread: Simple OOP question- Creating objects

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Simple OOP question- Creating objects

    I am very new to programming in Java. Can anyone take this code and add comments ( // ) so I can better understand what each line does?



     
    class TestNumber                                            // creates Class  named TestNumber
    {
       public static void main(String[] args)                // This is the main method
       {
          Number n1 = new Number(10);                   // To my understanding, this will create an object n1  and assign 10 to that object?
          Number n2 = new Number(20);
          Number n3 = n1.add(n2);                            
          System.out.println("n3 = " + n3.toString());
       }
    }
    //================================================
    class Number
    {
       private int x;
       //----------------------------------
       public Number(int xx)
       {
          x = xx;
       }
       //----------------------------------
       public Number add(Number r)
       {
          return new Number(x + r.x);
       }
       //----------------------------------
       public String toString()
       {
          return "" + x;      // return value in x as String
       }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Simple OOP question- Creating objects

     
    	/*
    	 * [url=http://docs.oracle.com/javase/tutorial/java/concepts/class.html]What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)[/url]
    	 */
    	class Number {
    		private int x;
     
    		/*
    		 * [url=http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html]Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[/url] 
    		 */
    		public Number(int xx) {
    			x = xx;
    		}
     
    		/*
    		 * [url=http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html]Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[/url]
    		 */
    		public Number add(Number r) {
    			return new Number(x + r.x);
    		}
     
    		/*
    		 * [url=http://javaexamplebyme.blogspot.co.uk/2012/07/how-to-override-tostring-method-in-java.html]javaexample: How to override toString() Method in java[/url]
    		 * @see java.lang.Object#toString()
    		 */
    		public String toString() {
    			return "" + x; // return value in x as String
    		}
    	}

    Always happy to help
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple OOP question- Creating objects

    Quote Originally Posted by TSSF44 View Post
    I am very new to programming in Java. Can anyone take this code and add comments ( // ) so I can better understand what each line does?



     
    class TestNumber                                            // creates Class  named TestNumber
    {
       public static void main(String[] args)                // This is the main method
       {
          Number n1 = new Number(10);                   // To my understanding, this will create an object n1  and assign 10 to that object? (yes, type of the object is Number)
          Number n2 = new Number(20);                   // Samething
          Number n3 = n1.add(n2);                          // This will create object and assign return value from add method.  
          System.out.println("n3 = " + n3.toString());  // this line will print n3 to console
       }
    }
    //================================================
    class Number // Class definition
    {
       private int x; // Class variable called x
       //----------------------------------
       public Number(int xx) // Constructor
       {
          x = xx; // While creating number objects, you need to send integer values as you do above, so this line will assign passed number to class variable x, so you can use x in other methods.
       }
       //----------------------------------
       public Number add(Number r) // Defining method add
       {
          return new Number(x + r.x); // Creates a new Number 
       }
       //----------------------------------
       public String toString() // This is class method overriding, you describe how toString method of Number class will display the number here.
       {
          return "" + x;      // return value in x as String
       }
    }
    Here you go

Similar Threads

  1. Creating Objects at specified indexes
    By dougie1809 in forum Object Oriented Programming
    Replies: 4
    Last Post: March 1st, 2013, 08:20 PM
  2. where to put list of objects method OOP
    By stanlj in forum Object Oriented Programming
    Replies: 3
    Last Post: January 14th, 2013, 02:08 PM
  3. Creating Objects: A question about the Java Tutorials example program
    By chaucer345 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2012, 05:47 PM
  4. Simple question regarding objects.
    By alex067 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 11th, 2012, 01:18 AM
  5. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM