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

tutorial05_1

Uploaded by

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

tutorial05_1

Uploaded by

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

King Saud University

College of Computer & Information Science


CSC111 – Tutorial 05

Expressions, operators, conditional statement


Objectives:
After completing the following exercises, students will be able to:
 express logical statements as correct Java expressions
 use the Java if-then statement
 use the Java if-else statement
 rewrite if-else statements as two independent if-then statements

Exercise 1:
Convert each of the following phrases to a Java boolean expression as in the first example:

English expression Java expression


1 whether x is positive x > 0
2 whether x is a multiple of y
3 whether x is between -2 and 13
4 whether the difference between x and y is less than 5
5 whether x is not between 5 and 27
6 whether x has more than 4 digits
7 whether x has exactly 6 digits

Answers:
2 x % y == 0

3 x >= -2 && x <= 13

4 x - y < 5 || y – x < 5
or
x - y < 5 || x – y > -5
or
Math.abs(x - y) < 5

5 !(x >= 5 && x <= 27)


or
x < 5 || x > 27)

6 x >= 10000
or
Math.log10(x) >= 4

7 x>=100000 && x<1000000


or
Math.log10(x) == 6
Exercise 2:
Write a Java program that prompts the user to enter the width and the length for a rectangle,
then to enter the width and the length for a second rectangle, and finally it displays a message
stating which rectangle (the first or the second) has greater area. (Note: there are three cases)
Answer:
import java.util.Scanner;
class Ex2 {
public static void main(String[] args) {
Scanner KB = new Scanner(System.in);
System.out.print(“Enter length for rectangle 1: ”);
int length1 = KB.nextInt();
System.out.print(“Enter width for rectangle 1: ”);
int width1 = KB.nextInt();
System.out.print(“Enter length for rectangle 2: ”);
int length2 = KB.nextInt();
System.out.print(“Enter width for rectangle 2: ”);
int width2 = KB.nextInt();
if (length1*width1 > length2*width2)
System.out.println(Rectangle 1 has bigger area);
if (length1*width1 < length2*width2)
System.out.println(Rectangle 2 has bigger area);
if (length1*width1 == length2*width2)
System.out.println(Rectangles have same area);
}
}

Exercise 3:
Write a Java program that prompts the user to enter two positive integers, then displays whether
the first is a multiple of the second or not.
Answer:
import java.util.Scanner;
class Ex3 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.print(”Please enter the first number: );
int num1 = SC.nextInt();
System.out.print(”Please enter the second number: );
int num2 = SC.nextInt();
if (num1 % num2 == 0)
System.out.println(num1 + ” is a multiple of ” + num2);
else
System.out.println(num1 + ” is not a multiple of ” + num2);
}
}
Exercise 4:
Rewrite the following Java program replacing if-else statement with if-then statements.

import java.util.Scanner;
class Ex4 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.print(”Please enter your age: );
int age = SC.nextInt();
if (age >= 13 && age <= 60)
System.out.println(”You can proceed.”);
else
System.out.println(”Your age does not qualify you to procees”);
}
}

Answer:

import java.util.Scanner;
class Ex4 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.print(”Please enter your age: );
int age = SC.nextInt();
if (age >= 13 && age <= 60)
System.out.println(”You can proceed.”);
If (age < 13 || age > 60)
System.out.println(”Your age does not qualify you to procees”);
}
}

Exercise 5:
Trace the following two code fragments for a = +3, a = 0, a = -5, then tell whether these
fragments are equivalent or not.

if (a < 0) { if (a < 0) {
System.out.println(“Negative”); System.out.println(“Negative”);
a = a * -1; a = a * -1;
System.out.println(“Absolute System.out.println(“Absolute
value is: ” + a); value is: ” + a);
} }
else { if (a >= 0) {
System.out.println(“Positive”); System.out.println(“Positive”);
System.out.println(“Absolute System.out.println(“Absolute
value is: ” + a); value is: ” + a);
} }
Answer:

a = +3
Positive Positive
Absolute value is: 3 Absolute value is: 3

a=0
Positive Positive
Absolute value is: 0 Absolute value is: 0

a = -5
Negative Negative
Absolute value is: 5 Absolute value is: 5
Positive
Absolute value is: 5

You might also like