0% found this document useful (0 votes)
5 views

CS110T-Lab4-student-1446-Wednesday

The document outlines a lab exercise for CS110T: Programming Language I, focusing on Java basics, including defining variables, using format specifiers, compiling applications, and reading user input. It includes several exercises that involve debugging code, performing arithmetic operations, and calculating areas of geometric shapes. Additionally, it presents programming problems related to a moving service company and calculating prices with tax.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CS110T-Lab4-student-1446-Wednesday

The document outlines a lab exercise for CS110T: Programming Language I, focusing on Java basics, including defining variables, using format specifiers, compiling applications, and reading user input. It includes several exercises that involve debugging code, performing arithmetic operations, and calculating areas of geometric shapes. Additionally, it presents programming problems related to a moving service company and calculating prices with tax.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CS110T: Programming Language1

Lab 4: Java basics II Cont.

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;

System.out.println("a > b: " + (a > b));


System.out.println("c < a: " + (c < a));
System.out.println("b == c: " + (b == c));
System.out.println("(a > b) && (b > c): " + ((a > b) && (b > c)));
System.out.println("(a < b) || (c > b): " + ((a < b) || (c > b)));
System.out.println("!(b == 10): " + (!(b == 10)));
}
}
Output:

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.

public class DebuggingExercise


public static void main(String[] args) {
int x = 10;
int y = "20";

double pi = 3.14

System.out.println("The square of 5 = ");


System.out.println(5 * 3);

double radius = 5;
double area = radius * radius;

int z = 0;
int divisionResult = x / z;

System.out.printl("End of debugging exercise.");


}
}

Lab Exercise 3: Java statements (code writing 1)


Problem Description: for the following problem do the arithmetic operations by hand
then write the Java statements in Netbeans to double check your answers.
The problem should accomplish the following:
a. Declare six variables a, b, c, d, e, g as an integer number.
b. Initialize them as: ( a=2 , b= 3, c= 6, d=4 , e=7, g=10)
c. Update their values as:
(c +=7 , d -=2, e *=3, g /=2, a %=2)
d. Perform these operations:
a=c+g*d
g = a – a%e
b = (g +c ) * ++d
e. Print the value of all these variables using the format specifier.

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

Lab Exercise 5: Code writing (3)


A. Rectangle Area You are developing a simple application to calculate the area of a
rectangle. The application should prompt the user to input the length and width of the
rectangle and calculate the area of the rectangle. 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 input the length of the rectangle as int value.
4. Ask the user to input the width of the rectangle as int value.
5. Create an int variable named rectangleArea to store the area, and calculate it using
the formula: length * width.
6. Print the calculated area of the rectangle

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:

// Lab Exercise 1: Math.java


// Computing and displaying the result of different
expressions
public class Math
{
// main method begins execution of Java application
public static void main( String args[] )
{
myNum = 9;
myFloatNum = 8.99f;
myLetter = 'A';
myBool = false;
myText = "Hello World";
System.out.println(myText);
System.out.println("4.0+9="+4.0+myNum);
System.out.print("((2-2) × 1×3) - 3/1.0= ");
System.out.println(((2-2)*1*3)-3/1.0);
System.out.println("(1+2)*3*4/5 ="+(1+2)*3*4/5);
}
}

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).

Input: Change in cents


Output: Equivalent change
Hint: you need to use divide (/) and remainder (%) operations.

You might also like