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

Calculator Lab Objective: To understand GUI components and layout managers. Part

ID: 3846951 • Letter: C

Question

Calculator Lab

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 c

Explanation / Answer

CalculatorDriver.java

import java.applet.*;
import java.awt.*;
public class Calculator extends Applet
{
Frame window;
   public void init()
{
window = new Frame();               // Creating a new frame for calculator

window.setTitle( "Calculator" );           // Setting the title
window.setLayout( new FlowLayout() );       // Using the FlowLayout
window.setFont( new Font("Helvetica",Font.PLAIN,12) );
window.setBackground(Color.white);
window.add( new CalculatorPad() );          
window.resize( window.preferredSize() );
window.pack();
window.show();
}
}

CalculatorPanel.java

import java.applet.*;
import java.awt.*;

public class CalculatorPad extends Panel
{
TextField display;      
Panel keys;          

int maxLength = 20;      
String output = "0";                  
boolean decimal = false;
float result=0.0f;      
String function="";      
boolean newNumber = true;  
boolean finished = false;  
boolean memory = false;  
float memoryValue = 0.0f;

public CalculatorPad()
{
setLayout( new BorderLayout() );
setFont( new Font("Helvetica", Font.PLAIN, 12) );
setBackground(Color.lightGray);

display = new TextField(maxLength+1);
display.setEditable(false);
display.setFont( new Font("Helvetica", Font.PLAIN, 12) );
display.setBackground(Color.white);

keys = new Panel();
keys.setLayout( new GridLayout(5,5) );
keys.setFont( new Font("Helvetica", Font.PLAIN, 12) );
keys.setBackground(Color.lightGray);

keys.add( new Button("+/-") );
keys.add( new Button("") );
keys.add( new Button("") );
keys.add( new Button("") );
keys.add( new Button("AC") );

keys.add( new Button("M+") );
keys.add( new Button("7") );
keys.add( new Button("8") );
keys.add( new Button("9") );
keys.add( new Button("/") );

keys.add( new Button("M-") );
keys.add( new Button("4") );
keys.add( new Button("5") );
keys.add( new Button("6") );
keys.add( new Button("x") );

keys.add( new Button("MR") );
keys.add( new Button("1") );
keys.add( new Button("2") );
keys.add( new Button("3") );
keys.add( new Button("-") );

keys.add( new Button("MC") );
keys.add( new Button("0") );
keys.add( new Button(".") );
keys.add( new Button("=") );
keys.add( new Button("+") );

add("North", display);      
add("Center", new Label(""));  
add("South", keys);      

display.resize( display.preferredSize() );
keys.resize( keys.preferredSize() );

updateDisplay();          

show();
}

public void updateDisplay()
{
String output_right = "";      
for(int i=1; i<=(maxLength-output.length()); i++)
{
if ((i==1) && (memory))
output_right = output_right + "M";
else
output_right = output_right + "_";

}
output_right = output_right + output;
display.setText(output_right);
}

public void appendDigit(String new_d)
{
if (output == "0")      
output = "";      

if (output.length() < maxLength)
{
if (newNumber)          
{
output = new_d;      
newNumber = false;      
}
else
output = output + new_d;  
updateDisplay();      
}

if (finished || function == "")  
{                  
result = Float.valueOf(output).floatValue();
finished = false;
function = "";
}
}

public void key_equals()
{
evaluate();
   function = "";          
newNumber = true;
decimal = false;
finished = true;
} // End =

public void evaluate()
{                  
if (function == "plus")
result += Float.valueOf(output).floatValue();
else
if (function == "minus")
result -= Float.valueOf(output).floatValue();
else
if (function == "times")
result *= Float.valueOf(output).floatValue();
else
if (function == "div")
result /= Float.valueOf(output).floatValue();
else
result = Float.valueOf(output).floatValue();

if (finished == true)      
finished = false;      
output = Float.toString(result);
updateDisplay();

newNumber = true;
decimal = false;
}

public boolean handleEvent(Event evt)
{
if (evt.arg == "AC")
{
result = 0;
output = Float.toString(result);

function = "";          
newNumber = true;
decimal = false;
finished = true;

updateDisplay();
}

if (evt.arg == "+/-")
{                  
float val = Float.valueOf(output).floatValue();
val *= -1;
output = Float.toString(val);
updateDisplay();
}

if (evt.arg == "1") appendDigit("1");
if (evt.arg == "2") appendDigit("2");
if (evt.arg == "3") appendDigit("3");
if (evt.arg == "4") appendDigit("4");
if (evt.arg == "5") appendDigit("5");
if (evt.arg == "6") appendDigit("6");
if (evt.arg == "7") appendDigit("7");
if (evt.arg == "8") appendDigit("8");
if (evt.arg == "9") appendDigit("9");
if (evt.arg == "0")
if (output.length() != 0) appendDigit("0");

if (evt.arg == ".")
if (output.length() < maxLength)
if (!decimal)
{
decimal = true;
if (output.length() == 0 || newNumber)
{
output = "0.";
newNumber = false;
}
else
output = output + ".";

updateDisplay();      
}

if (evt.arg == "=")      
key_equals();

if (evt.arg == "+")
{
evaluate();          
function = "plus";      
}

if (evt.arg == "-")
{
evaluate();          
function = "minus";      
}

if (evt.arg == "x")
{
evaluate();          
function = "times";      
}

if (evt.arg == "/")
{
evaluate();          
function = "div";      
}

if (evt.arg == "M+")
{
memory = true;          
key_equals();          
                  
memoryValue += Float.valueOf(output).floatValue();
updateDisplay();
}

if (evt.arg == "M-")
{
memory = true;          
key_equals();          
                  
memoryValue -= Float.valueOf(output).floatValue();
updateDisplay();
}

if (evt.arg == "MR")
{
if (memory)
{
output = Float.toString(memoryValue);
updateDisplay();
}
}

if (evt.arg == "MC")
{
memory = false;
memoryValue = 0.0f;
updateDisplay();
}

if (output == "")      
output = "0";      

return false;
}

}

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