0

So the question is a 2 parter. I believe I have the part a completed right, but not 100% where to add in the second part. The first question is as follows:

A. Create an application named Numbers whose main() method hold two integer
variables. Assign values to the variables. Pass both variables to methods named
sum() and difference(). Create the methods sum() and difference(); they compute the sum of and difference between the values of two arguments, respectively. Each method should perform
the appropriate computation and display the results.

My answer is:
start
Declarations
num number1
num number2
input number1,number2
sum(number1,number2)
difference(number1,number2)
stop

void sum(num n1,num n2)
Declarations
num result
result=n1+n2
output "The sums of",n1, "and",n2, "is", result
return

void difference(num n1,num n2)
Declarations
num result
result=n1-n2
output "The difference of",n1, "and",n2, "is", result
return

The question for b is:

B. Add a method named product() to the Numbers class. The product() method should compute the multiplication product of two integers, but not display the answer. Instead, it should return the answer to the calling method, which displays the answer.

The instructor suggested that a case structure may help.

2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by Vedoo
0

For case structure, you can try to do switch statements. I will give you a brief idea to complete your pseudocode. I don't see why return statement is needed as the method is declared as void. The method sum() and difference() doesn't return anything to main() as the value is printed in the function itself.

Next thing i see is the product() method that returns its computation to a variable declared in the calling method which in this case is the main() method. Try asking the user for option to be selected. For example, 1 for sum, 2 for difference and 3 for product. whatever the user input, use the switch case to execute the method according to user selection. For product() method, set a variable in main method like int product. set the value of product to be equivalent to the value returned from product() method. then print the value of product variable.

Another way of doing this is using the easy if else statement. Ask the user what he/she wants to do then execute the method. Since ther are only 3 choices for user, if else statement would be okay. A switch statement might make it look more readable and organised. Good luck :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.