Problem Description: Write a program which will have three methods, as follows:
ID: 667855 • Letter: P
Question
Problem Description: Write a program which will have three methods, as follows:
add() method: This method has two integer type parameters: min and max. It will return the summation of min to max.
For example, if the value of min = 1 and the value of max = 4, the add method will return 10 because 1+2+3+4 =10.
Another example, if the value of min = -1 and the value of max = 1, the add method will return 0 because -1+0+1 =0.
mul() method: This method has two integer type parameters: min and max. It will return product of min to max.
For example, if the value of min = 1 and the value of max = 4, the add method will return 24 because 1*2*3*4 =24.
Another example, if the value of min = -1 and the value of max = 1, the add method will return 0 because -1*0*1 =0.
main() method: This method will ask the user to enter 1 or 2 from keyboard.
If 1 is entered, the main method will call the add method and print the result.
If 2 is entered, the main method will call the mul method and print the result.
If the users enter a value other than 1 or 2, the main method will print a message “Operation failed. You need to enter 1 or 2”.
This is my program I am trying to do, but still needs a lot of help of compiling
import java.util.Scanner;
public class methodSet
{
public static void main(String[] args)
{
int totalSUM1;
int totalSUM2;
int selection;
int selectionSet;
String inputString;
inputString = JOptionPane.showInputDialog("Please enter a method: ");
selection = Integer.parseInt(inputString);
inputString = JOptionPane.showInputDialog("Please enter a min and max result: ");
selectionSet = Integer.parseInt(inputString);
totalSUM1 = sum1;
totalSUM2 = sum2;
totalPRODUCT1 = product1;
totalPRODUCT2 = product2;
if (selection = 1 && selectionSet = 1)
{
JOptionPane.showMessageDialog(null,"1 + 2 + 3 + 4" + sum1);
}
if (selection = 1 && selectionSet = 2)
{
JOptionPane.showMessageDialog(null,"-1 + 0 + 1" + sum2);
}
if (selection = 2 && selectionSet = 1)
{
JOptionPane.showMessageDialog(null,"1 * 2 * 3 * 4" + product1);
}
if (selection = 2 && selectionSet = 2)
{
JOptionPane.showMessageDialog(null,"-1 * 0 * 1 " + product2);
}
}
/**
The sum method returns the sum of its two parameters.
@param num1 The first number to be added.
@parum num2 The second number to be added.
@return The result of num1 and num2.
*/
{
int sums1 = 0;
for (int i = 1; i <= 4; i++)
{
int resultSums1;
sum += i;
return resultSums1;
}
int sums2 = 0;
for (int i = -1; i <= 1; i++)
{
int resultSums2;
sum += i;
return resultSums2;
}
int products1 = 0;
for (int i = 1; i <= 4; i++)
{
int resultProducts1;
product *= i;
return resultProducts1;
}
int products2 = 0;
for (int i = -1; i <= 1; i++)
{
int resultProducts2;
product *= i;
return resultProducts2;
}
}
}
Explanation / Answer
/*
COMPLIED AND RUN SUCCESSFULLY IN NETBEANS 7.4
CREATE NEW PROJECT Chegg_Add_Mul and PASTE THE CODE
PLEASE GET BACK FOR FURTHER QUERIES
*/
package chegg_add_mul;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Chegg_Add_Mul {
public static void main(String[] args) {
int result,min,max;
int selection;
String inputString;
inputString = JOptionPane.showInputDialog("Please enter a method number: ");
selection = Integer.parseInt(inputString);
if(selection==1)
{
inputString = JOptionPane.showInputDialog("Please enter min value ");
min = Integer.parseInt(inputString);
inputString = JOptionPane.showInputDialog("Please enter max value ");
max = Integer.parseInt(inputString);
result=add(min,max);
JOptionPane.showInputDialog("Sum is "+result);
}
else if(selection==2)
{
inputString = JOptionPane.showInputDialog("Please enter min value ");
min = Integer.parseInt(inputString);
inputString = JOptionPane.showInputDialog("Please enter max value ");
max = Integer.parseInt(inputString);
result=mul(min,max);
JOptionPane.showInputDialog("Multiplication is "+result);
}
else
JOptionPane.showInputDialog("Sorry Wrong Choice");
}
public static int add(int min, int max) {
int sum=0;
for(int i=min;i<=max;i++)
sum=sum+i;
return sum;
}
public static int mul(int min, int max) {
int mulp=1;
for(int i=min;i<=max;i++)
mulp=mulp*i;
return mulp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.