03-IntroJavaProg 2
03-IntroJavaProg 2
3. Introduction to java
programming _ 2
1
Data and Expressions: Introduction
We view computer programs as getting input, performing
computation on the input data, and outputting the results
of the computations.
For example, a metric converter program that
accepts measurements in U.S. units (input), converts the
measurements (computation), and displays their metric
equivalents (output).
Standard classes
The Math class: for mathematical formulas.
The DecimalFormat class: for formatting the numerical data.
The GregorianCalendar class: for manipulating the date.
The Random class: for generating different types of random
numbers.
Variables
Binary operator: x + 4;
All other arithmetic operators except the minus are also binary.
The minus and plus operators can be both binary and unary. A
unary operator operates on one operand, –x.
Subexpression: x + 3 * y
Implicit and Explicit Typecasting
When the data types of variables and constants in an
arithmetic expression are different data types, then a
casting conversion will take place.
A casting conversion, or typecasting, is a process
that converts a value of one data type to another data
type.
Two types of casting conversions in Java are implicit
and explicit.
An implicit conversion called numeric promotion is
applied to the operands of an arithmetic operator.
The promotion is based on the rules stated in Table
3.4.
Implicit and Explicit Typecasting
Implicit and Explicit Typecasting
Explicit conversion is applied to an operand by using a
typecast operator.
Typecasting syntax: ( <data type> ) <expression>
class Circle {
public static void main(String[] args) {
final double PI = 3.14159;
double radius, area, circumference;
radius = 2.35;
//compute the area and circumference
area = PI * radius * radius;
circumference = 2.0 * PI * radius;
System.out.println("Given Radius: " + radius);
System.out.println("Area: " + area);
System.out.println("Circumference: " + circumference);
}
}
A program that illustrates the use of both DecimalFormat and control
characters
import java.text.*;
class Circle3 {
public static void main(String[] args) {
final double PI = 3.14159;
final String TAB = "\t";
final String NEWLINE = "\n";
double radius, area, circumference;
DecimalFormat df = new DecimalFormat("0.000");
radius = 2.35;
//compute the area and circumference
area = PI * radius * radius;
circumference = 2.0 * PI * radius;
//Display the results
System.out.println(
"Given Radius: " + TAB + df.format(radius) + NEWLINE + "Area: " + TAB + df.format(area)
+ NEWLINE +
"Circumference: " + TAB + df.format(circumference));
}
}
Getting Numerical Input
To input strings, we use the next method of the Scanner
class.
For the numerical input values, we use an equivalent
method that corresponds to the data type of the value we
try to input.
For instance, to input an int value, we use the nextInt
method.
An example of inputting a person’s age:
Scanner scanner = new Scanner(System.in);
int age;
System.out.print("Enter your age: ");
age = scanner.nextInt( );
Getting Numerical Input…Cont.
Getting Numerical Input…Cont.
When we enter data using System.in, they are placed in
input buffer.
Consider the following code:
Scanner scanner = new Scanner(System.in);
int num1, num2, num3;
System.out.print("Enter Number 1: ");
num1 = scanner.nextInt( );
System.out.print("Enter Number 2: ");
num2 = scanner.nextInt( );
System.out.print("Enter Number 3: ");
num3 = scanner.nextInt( );
System.out.print("Values entered are " +
num1 + " " + num2 + " " + num3);
Look at this Code! Mixing of Inputs
Scanner scanner = new Scanner(System.in);
String horseName;
int age;
System.out.print("Enter the horse name: ");
horseName = scanner.next( );
System.out.print("Enter the age: "); Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(System.getProperty("line.separa
age = scanner.nextInt( );
tor"));
System.out.print(horseName + " is " + age + "years old." );
Input of a Numerical Value in a String Format