-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumProdDifQuo.java
110 lines (90 loc) · 2.98 KB
/
SumProdDifQuo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package a1_jac444;
import java.util.Scanner;
/*
Alistair Godwin
SID: 020079158
UID: agodwin
Prof: Mehrnaz Zhian
A1 - Question 1: 2 Int manipulation
This program asks the user for 2 ints, which it will verify are ints(if not set to 0) and then display
sum, product, quotient, and divisor to the user. The program loops until the
*/
public class SumProdDifQuo {
public static void main(String[] args) {
int exit = 0;
//Loop, exit on int 0 entry
do{
System.out.println("======Assignment 1: Question 1: Sum, Product, Differance, and Divisor.=====\n\n");
//Make object
Scanner inp = new Scanner(System.in);
System.out.println("Enter the first Int: ");
//Get Int A with safety
int a = 0;
if (inp.hasNextInt()){
a = inp.nextInt();
}else{
System.out.println("That was not an int. It's now 0");
inp.next(); //Skip and protect buffer
}
System.out.println("\nInt a: " + a);
System.out.println("Enter the second Int: ");
//Get Int B with safety
int b = 0;
if (inp.hasNextInt()){
b = inp.nextInt();
}else{
System.out.println("That was not an int. It's now 0\n");
inp.next(); //Skip and protect buffer
}
System.out.println("\nInt b: " + b);
//===========Output and method calls=============//
System.out.println("The Sum of "+ a +" and "+ b + " is: " + Sum(a,b));
System.out.println("The Product of "+ a +" and " + b + " is: " + Product(a,b));
System.out.println("The Differance of "+ a +" and " + b + " is: " + Difference(a,b) + " digits.");
System.out.println("The Quotient of "+ a +" divided by " + b + " is: " + Quotient(a,b));
//================================================//
//Query exit
System.out.println("Enter 0 to exit the program, or anything else to continue...");
if (inp.hasNextInt()){
exit = inp.nextInt();
}else{
exit = 1;
}
//Graceful exit and closure of scanner
if (exit == 0)
inp.close();
}while(exit != 0);
System.out.println("\nGoodbye!");
}
//Return sum of 2 ints
public static int Sum(int a, int b){
return (a + b);
}
//Return product of 2 ints
public static int Product(int a, int b){
return (a * b);
}
//Returns the difference(positive digit) between two ints
//Checks for greater value digit for positive difference.
public static int Difference(int a, int b){
if(a > b){
return (a - b);
}else if(a < b) {
return (b - a);
}else{
return 0;
}
}
//Returns the double quotient of two ints.
//Also prevent a divide by 0 scenario.
//Since ints dont divide kindly as ints(truncation), would like double next time.
public static int Quotient(int a, int b){
if(a == 0 || b == 0){
return 0;
}
else{
int c = ( a / b);
return c;
}
}
}