Lecture 3:
Java Basics 2
CS110: Programming Language I
Table of contents
01 02
Types of operator: Scanner
Arithmetic
Increment / Decrement
Relational
Logic
Compound assignment Operators
03 04
Errors types Debugging
Table of contents
This icon means :
Self Study
01
Types of operator
Types of operators
Five types of operators:
1. Arithmetic
2. Increment / Decrement
3. Relational
4. Logic
5. Compound assignment Operators
Types of operators
1. Arithmetic Operators
Name Meaning Example Result
+ Addition 34 + 1 35
- Subtraction 34.0 – 0.1 33.9
* Multiplication 300 * 30 9000
/ Division 1.0 / 2.0 0.5
% Remainder 20 % 3 2
▪ The asterisk (*) indicates multiplication
▪ The arithmetic operators are binary operators because they
each operate on two operands.
▪ 5/2 yields an integer 2 _ 5.0/2 yields a double value 2.5
▪ Division by 0 returns an error.
Types of operators
1. Arithmetic Operators
❑ The percent sign (%) is the remainder operator
❑ The remainder operator, %, yields the remainder after division.
❑ X=8%3 // X=2
❑ X=5%2 // X = 1
❑ Remainder is very useful in programming.
❑ an even number % 2 is always 0 and
❑ an odd number % 2 is always 1.
❑ So you can use this property to determine whether a number is even or odd
Types of operators
1. Arithmetic Operator precedence
Types of operators
1. Arithmetic Operator precedence
Types of operators
1. Arithmetic Operator precedence
What is the order of evaluation in the following expressions?
3 + 4 + 5 + 6 + 2 3 + 4 * 5 - 6 / 2
1 2 3 4 3 1 4 2
3 / (4 + 5) - 6 % 2
2 1 4 3
3 / (4 * (5 + (6 - 2)))
4 3 2 1
Types of operators
1. Arithmetic Division Operator
Types of operators
2. Increment / Decrement
Types of operators
2. Increment / Decrement Example
what is the value of res and n?
Post-Increment Pre-Increment
int n=2, res; int n = 2 , res;
res = 2 * n++; res = 2 * ++n;
No difference if "alone" in statement:
X++; and ++X; → identical result
Types of operators
2. Increment / Decrement Example
Types of operators
3. Relational Operators
❑ Relational operators are used to compare two values.
❑ The result of the comparison is a Boolean value: true or false.
❑ Java uses the following relational operators:
Types of operators
3. Relational Operators Example
Note: Use the equal sign (=) to make an assignment and use the == sign to
make a comparison and return a boolean.
Types of operators
3. Relational Operators, comparing Strings
❑ You should almost never compare Strings using = =.
❑ Compare Strings using the equals() method.
➢ This method is part of the String class.
➢ It accepts one String argument, checks whether the contents of Strings
are equal, and then returns a boolean.
String x = "ja";
String y = "va";
String z = x + y;
Boolean test = [Link](x + y);
[Link](test);//true
Types of operators
4. Logical Operators
❑ Java’s logical operators enable you to combine multiple
Boolean expressions into one Boolean expression
❑ The logical operators are
➢ && (conditional AND)
➢ || (conditional OR)
➢ ! (logical NOT).
❑ Logical operators take only Boolean values as operands
Types of operators
4. Logical Operators:
Truth Table for Operator
Types of operators
4. Logical Operators Example
What will be the value of result :
Types of operators
5 Compound assignment Operators
❑ Casting in compound assignment Expression
int sum = 0;
sum += 4.5; // sum becomes 4 after this statement
sum += 4.5 is equivalent to sum = (int)(sum + 4.5).
02
Scanner
Getting input with scanner
Scanner
❑ JAVA affords a pre-written class named Scanner, which allows
getting input from a user.
❑ To tell the compiler we want to use the Scanner class, insert the
following import statement at the very beginning of the program:
❑ import [Link];
❑ Data can come from many sources, such as the user at the keyboard
or a file on disk.
Getting input with scanner
1) Before using a Scanner, you must create it and specify the source of the data.
(declaration and initialization could be done in one step)
Scanner input = new Scanner( [Link] );
➢ Standard input object, [Link], enables applications to read bytes of
information typed by the user.
➢ translates these key strokes into types that can be used in a program.
2) After creating input scanner as shown, you can read and store the data
entered from the user. For example, to read int data, you use method nextInt()
like this: int x = [Link]();
Getting input with scanner
Common scanner methods
Type Method name
Scanner input = new Scanner([Link]);
byte b; b = [Link]();
short s; s = [Link]();
long l; l = [Link]();
float f; f = [Link]();
double d; d = [Link]();
char c; c = [Link]().charAt(0);
String st; st = [Link]();
Getting input with scanner
1. Import Scanner
import [Link];
2. Declare and initialize an
public class MyProgram { object of the Scanner
class.
public static void main(String[] args) { “S” is the name of the object
Scanner S = new Scanner([Link]);
[Link]("Enter a double value"); 3. Define variable to
receive a value
double x;
x = [Link](); 4. Read input
nextDouble() is method of
[Link]("You entered :"+ x); Scanner class used to
input a double value
}
Case study: Adding numbers
Write a program that computes and displays the sum of
two integer numbers entered by the user.
Case study: Adding numbers
Programming Errors
❑ Syntax Errors
✓ Detected by the compiler
[Link]("Welcome to Java);
❑ Runtime Errors
✓ Causes the program to abort
[Link]( 1 / 0 );
❑ Logic Errors
✓ Produces incorrect result
[Link]("The triple value of 3 = ");
[Link](3 * 2);
Programming Errors: Logic Errors Types
A logic error (e.g., when both braces in a block are left
out of the program) has its effect at execution time.
➢ A fatal logic error causes a program to fail
and terminate prematurely.
➢ A nonfatal logic error allows a program to
continue executing but causes it to produce incorrect
results.
04
Debugging
Debugging
❑ errors are called bugs.
❑ When you type a program, typos and unintentional syntax errors are
likely to occur.
❑ Therefore, when you compile a program, the compiler will identify the
syntax errors.
❑ Debugging means to identify and fix errors.
Debugging
Debugging
Debugging
Find and fix syntax error!!
Case Study: TO APPLY IN NETBEANS AT HOME
Case Study: TO APPLY IN NETBEANS AT HOME
Case Study: TO APPLY IN NETBEANS AT HOME
Case Study: TO APPLY IN NETBEANS AT HOME
Case Study: TO APPLY IN NETBEANS AT HOME
That’s all for
this Lecture!!
Extra
Self study
materials
Short Review
❑ Java is a case-sensitive language.
❑ All Java programs must be stored in a file with a .java
file extension.
❑ Comments are ignored by the compiler.
❑ If a .java file has a public class, the class must have
the same name as the file.
❑ Java applications must have a main method.
❑ For every left brace, or opening brace, there must be a
corresponding right brace, or closing brace.
❑ Statements are terminated with semicolons.
❑ Comments, class headers, method headers, and
braces are not considered Java statements.
Arithmetic Expressions
3 + 4 x 10( y − 5)( a + b + c) 4 9+ x
− + 9( + )
5 x x y
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x
+ 9*(4/x + (9+x)/y)
Types of operators
3. Increment / Decrement example
What is the output of the following:
Practice
Practice (cont.)
• Let's explain what happens in each statement
• //Store a value into the variable num1.
• num1 = 34;
• /* Assign the result of adding a value to the variable num1.
• This assignment replaces the old value of num1. */
• num1 = num1 + 38;
• // Copy the value of num1 into num2.
• num2 = num1;
• // Assign the result of the division to the variable num3
• num3 = num2 / 2;
• /* Assign the result of the division to the variable num3, which replaces the
old value of num3. */
• num3 = num3 / 4;
Increment / Decrement
• Increment & Decrement Operators
• Increment operator, ++
intVar++; → intVar = intVar + 1;
• Decrement operator, --
intVar--; → intVar = intVar – 1;
• Post-Increment : intVar++
• Uses current value of variable, THEN increments it
• a = 7;
• [Link](a++) // The program output is 7
• Pre-Increment : ++intVar
• Increments variable first, THEN uses new value
• a = 7;
• [Link](++a) // The program output is 8
Increment / Decrement
What is the output of the followings:
int i = 10;
int newNum = 10 * i++;
[Link]("i = " + i + ",
newNum = " + newNum);
int i = 10;
int newNum = 10 * ++i;
[Link]("i is " + i + ",
newNum is " + newNum);