Skip to content

Commit 3967e85

Browse files
committed
Added return types
1 parent 647f395 commit 3967e85

10 files changed

+37
-34
lines changed
45 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-2 Bytes
Binary file not shown.

Java Calculators/src/JavaCalculators/ConsoleCalculator.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
import java.util.Scanner;
44

55
/** Program that allows a person to use the Java console as a 4 function calculator
6-
* @author Monty a.k.a MintyTheCoder & Brodie*/
6+
* @author Monty a.k.a MintyTheCoder*/
77
public class ConsoleCalculator
88
{
99
/** a float variable to store a number given by the user */
1010
// float is a number with decimals
1111
//declares two float variables to store numbers given by the user
1212
public static float number1, number2;
1313

14-
/** a float variable to store the outcome of the function used between number1 and number2 */
15-
// declares a float variable to store the outcome of the function executed between number1 and number2
16-
public static float answer;
17-
1814
/** a String variable to store/know what function is given by the user */
1915
//declares a string variable to store the function given by the user
2016
static String function;
@@ -41,50 +37,58 @@ public void getNumbers()
4137
System.out.println("Please enter the second number");
4238

4339
//store the number in the variable number2
44-
number2 = keyboard.nextFloat();
40+
number2 = keyboard.nextFloat();
4541

4642
//print blank line
4743
System.out.println(" ");
4844
}
4945

5046
/** a method to add the numbers from getNumbers and print the sum in the console*/
5147
//creates a method to add the numbers from getNumbers and print the sum in the console
52-
public void doSum()
48+
public float doSum()
5349
{
5450
//getNumbers();
55-
answer = number1 + number2;
51+
float sum;
52+
sum = number1 + number2;
5653
System.out.println("The sum of your two numbers is:");
57-
System.out.println(answer);
54+
System.out.println(sum);
55+
return sum;
5856
}
5957

6058
/** a method to multiply the numbers from getNumbers and print the product in the console*/
6159
//creates a method to multiply the numbers from getNumbers and print the product in the console
62-
public void doProduct()
60+
public float doProduct()
6361
{
6462
//getNumbers();
65-
answer = number1 * number2;
63+
float product;
64+
product = number1 * number2;
6665
System.out.println("The product of your two numbers is:");
67-
System.out.println(answer);
66+
System.out.println(product);
67+
return product;
6868
}
6969

7070
/** a method to subtract the numbers from getNumbers and print the difference in the console*/
7171
//creates a method to subtract the numbers from getNumbers and print the difference in the console
72-
public void doDifference()
72+
public float doDifference()
7373
{
7474
//getNumbers();
75-
answer = number1 - number2;
75+
float difference;
76+
difference = number1 - number2;
7677
System.out.println("The difference between your two numbers is:");
77-
System.out.println(answer);
78+
System.out.println(difference);
79+
return difference;
7880
}
7981

8082
/** a method to divide the numbers from getNumbers and print the quotient in the console*/
8183
//create a method to divide the numbers from getNumbers and print the quotient in the console
82-
public void doQuotient()
84+
public float doQuotient()
8385
{
8486
//getNumbers();
85-
answer = number1 / number2;
87+
float quotient;
88+
quotient = number1 / number2;
8689
System.out.println("The quotient of your two numbers is:");
87-
System.out.println(answer);
90+
System.out.println(quotient);
91+
return quotient;
8892
}
8993

