Here is the file In Java Write a class definition not a pro

Here is the file :-

In Java,

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?”, “what is a prime number?”, among other things. 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 Geek’s name and number of questions asked so far

Methods

Save the Geek class in a file called Geek.java and use the following program stored in Assignment5.java, which has the main method to create new Geek object and to test the methods in the class Geek. A sample output is shown below

Important Notes: Your class should have exactly the method headers that are described or otherwise your class will not work with the test driver program (Assignment5.java) that is provided. You should never change the test driver program if the test driver is provided but instead make changes to Geek class to make it work.

Helpful hints for doing this assignment:

Description of the methods

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

Assignment5.java will ask a user to enter one of the following commands. Based on the user\'s choice, the program needs to perform corresponding operation. This will be done by using a method you defined in the Geek class. The program will terminate when the user enters \'q\'.

Here is a sample output:

Command Options -----------------------------------
a: get name
b: number of questions asked c: is Odd
d: reverse
e: factorial
f: is Alphabetic
g: smallest
h: Is prime
?: display the menu again
q: quit this program

Please enter a command or type ? a
Albert Einstein

Please enter a command or type ? b
0

Please enter a command or type ? c
Enter a number: 5
5 is odd

Please enter a command or type ? d
Enter a String: Java
avaJ

Please enter a command or type ? e
Enter an integer: 4
4! is: 24

Please enter a command or type ? f
Enter a character:
H

It is an alphabet

Please enter a command or type ? b
4

Please enter a command or type ? i
Invalid input

Please enter a command or type ? h
Enter an integer:
56

56 is not prime

Please enter a command or type ? q

Description of the methods

  the Geek\'s name, the number of questions is  assigned to zero  
  takes no parameters and returns the Geeks’s  name as a String (don’t count this request  in the total questions)  
  takes no parameters and returns as an int  how many questions has been asked (don’t  count this request in the total)  
  takes an integer and returns a boolean  value indicating if the num is odd or not  
  takes a String parameter and returns a  string reverse of it. For example  reverse(“Java”) should return “avaJ”  
  takes an integer and computes the factorial  of it. The factorial of a positive integer  n (written n!) is equal to the product of  the positive integers from 1 to n.  
  takes a character parameter and return true  if that character is either uppercase or  lowercase alphabetic letter  
  takes three integer parameters and returns  the smallest  
  Takes an integer and returns true if the  number is prime and false otherwise. A  Prime Number can be divided evenly only by  1, or itself. And it must be a whole number  greater than 1.  

Solution

Answer:

Note: Geek.java is not given. Hence I have used “geeksName” to represent Geek’s name and “noq” to represent the number of questions asked.

Methods definition:

//constructor

public Geek (String name)

{

     //set geek\'s name

     geeksName=name;

     //set no. of questions

     noq=0;

}

//Get name

public String getName()

{

     //return geeks name

     return geeksName;

}

//Get no. of ques asked

public int getNumberOfQuestions()

{

     //return no. of questions asked

     return noq;

}

//Method check given number is odd

public boolean isOdd (int num)

{

     //Increment noq

     noq++;

     //check remainder is 1 when num is dvided by 2

     if(num%2==1)

          //Return true

          return true;

     //otherwise return false

     return false;

}

//Method to reverse a text

public String reverse(String text)

{

     //imcrement noq

     noq++;

     //declare empty string-variable

     String rev=\"\";

     //Process the text

     for(int kk=text.length()-1;kk>=0;kk--)

          //Add character to rev

          rev+=text.charAt(kk);

     //return rev

     return rev;

}

//ethod find num!

public int factorial(int num)

{

     //increment the noq

     noq++;

     //default value

     int ft=1;

     //Loop to find num!

     for(int kk=1;kk<=num;kk++)

     {

          //find factorial

          ft=ft*kk;

     }

     //return ft;

     return ft;

}

//method check letter is alphabet

public boolean isAlpha (char letter)

