ICT 143-2 Data Structures and Algorithms: o o o o o o o o
ICT 143-2 Data Structures and Algorithms: o o o o o o o o
System.out.println("Happy coding!");
Task:
Create a global variable.
Create a new method named testMethod with no parameters and no return type. Try to use
it inside the testMethod.
To call testMethod inside the main method, create an object (instance) of class First.
Call testMethod inside the main method.
4. Operators
Arithmetic Operators Bitwise Operators
+ Addition & bitwise and
- Subtraction | bitwise or
* Multiplication ^ bitwise XOR
/ Division ~ bitwise compliment
% Modulus << left shift
++ Increment >> right shift
-- Decrement >>> zero fill right shift
Relational Operators Assignment Operators
== Equal To = Simple assignment
!= Not Equal To += Add AND assignment
> Greater Than -= Subtract AND assignment
< Less Than *= Multiply AND assignment
>= Greater Than Or Equal To /= Divide AND assignment
<= Less Than Or Equal To %= Modulus AND assignment
<<= Left shift AND assignment
Logical Operators >>= Right shift AND assignment
&= Bitwise AND assignment
&& Logical And
^= Bitwise exclusive OR and assignment
|| Logical Or
|= Bitwise inclusive OR and assignment
! Logical Not
5. If/else condition
Syntax (if-else):
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
true
} else {
// block of code to be executed if the condition1 is false and condition2 is
false
}
6. Switch
Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
7. While/ do while
Syntax (while):
while (condition) {
// code block to be executed
}
Syntax (do-while):
do {
// code block to be executed
}
while (condition);
The difference between do-while and while is that do-while evaluates its expression at the
bottom of the loop instead of the top. Therefore, the statements within the do block are
always executed at least once.
8. For loop
Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
• Statement 1 is executed (one time) before the execution of the code block.
• Statement 2 defines the condition for executing the code block.
• Statement 3 is executed (every time) after the code block has been executed.
There are various ways to read input from the keyboard. The java.util.Scanner class is one of
them. The Java Scanner class breaks the input into tokens using a delimiter (whitespace by
default). It provides many methods to read and parse various primitive values.
The java command-line argument is an argument passed at the time of running the java
program. At the top of the class where you want to get command line input, you must import
the Scanner class. This means that you explicitly tell the compiler that you will be using this
class. You can do this as follows:
import java.util.Scanner;
By doing this, we import a Java library class into our code. To use the class in our own class you
must create your own class create an ’object’ from the Scanner class in it. To do that, include
the following lines in you class.
public class MyOwnClass{
The nextLine method of Scanner takes no input and returns the next line of text, ending with
a newline. Once you have created a Scanner object, you can use it repeatedly to get input. The
Scanner also provides method nextInt, nextDouble, and so on. You can use these methods to
retrieve data of a particular type.
1. Compare number 5 with number 6 using six relational operators and print the result in 6
lines.
2. Write a Java program that determines a student’s grade. The program will consider three
types of scores(quiz, mid-term, and final scores) and determine the grade based on the
following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
-if the average of quiz+mid-term < 35% =>grade=F
3. Write a Java program to calculate the number of different notes and coins (Rs.1000,
500.100,50,20,10 notes and Rs. 5, 2, 1 coins) required to pay a given amount of money.
Assume that the amount is a total rupee value and no cents included.
Eg.1272 1* Rs.1000 note
2* Rs.100 note
1* Rs.50 note
1* Rs.20 note
1* Rs.2 coin
4. Write java program for following scenario. A Bank accepts deposit for one year or more
and the policy it adopts on interest rate is as follows:
If a deposit is less than Rs. 1000 and for 2 or more years the interest rate is 5 percent
compound annually.
If a deposit is Rs. 1000 or more but less than Rs. 5000 and for 2 or more years the interest
rate is 7 percent compound annually.
If a deposit is more than Rs. 5000 and is for 1 year or more the interest rate is 8 percent
compound annually.
7. There is a list of commonly used Scanner class methods. Find them all and fill the
following table. One row is completed for you.
public String nextLine() Moves the scanner position to the next line and
returns the value as a string.