-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay7_Calculator.java
More file actions
84 lines (73 loc) · 1.83 KB
/
Day7_Calculator.java
File metadata and controls
84 lines (73 loc) · 1.83 KB
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
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 7 : Challenge IV - Basic Calculator
// Day 7 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.util.Scanner;
public class Day7_Calculator
{
/**
*
Create a basic calculator that let the user
choose between the following operators
+,-,*,/
and let the user enter the operands a and b
*/
// constructor
public Day7_Calculator()
{
System.out.println("\n> Calculator created.");
}
// operations
/**
* add the parameters a and b
* @param a
* @param b
* @return
*/
public double add_operands(double a, double b)
{
return a + b;
}
/**
* subtract the b from a
* @param a
* @param b
* @return
*/
public double subtract_operands(double a, double b)
{
return a-b;
}
/**
* multiply the operands a and bs
* @param a
* @param b
* @return
*/
public double multiply_operands(double a, double b)
{
return a*b;
}
public double divide_operands(double a, double b)
{
if(b == 0)
{
Scanner sc = new Scanner(System.in);
System.out.println("You can not divide an integer by 0");
while( b == 0)
{
System.out.printf("Please enter a correct value for b:");
b = sc.nextDouble();
}
sc.close();
System.out.printf("\n> For this division, the new value of b is set to %.2f\n",b);
}
return a / b;
}
}