{

     //increment the noq

     noq++;

     //Check letter is uppercase

     if((letter>=\'A\') && (letter<=\'Z\'))

          //If so return true

          return true;

     //otherwise, check letter is lowercase

     else if((letter>=\'a\') && (letter<=\'z\'))

          //If so, return true

          return true;

     //otherwise return false

     return false;

}

//method return smallest of num1, num2, num3

public int smallest(int num1, int num2, int num3)

{

     //increment noq

     noq++;

     //check num1 is smallest

     if((num1<num2) &&(num1<num3))

          //if so, num1 is smallest

          return num1;

     //check num2 is smallest

     if((num2<num1) && (num2<num3))

          //If so return num2

          return num2;

     //otherwise return num3

     return num3;

}

//Method check num is prime

public boolean isPrime(int num)

{

     //increment the noq

     noq++;

     //Divide nu from 2 to num-1

     for(int kk=2;kk<num;kk++)

     {

          //If num is divided by kk then

        if(num%kk==0)

              //return false

                   return false;

   }

     //otherwise return true

    return true;

}

Complete class: Geek.java

public class Geek

{

String geeksName;

int noq;

//constructor

public Geek (String name)

{

     //set geek\'s name

     geeksName=name;

     //set no. of questions

     noq=0;

}

//Get name

public String getName()

{

     //return geeks name

     return geeksName;

}

//Get no. of ques asked

public int getNumberOfQuestions()

{

     //return no. of questions asked

     return noq;

}

//Method check given number is odd

public boolean isOdd (int num)

{

     //Increment noq

     noq++;

     //check remainder is 1 when num is dvided by 2

     if(num%2==1)

          //Return true

          return true;

     //otherwise return false

     return false;

}

//Method to reverse a text

public String reverse(String text)

{

     //imcrement noq

     noq++;

     //declare empty string-variable

     String rev=\"\";

     //Process the text

     for(int kk=text.length()-1;kk>=0;kk--)

          //Add character to rev

          rev+=text.charAt(kk);

     //return rev

     return rev;

}

//ethod find num!

public int factorial(int num)

{

     //increment the noq

     noq++;

     //default value

     int ft=1;

     //Loop to find num!

     for(int kk=1;kk<=num;kk++)

     {

          //find factorial

          ft=ft*kk;

     }

     //return ft;

     return ft;

}

//method check letter is alphabet

public boolean isAlpha (char letter)

{

     //increment the noq

     noq++;

     //Check letter is uppercase

     if((letter>=\'A\') && (letter<=\'Z\'))

          //If so return true

          return true;

     //otherwise, check letter is lowercase

     else if((letter>=\'a\') && (letter<=\'z\'))

          //If so, return true

          return true;

     //otherwise return false

     return false;

}

//method return smallest of num1, num2, num3

public int smallest(int num1, int num2, int num3)

{

     //increment noq

     noq++;

     //check num1 is smallest

     if((num1<num2) &&(num1<num3))

          //if so, num1 is smallest

          return num1;

     //check num2 is smallest

     if((num2<num1) && (num2<num3))

          //If so return num2

          return num2;

     //otherwise return num3

     return num3;

}

//Method check num is prime

public boolean isPrime(int num)

{

     //increment the noq

     noq++;

     //Divide nu from 2 to num-1

     for(int kk=2;kk<num;kk++)

     {

          //If num is divided by kk then

        if(num%kk==0)

              //return false

                   return false;

    }

     //otherwise return true

    return true;

}

}

Sample Output:

sh-4.3$ javac Assignment5.java                                                                                                       

sh-4.3$ java -Xmx128M -Xms16M Assignment5

Command Options                                                                                                                    

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

a: get name                                                                                                                         

b: number of questions asked                                                                                                       

c: is Odd                                                                                                                           

d: reverse                                                                                                                         

e: factorial                                                                                                                        

f: is Alphabetic                                                                                                                   

g: smallest                                                                                                                         

h: Is prime                                                                                                                        

?: display the menu again                                                                                                           

q: quit this program                                                                                                               

                                                                                                                                    

                                                                                                                                   

Please enter a command or type ?                                                                                                    

a                                                                                                                                  

Albert Einstein                                                                                                                     

                                                                                                                                   

Please enter a command or type ?                                                                                                    

b                                                                                                                                  

0                                                                                                                                   

                                                                                                                                   

Please enter a command or type ?                                                                                                   

c                                                                                                                                  

Enter a number: 543                                                                                                                

543 is odd                                                                                                                         

                                                                                                                                    

Please enter a command or type ?                                                                                                    

d                                                                                                                                  

Enter a String: Java                                                                                                               

avaJ                                                                                                                                

                                                                                                                                   

Please enter a command or type ?                                                                                                    

e                                                                                                                                  

Enter an integer: 3                                                                                                                 

3! is: 6                                                                                                                           

                                                                                                                                    

Please enter a command or type ?                                                                                                   

f                                                                                                                                   

Enter a character:                                                                                                                 

4                                                                                                                                   

It is NOT an alphabet       

Please enter a command or type ?                                                                                                   

g                                                                                                                                   

Enter 3 integers:                                                                                                                  

2                                                                                                                                   

-1                                                                                                                                 

5                                                                                                                                   

The smallest integer is: -1                                                                                                        

                                                                                                                                    

Please enter a command or type ?                                                                                                    

h                                                                                                                                  

Enter an integer:                                                                                                                   

153                                                                                                                                

153 is not prime                                                                                                                    

                                                                                                                                   

Please enter a command or type ?                                                                                                    

q   

Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i
Here is the file :- In Java, 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 i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site