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