Question

In: Computer Science

Programming Problem Design a program in java that can perform three different arithmetic operations based on...

Programming Problem

Design a program in java that can perform three different arithmetic operations based on the user’s input.

The three operations are

1. Summation of integers from 1 to m

2. Factorial of a given number n (n!)

3. Finding the leftmost digit of a given integer (note: you have to use int)

This program should prompt the user a menu including four options, ask the user for one option, and perform the corresponding computation. This process will repeat until the user chooses “Quit”.

Please use this menu in your program: (use a switch statement)

Please choose one option from the following menu:

1) Calculate the sum of integers from 1 to m

2) Calculate the factorial of a given number

3) Display the leftmost digit of a given number

4) Quit

Sample Output

Below is an example of what your output should roughly look like when this lab is completed. Text in RED represents user input.

Please choose one option from the following menu:

1) Calculate the sum of integers from 1 to m

2) Calculate the factorial of a given number

3) Display the leftmost digit of a given number

4) Quit

1

Enter a number:

4

The sum of 1 to 4 is 10

Please choose one option from the following menu:

1) Calculate the sum of integers from 1 to m

2) Calculate the factorial of a given number

3) Display the leftmost digit of a given number

4) Quit

2

Enter a number: 5

The factorial of 5 is 120

Please choose one option from the following menu:

1) Calculate the sum of integers from 1 to m

2) Calculate the factorial of a given number

3) Display the leftmost digit of a given number

4) Quit

3

Enter a number:

987654321

The leftmost digit of 987654321 is 9

Please choose one option from the following menu:

1) Calculate the sum of integers from 1 to m

2) Calculate the factorial of a given number

3) Display the leftmost digit of a given number

4) Quit 4 Bye

PLEASE USE THE FOLLOWING FORMAT

I need it in the format of a do while loop

Due today please help!

public class Lab4 {

public static void main(String[] args) {
// Declare some variables you need
// -->

do {
// Display the menu
displayMenu();

// Ask the user for one option
// -->

switch (?????) {
// Define four cases for different options. Don't forget "break".
// -->
}
} while (?????);

}

/**
* Print the menu
*/
private static void displayMenu() {
System.out.println("Please choose one option from the following menu:");
System.out.println("1) Calculate the sum of integers from 1 to m");
System.out.println("2) Calculate the factorial of a given number");
System.out.println("3) Display the leftmost digit of a given number");
System.out.println("4) Quit");
}
}

If you can explain that would be awesome, thank you!

Solutions

Expert Solution

import java.util.*;
import java.util.Scanner;
//main class
public class Lab4 {

//function displays the menu
private static void displayMenu()
{
System.out.println("Please choose one option from the following menu:");
System.out.println("1) Calculate the sum of integers from 1 to m");
System.out.println("2) Calculate the factorial of a given number");
System.out.println("3) Display the leftmost digit of a given number");
System.out.println("4) Quit");
}   
//main function
public static void main(String[] args)
{
//scanner
Scanner sc = new Scanner(System.in);
//varibles declaration
int input = 0,sum=0,fact=1,number;
String str1,str2;
char arr[] = new char[200];
//do while loop
do
{
// Display the menu
//calling the function
displayMenu();

// Ask the user for one option
//reads the input from user
input = sc.nextInt();
  
//switch case
switch (input)
{
// Define four cases for different options. Don't forget "break".
// -->
//case 1 is sum of number from 1 to number
case 1:
System.out.println("Enter a Number: ");
//reads the number from user
number = sc.nextInt();
//runs the loop tocalculate the sum of numbers
for(int i=1;i<=number;i++){
sum = sum+i;
}
//prints the sum of integers
System.out.println("Sum of Integers: "+sum);   
//breaks the switch block
break;
//case 2 factorial of anumber
case 2:
System.out.println("Enter a Number: ");
//reads the input from user
number = sc.nextInt();
//runs the loop to calculate the factorial of a number
for(int i=1;i<=number;i++){
fact*=i;
}
//prints the factorial of number
System.out.println("The factorial of "+number+" is "+fact);
break;
//case3 leftmost of number
case 3:
System.out.println("Enter a Number: ");
//reads the input
number = sc.nextInt();
// number is converting to string by using toString() function
str1 = Integer.toString(number);
//converting string to char array by using toCharArray();
arr = str1.toCharArray();
//printst the left most bit by index value of array
System.out.println("The leftmost most digit of: "+number+" is "+arr[0]);
break;
  
//case 4
case 4:
System.out.println("Bye");
break;
  
}
}//while loop fails when input is 4
while (input!=4);
  
}
}

OUTPUT


Related Solutions

Java program - you are not allowed to use arithmetic operations such as division (/), multiplication,...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication, or modulo (%) to extract the bits. In this exercise use only logic bit-wise operations. Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you CANNOT use the arithmetic division by...
Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions.
Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a / b, in which a and b are integers and b != 0.Your program must:be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numerator and denominator of each fraction.The input must be validated for correct operation (+, -, *, or /) and b != 0. If any of these cases are...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form: <binary operand 1> <operator> <binary...
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the...
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in the lecture notes. The solution presented in the notes uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this assignment, standard counting semaphores will be...
Java Programming language. Proof of concept class design based on the following ideas Look at your...
Java Programming language. Proof of concept class design based on the following ideas Look at your refrigerator and think about how you would model it as a class. Considerations include: A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables A refrigerator also has storage areas on the door for things like bottled...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions: 2+6*4 Its postfix notation is 2 6 4 * + 2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms are shell sort and quick sort. Assume that input data is stored in a text file. Experimenting with a prototype data (integers from 1 to 10) to ensure that your implementation works correctly and the results match expectations. The program will sort what is in the text file and print the amount of comparisons and exchanges for both algorithms.
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be...
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be used to determine the length of the permutation m and the key permutation.
Java Programming: Can I get an example of a program that will allow 4 options? Example...
Java Programming: Can I get an example of a program that will allow 4 options? Example Output: Choose on of the options below: 1. Display all items in CSV file. (CSV file includes for example brand of phone, size, price) 2. Pick an item by linear searching 3. Pick an item by bineary searching 4. Quit Program
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT