Write a separate algorithm(preferably using java) for each of the following that
ID: 639626 • Letter: W
Question
Write a separate algorithm(preferably using java) for each of the following that will allow you to:
a. Input three numbers from the user, compute their sum and output the sum to the screen.
b. Input three positive numbers from the user. Make sure each of the three numbers is positive (greater than zero). Each time the user inputs an invalid number for input, you will give them an error message, and ask them to input another number. There is no limitation on how many times a user can input a wrong value.
c. Repeat (b), imposing a limit of three wrong attempts for each number, after which you quit the
program.
d.Input a Celcius temperature from the user, convert it to Fahrenheit, and output the Fahrenheit temperature.
NOTE: Your algorithm does not necessarily have to be in Java, but you need to detail the steps involved. This includes but is not limited to:
Explanation / Answer
import java.util.Scanner;
//App to perform multiple tasks
public class MultiFunctionApp {
// declared and initialized the scanner variable to be used globally
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// multiple choice program
int choice = 0;
do {
// options for multiple choice
System.out.println(" What do you want to do?");
System.out.println("1. Sum of any three numbers");
System.out.println("2. Sum of three positive numbers");
System.out
.println("3. Sum of three positive numbers imposing 3 wrong attempts");
System.out
.println("4. Temprature conversion from celsius to fahrenheit");
System.out.println("5. Exit");
System.out.println("Enter your choice");
choice = sc.nextInt();
switch (choice) {
case 1:
// calling the calculateSum() to add three numbers
calculateSum();
break;
case 2:
// calling the calculateSumForPositiveNumbers() to add three positive numbers
calculateSumForPositiveNumbers();
break;
case 3:
// calling the calculateSumWithMaxAttemptsImposed() to add three positive numbers by imposing 3 wrong attempts limit
calculateSumWithMaxAttemptsImposed();
break;
case 4:
//caling the temperatureConverter() method to convert the temperature from celsius to fahrenheit
temperatureConverter();
break;
case 5:
//Exit the app
System.out.println("Good Bye!");
break;
default:
// loop will break when user enters wrong choice
System.err.println("Please enter correct choice");
break;
}
} while (choice != 4);
}
// print the tax applied for salary
private static void calculateSum() {
System.out.println("Please enter first number");
int number1 = sc.nextInt();
System.out.println("Please enter second number");
int number2 = sc.nextInt();
System.out.println("Please enter third number");
int number3 = sc.nextInt();
int sum = number1 + number2 + number3;
System.out.println("Sum of the numbers ("+number1+","+number2+","+number3+") is " + sum);
}
// swap the two numbers
private static void calculateSumForPositiveNumbers() {
outerloop:
while(true){
System.out.println("Please enter first number");
int number1 = sc.nextInt();
if (number1 > 0) {
while(true){
System.out.println("Please enter second number");
int number2 = sc.nextInt();
if (number2 > 0) {
while(true){
System.out.println("Please enter third number");
int number3 = sc.nextInt();
if (number3 > 0) {
int sum = number1 + number2 + number3;
System.out.println("Sum of the numbers ("+number1+","+number2+","+number3+") is " + sum);
break outerloop;
} else {
System.err.println("Please enter any positive number greater than 0");
continue;
}
}
} else {
System.err
.println("Please enter any positive number greater than 0");
continue;
}
}
} else {
System.err
.println("Please enter any positive number greater than 0");
continue;
}
}
}
// to count up to any number
private static void calculateSumWithMaxAttemptsImposed() {
//declaring the max attempts for each variable
int attemptForFirstNumber = 0;
int attemptForSecondNumber = 0;
int attemptForThirdNumber = 0;
outerloop: //declaring outer loop
while(true){
System.out.println("Please enter first number");
int number1 = sc.nextInt();
if (number1 > 0) {
while(true){
System.out.println("Please enter second number");
int number2 = sc.nextInt();
if (number2 > 0) {
while(true){
System.out.println("Please enter third number");
int number3 = sc.nextInt();
if (number3 > 0) {
int sum = number1 + number2 + number3;
System.out.println("Sum of the numbers ("+number1+","+number2+","+number3+") is " + sum);
break outerloop;
} else {
if(attemptForThirdNumber==2){ //checking if the attempt reached the limit of 3 or not
System.out.println("You have reached three attempts limit");
break outerloop; //quit the program
}else{
attemptForThirdNumber++; //increase the attempt by one
System.err.println("Please enter any positive number greater than 0 (attempt left : "+(3-attemptForThirdNumber)+")");
continue; //continue the loop
}
}
}
} else {
if(attemptForSecondNumber==2){ //checking if the attempt reached the limit of 3 or not
System.out.println("You have reached three attempts limit");
break outerloop; //quit the program
}else{
attemptForSecondNumber++; //increase the attempt by one
System.err.println("Please enter any positive number greater than 0 (attempt left : "+(3-attemptForSecondNumber)+")");
continue; //continue the loop
}
}
}
} else {
if(attemptForFirstNumber==2){ //checking if the attempt reached the limit of 3 or not
System.out.println("You have reached three attempts limit");
break; //quit the program
}else{
attemptForFirstNumber++; //increase the attempt by one
System.err.println("Please enter any positive number greater than 0 (attempt left : "+(3-attemptForFirstNumber)+")");
continue; //continue the loop
}
}
}
}
//convert the temperature from celsius to fahrenheit
private static void temperatureConverter() {
double celsius, fahrenheit;
System.out.print("Enter a temperature in Celsius: ");
celsius = sc.nextDouble();
fahrenheit = 32 + (celsius * 9 / 5);//using formulae for conversion
System.out.println(celsius +"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.