 # Branching and Looping in Java

## Question

Branching
import java.util.Scanner;

public class MyBranching {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int selection = 0;
while (selection != 3) {
System.out.print("1. Find the log of a floating point number\n"
+ "2. Find the absolute value of an integer:\n"
+ "3. Quit\n"
+ "--> ");
selection = s.nextInt();
switch (selection){
case 1:
log(s);
break;
case 2:
abs(s);
break;
case 3:
System.out.println("End program");
break;
default:
System.out.println("Invalid selection");
break;
}
System.out.println("");
}

}

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));

Looping

• 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 factorial of a number:
- A factorial is the product of multiplying all numbers between 1 and the given number. The symbol for factorial is the exclamation mark !. For example:
- 3! = 1 * 2 * 3 = 6
- 5! = 1 * 2 * 3 * 4 * 5 = 120
- 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720
- Prompt the user for the number to find the factorial of. The number must be an integer.
- Use a for statement to calculate the factorial
- Display the result with an 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 factorial to the menu. You should have 4 options (3 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 != 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;...
\$9.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.