Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Calculator Lab In JAVA jGrasp Objective: To understand GUI components and layout

ID: 3846824 • Letter: C

Question

Calculator Lab In JAVA jGrasp

Objective:

    To understand GUI components and layout managers.

Part 1 – Design the Calculator Interface

Design the interface for a simple four function calculator. Think about the different components that make up the interface and the layout managers you will need to use to achieve the desired look. Don’t forget about the components that you can’t see in the picture. Be creative. Customize the look of your calculator by changing the colors of the buttons, the font, or add images to the buttons.

Specification

Write 2 classes:

CalculatorDriver.java

This file should contain the main method and create the frame for your calculator. Remember to add the calculator panel to the frame and make your frame visible.

CalculatorPanel.java

Create CalculatorPanel to be a subclass of JPanel. Define each component that makes up the interface as an instance variable.

The constructor should create each component, set the appropriate layout managers, and customize any components (color, font, size, etc) .

      Don’t worry about getting the buttons to work yet. You are simply designing the interface.

Testing

Be sure to test resizing the frame of your calculator. How does resizing the frame affect the layout of your interface?

Part 2- Make your calculator functional. Get your 4 basic math operation buttons (+, -, x, /) to work like a real calculator. Think about how many listeners you need. You should add a “clear” button to your calculator.

Explanation / Answer

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class CalculatorPanel extends JFrame implements ActionListener {
  
JPanel[] panels = new JPanel[5];
JButton[] button = new JButton[19];
String[] buttonString = {"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
".", "/", "C", "sq",
"+/-", "=", "0"};
  
char op;
double[] operand = {0, 0};
JTextArea lcd = new JTextArea(1,20);
  
  
CalculatorPanel() {
super("Calculator");
setDesign();
setSize(380, 250);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(5,5);
setLayout(grid);
  
  
  
FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
FlowLayout f2 = new FlowLayout(FlowLayout.CENTER,1,1);
for(int i = 0; i < 5; i++)
panels[i] = new JPanel();
panels[0].setLayout(f1);
for(int i = 1; i < 5; i++)
panels[i].setLayout(f2);
  
for(int i = 0; i < 19; i++) {
button[i] = new JButton();
button[i].setText(buttonString[i]);
  
button[i].addActionListener(this);
}
  
  
lcd.setEditable(false);
  
lcd.setPreferredSize(new Dimension(280,32));
for(int i = 0; i < 14; i++)
button[i].setPreferredSize(new Dimension(45, 40));
for(int i = 14; i < 18; i++)
button[i].setPreferredSize(new Dimension(100, 40));
button[18].setPreferredSize(new Dimension(90,40));
  
panels[0].add(lcd);
add(panels[0]);
  
for(int i = 0; i < 4; i++)
panels[1].add(button[i]);
panels[1].add(button[14]);
add(panels[1]);
  
for(int i = 4; i < 8; i++)
panels[2].add(button[i]);
panels[2].add(button[15]);
add(panels[2]);
  
for(int i = 8; i < 12; i++)
panels[3].add(button[i]);
panels[3].add(button[16]);
add(panels[3]);
  
panels[4].add(button[18]);
for(int i = 12; i < 14; i++)
panels[4].add(button[i]);
panels[4].add(button[17]);
add(panels[4]);
  
setVisible(true);
}
  
public void clear() {
try {
lcd.setText("");
for(int i = 0; i < 4; i++)
op=' ';
  
} catch(NullPointerException e) {
}
}
  
public void getSqrt() {
try {
double value = Math.sqrt(Double.parseDouble(lcd.getText()));
lcd.setText(Double.toString(value));
} catch(NumberFormatException e) {
}
}
  
public void getPosNeg() {
try {
double value = Double.parseDouble(lcd.getText());
if(value != 0) {
value = value * (-1);
lcd.setText(Double.toString(value));
}
else {
}
} catch(NumberFormatException e) {
}
}
  
public void getResult() {
double result = 0;
operand[1] = Double.parseDouble(lcd.getText());
String temp0 = Double.toString(operand[0]);
String temp1 = Double.toString(operand[1]);
try {
if(temp0.contains("-")) {
String[] temp00 = temp0.split("-", 2);
operand[0] = (Double.parseDouble(temp00[1]) * -1);
}
if(temp1.contains("-")) {
String[] temp11 = temp1.split("-", 2);
operand[1] = (Double.parseDouble(temp11[1]) * -1);
}
} catch(ArrayIndexOutOfBoundsException e) {
}
try {
if(op=='*')
result = operand[0] * operand[1];
else if(op=='/')
result = operand[0] / operand[1];
else if(op=='+')
result = operand[0] + operand[1];
else if(op=='-')
result = operand[0] - operand[1];
lcd.setText(Double.toString(result));
  
} catch(NumberFormatException e) {
}
}
  
public final void setDesign() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e) {   
}
}
  
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button[0])
lcd.append("7");
if(e.getSource() == button[1])
lcd.append("8");
if(e.getSource() == button[2])
lcd.append("9");
if(e.getSource() == button[3]) {
//add function[0]
operand[0] = Double.parseDouble(lcd.getText());
op='+';
  
lcd.setText("");
}
if(e.getSource() == button[4])
lcd.append("4");
if(e.getSource() == button[5])
lcd.append("5");
if(e.getSource() == button[6])
lcd.append("6");
if(e.getSource() == button[7]) {
//subtract function[1]
operand[0] = Double.parseDouble(lcd.getText());
op='-';
  
lcd.setText("");
}
if(e.getSource() == button[8])
lcd.append("1");
if(e.getSource() == button[9])
lcd.append("2");
if(e.getSource() == button[10])
lcd.append("3");
if(e.getSource() == button[11]) {
//multiply function[2]
operand[0] = Double.parseDouble(lcd.getText());
op='*';
  
lcd.setText("");
}
if(e.getSource() == button[12])
lcd.append(".");
if(e.getSource() == button[13]) {
//divide function[3]
operand[0] = Double.parseDouble(lcd.getText());
op='/';
  
lcd.setText("");
}
if(e.getSource() == button[14])
clear();
if(e.getSource() == button[15])
getSqrt();
if(e.getSource() == button[16])
getPosNeg();
if(e.getSource() == button[17])
getResult();
if(e.getSource() == button[18])
lcd.append("0");
}
  
}

public class CalculatorDriver
{
public static void main(String[] arguments) {
CalculatorPanel c = new CalculatorPanel();
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote