You are on page 1of 22

Variables, Operators,

Accepting Input from


Users
Java Variables
• A variable is an item of data used to store the state
of objects.
• A variable has a:
– data type
• The data type indicates the type of value that the
variable can hold.
– name
• The variable name must follow rules for identifiers.
Java Identifiers
• Identifiers
– are tokens that represent names of variables, methods,
classes, etc.
– Examples of identifiers are: Hello, main, System, out.
• Java identifiers are case-sensitive.
– This means that the identifier Hello is not the same as hello.
• Identifiers must begin with either a letter, an underscore “_”,
or a dollar sign “$”. Letters may be lower or upper case.
Subsequent characters may use numbers 0 to 9.
• Identifiers cannot use Java keywords like class, public, void,
etc. We will discuss more about Java keywords later.
Java Keywords
Exercise
• Identify the following as legal or not legal
identifiers:
accounts a123 ABC123 BILLING void
Time_Of_Day
_amount 10percent US_$
const while for
if else #number
Categories of Data Types
• Primitive Data Type: such as boolean, char, int,
short, byte, long, float, and double
• Non-Primitive Data Type or Object Data type: such
as String, Array, etc.
Primitive Data Type
Arithmetic Operators
• Arithmetic operators are used in mathematical expressions in the
same way that they are used in algebra.
• Assume integer variable A holds 10 and variable B holds 20, then:
Relational Operators
• Relational operators compare two values and determines
the relationship between those values.
• Assume variable A holds 10 and variable B holds 20, then:
Logical Operators
• Logical operators have one or two boolean
operands that yield a boolean result.
• Assume Boolean variables A holds true and variable
B holds false, then:
Operators and Expressions
• An operator is a symbol which represents an operation to be
performed.
• Expressions in the program may be mathematical formulas.

Mathematical Formula Equivalent Expression

b2 – 4ac b*b – 4 * a * c

a+b–c a+b–c

ab (a+b)/(c+d)
cd
1 1/(1+ x*x)
1 x2

ax – (b + c) a*x – (b + c)
Exercise
• What is the equivalent expression of the following
mathematical equations:

1. 4a3 + 2a2 + 1 = 4 * a *a *a + 2*a*a+1


2. 5j3 – 2(m+n) = 5 * j * 3 – 2 * (m + n)
3. 3 (i + j) = 3 * ( i + j)
4.
kA(T1  T2 )
L * (T1 – T2) / L
k *A
Syntax for getting user input
Scanner user_input = new Scanner(System.in);
//accepting a string
String first_name;
first_name = user_input.next( );
//accepting integer
int num = 0;
num = user_input.nextInt();
//accepting decimal number
double dnum = 2.5;
dnum = user_input.nextDouble();
Sample Activity
• AREA AND PERIMETER OF RECTANGLE: Create a
new Java class and design a program to calculate
the area and perimeter of a rectangle which is
represented by the following formulas:
• A = LW
• P = 2L + 2W
Finding Sum, Difference,
Product and Quotient
• Create a Java class and design a program that takes
two numbers as input data and display their sum,
difference, product and quotient.
• The name of the class should be LastnameActivity3.
Activity 2 – Distance in Miles to
Kilometer
• Create a Java class and design a program that takes
in the distance in miles as input and compute its
equivalent into kilometers and display result. Use
the formula:
• kms = 1.609 x distance in miles
• Define 1.609 as a constant macro kms_per_mile.
• The name of the class should be LastnameActivity2.
Activity 3 – Weight in Pounds to
Kilograms and Grams
• Create a Java class and design a program that read
in the weight (in pounds) of an object and then
compute and print the weight in kilograms and
grams. (Hint: 1 pound is equal to 0.453592
kilogram and 453.59237 grams.)
• The name of the class should be LastnameActivity3.
Activity 4 – Seconds to Hours
and Minutes
• Create a Java class and design a program that
inputs a number of seconds and outputs the hours
and minutes equivalent.
• The name of the class should be LastnameActivity4.

You might also like