Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added Bash
Empty file.
44 changes: 44 additions & 0 deletions src/main/TestScientificCalculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.zipcodewilmington.scientificcalculator;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestScientificCalculator {

@Test
void newCalculatorShouldStartAtZero() {
ScientificCalculator calculator = new ScientificCalculator();

assertEquals(0, calculator.getDisplay());
}

@Test
void setDisplayShouldChangeDisplay() {
ScientificCalculator calculator = new ScientificCalculator();

calculator.setDisplay(25);

assertEquals(25, calculator.getDisplay());
}

@Test
void clearShouldResetDisplayToZero() {
ScientificCalculator calculator = new ScientificCalculator();

calculator.setDisplay(25);
calculator.clear();

assertEquals(0, calculator.getDisplay());
}

@Test
void addShouldUpdateDisplay() {
ScientificCalculator calculator = new ScientificCalculator();

calculator.setDisplay(5);
calculator.add(3);

assertEquals(8, calculator.getDisplay());
}
}
241 changes: 241 additions & 0 deletions src/main/java/com/zipcodewilmington/CalculatorGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package com.zipcodewilmington.scientificcalculator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class CalculatorGUI {

private static final Color PINK = new Color(255, 105, 180);
private static final Color YELLOW = new Color(255, 215, 0);
private static final Color LIGHT_PINK = new Color(255, 230, 240);

private final ScientificCalculator calculator;
private final JTextField displayField;

private String pendingOperator = "";
private boolean startNewNumber = true;

public CalculatorGUI() {
calculator = new ScientificCalculator();
displayField = new JTextField("0");

createWindow();
}

private void createWindow() {
JFrame frame = new JFrame("Team TI Scientific Calculator");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 560);
frame.setLayout(new BorderLayout(10, 10));

JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBackground(LIGHT_PINK);
mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

JLabel title = new JLabel(
"Team TI SCIENTIFIC CALCULATOR",
SwingConstants.CENTER
);

title.setFont(new Font("Arial", Font.BOLD, 22));
title.setOpaque(true);
title.setBackground(YELLOW);
title.setForeground(Color.BLACK);
title.setBorder(BorderFactory.createEmptyBorder(12, 8, 12, 8));

displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
displayField.setFont(new Font("Arial", Font.BOLD, 34));
displayField.setBackground(Color.WHITE);
displayField.setBorder(
BorderFactory.createLineBorder(PINK, 4)
);

JPanel topPanel = new JPanel(new BorderLayout(0, 10));
topPanel.setBackground(LIGHT_PINK);
topPanel.add(title, BorderLayout.NORTH);
topPanel.add(displayField, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel(new GridLayout(6, 4, 8, 8));
buttonPanel.setBackground(LIGHT_PINK);

String[] buttons = {
"7", "8", "9", "÷",
"4", "5", "6", "×",
"1", "2", "3", "−",
"0", ".", "=", "+",
"C", "x²", "√", "+/−",
"1/x", "xʸ", "", ""
};

for (String label : buttons) {
JButton button = new JButton(label);

if (label.isEmpty()) {
button.setEnabled(false);
}

button.setFont(new Font("Arial", Font.BOLD, 18));
button.setFocusPainted(false);

if (isOperator(label)) {
button.setBackground(PINK);
button.setForeground(Color.WHITE);
} else {
button.setBackground(YELLOW);
button.setForeground(Color.BLACK);
}

button.addActionListener(event -> handleButton(label));
buttonPanel.add(button);
}

mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);

frame.add(mainPanel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private boolean isOperator(String label) {
return label.equals("+")
|| label.equals("−")
|| label.equals("×")
|| label.equals("÷")
|| label.equals("=")
|| label.equals("C");
}

private void handleButton(String label) {
if (label.matches("[0-9]") || label.equals(".")) {
enterNumber(label);
return;
}

switch (label) {
case "+":
case "−":
case "×":
case "÷":
case "xʸ":
chooseOperator(label);
break;

case "=":
calculateResult();
break;

case "C":
clearCalculator();
break;

case "x²":
calculator.setDisplay(readDisplay());
calculator.square();
updateDisplay();
break;

case "√":
calculator.setDisplay(readDisplay());
calculator.squareRoot();
updateDisplay();
break;

case "+/−":
calculator.setDisplay(readDisplay());
calculator.switchSign();
updateDisplay();
break;

case "1/x":
calculator.setDisplay(readDisplay());
calculator.inverse();
updateDisplay();
break;
}
}

private void enterNumber(String value) {
if (startNewNumber) {
displayField.setText("");
startNewNumber = false;
}

if (value.equals(".") && displayField.getText().contains(".")) {
return;
}

displayField.setText(displayField.getText() + value);
}

private void chooseOperator(String operator) {
calculator.setDisplay(readDisplay());
pendingOperator = operator;
startNewNumber = true;
}

private void calculateResult() {
double secondNumber = readDisplay();

switch (pendingOperator) {
case "+":
calculator.add(secondNumber);
break;

case "−":
calculator.subtract(secondNumber);
break;

case "×":
calculator.multiply(secondNumber);
break;

case "÷":
calculator.divide(secondNumber);
break;

case "xʸ":
calculator.power(secondNumber);
break;
}

updateDisplay();
pendingOperator = "";
}

private void clearCalculator() {
calculator.clear();
displayField.setText("0");
pendingOperator = "";
startNewNumber = true;
}

private double readDisplay() {
return Double.parseDouble(displayField.getText());
}

private void updateDisplay() {
displayField.setText(
String.valueOf(calculator.getDisplay())
);

startNewNumber = true;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(CalculatorGUI::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/
public class MainApplication {
public static void main(String[] args) {
Console.println("Welcome to my calculator!");
String s = Console.getStringInput("Enter a string");
Integer i = Console.getIntegerInput("Enter an integer");
Double d = Console.getDoubleInput("Enter a double.");

Console.println("The user input %s as a string", s);
Console.println("The user input %s as a integer", i);
Console.println("The user input %s as a d", d);
}
ScientificCalculator calculator = new ScientificCalculator();

calculator.setDisplay(5);

calculator.switchSign();

Console.println("Result: %s", calculator.getDisplay());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.zipcodewilmington.scientificcalculator;

public class ScientificCalculator {

private double display;
private double memory;
private boolean error;
private String displayMode;
private String unitsMode;

public ScientificCalculator() {
display = 0;
memory = 0;
error = false;
displayMode = "decimal";
unitsMode = "degrees";
}

public double getDisplay() {
return display;
}
public void setDisplay(double value) {
display = value; }
public void clear() {
display = 0;
error = false;
}
public void add(double value) {
display = display + value;
}
public void subtract(double value) {
display = display - value;
}
public void multiply(double value) {
display = display * value;
}
public void divide(double value) {
if (value == 0) {
error = true;
} else {
display = display / value;
}
}
public void square() {
display = display * display;
}
public void squareRoot() {
if (display < 0) {
error = true;
} else {
display = Math.sqrt(display);
}
}
public void inverse() {
if (display == 0) {
error = true;
} else {
display = 1 / display;
}
}
public void switchSign() {
display = -display;
}
public void power(double exponent) {
display = Math.pow(display, exponent);
}
}