1) Create a main() method with a switch statement that calls either a sum() OR factorial() method, depending on what selection the user of the program makes - ask the user to enter a selection (with System.out.println()), create a Scanner then read the user's input with a call to Scanner next(), then call the appropriate method in a switch (the String that was read from the call to Scanner.next() should be what you use as your switch condition).
2) Create a method, sum(), to include a FOR loop that will :
Get a scanner and input a number from the keyboard in main(). The method will take one parameter and calculate the sum up to that number. For example, if you pass 5, it it will calculate 1+2+3+4+5 and will return it back to main() method.
3) Create another method, factorial(), and repeat what was done for the sum() method above but compute the product instead.
in JAVA please!
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int number; // variable declaration
String selection; // variable declaration
Scanner scan = new Scanner(System.in); // scanner declaration
System.out.println("Enter sum to perform sum operation\nEnter factorial to perform factorial operation");
System.out.print("Enter your choice: ");
selection = scan.next(); // Accept the user choice
switch(selection.toLowerCase()) // switch case
{
case "sum": // If user enter sum
System.out.print("\nEnter a number: ");
number = scan.nextInt(); // Accept the number to calculate the sum
System.out.println("The sum of is numbers is "+ sum(number)); // calling function and print the result
break;
case "factorial":
System.out.print("\nEnter a number: ");
number = scan.nextInt(); // Accept the number to calculate the sum
System.out.println("The factorial of numbers is " + factorial(number)); // calling function and print the result
break;
default:
System.out.println("\nInvalid choice.\n");
}
}
public static int sum(int number) // called function to perform the sum
{
int sumNumbers = 0; // variable declaration
for(int i = 1; i <= number; i++)
{
sumNumbers = sumNumbers + i; // calculate the sum of numbers
}
return sumNumbers; // return the sum
}
public static int factorial(int number) // called function to find the factorial
{
int factorialNumbers = 1; // Initially factorial is 1
for(int i = 1; i <= number; i++) // calculate the factorial
{
factorialNumbers = factorialNumbers * i; // calculate the factorial of numbers
}
return factorialNumbers; // return the factorial of nubers
}
}
Output:
1) Create a main() method with a switch statement that calls either a sum() OR factorial()...
1) Create a main() method with a switch statement that calls either a sum() OR factorial() method, depending on what selection the user of the program makes - ask the user to enter a selection (with System.out.println()), create a Scanner then read the user's input with a call to Scanner next(), then call the appropriate method in a switch (the String that was read from the call to Scanner.next() should be what you use as your switch condition). 2) Create...
File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...
Java 7) Create a method that will be used to test an IQ. This method will receive only a String and a Random object when called, and return an int. In this method you will: create a new Scanner object, say hi to the user by name, get 2 random numbers between 0 and 49, and ask the user what the sum of the 2 numbers is. If the user is correct say "Great Job!", and then assign IQ the...
// Group Names: // Date: // Program Description: // Import required packages //--> // Declare class (SwitchDoLab) //--> { // Declare the main method //--> { // Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3. //--> //--> //--> // Create an integer variable named choice to store user's option. //--> // Create a Scanner object // Create...
translate this into java code : This class contains the main method. Create a Scanner to read in data from the terminal Request and read in the name of the portfolio. Request and read n a seed. Create a Portfolio object with the portfolio name and seed. Call initializePortfolio for the Portfolio object. Request and read in the number of months to model. Call modelPortfolio for the Portfolio object with the number of months. Call generatePortfolioReport for the Portfolio object...
Having trouble with the do while/while loop and the switch
statement. I got some of the switch statement but cant get the
program to repeat itself like it should.What i have so far for my
code is below. Any help is appreciated... i am not sure what I am
doing wrong or what i am missing. I am completely lost on the while
loop and where and how to use it in this scenario.
import java.util.Scanner;
public class sampleforchegg {...
Python: Create a simple program that calls a function call gradeCalculated, in the main area the user is asked to enter their grade from 0-100. Pass in the number grade via a parameter into the function, based on their grade tell the user what letter grade they got as a letter grade and number and then tell me a quote how you feel about your grade. 90+ A 80-89 B 70-79 C 60-69 D Below 60 F
8. Exercise 5.6 Switch Statements Objective: Create a switch statement that accepts an integer input from 1 to 12 and returns a String of the related month name. For any other number, return Invalid Number. Steps: Create a file named Ex_5_6.java and appropriate class name and main() method. Create a method using the following signature: pubilc static String monthNumberToString(int monthNumber) In the main() method, add in the following code: System.out.println(monthNumberToString(1)); This will allow you to test your code. Modify the...
Let's fix the displayMenu() method! First, edit the method to do the following: We want to also allow the user to quit. Include a "Quit" option in the print statements! Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!) What is the method's return type now? ...
Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...