In: Computer Science
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings, and improve your skills with for loops. The methods you will write will be part of a short trivia game, feel free to try it out after you finish.
import java.util.Scanner;
public class Trivia {
//TODO: isLeapYear
//TODO: isPrime
//TODO: aWord
//TODO: reverse
public static void main(String[] args){
Scanner answers = new Scanner(System.in);
int score = 0;
System.out.println("What year is a Leap Year?");
// if(isLeapYear(answers.nextInt())){
// System.out.println("Correct!");
// score++;
// }
// else{
// System.out.println("Incorrect");
// }
// System.out.println("What is a prime number between
100-300?");
// if(isPrime(answers.nextInt())){
// System.out.println("Correct!");
// score++;
// }
// else{
// System.out.println("Incorrect");
// }
// System.out.println("What is a five letter word with an 'a' in
the middle?");
// if(aWord(answers.next())){
// System.out.println("Correct!");
// score++;
// }
// else{
// System.out.println("Incorrect");
// }
// System.out.println("What word is a palindrome?");
// if(reverse(answers.next())){
// System.out.println("Correct!");
// score++;
// }
// else{
// System.out.println("Incorrect");
// }
if (score == 0){
System.out.println("Oof, better luck next time");
}
else{
System.out.println("Nice! You got " + score + " out of 4
right!");
}
}
}
Program output displayed here
What year is a Leap Year? Oof, better luck next time
Hi. I have answered this same question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
EDIT: For some reason, I'm unable to reply in comments. Some technical error. So I'll respond here. The problem is not with the code, you must input two integers and two strings exactly in that order. If the input is non numeric or if there is no input, exception will be thrown. If you are using automated input, or using an online compiler, type two integers followed by two strings in the input field.
// Trivia.java
import java.util.Scanner;
public class Trivia {
public static boolean isLeapYear(int year) {
// returning true if (year is divisible by 4 but not by 100) OR
// divisible by both 100 and 400
return (year % 4 == 0 && year % 100 != 0)
|| (year % 100 == 0 && year % 400 == 0);
}
public static boolean isPrime(int num) {
// if num is out of range, returning false
if (num < 100 || num > 300) {
return false;
}
// looping from 2 to num/2, checking if num is evenly divided by any of
// the value
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return false; // not prime.
}
}
return true; // prime
}
public static boolean aWord(String word) {
// returning true if word has length=5 and 'a' on the middle
return word.length() == 5 && word.charAt(2) == 'a';
}
public static boolean reverse(String word) {
String reversed = "";
// appending characters in reverse order into reversed.
for (int i = word.length() - 1; i >= 0; i--)
reversed += word.charAt(i);
// returnining true if word and reversed are equal
return word.equals(reversed);
}
public static void main(String[] args) {
Scanner answers = new Scanner(System.in);
int score = 0;
System.out.println("What year is a Leap Year?");
if (isLeapYear(answers.nextInt())) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect");
}
System.out.println("What is a prime number between 100-300?");
if (isPrime(answers.nextInt())) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect");
}
System.out
.println("What is a five letter word with 'a' in the middle?");
if (aWord(answers.next())) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect");
}
System.out.println("What word is a palindrome?");
if (reverse(answers.next())) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect");
}
if (score == 0) {
System.out.println("Oof, better luck next time");
} else {
System.out.println("Nice! You got " + score + " out of 4 right!");
}
}
}
/*OUTPUT*/
What year is a Leap Year?
2012
Correct!
What is a prime number between 100-300?
107
Correct!
What is a five letter word with 'a' in the middle?
craft
Correct!
What word is a palindrome?
malayalam
Correct!
Nice! You got 4 out of 4 right!