You are on page 1of 8

ICT711 Programming and Algorithms

T323

Tutorial – Week 2

Name: Nusrat JAHAN


Student id : 20022074
Part A:
Exercise 1: Payslip.java
Write a program that asks the number of hours and pay rate for an employee, then print the
net pay after you deduct the tax of 35%.
Code:
import java.util.Scanner;

public class PaySlip{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// user input

System.out.print("Enter the number of hours worked: ");

double hoursWorked = scanner.nextDouble();

System.out.print("Enter the pay rate per hour: ");

double payRate = scanner.nextDouble();

scanner.close();

// Calculate gross pay

double grossPay = hoursWorked * payRate;

// Calculate net pay after deducting 35% tax

double taxRate = 0.35;

double taxAmount = grossPay * taxRate;

double netPay = grossPay - taxAmount;

System.out.println("Gross Pay: $" + grossPay);

System.out.println("Tax Deducted : $" + taxAmount);

System.out.println("Net Pay: $" + netPay);

}
Output:

Exercise 2:

Rectangle.java Write a program that asks the length and width of a rectangle from the argument
input, and then prints:

• The area and perimeter of the rectangle

• The length of the diagonal


public class Rectangle {

public static void main(String[] args) {

// Check if there are exactly two command-line arguments

if (args.length != 2) {

System.out.println("Please provide both length and width as


command-line arguments.");

return;

// Parse length and width from command-line arguments

double length = Double.parseDouble(args[0]);

double width = Double.parseDouble(args[1]);

// Calculate area, perimeter, and diagonal length

double area = length * width;

double perimeter = 2 * (length + width);

double diagonalLength = Math.sqrt(length * length + width * width);

// Display the results

System.out.println("Area of the rectangle: " + area);


System.out.println("Perimeter of the rectangle: " + perimeter);

System.out.println("Length of the diagonal: " + diagonalLength);

Part B
1) There are three stages in developing Java programs. First, the program is written in
human- readable form
a. What should the file name be for a public class named HelloWorld?
Ans: For a public class named HelloWorld, the file name should be HelloWorld.java

b. What full command would you type to compile the code?


Ans: The full command to compile the code would be:
javac HelloWorld.java

c. How would you execute this program?


Ans: To execute the program, you would use the following command after compilation:
java HelloWorld

2) What is the difference between variable declaration and variable assignment? What is
variable initialization?

Variable Declaration:

 Definition: Variable declaration is the process of introducing a new variable to the


program.
 Syntax: dataType variableName;
 Example: int x;

Variable Assignment:

 Definition: Variable assignment is the process of giving a value to a declared


variable.
 Syntax: variableName = value;
 Example: x = 10;

Variable Initialization:

 Definition: Variable initialization is the process of assigning an initial value to a


variable at the time of declaration.
 Syntax: dataType variableName = value;
 Example: int y = 20;

Difference:
 Declaration: It announces the existence and data type of a variable.
 Assignment: It assigns a value to a variable that has already been declared.
 Initialization: It is a specific form of assignment that takes place during declaration.

3.

a. There's an error in X = X + y; because X is declared as final, and you cannot reassign a


value to a final variable.

b. There's an error in x+1 = y+1; because the left side of the expression is not a variable.

c. There are errors in z = x+y; because z is not declared, and x is not initialized.

d. There are errors in System.out.println(S1,S2); because variable names are case-sensitive in


Java, and it should be s1 and s2.

e. There's an error in String 9tails="cat"; because variable names cannot start with a digit.

f. There's an error in int this=4, that=5; because this is a keyword and cannot be used as an
identifier.

4. Trace through the following code. Make sure to write down the values of variables when
you have carried out each line.

int a = 5 + 4; // a = 9

int b = a * 2; // b = 18

int c = b / 4; // c = 4

int d = b - c; // d = 14

int e = -d; // e = -14

int f = e % 4; // f = -2

double g = 18.4; // g = 18.4

double h = g % 4; // h = 2.4 (remainder of 18.4 / 4)


int i = 3; // i = 3

int j = i++; // j = 3 (post-increment, j gets the value of i before increment)

int k = ++i; // k = 5 (pre-increment, i gets incremented before assigning to k)

5) Assume num1=5 and num2=10, trace the calculation of Boolean expression

((num1!=5)||(num2==10))&&!(num1==5) step by step.

Solve:

1. num1 != 5:
o This is false because num1 is indeed equal to 5.
2. num2 == 10:
o This is true because num2 is equal to 10.
3. (false || true):
o The result is true because it's an OR (||) operation,
and only one side needs
to be true for the overall expression to be true.
4. !(num1 == 5):
o This is true because num1 == 5 is false, and the ! (NOT) operator negates
it.
5. (true && true):
o The result is true because it's an AND (&&) operation, and both sides are true.

6) Calculate the results of the following Boolean operations

Result:

a. (3 < 5) && (5 == 4 + 1) - (true) && (true) - true

b. (3 < 5) || (6 == 5) || (3 != 3) - (true) || (false) || (false) - true

c. (5 != 10) && (3 == 2 + 1) || (4 < 2 + 5) - (true) && (true) || (true) - true

d. !(5 == 2 + 3) && !(5 + 2 != 7 - 5) - (false) && (false) - false

7) Trace the following code segments, giving the value of all variables after the segment is
executed.

a.

java
int x = 0;
int y = 1;
x = y; // x = 1
y = x; // y = 1

After this segment, x and y both have the value 1.


b.

java
int x = 10;
x = x + 10; // x = 20

After this segment, the variable x has the value 20.

c.

java
String title;
String s = "Get ";
String t = "Shorty";
title = s + t; // title = "Get Shorty"

After this segment, the variable title has the value "Get Shorty".

d.

java
int a = 5 + 4; // a = 9
int b = a * 2; // b = 18
int c = b / 4; // c = 4
int d = b - c; // d = 14
int e = -d; // e = -14
int f = e % 4; // f = -2
double g = 18.4; // g = 18.4
double h = g % 4; // h = 2.4
int i = 3; // i = 3
int j = i++; // j = 3, i = 4 (post-increment)
int k = ++i; // k = 5, i = 5 (pre-increment)

After this segment, variables have the following values:

 a = 9
 b = 18
 c = 4
 d = 14
 e = -14
 f = -2
 g = 18.4
 h = 2.4
 i = 5
 j = 3
 k = 5

You might also like