Question
import java.util.Scanner;
public class MyBranching {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int selection = 0;
while (selection != 4) {
System.out.print("1. Find the log of a floating point number\n"
+ "2. Find the absolute value of an integer:\n"
+ "3. Find the factorial of an integer:\n"
+ "4. Quit\n"
+ "--> ");
selection = s.nextInt();
switch (selection){
case 1:
log(s);
break;
case 2:
abs(s);
break;
case 3:
factorial(s);
break;
case 4:
System.out.println("End program");
break;
default:
System.out.println("Invalid selection");
break;
}
System.out.println("");
}
}
private static void factorial (Scanner s){
int number = 0;
boolean isInteger = false;
while (true) {
System.out.print("Enter the number to find the factorial of : ");
try{
number = Integer.parseInt(s.next());
} catch (Exception e){
System.out.println("The number must be an integer.");
continue;
}
break;
}
int fac = 1;
if (number > 1) {
for (int i = 2; i <= number; i++) {
fac *= i;
}
}
System.out.println("factorial of " + number + " is " + fac);
}
private static void abs(Scanner s){
System.out.print("Enter the number find the absolute value of: ");
int number = s.nextInt();
int positive = number;
if (number < 0) {
positive = number * (-1);
}
System.out.println("absolute value of " + number + " is " + positive);
}
private static void log(Scanner s){
System.out.print("Enter the number to find the log of: ");
double number = s.nextDouble();
System.out.println("log of " + number + " is " + Math.log(number));
}
}
Arrays
• Modify your previous Java program:
o Do not delete any methods that already exist.
o Add a method (not the main method) to find the average of a list of integers:
- Use an array to store the input list.
- Prompt the user for the number of integers. Use this input to set the size of the array.
- Prompt the user for the numbers to find the average of. Store these numbers in the array (use a for loop to gather the input). Ask for the numbers one at a time (ask for a number, wait for user input, ask for the next number, wait for user input, etc.)
- Use a for statement to calculate the average. Note that the average will be a floating point data type so you will need to do a conversion. For instance, the average of 1, 2, 3, 4, and 6 is 3.2, not 3.0.
- Display the result with an output message. Include the numbers being averaged in the output message. Make sure the output message clearly states what the user is looking at.
o In the main method:
- Add the option to find the average to the menu. You should have 5 options (4 possible calculations and the quit option).
- Repeat the program until the user chooses to quit. You may use either a while or do-while statement to repeat the program.
o Compile and run your code.
o Submit your source code as a plain text file with a .java extension. Make sure it compiles without error before submitting it.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
import java.util.Scanner;public class MyBranching {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int selection = 0;
while (selection != 5) {
System.out.print("1. Find the log of a floating point number\n"
+ "2. Find the absolute value of an integer:\n"
+ "3. Find the factorial of an integer:\n"
+ "4. Find the average of a list of integers:\n"
+ "5. Quit\n"
+ "--> ");
selection = s.nextInt();
switch (selection){
case 1:
log(s);
break;
case 2:
abs(s);
break;...