LAB ACTIVITY 3
IAS1313 | OBJECT ORIENTED PROGRAMMING
TOPIC: SELECTION
NAME : Mohamad Hamizan PHONE : 0173547356
MATRIC NO. : 4231002051 EMAIL : hamizanozair@gmail.com
INSTRUCTION: Answer ALL questions. Screenshot the output if you can.
QUESTION 1
Write a Java program that asks the user to enter an integer. If the number is even, print “The
number is even”.
Answer:
QUESTION 2
Write a Java program that asks the user to enter a boolean value. If the value is true, print “The
value is true”.
Answer:
QUESTION 3
Write a Java program that asks the user to enter a character. If the character is ‘A’, print “The
character is A”.
Answer:
QUESTION 4
Write a Java program that asks the user to enter a float. If the float is greater than 0, print “The
number is positive”. Else, print “The number is not positive”.
Answer:
QUESTION 5
Write a Java program that asks the user to enter a string. If the string is “Hello”, print “Hello,
world!”. Else, print “Goodbye, world!”.
Answer:
QUESTION 6
Write a Java program that asks the user to enter a double. If the double is less than 0, print “The
number is negative”. Else, print “The number is not negative”.
Answer:
QUESTION 7
Write a Java program that asks the user to enter an integer representing a day of the week (1
for Monday, 2 for Tuesday, etc.). Use a switch-case statement to print the name of the day.
Answer:
QUESTION 8
Question: Write a Java program that asks the user to enter a character representing a grade
(A, B, C, D, or F). Use a switch-case statement to print a message based on the grade.
Answer:
QUESTION 9
Write a Java program that asks the user to enter a string representing a color (red, blue, green,
etc.). Use a switch-case statement to print a message based on the color.
Answer:
QUESTION 10
Create a text-based Rock, Paper, Scissors game. The user should enter “rock”, “paper”, or
“scissors”. The program will randomly select one of these three options and then determine the
winner based on the rules of the game.
Answer:
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("Welcome to Rock, Paper, Scissors!");
while (true) {
System.out.print("Enter 'rock', 'paper', or 'scissors': ");
String userChoice = scanner.nextLine().toLowerCase();
if (userChoice.equals("rock") || userChoice.equals("paper") ||
userChoice.equals("scissors")) {
String computerChoice = generateComputerChoice();
System.out.println("You chose " + userChoice + ".");
System.out.println("Computer chose " + computerChoice + ".");
String result = determineWinner(userChoice, computerChoice);
System.out.println(result);
} else {
System.out.println("Invalid choice. Please enter 'rock',
'paper', or 'scissors'.");
}
System.out.print("Do you want to play again? (yes/no): ");
String playAgain = scanner.nextLine().toLowerCase();
if (!playAgain.equals("yes")) {
System.out.println("Thanks for playing! Goodbye.");
break;
}
}
scanner.close();
}
private static String generateComputerChoice() {
String[] choices = {"rock", "paper", "scissors"};
int randomIndex = new Random().nextInt(choices.length);
return choices[randomIndex];
}
private static String determineWinner(String userChoice, String
computerChoice) {
if (userChoice.equals(computerChoice)) {
return "It's a tie!";
} else if (
(userChoice.equals("rock") &&
computerChoice.equals("scissors")) ||
(userChoice.equals("paper") && computerChoice.equals("rock"))
||
(userChoice.equals("scissors") &&
computerChoice.equals("paper"))
) {
return "You win!";
} else {
return "Computer wins!";
}
}
}