Skip to content

Commit bdd3016

Browse files
committed
Lecture 01 Exercises added
1 parent ba0bd9d commit bdd3016

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

Exercises/L01/Exercise1.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Lecture 01 Exercise 01
2+
/*
3+
Write a Java program to display your name and address in 3 lines.
4+
*/
5+
6+
public class Exercise1 {
7+
8+
public static void main(String args[]) {
9+
System.out.println("Thushara Thiwanka");
10+
11+
System.out.println("Z/00,");
12+
System.out.println("Earth,");
13+
System.out.println("Solar System");
14+
}
15+
16+
}

Exercises/L01/Exercise2.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Lecture 01 Exercise 02
2+
/*
3+
Write a java program to input the length and the width of a rectangle and
4+
calculate and print the perimeter.
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class Exercise2 {
10+
public static void main(String args[]) {
11+
float length, width;
12+
float perimeter = 0;
13+
14+
Scanner myScanner = new Scanner(System.in);
15+
16+
System.out.print("Enter length : ");
17+
length = myScanner.nextFloat();
18+
System.out.print("Enter width : ");
19+
width = myScanner.nextFloat();
20+
21+
perimeter = (length + width) * 2;
22+
23+
System.out.println("Perimeter = " + perimeter);
24+
}
25+
}

Exercises/L01/Exercise3.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Lecture 01 Exercise 03
2+
/*
3+
WWrite a program to input 3 integers and print the largest and
4+
the smallest of the 3 numbers entered.
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class Exercise3 {
10+
public static void main(String[] args) {
11+
int max = 0, min = 0, num = 0, count = 1;
12+
13+
Scanner myScanner = new Scanner(System.in);
14+
15+
System.out.print("Enter Number : ");
16+
max = myScanner.nextInt();
17+
min = max;
18+
while(count < 3) {
19+
System.out.print("Enter Number : ");
20+
num = myScanner.nextInt();
21+
if(num > max) {
22+
max = num;
23+
}
24+
else if(num < min) {
25+
min = num;
26+
}
27+
count++;
28+
}
29+
30+
System.out.println("Largest Number = " + max);
31+
System.out.println("Smallest Number = " + min);
32+
33+
}
34+
}

Exercises/L01/Exercise4.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Lecture 01 Exercise 04
2+
/*
3+
Write a program to input 10 numbers from the keyboard and
4+
find how many odd numbers and how many even numbers were entered.
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class Exercise4 {
10+
public static void main(String[] args) {
11+
int number, evenCount = 0, oddCount = 0, count = 0;
12+
13+
Scanner myScanner = new Scanner(System.in);
14+
15+
while (count < 10) {
16+
System.out.print("Enter Number : ");
17+
number = myScanner.nextInt();
18+
if (number % 2 == 0) {
19+
evenCount += 1;
20+
} else {
21+
oddCount += 1;
22+
}
23+
++count;
24+
}
25+
26+
System.out.println("Even Numbers : " + evenCount);
27+
System.out.println("Odd Numbers : " + oddCount);
28+
}
29+
}

0 commit comments

Comments
 (0)