edumeister_logo

/*------------------------------------------------showing page 1-3 out of 3

/
*------------------------------------------------------------------------
----------------------------------------
// AUTHOR:
// FILENAME: Geek.java
// SPECIFICATION: Creation and methods of the class Geek
// FOR: CSE 110- homework 5
// TIME SPENT: 2 hours and 20 minutes
//-----------------------------------------------------------------------
---------------------------------------*/
//opens class
public class Geek {
//initializes the instance variables geeksName and
numberOfQuestions
private String geeksName;
private int numberOfQuestions;
//constructor method takes in Geek's name
public Geek(String name)
{
numberOfQuestions = 0;
geeksName = name;
}
//method getName() returns Geek's name
public String getName()
{
return geeksName;
}
//method getNumberOfQuestions() returns the numbers of
questions asked specified by the assignment
public int getNumberOfQuestions()
{
return numberOfQuestions;
}
//method isEven(int) evaluates using boolean data if sum of
the entered integers is even or odd
public boolean isEven(int num1, int num2)
{
int sum=num1+num2;
if(sum%2 == 0)//if the remainder of the integer divided
by two is zero...
{
numberOfQuestions++;//adding to the count of
number of questions
return true;//the number is even
}
else//otherwise
{
numberOfQuestions++;//adding to the count of
number of questions
return false;//the number is odd
}
}
public boolean allTheSame(int x, int y, int z){
if(x==y && x==z){
return true;
}
return false;
}
//method that takes the sum of the numbers in between and
including the two entered
public int sum(int num1, int num2)
{
int summation = 0;//initializes summation variable
if(num1==num2)
{
numberOfQuestions++;//adding to the count of
number of questions
return num1;//returning one of the numbers
}
else if(num2>num1)
{
for(int i = num1; i <=num2; i++)//for loop with
condition that i starts as num1 and for
//as long as i is less than or equal to num2
i will increase
//that sums the numbers in between and
including num1 and num2
{
summation = summation + i; //adds i to
summation after each loop
}
numberOfQuestions++;//adding to the count of
number of questions
return summation;//returning the summation value
for the method
}
else//if num1 is greater than num2
{
for(int i = num2; i<=num1; i++)//essentially the
same for loop as before just with the values
//switched
{
summation = summation + i;//adds i to
summation after each loop
}
numberOfQuestions++;//adding to the count of the
number of questions
return summation;//returning the summation value
for the method
}
}
//method that repeats a given string n times in a row
public String repeat(String str, int n){
StringBuilder repeated=new StringBuilder();
for(int i=0; i<n; i++){
repeated.append(str);
}
return repeated.toString();
}
//method that determines the number of digits in an integer
public int digits(int n){
int length=String.valueOf(n).length();
return length;
}
//method that finds the middle letter of a string
//or the two middle numbers of a string
public String middle(String str){
if(str.length()%2==0){
if(str.length()>2){
return str.substring(str.length()/2-
1,str.length()/2+1);
}
}
return
str.substring(str.length()/2,str.length()/2+1);}
}