0% found this document useful (0 votes)
15 views11 pages

Assignment 02 - Branching

The document outlines Lab Assignment 02 for CSE110, which includes various Java programming tasks and home tasks that students need to complete. Students are required to submit specific home tasks via a Google Form and handwritten tracing tasks to their lab instructors. The lab tasks cover topics like finding the largest number, calculating grades, determining divisibility, and more.

Uploaded by

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

Assignment 02 - Branching

The document outlines Lab Assignment 02 for CSE110, which includes various Java programming tasks and home tasks that students need to complete. Students are required to submit specific home tasks via a Google Form and handwritten tracing tasks to their lab instructors. The lab tasks cover topics like finding the largest number, calculating grades, determining divisibility, and more.

Uploaded by

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

Lab Assignment 02

Branching

CSE110: Programming Language I

Lab Tasks
Home Tasks
Coding
5
6
Tracing
1
2

[You need to submit only the Home Tasks. Submit all the Homework Coding Tasks (Task
1 to 6) in the Google Form shared on buX. Submit all the Homework Tracing Tasks
(Task 7 & 8) handwritten to your Lab Instructors before the next lab.]

LAB Tasks
(No Need to Submit)
1. Write the Java code of a program to find the largest among three numbers entered
by the user.

Sample Input
Output
100
23
-4
largest number: 100
5
17
-5
largest number: 17

2. Write a Java program that takes a student's numerical score as input as an


integer and prints their corresponding letter grade according to the following
grading system:
Scores
90-100
85-89
70-84
57-69
50-56
<50
Grades
A
A-
B
C
D
F

Sample Input
Output
9
Your grade is F
82
Your grade is B

3. Write a Java code of a program that takes an integer number as user input and
then determines if that number is divisible by both 5 and 7; otherwise display
“No”. For example, numbers like 35, 70, 105, 140, 175, 210, 245, 280 etc. can be
divisible by both 5 and 7.

Sample Input
Output
15
Invalid: Divisible by 5 Only
28
Invalid: Divisible by 7 Only
105
Divisible by Both
36
No

4. Write a Java program that will take a year as input and print whether that year
is a leap year or not.

* A year may be a leap year if it is evenly divisible by 4.


* Years that are divisible by 100 (century years such as 1900 or 2100) cannot be
leap years unless they are also divisible by 400.

Sample Input
Output
2020
2020 is a leap year
2001
2001 is not a leap year
1900
1900 is not a leap year
2000
2000 is a leap year

5. Let’s consider the following piecewise function:

Design a Java code of a program that takes the value of x as user input and then
displays the output based on the given piecewise function.

Sample Input
Output
-3
output: -6
1
output: 2
4
output: 15
10
output: 302
________________

6. What will be the output of the following program? Show the workings.
1
public class Tracing1 {

Output
2
public static void main(String[] args) {
3
int num1 = 10;
4
int num2 = -3;
5
int num3 = -1;
6
int sum = num1 + num2 + num3;
7
if (num3 < 0) {
8
System.out.println(num3 * (-2));
9
}
10
else {
11
System.out.println(sum);
12
}
13
if (num1 < 5) {
14
System.out.println(num1 + 10);
15
}
16
else if (num2 == -3) {
17
num2 = num1;
18
System.out.println(num2);
19
}
20
else {
21
System.out.println(num1 + num2 + num3);
22
}
23
if (num1 > 15) {
24
System.out.println(num1);
25
}
26
if (num2 == 0) {
27
System.out.println(num2 + num3);
28
}
29
else {
30
System.out.println(num3);
31
}
32
if (sum != 0) {
33
System.out.println(100);
34
}
35
else {
36
System.out.println(sum + 100);
37
}
38
if (num1 > 0 && num2 < 0) {
39
System.out.println(num1 == num2);
40
}
41
else {
42
System.out.println("End");
43
}
44
}
45
}

________________

Home Tasks
1. Write a Java code of a program that takes an integer number as user input and
then determines if that number is divisible by either 5 or 7 but not both;
otherwise display “No”. For example, numbers like 35, 70, 105, 140, 175, 210, 245,
280 etc. can be divisible by both 5 and 7. These numbers are invalid.

Sample Input
Output
15
Divisible by 5 Only
28
Divisible by 7 Only
105
Invalid: Divisible by both
36
No
2. Write the Java code of a program that calculates the tax and prints it as
follows:
a) No tax if you get paid less than 10,000
b) 5% tax if you get paid between 10,000 and 20,000 (both inclusive)
c) 10% tax if you get paid more than 20,000
d) NO TAX IF YOU ARE LESS THAN 18 YEARS OLD.
Hint: Take payment and age as user as inputs; then calculate tax and print it.

Sample Input
Output
9000
23
Your tax amounts in 0 Tk
15000
17
Your tax amounts in 0 Tk
15000
18
Your tax amounts in 750 Tk
30000
25
Your tax amounts in 3000 Tk

3. Write a Java program that takes 3 float numbers as input from the user and
prints the maximum and minimum number from the inputs.

Sample Input
Output
18.83
-4.02
83.12
Maximum number is 83.12
Minimum number is -4.02

