Question

In: Computer Science

For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings,...

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

Solutions

Expert Solution

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!

Related Solutions

using mysql and the schema is provided below. thanks In this lab, you will be manipulating...
using mysql and the schema is provided below. thanks In this lab, you will be manipulating the database to add, delete and modify the values in the database. Please use a "select * from..." after each query to show the effects of your data manipulation query. 1. The title 'Time Flies' now has a new track, the 11th track 'Spring', which is 150 seconds long and has only a MP3 file. Insert the new track into Tracks table (Don’t hand-code...
Data Manipulation In this lab, you will be manipulating the database to add, delete and modify...
Data Manipulation In this lab, you will be manipulating the database to add, delete and modify the values in the database. Please use a "select * from..." after each query to show the effects of your data manipulation query. 1. The title 'Time Flies' now has a new track, the 11th track 'Spring', which is 150 seconds long and has only a MP3 file. Insert the new track into Tracks table (Don’t hand-code any data for insert that can be...
Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and...
Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and call it parks. Read in the names of national parks from the user until the user enters done(or DONE, or dOnE, .. ) Keep in mind, that the names of some national parks consist of more than one word, for example, Mesa Verde. As you read in the national parks, add them to the list. Next, we are going to build a string based...
Pre-lab Activities Practice the Scientific Method Step 1. Observation. Write down something surprising that you noticed...
Pre-lab Activities Practice the Scientific Method Step 1. Observation. Write down something surprising that you noticed this week and you don’t understand. NOTE: if you can’t think of anything interesting that happened this week, you may use an example from any part of your life. EXAMPLE: This morning I woke up and there was a dead raccoon on my front doorstep! Observation(s): This morning I woke up and the tree in me from the yard had fallen over. Step 2...
for loop practice – Taylor Series!The moment you finish implementing the trigonometric functions, you realize that...
for loop practice – Taylor Series!The moment you finish implementing the trigonometric functions, you realize that you have forgotten your favorite function: the exponential! The Taylor Series for the exponential function is 푒푥=1+푥1!+푥22!+푥33!+...,‒∞<푥<∞a) Using a for loop, compute and display the error of the Taylor series when using 12 and 15 terms, each as compared to the exponential function when evaluated at x = 2. b) Can you figure out how to compute and display the number of terms necessary...
Writing a business plan is your map to how you are going to operate. Regardless of...
Writing a business plan is your map to how you are going to operate. Regardless of whether you are interested in taking over an existing business or starting a new one; they both have challenges that you will need to overcome in order to succeed. Discuss one of the challenges in starting a new business and how you may be able to address that challenge with a well-designed business plan. Your challenge can be unique to either a start-up or...
The Problem Statement In this lab section, you are going to learn how to sort an...
The Problem Statement In this lab section, you are going to learn how to sort an array of integers, and to sort an array of objects. We are going to divide the work into two parts. Part 1. Sorting an array of integers using selection sort In this part, you are given the code (see List 1) for sorting an array of integers into ascending order. The sorting method used is the selection sort. You can cut-and-paste the code into...
Process: In this lab, you will be implementing a simple “Bop-it!”-like game. Your program will run...
Process: In this lab, you will be implementing a simple “Bop-it!”-like game. Your program will run for a single game. Start with implementing a “start menu”, output a line asking the user to push a keyboard key to start and wait for the user to push a key. The game will involve printing a line telling the user which key to press (chosen randomly by the program) and will wait a certain time for a response. After each successful action...
In this lab, you will practice the use of array by building an simple maze game,...
In this lab, you will practice the use of array by building an simple maze game, where your task is to control the prince to defeat the monster and save Snow White. You have to work based on the given skeleton code. Game Description The Snow White is detained in the maze by the monster, who needs the prince's help to rescure her out from the exit. The pre-set maze is shown below, with width 11 and height 8. The...
Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT