Existing classes and class libraries can be utilized as appropriate to create objects. One example of an existing class in Java is the Scanner class. The Scanner class is used to capture user input to use in program code. The Scanner class library is part of the java.util package and must be imported in order to be used.
nextInt()
returns the next integer
nextDouble()
returns the next double
nextBoolean()
returns the next boolean
next()
returns the next single string (stops at whitespace)
nextLine()
returns the next line as a string (stops at new line)
close()
void method that tells the program you are no longer using the scanner
void setNums(int, int)
provide 2 integers as parameters. The 2 numbers in Adder
are set to those values.
void setFirst(int)
provide 1 integer as a parameter. The first number in Adder
is set to that value.
void setSecond(int)
provide 1 integer as a parameter. The second number in Adder
is set to that value.
int getSum()
The sum of the 2 numbers is calculated and returned.
void displaySum()
This method has no parameters and adds the two instance variables together and displays the sum.
Purpose of the Scanner Class
The Scanner
class is used to capture user input to use in program code.