5. (a) Create the logic for a program that performs arithmetic functions. Design
ID: 3758960 • Letter: 5
Question
5.
(a) Create the logic for a program that performs arithmetic functions. Design the program to contain two numeric variables, and prompt the user for values for the variables. Pass both variables to methods named sum() and difference(). Create the logic for 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.
(b) Modify the program in Exercise 5a so that the two entered values are passes to a method named getChoice(). The getChoice() method asks the user whether addition or subtraction should be performed and then passes the two values to the appropriate method, where the result is displayed.
Explanation / Answer
Solution a)
package com.chegg.nancy.solutions;
import java.util.Scanner;
public class Arithmetics {
public static void main(String[] args) {
int FirstNumVal;
int secNumVal;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter first value : ");
FirstNumVal = scan.nextInt();
System.out.println("Please enter second value : ");
secNumVal = scan.nextInt();
scan.close();
sum(FirstNumVal, secNumVal);
difference(FirstNumVal, secNumVal);
}
private static void difference(int firstNumVal, int secNumVal) {
int diff = firstNumVal - secNumVal;
System.out.println("Diffrence of " + firstNumVal + " and " + secNumVal + " is " + diff);
}
private static void sum(int firstNumVal, int secNumVal) {
int sum = firstNumVal + secNumVal;
System.out.println("Sum of " + firstNumVal + " and " + secNumVal + " is " + sum);
}
}
____________________________________________________________________________________________
Solution b)
package com.chegg.nancy.solutions;
import java.util.Scanner;
public class Arithmetics {
public static void main(String[] args) {
int firstNumVal;
int secNumVal;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter first value : ");
firstNumVal = scan.nextInt();
System.out.println("Please enter second value : ");
secNumVal = scan.nextInt();
getChoice(firstNumVal, secNumVal, scan);
scan.close();
}
private static void getChoice(int firstNumVal, int secNumVal, Scanner scan) {
System.out.println("For sum operation enter 1.For diffrence operation enter 2.");
int choice = scan.nextInt();
if (choice == 1)
sum(firstNumVal, secNumVal);
else if (choice == 2)
difference(firstNumVal, secNumVal);
else
System.out.println("you have entered wrong input.Please try again!");
}
private static void difference(int firstNumVal, int secNumVal) {
int diff = firstNumVal - secNumVal;
System.out.println("Diffrence of " + firstNumVal + " and " + secNumVal + " is " + diff);
}
private static void sum(int firstNumVal, int secNumVal) {
int sum = firstNumVal + secNumVal;
System.out.println("Sum of " + firstNumVal + " and " + secNumVal + " is " + sum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.