Thread: A little something I discovered while experimenting

Results 1 to 5 of 5
  1. #1 A little something I discovered while experimenting 
    Registered Member CrypticDev's Avatar
    Join Date
    Oct 2015
    Posts
    7
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    So in my earlier class today I learned about Scanners and user input, on my current class I was learning about object oriented classes. So I did a experiment to both together and this is what happened.

    Code:
    package xyz.crypticdev.section2;
    
    import java.util.Scanner;
    
    /**
     * 
     * @author crypticdev
     *
     */
    public class ObjectOrientedClass {
    	
    	public int a = 0;
    	public int i = 0;
    	
    	Scanner input = new Scanner(System.in);
    	
    	public ObjectOrientedClass() {
    		System.out.println("Hello");
    	}
    	// Constructors with parameters
    	public ObjectOrientedClass(int b, int c) {
    		this.a = b;
    		this.i = c;
    		System.out.println("a = " + a + " i = " + i);
    	}
    	
    	public void printSomething() {
    		System.out.println("Hello");
    	}
    	
    	public void printWord(String word) {
    		System.out.println(word);
    	}
    	
    	public int returnSomething() {
    		return 2;
    	}
    	
    	public int calculate(int a, int b){
    		a = input.nextInt();
    		b = input.nextInt();
    		return a * b;
    	}
    	
    	public static void main(String[] args) {
    		
    		ObjectOrientedClass ourClass = new ObjectOrientedClass(10, 50);
    		ourClass.printSomething();
    		ourClass.printWord("World");
    		System.out.println(ourClass.returnSomething());
    		System.out.println("Enter two numbers to multiply them.");
    		System.out.println(ourClass.calculate(ourClass.a, ourClass.i));
    	}
    }
    and Here is my console output.

    Code:
    a = 10 i = 50
    Hello
    World
    2
    Enter two numbers to multiply them.
    5
    8
    40
    Reply With Quote  
     

  2. #2  
    Donator


    Join Date
    Jun 2011
    Posts
    2,549
    Thanks given
    539
    Thanks received
    402
    Rep Power
    683
    Neat !
    All the best,
    Nirvana

    Reply With Quote  
     

  3. #3  
    Registered Member

    Join Date
    May 2012
    Posts
    474
    Thanks given
    93
    Thanks received
    74
    Rep Power
    634
    You should try declaring a variable within your returnSomething method and returning that variable.

    Something like this:
    Code:
    public int returnSomething() {
        int returningInt = 2;
    
        return returningInt;
    }
    Edit: Nevermind, I misread your code vs. output.

    What exactly am I supposed to see? This all seems normal to me.
    Reply With Quote  
     

  4. #4  
    Donator

    Join Date
    Feb 2013
    Age
    24
    Posts
    137
    Thanks given
    41
    Thanks received
    21
    Rep Power
    20
    good job, looks pretty cool
    Attached image
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Jan 2016
    Posts
    4
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    wow gj man
    next, you should look at using multiple different object classes and using them to interact with each other.

    For example, why don't you make a Calculator class (that processes all calculation), and also a Handler class that handles user input and also parses the user input to the calculator object?
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 3
    Last Post: 02-06-2015, 09:45 PM
  2. little something i been working on
    By Sean in forum RS 503+ Client & Server
    Replies: 8
    Last Post: 09-14-2008, 08:40 PM
  3. A little something.
    By Stanyer in forum Tutorials
    Replies: 5
    Last Post: 09-06-2008, 02:26 PM
  4. In need of a certain little something
    By siroober in forum RS2 Client
    Replies: 5
    Last Post: 06-30-2008, 04:42 PM
  5. Replies: 12
    Last Post: 03-17-2008, 11:49 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
  •