Programming 1
Lecture 5
Chapter 2:
Introduction to Java Programming
Dr/ Mahmoud Gamal
1
Accepting Input from the User (Scanner):
• To call Scanner:
import [Link];
• [Link] ……>> Package
• Scanner……>> Class
• To create object (Data + Methods) from class scanner:
Scanner Name = new Scanner ([Link])
[Link] …..>> refer to accepting input from keyboard
Example:
Scanner input = new Scanner ([Link])
2
Scanner methods
Method Description
nextInt() reads an int from the user
nextDouble() reads a double from the user
nextFloat() reads a float from the user
next() reads a one-word String from the user
next().charAt(0) reads one char from the user
• Each method waits until the user presses Enter.
• The value typed by the user is returned.
Example:
[Link]("How old are you? ");
int age = [Link]();
[Link]("You typed " + age);
Scanner Example
import [Link]; // so that I can use Scanner
public class UserInputExample {
public static void main(String[] args) {
Scanner console = new Scanner([Link]);
[Link]("How old are you? ");
int age = [Link]();
age 29
years 36
int years = 65 - age;
[Link](years + " years to
retirement!");
}
}
• Console (user input underlined):
How old are you? 29
36 years until retirement!
Example: A program to calculate the sum of two numbers
Output:
5
Example: A Program to calculate the area of rectangle.
Output:
6
Example: A Program to calculate the net salary
7
Java's Math class
Method name Description
[Link](value) absolute value
[Link](value) rounds up
[Link](value) rounds down
Math.log10(value) logarithm, base 10
[Link](value1, value2) larger of two values
[Link](value1, value2) smaller of two values
[Link](base, exp) base to the exp power
[Link]() random double between 0 and 1
[Link](value) nearest whole number
[Link](value) square root
[Link](value) sine/cosine/tangent of
[Link](value) an angle in radians Constant Description
[Link](value) Math.E 2.7182818...
[Link](value) convert degrees to [Link] 3.1415926...
[Link](value) radians and back
Calling Math methods
[Link](parameters)
Examples:
double squareRoot = [Link](121.0);
[Link](squareRoot); // 11.0
int absoluteValue = [Link](-50);
[Link](absoluteValue); // 50
[Link]([Link](3, 7) + 2); // 5
double exp = [Link](2, 4);
[Link](exp); // 16.0
• The Math methods do not print to the console.
• Each method produces ("returns") a numeric result.
• The results are used as expressions (printed, stored, etc.).
Example:
10
Remember: Type casting
• type cast: A conversion from one type to another.
• To promote an int into a double to get exact division from /
• To truncate a double from a real number to an integer
• Syntax:
(type) expression
Examples:
double result = (double) 19 / 5; // 3.8
int result2 = (int) result; // 3
int x = (int) [Link](10, 3); // 1000
Calling Math methods
• Parameters send information in from the caller to the method.
• Return values send information out from a method to its caller.
• A call to the method can be used as part of an expression.
-42 [Link](-42)
42
main
2.71
3
[Link](2.71)
Example:
Java program to find the Hypotenuse of a right-angled triangle
13