0% found this document useful (0 votes)
26 views8 pages

ICT 143-2 Data Structures and Algorithms: o o o o o o o o

Uploaded by

melissadjay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views8 pages

ICT 143-2 Data Structures and Algorithms: o o o o o o o o

Uploaded by

melissadjay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Uva Wellassa University

Faculty of Technological Studies


Department of Information and Communication Technology
Bachelor of Information and Communication Technology
ICT 143-2 Data Structures and Algorithms
Lab 1
(22/04/2024)
Outline:
o First Java program
o Variables and Data types
o Scope of Variables in Java
• Control Structures
o Operators
o If/else statement
o Switch statement
o While and do-while loops
o For loops and Nested Loops
• Java Scanner Class

1. First Java program


Create new file named First.java
public class First{
}

Create main method,


public static void main(String[] args){

System.out.println("Happy coding!");

2. Variables and Data types


o For all data, assign a name (identifier) and a data type
o Data type tells compiler:
▪ How much memory to allocate.
▪ Format in which to store data.
▪ Types of operations you will perform on data.
o Java “primitive data types”
▪ Byte, short, int, long, float, double, char, Boolean
o Java “non-primitive data types”
▪ String, Array and Classes.
Task:
i. Create variable using each primitive data type.
ii. Print each variable value.
iii. Consider equation, S = ut + (1 / 2) at2 , when values of u, t and a are given calculate S.
iv. Print the value of variable S.

3. Scope of Variables in Java


• Scope of a variable is the part of the program where the variable is accessible.
• Member Variables (Class Level Scope)
o These variables must be declared inside class (outside any methods). They
can be directly accessed anywhere in class.
• Local Variables (Method Level Scope)
o Variables declared inside a method have method level scope and can’t be
accessed outside the method.
• Loop Variables (Block Scope)
o A variable declared in a for loop condition has scope withing the loop only.

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
}

Syntax (if -else if):

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
}

• The switch expression is evaluated once.


• The value of the expression is compared with the values of each case.
• If there is a match, the associated block of code is executed.
• The break and default keywords are optional.
• When java reaches a break keyword, it breaks out of the switch 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.

9. The java Scanner Class

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{

public static void main(String[] args) {

//create a scanner object, so you can read the command-line input


Scanner scanner = new Scanner(System.in);

//prompt for your name


System.out.print("Enter your name: ");

//get the input from command line as a String


String name = scanner.nextLine();

//prompt for your age


System.out.println("Enter your age: ");

// get the age as an int


int age = scanner.nextInt();

System.out.println(name + " Your age is " + age);

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.

System.out.println("Enter a floating point number: ");


double y = scan.nextDouble();
Activities:

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.

5. Print following star pattern in Java using nested for-loops.


*
***
*****
***
*
6. Write a java program for following scenario.

• For customers of a pharmacy, discounts are given considering following


procedure.
• If the total amount is greater than Rs.5000, discount rate is 10%.
• If the total amount is greater than Rs.2000, the discount rate is 5%.
• If the customer is a senior citizen (60+) and total amount is greater than Rs 1000,
discount rate is 5%.
• If the customer is a senior citizen (60+) and total amount is greater than Rs 2000,
discount rate is 8%.
• Only one discount rate is applicable for a single purchase.
Calculate the discount for the following total amount.
• When total amount is 6430
• When total amount is 1560

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.

Method Name Description

public String nextLine() Moves the scanner position to the next line and
returns the value as a string.

You might also like