Question

In: Computer Science

Write a Java Program using the following instruction: Create the following 3 methods that calculate and...

Write a Java Program using the following instruction:

Create the following 3 methods that calculate and display the sum or product of the integers from 1 to the number input.

getNum Receives no input. Prompts the user to enter a number, use a loop to validate. Returns the result.

calcSum Receives an integer as input. Calculates the sum of the integers from 1 to this number. Returns the result.

calcProd Receives an integer as input. Calculates the product of the integers from 1 to this number. Returns the result.

Main should: Prompt for a character that determines if you want to calculate the sum (‘S’ or ‘s’) or product (‘P’ or ‘p’). You should use a switch to process the choices. Upper and lower case values should work the same way. Use default to give an error message if anything else is entered. (You can input as String or char)

if a valid option is entered, use getNum to prompt for the number

Note that calcSum and calcProduct RETURN the result to main, and the output is printed in main. For example, if you input 5 as the number, and ‘S’, the program should calculate 1 + 2 + 3 + 4 + 5 and display the sum as 15. If you input 5 and ‘P’ the program should calculate 1 x 2 x 3 x 4 x 5 and display the product as 120. . The process should repeat as long as the user wants to continue. This loop should be in main. DO NOT use System.exit command to end the program. . See sample output below.

Enter S for sum, P for prod:z

Invalid choice Again(y/n)? y

Enter S for sum, P for prod:s

Enter an integer greater than 1: 1

Must be greater than 1, re-enter: -3

Must be greater than 1, re-enter: 3

The sum of the numbers from 1 to 3 is 6 Again(y/n)? y

Enter S for sum, P for prod:p

Enter an integer greater than 1: 4

The product of the numbers from 1 to 4 is 24 Again(y/n)? n

Solutions

Expert Solution

Answer

import java.util.*;
import java.util.Scanner;
class MyCode
{
   public static void main (String[] args) throws java.lang.Exception
   {
       int num;
       String ch,choice;
       Scanner in=new Scanner(System.in);
       do
       {
           System.out.println("Enter S for sum, P for prod : ");
           ch=in.next();
           switch(ch.charAt(0))
           {
               case 'S':
               case 's':
               num=getNum();
               System.out.println("The sum of the numbers from 1 to "+num+" is : "+calcSum(num));
               break;
               case 'P':
               case 'p':
               num=getNum();
               System.out.println("The product of the numbers from 1 to "+num+" is : "+calcProd(num));
               break;
               default:
                   System.out.println("Invalid choice! Try Again..");                      
           }
           System.out.println("Try Again (Y/N) : ");
           choice=in.next();
       }while(choice.charAt(0)!='N'&&choice.charAt(0)!='n');
   }
   public static int getNum()
   {
       Scanner in=new Scanner(System.in);
       int n;
       do
       {
           System.out.println("Enter an integer greater than 1 : ");
           n=in.nextInt();
           if(n<=1)
               System.out.println("Must be greater than 1, re-enter..");
           else
               break;  
       }while(n<=1);
       return n;
   }
   public static int calcSum(int num)
   {
       int sum=0;
       for(int i=1;i<=num;i++)
           sum+=i;
       return sum;
   }
   public static int calcProd(int num)
   {
       int product=1;
       for(int i=1;i<=num;i++)
           product=product*i;
       return product;
   }
}

OUTPUT


Related Solutions

(Java) Create a program using 3 methods. The methods must be public and static. Ask the...
(Java) Create a program using 3 methods. The methods must be public and static. Ask the user for 3 numbers, then call the methods from main to print max, min, and average. The first method max (int x, int y, int z) returns the maximum value of 3 integer values. The second method min (int X, int y, int z) returns the minimum value of 3 integer values. And the third average (int x, int y, int z) returns the...
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
Write a java program using the following instructions: Write a program that determines election results. Create...
Write a java program using the following instructions: Write a program that determines election results. Create two parallel arrays to store the last names of five candidates in a local election and the votes received by each candidate. Prompt the user to input these values. The program should output each candidates name, the votes received by that candidate, the percentage of the total votes received by the candidate, and the total votes cast. Your program should also output the winner...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable radius = -999 declare character choice create do while loop inside of do loop write: System.out.println(); System.out.println("*** CIRCLE CALCULATIONS ***"); System.out.println(); System.out.println("1. Enter the radius of the circle"); System.out.println("2. Display the area of the circle"); System.out.println("3. Display the circumference of the circle"); System.out.println("4. Quit"); System.out.println(); System.out.println("Enter a number from 1 - 4"); System.out.println(); Declare choice character and relate to scanner declare switch (choice) case...
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
4.) Create a Java program using the “extends” and “runnable” methods. You will be creating your...
4.) Create a Java program using the “extends” and “runnable” methods. You will be creating your own threaded program using the threadExtend.java code and the threadRunnable.java code. A.) Focus the threadExtend.java code DETAILS TO NOTE: - It creates 4 instances of the same thread. This means that the thread code is actually running 4 separate times - The thread accepts 2 parameters, an INTEGER from 1 to 4, as well as an ID - Note the parameters are hard coded...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT