Write a class definition (not a program, there is no main method) named Geek (saved in a file Geek.java) that models a person who is a geek. For our purposes, a geek is someone who delights in answering all sorts of questions, such as what is the sum of all numbers between two numbers?, If a number is even?, and etc. A Geek has a name and also keeps track of how many questions s/he has answered.

Your Geek class must have:

  • only two instance variables the Geeks name and number of questions asked so far
  • public Geek (String name) Sets the Geek's name and the number of questions asked
  • public String getName() takes no parameters and returns the Geeks name as a String (dont count this request in the total questions)
  • public int getNumberOfQuestions() It takes no parameters and returns as an integer how many questions s/he has been asked (dont count this request in the total)
  • public boolean isEven (int num1, int num2) It takes two integers and returns a boolean value indicating if the sum of the numbers is even or not
  • public boolean allTheSame(int x, int y, int z) Determines if the three arguments are all equal. returns true if all the same, otherwise false
  • public int sum (int num1, int num2) It takes two integers and computes and returns an integer which is the sum of all numbers between the two inclusive (include the numbers in the sum) for full credit this method should work even if the two numbers are the same (the sum is just one of the numbers) or if the first number is larger than the second. Also, you cannot assume which number will be greater.
  • public String repeat(String str, int n) Repeats a given input string. return string repeated n time
  • public int digits(int n) Determines number of digits in a number. It returns the number of digits
  • public String middle(String str) Finds the middle substring of a string, and return a string containing the middle substring. Determine if string length is odd or even if it is even print two characters if it is odd print one character

Save the Geek class in a file called Geek.java and Assignment5.java in the same folder. Assignment5.java is a test driver code for the Geek class.

Assignment5.java will ask a user to enter one of the following commands. Based on the user's choice, the program performs corresponding operation. This will be done by calling (invoking) one of the methods you defined in the Geek class. The program will terminate when the user enters 'q'.

Command Options
-----------------------------------
a: Get name
b: Num of questions asked
c: Is it Even
d: All the same
e: Sum between two integers
f: Repeat
g: Number of Digits
h: Middle of String
?: Display
q: Quit

Here is the description for each option:

a: asks for the Geek's name
b: returns the number of questions asked so far
c: asks for two integers and returns Boolean indicating if the sum is even or odd
d: asks for three positive integers and returns true if they are the same, false otherwise
e: asks for two integers and returns the sum between two integers
f: asks for a string and repeats count number of times
g. asks for an integer and counts how many digits it has
h: returns the middle character of a string if is odd, 2 characters if it is even
?: displays the menu
q: quits

Helpful hints for doing this assignment:

  • work on it in steps write one method, test it with a test driver and make sure it works before going on to the next method
  • always make sure your code compiles before you add another method
  • your methods should be able to be called in any order

Sample Output (user input is in RED):

Command Options
-----------------------------------
a: Get name
b: Num of questions asked
c: Is it Even
d: All the same
e: Sum between two integers
f: Repeat
g: Number of Digits
h: Middle of String
?: Display
q: Quit


Please enter a command or type ?
a
Eisenstein

Please enter a command or type ?
b
0

Please enter a command or type ?
c
Enter two numbers: 4 5
Sum is not even

Please enter a command or type ?
f
Enter a string: hi How many times to repeat: 4
hihihihi

Please enter a command or type ?
g
Enter a number: 5
The number 5 has 1 digits

Please enter a command or type ?
g
Enter a number: 77854
The number 77854 has 5 digits

Please enter a command or type ?
h
Enter a string: java
The middle of string "java" is av

Please enter a command or type ?
h
Enter a string: hello
The middle of string "hello" is l

Please enter a command or type ?
b
6

Please enter a command or type ?
c
Enter two numbers: 4 5
Sum is not even

Please enter a command or type ?
d
Enter 3 integers: 4 5 6
All are NOT the same

Please enter a command or type ?
d
Enter 3 integers: 4 4 4
All the same!

Please enter a command or type ?
e
Enter the first number: 5
Enter the second number: 10
The sum between 5 and 10 is 45

Please enter a command or type ?
e
Enter the first number: 10
Enter the second number: 5
The sum between 10 and 5 is 45

Please enter a command or type ?
q
Press any key to continue . . .
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.