9094
/** main method that every Java project must have,

Java Calculators/src/JavaCalculators/SwingCalculator.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class SwingCalculator
1515
static JFrame calcBox;
1616

1717
//create 2 text boxes for text input
18-
static final JTextField textInput1 = new JTextField("Enter Your First Integer");
19-
static final JTextField textInput2 = new JTextField("Enter Your Second Integer");
18+
static final JTextField intInput1 = new JTextField("Enter Your First Integer");
19+
static final JTextField intInput2 = new JTextField("Enter Your Second Integer");
2020

2121
//create an object out of the ConsoleCalculator script
2222
static ConsoleCalculator calculator = new ConsoleCalculator();
@@ -53,17 +53,17 @@ public static void setup()
5353
division.addActionListener(divideButton);
5454

5555
//connects Action Listeners to corresponding text fields
56-
textInput1.addActionListener(text1);
57-
textInput2.addActionListener(text2);
56+
intInput1.addActionListener(text1);
57+
intInput2.addActionListener(text2);
5858

5959
//adds buttons to window/GUI
60-
calcBox.add(textInput1);
60+
calcBox.add(intInput1);
6161
calcBox.setLayout(new FlowLayout());
6262
calcBox.add(addition);
6363
calcBox.add(subtraction);
6464
calcBox.add(multiplication);
6565
calcBox.add(division);
66-
calcBox.add(textInput2);
66+
calcBox.add(intInput2);
6767

6868

6969

@@ -84,7 +84,7 @@ public void actionPerformed(ActionEvent buttonPress)
8484
//what happens when button is pressed
8585
//sets the number1 variable to what the user puts in the text box
8686
//Float.parseFloat changes input from a String to a float
87-
ConsoleCalculator.number1 = Float.parseFloat(textInput1.getText());
87+
ConsoleCalculator.number1 = Float.parseFloat(intInput1.getText());
8888
}
8989
}
9090

@@ -97,7 +97,7 @@ public void actionPerformed(ActionEvent buttonPress)
9797
//what happens when button is pressed
9898
//sets the number2 variable to what the user puts in the text box
9999
//Float.parseFloat changes input from a String to a float
100-
ConsoleCalculator.number2 = Float.parseFloat(textInput2.getText());
100+
ConsoleCalculator.number2 = Float.parseFloat(intInput2.getText());
101101
}
102102
}
103103

@@ -110,9 +110,10 @@ public void actionPerformed(ActionEvent buttonPress)
110110
{
111111
//what happens when button is pressed
112112
System.out.println("Addition Selected");
113-
calculator.doSum();
114-
JLabel label = new JLabel(Float.toString(ConsoleCalculator.answer));
113+
//calculator.doSum();
114+
JLabel label = new JLabel(" = " + calculator.doSum());
115115
calcBox.add(label);
116+
116117
calcBox.invalidate();
117118
calcBox.validate();
118119
calcBox.repaint();
@@ -128,8 +129,7 @@ public void actionPerformed(ActionEvent buttonPress)
128129
{
129130
//what happens when button is pressed
130131
System.out.println("Subtraction Selected");
131-
calculator.doDifference();
132-
JLabel label = new JLabel(Float.toString(ConsoleCalculator.answer));
132+
JLabel label = new JLabel(" = " + calculator.doDifference());
133133
calcBox.add(label);
134134

135135
//reloads the GUI
@@ -148,10 +148,9 @@ public void actionPerformed(ActionEvent buttonPress)
148148
{
149149
//what happens when button is pressed
150150
System.out.println("Multiplication Selected");
151-
calculator.doProduct();
152151

153152
//creates new text to display answer
154-
JLabel label = new JLabel(Float.toString(ConsoleCalculator.answer));
153+
JLabel label = new JLabel(" = " + calculator.doProduct());
155154

156155
//reloads the GUI
157156
calcBox.add(label);
@@ -169,10 +168,10 @@ public void actionPerformed(ActionEvent buttonPress)
169168
{
170169
//what happens when button is pressed
171170
System.out.println("Division Selected");
172-
calculator.doQuotient();
171+
173172

174173
//creates new text to display answer
175-
JLabel label = new JLabel(Float.toString(ConsoleCalculator.answer));
174+
JLabel label = new JLabel(" = " + calculator.doQuotient());
176175
calcBox.add(label);
177176

178177
//reloads the GUI

0 commit comments

Comments
 (0)