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

For this lab, you will be writing method definitions. These method definitions will be based on...
For this lab, you will be writing method definitions. These method definitions will be based on method calls I have already written for you. Do all of the following steps in order. DO NOT move onto the next step until the previous one is complete. 1. Download the starting file called Lab10.java. 2. Within this file, you will find a completely written main method. Please DO NOT modify this code. Do read the code and familiarize yourself with what it...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
Writing a Modular Program in Python In this lab, you add the input and output statements...
Writing a Modular Program in Python In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared...
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...
The aim of this lab session is to make you familiar with implementing a linked list....
The aim of this lab session is to make you familiar with implementing a linked list. In this lab you will complete the partially implemented LinkedIntegerList. Specifically, you will implement the following methods. • public boolean remove(int value) • public int removeAt(int index) • public boolean set(int index, int value) • public int indexOf(int value) Task-1 Add a new class named Lab3Tester class to test the current implementation of the linked list. (Note: you may reuse the code from SimpleIntegerListTester...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT