Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you will implement a class named Geek in a file named Geek.java . For our purposes, a geek is someone who delights

For this assignment, you will implement a class named Geek in a file named Geek.java. For our purposes, a geek is someone who delights in answering all sorts of questions, such as:

  • Is the number 381 evenly divisible by 3?
  • Which of these numbers is closes to Pi, 2.91 or 3.29?
  • How many times does the letter 'a' appear in the statement "an apple a day keeps the doctor away."?
  • Is the number 7 between the numbers 9 and 6?
  • What number do you get if you reverse the digits of the number 38714?

In addition to being able to answer questions like these, a Geek will keep track of their name and the number of questions he or she has been asked.

you are given the following driver file named Assignment5.java. This file contains a main method and some supporting methods. This driver file will be used to test the Geek class that you implement. Make no changes to this driver file.

Your Geek class must have only two instance variables one for the Geeks name and one for thenumberOfQuestions asked so far.

Here is a table listing and describing the methods you must implement in your Geek class.

image text in transcribed

Sample run:

image text in transcribed

Code provided in Assignment5.java -------------------------------------------- import java.util.Scanner; public class Assignment5 { public static void main(String[] args) { Scanner console = new Scanner(System.in); String menuChoice = "none"; char command = 'x'; int intInputA = 0, intInputB = 0, intInputC = 0; float floatInputA = 0.0f, floatInputB = 0.0f; String stringInput = ""; char charInput = 'x'; // instantiate an object of type Geek Geek myGeek = new Geek("Bob Smith"); displayMenu(); do { // ask a user to choose a command System.out.print(" Please enter a command: "); menuChoice = console.nextLine().toLowerCase(); command = menuChoice.charAt(0); switch (command) { case 'a': // display the Geek's name System.out.println("Geek's name: " + myGeek.getName()); break; case 'b': // Display Number of questions asked so far System.out.println("Number of questions asked so far: " + myGeek.getNumberOfQuestions()); break; case 'c': // Is number evenly divisible by 3 System.out.print("Enter an integer: "); intInputA = Integer.parseInt(console.nextLine()); if (myGeek.divisibleBy3(intInputA)) System.out.println(intInputA + " is evenly divisible by 3"); else System.out.println(intInputA + " is not evenly divisible by 3"); break; case 'd': // Which of two numbers is closer to Pi System.out.print("Enter the first number: "); floatInputA = Float.parseFloat(console.nextLine()); System.out.print("Enter the second number: "); floatInputB = Float.parseFloat(console.nextLine()); System.out.println(myGeek.closerToPi(floatInputA, floatInputB) + " is closer to Pi"); break; case 'e': // Count number of times a letter appears in a string System.out.print("Enter a statement: "); stringInput = console.nextLine(); System.out.print("Enter a letter to search for: "); charInput = console.nextLine().charAt(0); myGeek.countIn(stringInput, charInput); System.out.println(charInput + " appears " + myGeek.countIn(stringInput, charInput) + " times"); break; case 'f': // Is number between two other numbers System.out.println("Enter three integers: "); intInputA = Integer.parseInt(console.nextLine()); intInputB = Integer.parseInt(console.nextLine()); intInputC = Integer.parseInt(console.nextLine()); if (myGeek.isBetween(intInputA, intInputB, intInputC)) System.out.println(intInputB + " is between " + intInputA + " and " + intInputC); else System.out.println(intInputB + " is not between " + intInputA + " and " + intInputC); break; case 'g': // Reverse a number System.out.print("Enter an integer: "); intInputA = Integer.parseInt(console.nextLine()); System.out.println("The digits in reverse are " + myGeek.reverseNumber(intInputA)); break; case '?': // Display the menu displayMenu(); break; case 'q': // quit the progem break; default: // invalid choice System.out.println("Invalid input!!!"); } // end of switch } while (command != 'q'); } // end of the main method public static void displayMenu() { System.out.println("Command Options"); System.out.println("-----------------------------------"); System.out.println("a) Display Geek's name"); System.out.println("b) Display Number of questions asked so far"); System.out.println("c) Is number evenly divisible by 3"); System.out.println("d) Which of two numbers is closer to Pi"); System.out.println("e) Count number of times a letter appears in a string"); System.out.println("f) Is number between two other numbers"); System.out.println("g) Reverse a number"); System.out.println("?) Display this menu"); System.out.println("q) Quit"); } // end of the displayMenu method } // end of class

-----------------------------------------------------------------------------------

Method Description This constructor method will take a String argument with the name of a new Geek. This method must initialize the Geek's name to the value of this argument, and will initialize the Geek's numberOfQuestions to 0 public Geek(String name This method will return a copy of the value stored in the Geek' name variable This method will return a copy of the value stored in the Geek' numberOfQuestions variable This method will return the boolean value true if the argument is evenly divisible by 3. Other wise the method will return false. This method will also increment the value of the numberOfQuestions variable by one This method will return the the value of the first argument if the first argument is closer to 3.1415927 than the second argument value. Other wise the method will return the second argument value. This method will also increment the value of the numberOfQuestions variable by one this method will count the number of times that the char letter appears in the String statement. The method will return that count. This method will also increment the value of the numberOfQuestions variable by one This method will return the boolean value true if the argument value b is numerically between the values of a and c. Other wise the method will return false. This method will also increment the value of the numberOfQuestions variable by one This method will return an int where the digits from the argument number are reversed. For example, called with the argument value 43125, this method would return the int 52134. This method will also increment the value of the numberOfQuestions variable by one public String getName0 public int getNumberOfQuestions0 public boolean divisibleBy3(int value) public float closerToPi(float value1, float value2) public int countin(String statement, char letter) public boolean isBetween(int a, int b, int c) public int reverseNumber(int number) Connand Options a) Display Geek's nane b) Display Nunber of questions asked so far c) Is number evenly divisible by 3 d) Which of tuo numbers is closer to Pi e) Count nunber of tines a letter appears in a strine f) Is number bet ween two other numbers ) Reverse a number Display this nenu q) Quit Please enter a conmand: a Geek 's nane: Bob Snith Please enter a conmand: D Number of questions asked so far: e Please enter a conmand: c Enter an integer: 123 123 is evenly divisible by 3 Please enter a conmand: c Enter an integer: 124 124 is not evenly divisible by3 Please enter a conmand: d Enter the first number: 1.11 Enter the second number: 9.99 1.11 is closer to Pi Please enter a conmand: e Enter a statenent: over the river, and through the wood Enter a letter to search for: o o appears 4 tines Please enter a conmand: f Enter three integers: 2 is between 1 and 3 Please enter a conmand: f Enter three integers: 2 is betueen 1 and 3 Please enter a connand: f Enter three integers: 5 is not between 1 and 3 Please enter a connand: g Enter an integer: 123456 The digits in reverse are 654321 Please enter a connand: ? Connand Options a) Display Geek 's nane b) Display Nunber of questions asked so far c) Is nunber evenly divisible by 3 d) Which of two nunbers is closer to Pi e) Count nunber of tines a letter appears in a strine f) Is number between tuo other numbers g) Reverse a nunber ) Display this menu q) Quit Please enter a connand

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

Summarise the scope of HRM and the key HRM functions

Answered: 1 week ago