import java.util.*; public class Assignment { public static void main(String[] a
ID: 3550514 • Letter: I
Question
import java.util.*;
public class Assignment
{
public static void main(String[] args)
{
Scanner console = new Scanner (System.in);
String choice;
char command;
printMenu();//print the menu
Geek myGeek = new Geek("Geek",0);
do
{
//ask user to choose command
System.out.println(" Please enter a command or type.");
choice = console.next().toLowerCase();
command = choice.charAt(0);
switch(command)
{
case 'a'://prints geeks name
System.out.println("
break;
case 'b':
System.out.println("Number of questions: " + myGeek.getNumberOfQuestions());
break;
case 'c'://asks for two integers and finds and prints if their sum is odd or even
break;
case 'd'://asks for two integers and finds and prints the sum of all integers between them (inclusive)
break;
case 'e'://asks for year and finds out whether it is a leap year or not
break;
case '?':
printMenu();
break;
case 'q':
break;
default:
System.out.println("Invalid input");
}
} while(command != 'q');
}//end of main method
public static void printMenu()
{
System.out.println(" Command Options "
+ "------------------------------------ "
+ "a: get name "
+ "b: number of questions asked "
+ "c: sum is even "
+ "d: sum between two integers "
+ "e: leap year "
+ "?: display menu again "
+ "q: quit this program ");
}//end of print menu method
}//end Assignment5 class
public class Geek
{
private String geekName;
private static int NumberOfQuestions;
public Geek(String name, int numQuestions)
{
geekName = name;
NumberOfQuestions = numQuestions;
}
public String getName()
{
return geekName;
}
public int getNumberOfQuestions()
{
return NumberOfQuestions;
}
public boolean isEven(int num1, int num2)
{
boolean even;
if((num1 + num2) %2 == 0)
even = true;
else
even = false;
return even;
}
public int sum(int num1, int num2)
{
NumberOfQuestions++;
if (num1 == num2)
{
return num2;
}
int total = 0;
while(num1 <= num2)
{
total += num1;
num1++;
}
return total;
}
public boolean leapYear (int year)
{
//It takes an integer and returns a boolean value indicating
//if the year is a leap year. A leap year is one with 366 days.
//A year is a leap year if
//? it is divisible by 4 (for example, 1980),
//? except that it is not a leap year if it is divisible by
//100 (for example 1900);
//? however, it is a leap year if it is divisible by 400
//for example, 2000).
//There were no exceptions before the introduction of the
//Gregorian calendar on October 15, 1582 (for example,
//1500 was a leap year). You may NOT use Javaâs
//GregorianCalendar class.
}
}
Explanation / Answer
/* OUTPUT
Command Options
------------------------------------
a: get name
b: number of questions asked
c: sum is even
d: sum between two integers
e: leap year
?: display menu again
q: quit this program
Please enter a command or type.
a
Name is Geek
Please enter a command or type.
b
Number of questions: 0
Please enter a command or type.
c
Enter two integers num1 and num2
4 5
sum of numbers 4 and 5 is Odd
Please enter a command or type.
2 9
Invalid input
Please enter a command or type.
Invalid input
Please enter a command or type.
d
Enter two integers num1 and num2
2 9
sum of numbers from 2 to 9 is 44
Please enter a command or type.
e
Enter an year to find leap year or not ?
2000
2000 is Leap Year
Please enter a command or type.
?
Command Options
------------------------------------
a: get name
b: number of questions asked
c: sum is even
d: sum between two integers
e: leap year
?: display menu again
q: quit this program
Please enter a command or type.
q
BUILD SUCCESSFUL (total time: 34 seconds)
*/
import java.util.*;
public class Assignment
{
public static void main(String[] args)
{
Scanner console = new Scanner (System.in);
String choice;
char command;
printMenu();//print the menu
Geek myGeek = new Geek("Geek",0);
do
{
//ask user to choose command
System.out.println(" Please enter a command or type.");
choice = console.next().toLowerCase();
command = choice.charAt(0);
switch(command)
{
case 'a'://prints geeks name
System.out.println("Name is " + myGeek.getName());
break;
case 'b':
System.out.println("Number of questions: " + myGeek.getNumberOfQuestions());
break;
case 'c'://asks for two integers and finds and prints if their sum is odd or even
{
int num1,num2;
System.out.println("Enter two integers num1 and num2 ");
num1 = console.nextInt();
num2 = console.nextInt();
System.out.println("sum of numbers "+ num1 + " and " + num2 + " is " +(myGeek.isEven(num1, num2)?"Even":"Odd"));
}
break;
case 'd'://asks for two integers and finds and prints the sum of all integers between them (inclusive)
{
int num1,num2;
System.out.println("Enter two integers num1 and num2 ");
num1 = console.nextInt();
num2 = console.nextInt();
System.out.println("sum of numbers from "+ num1 + " to " + num2 + " is " +myGeek.sum(num1, num2));
}
break;
case 'e'://asks for year and finds out whether it is a leap year or not
{
int year;
System.out.println("Enter an year to find leap year or not ? ");
year = console.nextInt();
System.out.println(year + " is "+(myGeek.leapYear(year)?"Leap Year":"Not a Leap Year"));
}
break;
case '?':
printMenu();
break;
case 'q': System.exit(0);
break;
default:
System.out.println("Invalid input");
}
} while(command != 'q');
}//end of main method
public static void printMenu()
{
System.out.println(" Command Options "
+ "------------------------------------ "
+ "a: get name "
+ "b: number of questions asked "
+ "c: sum is even "
+ "d: sum between two integers "
+ "e: leap year "
+ "?: display menu again "
+ "q: quit this program ");
}//end of print menu method
}//end Assignment5 class
public class Geek
{
private String geekName;
private static int NumberOfQuestions;
public Geek(String name, int numQuestions)
{
geekName = name;
NumberOfQuestions = numQuestions;
}
public String getName()
{
return geekName;
}
public int getNumberOfQuestions()
{
return NumberOfQuestions;
}
public boolean isEven(int num1, int num2)
{
boolean even;
if((num1 + num2) %2 == 0)
even = true;
else
even = false;
return even;
}
public int sum(int num1, int num2)
{
NumberOfQuestions++;
if (num1 == num2)
{
return num2;
}
int total = 0;
while(num1 <= num2)
{
total += num1;
num1++;
}
return total;
}
public boolean leapYear (int year)
{
//It takes an integer and returns a boolean value indicating
//if the year is a leap year. A leap year is one with 366 days.
//A year is a leap year if
//? it is divisible by 4 (for example, 1980),
//? except that it is not a leap year if it is divisible by
//100 (for example 1900);
//? however, it is a leap year if it is divisible by 400
//for example, 2000).
//There were no exceptions before the introduction of the
//Gregorian calendar on October 15, 1582 (for example,
//1500 was a leap year). You may NOT use Javaâs
//GregorianCalendar class.
return (year%400 ==0 || (year%100 != 0 && year%4 == 0));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.