CS110T-Lab4-student-1446-Wednesday
CS110T-Lab4-student-1446-Wednesday
Lab Objectives:
In this lab, the student will practice:
✔ Defining variables of different data types
✔ Using format specifiers in printing output
✔ Compiling and executing Java applications.
✔ Writing and evaluating mathematical expressions
✔ Reading data values from the user.
Lab Exercise 1: Program Output
Problem Description: What is the output of the following code?
public class OperatorsDemo {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 5;
a += b;
b -= c;
c *= 2;
2
Lab Exercise 2: Program Debugging
Problem Description: Identify and categorize errors in the code to (Syntax, Logical, or
Runtime errors) then Correct the identified errors.
Hint: To calculate the area of a circle Use the formula pi * radius * radius.
double pi = 3.14
double radius = 5;
double area = radius * radius;
int z = 0;
int divisionResult = x / z;
3
Lab Exercise 4: Code writing (2)
Problem Description : Consider a program that reads three integers and then find their
multiplication result and display it on the screen. It should look like this output:
The Java program should:
1. Use the Scanner class to handle user input
2. read three integers from the user.
3. Multiply the three integers.
4. Display the multiplication result on the screen.
Output :
Enter first number:
15
Enter Second number:
34
Enter Third number:
14
The result of (15*34*14) is 7140
Output:
Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
The area of the rectangle is: 15
4
B. Triangle Area You are developing a simple application to calculate the area of a
triangle. The application should prompt the user to input the base and height of the
triangle and calculate the area of the triangle. Following these steps:
1. Write a java statement that imports class Scanner.
2. Declare input to be Scanner object for inputting data.
3. Ask the user to enter the base of the triangle as a double.
4. Ask the user to enter the height of the triangle as a double.
5. Create a double variable named triangleArea to store the area, and calculate it using
the formula: (base * height) / 2.
6. Print the calculated area of the triangle.
Output:
Enter the base of the triangle: 5.5
Enter the height of the triangle: 4.2
The area of the triangle is: 11.55
C. Circle Area You are developing a simple application to calculate the area of a circle.
The application should prompt the user to input the radius of the circle and calculate
the area of the circle. Following these steps:
1. Write a java statement that imports class Scanner.
2. Declare input to be Scanner object for inputting data.
3. Ask the user to enter the radius of the circle as a double value.
4. Create a double variable named circleArea to store the area and calculate it using
the formula: 3.14159* radius * radius.
5. Print the calculated area of the circle.
Output:
Enter the radius of the circle: 3.0
The area of the circle is: 28.27431
5
LAB4 Assignment Problems
Problem Description1: As a programmer you have a client who owns a moving service
company called “WorldMovers”. This client needs a program to calculate the amount of
workers that are needed for any moving job. We know that one worker can move a maximum
of 10 boxes and that one worker’s service fee is 50$. The program should ask the client’s
consumers about how many boxes they have to move in order to calculate the service fee.
A simple Output
Welcome to WorldMovers
How many boxes you have?
25
You need 3 workers to do the job
This will cost you 150$
Problem Description2: Write a program that asks the user to type the price without tax of one kilogram of
tomatoes, the number of kilograms you want to buy. The program must calculate the total price including
taxes.
Note: taxes are 5% of product price.
Hint : total price with tax = Price + (price*tax as a decimal )
A simple Output
Please enter the price without tax of one kilogram of tomatoes: 10
Please enter the number of kilograms: 5
Total price: 52.5 S.R.
Problem Description3: Complete the following code and write the output of code:
6
Problem Description4: Write a program that takes as input any change expressed in
Halalah coins. It should then compute the number of one riyals, half-riyal coins, quarter-riyal
coins, 10-hallalah coins, 5- Halalah coins, and one Halalah coin to be returned, using as many
one riyal coins as possible, then halfs coins, quarters, 10’s, 5’s, and 1 Halalah, in that order.
For example, 483 coins would be returned as 4 (riyal coins), 1 (half-riyal coins), 1 (quarter-
riyal coins), 1 (5- Halalah coins), and 3 (one Halalah coin).