Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java program How to change the layout like the picture above. package cal; import java.awt.*; import java.awt.event.*; public class MyCalculator extends Frame { public boolean

Java program

image text in transcribed

How to change the layout like the picture above.

package cal;

import java.awt.*; import java.awt.event.*;

public class MyCalculator extends Frame {

public boolean setClear = true; double number, memValue; char op;

String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "." }; String operatorButtonText[] = {"/", "*", "-", "+", "=" }; String specialButtonText[] = {"

MyDigitButton digitButton[] = new MyDigitButton[digitButtonText.length]; MyOperatorButton operatorButton[] = new MyOperatorButton[operatorButtonText.length]; MySpecialButton specialButton[] = new MySpecialButton[specialButtonText.length];

Label displayLabel = new Label("0", Label.RIGHT); Label memLabel = new Label(" ", Label.RIGHT);

final int FRAME_WIDTH = 325, FRAME_HEIGHT = 325; final int HEIGHT = 30, WIDTH = 30, H_SPACE = 10, V_SPACE = 10; final int TOPX = 30, TOPY = 50; /////////////////////////// MyCalculator(String frameText) { //constructor super(frameText);

int tempX = TOPX, y = TOPY; displayLabel.setBounds(tempX, y, 240, HEIGHT); displayLabel.setBackground(Color.BLUE); displayLabel.setForeground(Color.WHITE); add(displayLabel);

memLabel.setBounds(TOPX, TOPY + HEIGHT + V_SPACE, WIDTH, HEIGHT); add(memLabel);

//set Co-ordinates for Special Buttons tempX = TOPX + 1 * (WIDTH + H_SPACE); y = TOPY + 1 * (HEIGHT + V_SPACE); for (int i = 0; i

//set Co-ordinates for Digit Buttons int digitX = TOPX + WIDTH + H_SPACE; int digitY = TOPY + 2 * (HEIGHT + V_SPACE); tempX = digitX; y = digitY; for (int i = 0; i

//set Co-ordinates for Operator Buttons int opsX = digitX + 2 * (WIDTH + H_SPACE) + H_SPACE; int opsY = digitY; tempX = opsX; y = opsY; for (int i = 0; i

addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } });

setLayout(null); setSize(FRAME_WIDTH, FRAME_HEIGHT); setVisible(true); } ////////////////////////////////// static String getFormattedText(double temp) { String resText = "" + temp; if (resText.lastIndexOf(".0") > 0) resText = resText.substring(0, resText.length() - 2); return resText; } //////////////////////////////////////// public static void main(String []args) { new MyCalculator("Calculator - JavaTpoint"); } }

/*******************************************/

class MyDigitButton extends Button implements ActionListener { MyCalculator cl;

////////////////////////////////////////// MyDigitButton(int x, int y, int width, int height, String cap, MyCalculator clc) { super(cap); setBounds(x, y, width, height); this.cl = clc; this.cl.add(this); addActionListener(this); } //////////////////////////////////////////////// static boolean isInString(String s, char ch) { for (int i = 0; i

if (tempText.equals(".")) { if (cl.setClear) { cl.displayLabel.setText("0."); cl.setClear = false; } else if (!isInString(cl.displayLabel.getText(), '.')) cl.displayLabel.setText(cl.displayLabel.getText() + "."); return; }

int index = 0; try { index = Integer.parseInt(tempText); } catch (NumberFormatException e) { return; }

if (index == 0 && cl.displayLabel.getText().equals("0")) return;

if (cl.setClear) { cl.displayLabel.setText("" + index); cl.setClear = false; } else cl.displayLabel.setText(cl.displayLabel.getText() + index); }//actionPerformed }//class defination

/********************************************/

class MyOperatorButton extends Button implements ActionListener { MyCalculator cl;

MyOperatorButton(int x, int y, int width, int height, String cap, MyCalculator clc) { super(cap); setBounds(x, y, width, height); this.cl = clc; this.cl.add(this); addActionListener(this); } /////////////////////// public void actionPerformed(ActionEvent ev) { String opText = ((MyOperatorButton)ev.getSource()).getLabel();

cl.setClear = true; double temp = Double.parseDouble(cl.displayLabel.getText());

if (!opText.equals("=")) { cl.number = temp; cl.op = opText.charAt(0); return; } // process = button pressed switch (cl.op) { case '+': temp += cl.number; break; case '-': temp = cl.number - temp; break; case '*': temp *= cl.number; break; case '/': try { temp = cl.number / temp; } catch (ArithmeticException excp) { cl.displayLabel.setText("Divide by 0."); return; } break; }//switch

cl.displayLabel.setText(MyCalculator.getFormattedText(temp)); //cl.number=temp; }//actionPerformed }//class

/****************************************/

class MyMemoryButton extends Button implements ActionListener { MyCalculator cl;

///////////////////////////////// MyMemoryButton(int x, int y, int width, int height, String cap, MyCalculator clc) { super(cap); setBounds(x, y, width, height); this.cl = clc; this.cl.add(this); addActionListener(this); } //////////////////////////////////////////////// public void actionPerformed(ActionEvent ev) { char memop = ((MyMemoryButton)ev.getSource()).getLabel().charAt(1);

cl.setClear = true; double temp = Double.parseDouble(cl.displayLabel.getText());

switch (memop) { case 'C': cl.memLabel.setText(" "); cl.memValue = 0.0; break; case 'R': cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue)); break; case 'S': cl.memValue = 0.0; case '+': cl.memValue += Double.parseDouble(cl.displayLabel.getText()); if (cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0") ) cl.memLabel.setText(" "); else cl.memLabel.setText("M"); break; }//switch }//actionPerformed }//class

/*****************************************/

class MySpecialButton extends Button implements ActionListener { MyCalculator cl;

MySpecialButton(int x, int y, int width, int height, String cap, MyCalculator clc) { super(cap); setBounds(x, y, width, height); this.cl = clc; this.cl.add(this); addActionListener(this); } ////////////////////// static String backSpace(String s) { String Res = ""; for (int i = 0; i

////////////////////////////////////////////////////////// public void actionPerformed(ActionEvent ev) { String opText = ((MySpecialButton)ev.getSource()).getLabel(); //check for backspace button if (opText.equals("

//it must be CE button pressed cl.displayLabel.setText("0"); cl.setClear = true; }//actionPerformed }//class

(3,5)*2+ 30

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago