LAB ACTIVITY 3 Question
LAB ACTIVITY 3 Question
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;
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'.");
}
scanner.close();
}