******* I need the methods and cases code completed in JAVA. I was able to get c
ID: 3566621 • Letter: #
Question
******* I need the methods and cases code completed in JAVA. I was able to get case 1 and 5 completed, but I still need help with case 2-4. Complete the cases and methods code for 2-4 in JAVA. The instructions to complete them have comments where the code to the cases are suppose to go. **********
import java.util.Scanner;
import java.util.Random;
public class MethodsLab
{
public static void main( String[] args )
{
// declare variables
int option;
int num1;
int num2;
char letter;
int twoNumbers;
Scanner reader = new Scanner( System.in );
do
{
System.out.print( "*********METHOD MENU********* " +
"1. powers of 2 " +
"2. sum of range " +
"3. evenly divisible " +
"4. random in range " +
"5. is alphabetic? " +
"6. quit " +
"Enter option: " );
option = reader.nextInt();
switch( option )
{
case 1: // call void method that will display the first 10 powers of 2
powers2(); // method call
break;
case 2: // 1. prompt user for two numbers in main().
// 2. call void method that will calculate the sum of all numbers (inclusive)
// between the two numbers.
// 3. pass the two numbers into the method with parameters
// 4. the method should display the result
System.out.println("Enter a number to start with ");
num1 = reader.nextInt();
System.out.println("Enter a number to end with ");
num2 = reader.nextInt();
System.out.println(" The Sum is: " + (num1 + num2));
twoNumbers = reader.nextInt();
break;
case 3: // 1. prompt the user for two numbers in main().
// 2. call boolean returning method that will determine if the first number is
// evenly divisible by the second number.
// 3. pass the two numbers into the method with parameters
// 4. display an appropriate message based on the result in main()
break;
case 4: // 1. prompt the user for two numbers in main()
// 2. call an integer returning method that will generate a random number between
// the two values (inclusive)
// 3. pass the two numbers into the method with parameters
// 4. display the random number in main()
break;
case 5: // 1. prompt the user for a character value.
// NOTE: You will need to perform a "dummy read" to remove the previous
// ENTER key from the input buffer before reading the character value.
// 2. call a boolean returning method that will true if the character is alphabetic.
// 3. pass the character into the method as a parameter
// 4. display an appropriate message based on the result in main()
System.out.print( "Enter a key: " );
reader.nextLine(); // skip enter key
letter = reader.nextLine().charAt(0); //read character
if( isAlpha(letter) == true )
System.out.println( letter + " is alphabetic." );
else
System.out.println( letter + " is not alphabetic." );
break;
case 6:
break;
default: System.out.println( " ERROR - choose an option 1-6. Try again. ");
}
}while( option != 6 );
} // end of main
//----------------------------------METHOD DECLARATIONS-------------------------------------------
// display powers of 2
public static void powers2()
{
int powers2 = 2;
for(int x = 0; x <= 9; x++)
{
powers2 = (int)Math.pow(2,x);
System.out.println( "Enter a integer" + powers2);
}
}
public static void twoNumbers( int num1, int num2 )
{
int twoNumbers;
twoNumbers = num1 + num2;
}
// determine if a character is an alphabetic letter
public static boolean isAlpha( char letter )
{
boolean alpha;
if( letter >= 'A' && letter <= '2' || letter >= 'a' && letter <= 'z')
alpha = true;
else
alpha = false;
return alpha;
}
} // end of class
Explanation / Answer
===============================================
Program
===============================================
import java.util.Scanner;
import java.util.Random;
public class MethodsLab
{
public static void main( String[] args )
{
// declare variables
int option;
int num1;
int num2;
char letter;
Scanner reader = new Scanner( System.in );
do
{
System.out.print( "*********METHOD MENU********* " +
"1. powers of 2 " +
"2. sum of range " +
"3. evenly divisible " +
"4. random in range " +
"5. is alphabetic? " +
"6. quit " +
"Enter option: " );
option = reader.nextInt();
switch( option )
{
case 1: // call void method that will display the first 10 powers of 2
powers2(); // method call
break;
case 2: // 1. prompt user for two numbers in main().
// 2. call void method that will calculate the sum of all numbers (inclusive)
// between the two numbers.
// 3. pass the two numbers into the method with parameters
// 4. the method should display the result
System.out.println("Enter a number to start with ");
num1 = reader.nextInt();
System.out.println("Enter a number to end with ");
num2 = reader.nextInt();
//Calling method
twoNumbers(num1,num2);
break;
case 3: // 1. prompt the user for two numbers in main().
// 2. call boolean returning method that will determine if the first number is
// evenly divisible by the second number.
// 3. pass the two numbers into the method with parameters
// 4. display an appropriate message based on the result in main()
System.out.println("Enter first number ");
num1 = reader.nextInt();
System.out.println("Enter second number");
num2 = reader.nextInt();
if(isDivisible(num1,num2))
System.out.println("First number is evenly divisible by second number");
else
System.out.println("First number is not evenly divisible by second number");
break;
case 4: // 1. prompt the user for two numbers in main()
// 2. call an integer returning method that will generate a random number between
// the two values (inclusive)
// 3. pass the two numbers into the method with parameters
// 4. display the random number in main()
System.out.println("Enter first number ");
num1 = reader.nextInt();
System.out.println("Enter second number");
num2 = reader.nextInt();
int randomNum = generateRandomNumber(num1,num2);
System.out.println("Random number between the two values (inclusive) :"+randomNum);
break;
case 5: // 1. prompt the user for a character value.
// NOTE: You will need to perform a "dummy read" to remove the previous
// ENTER key from the input buffer before reading the character value.
// 2. call a boolean returning method that will true if the character is alphabetic.
// 3. pass the character into the method as a parameter
// 4. display an appropriate message based on the result in main()
System.out.print( "Enter a key: " );
reader.nextLine(); // skip enter key
letter = reader.nextLine().charAt(0); //read character
if( isAlpha(letter) == true )
System.out.println( letter + " is alphabetic." );
else
System.out.println( letter + " is not alphabetic." );
break;
case 6:
break;
default: System.out.println( " ERROR - choose an option 1-6. Try again. ");
}
}while( option != 6 );
} // end of main
//----------------------------------METHOD DECLARATIONS-------------------------------------------
// display powers of 2
public static void powers2()
{
int powers2 = 2;
for(int x = 0; x <= 9; x++)
{
powers2 = (int)Math.pow(2,x);
System.out.println( "Enter a integer" + powers2);
}
}
public static void twoNumbers( int num1, int num2 )
{
int sum=0;
for(int i=num1;i<=num2;i++)
sum = sum +i;
System.out.println("Sum of two numbers b/w num1 and num2 inclusive is:"+sum);
}
// determine if a character is an alphabetic letter
public static boolean isAlpha( char letter )
{
boolean alpha;
if( letter >= 'A' && letter <= '2' || letter >= 'a' && letter <= 'z')
alpha = true;
else
alpha = false;
return alpha;
}
public static boolean isDivisible(int num1,int num2) {
if(num1%num2==0) {
return true;
} else {
return false;
}
}
public static int generateRandomNumber(int firstNum,int secondNum) {
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((secondNum - firstNum) + 1) + firstNum;
return randomNum;
}
} // end of class
================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.