26.45
0.02
13.56
Maximum number is 26.45
Minimum number is 0.02

4. A triangle has 3 sides. Write a program which asks the users for input. Based on
the input, your program should output whether it is an Equilateral, Isosceles or
Scalene.
* Equilateral triangle has three sides with equal length
* Isosceles triangle has two sides with equal length and another side is different
* Scalene triangle has different lengths in each side

Sample Input
Output
5
2
4
This is a Scalene triangle
5
5
3
This is a Isosceles triangle
3
3
3
This is a Equilateral triangle

5. Suppose you are hired by a supershop named Bastob. Now, your task is to create a
Java program that will help the cashier calculate the change to be returned.
The program takes two inputs. First input is an integer number which is the amount
of money to be paid in taka and the second integer is the amount of money the
customer gave to the cashier. Your program should print the following:

* If the customer gave more money than the actual amount, print change the cashier
should return in notes and coins.
* If the customer gave less money than the amount to be paid, then print the amount
the customer needs to pay.

Consider the following denomination for notes and coins in taka:


Notes: 100, 50, 20, 10.
Coins: 5, 2, 1.

Sample Input
Output
Enter the amount the customer need to pay(Taka)
35
Enter the amount, customer gave(Taka)
53
The returned amount is 18 taka.
100 taka note: 0
50 taka note: 0
20 taka note: 0
10 taka note: 1
5 taka coin: 1
2 taka coin: 1
1 taka coin: 1
Enter the amount the customer need to pay(Taka)
60
Enter the amount, customer gave(Taka)
500
The returned amount is 440 taka.
100 taka note: 4
50 taka note: 0
20 taka note: 2
10 taka note: 0
5 taka coin: 0
2 taka coin: 0
1 taka coin: 0
Enter the amount the customer need to pay(Taka)
50
Enter the amount, customer gave(Taka)
50
The returned amount is 0 taka.
Enter the amount the customer need to pay(Taka)
550
Enter the amount, customer gave(Taka)
520
Please pay 30 taka more.

6. Write a Java program that reads three numbers and prints "All numbers are equal"
if all three numbers are equal, "All numbers are different" if all three numbers
are different and "Neither all are equal nor different" otherwise.

User Input
Output
Input the 1st number: 2345
Input the 2nd number: 2452
Input the 3rd number: 4532
All numbers are different
Input the 1st number: 230
Input the 2nd number: 230
Input the 3rd number: 112
Neither all are equal or different

________________

7. What will be the output of the following program? Your answer will not be
accepted without the workings.
1
public class Tracing2 {
2
public static void main(String[] args) {
3
boolean var1, var2, var3, var4, var5, var6;
4
boolean result1, result2, result3, result4, result5;
5
boolean result6, result7, result8, result9, result10;
6
var1 = var2 = var3 = var4 = var5 = var6 = false;
7
result1 = result2 = result3 = result4 = result5 = false;
8
result6 = result7 = result8 = result9 = result10 = false;
9
var1 = (!false || false) && true;
10
var2 = var1 && true;
11
var3 = false && !true;
12
var4 = true;
13
var5 = false;
14
var6 = var3 && true;
15
result1 = (var1 && var2) && (40 % 3 > 45) || (var5 && var6);
16
result2 = (var1 || var2) || (result1 && false);
17
result3 = (var1 && result1) || result2 || var5;
18
result4 = (var1 || var2) || ((var3 && var1) && false);
19
result5 = (var1 && var2) && (result3 || var1);
20
result6 = ((var3 || !var2) && result5) || true;
21
result7 = (var4 && result1) && ((result1 && false) || true);
22
result8 = ((var1 && result3) && (!var5 || var6)) && true;
23
result9 = ((result2 && var2) || (!result7 && var1)) && !false;
24
result10 = !(var1 && true);
25
System.out.println(result1 + " " + result2);
26
System.out.println(result3 + " " + result4);
27
System.out.println(result5 + " " + result6);
28
System.out.println(result7 + " " + result8);
29
System.out.println(result9 + " " + result10);
30
}
31
}
________________

8. What will be the output of the following program? Your answer will not be
accepted without the workings.
1
public class Tracing3 {
2
public static void main(String[] args) {
3
int p = 5;
4
int q = 6;
5
int r = 9;
6
int sum = 0;
7
if (p < 12) {
8
System.out.println(r + 2);
9
}
10
else {
11
System.out.println(r + p);
12
}
13
if (q > 20){
14
System.out.println(r + 19);
15
}
16
else if (q <= 6) {
17
System.out.println(q + 3);
18
}
19
else{
20
System.out.println(p + q + r);
21
}
22
if (r > 15) {
23
System.out.println(r);
24
}
25
else if (r == 0) {
26
System.out.println(p + q);
27
}
28
else {
29
System.out.println(p);
30
}
31
if (sum != 0) {
32
System.out.println(3);
33
}
34
else {
35
System.out.println(sum + 32);
36
}
37
if(p > 0 && r < 10){
38
System.out.println(p + r);
39
}
40
else {
41
System.out.println(p - r);
42
}
43
}
44
}

You might also like