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

For the TextField, I want to set it to yellow for input, and green for output. M

ID: 3773906 • Letter: F

Question

For the TextField, I want to set it to yellow for input, and green for output. My prof said we should use show.setBackground(Color.GREEN); in 5 places in the Calculator class to achieve this. Where should they go?

-------------------------------------------

import java.awt.event.*;

import javax.swing.*;

public class BasicCalculator implements ActionListener

{

//attributes

protected JFrame calWin;

protected JPanel keyPad;

protected JTextField show;

protected JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp;

protected JButton add, sub, mul, div, mod, com, clr;

protected double o1, o2;

protected char op;

  

//constructors

public BasicCalculator()

{

clean();

}

  

//methods

public void clean()

{

o1 = 0;

o2 = 0;

op = ' ';

}

  

public void createUserInterface()

{

keyPad = new JPanel();

show = new JTextField(20);

b0 = new JButton("0");

b1 = new JButton("1");

b2 = new JButton("2");

b3 = new JButton("3");

b4 = new JButton("4");

b5 = new JButton("5");

b6 = new JButton("6");

b7 = new JButton("7");

b8 = new JButton("8");

b9 = new JButton("9");

bp = new JButton(".");   

add = new JButton("+");

sub = new JButton("-");

mul = new JButton("*");

div = new JButton("/");

mod = new JButton("%");

com = new JButton("=");

clr = new JButton("C");

  

keyPad.add(show);

keyPad.add(clr);

keyPad.add(b1);   

keyPad.add(b2);

keyPad.add(b3);

keyPad.add(b4);

keyPad.add(b5);

keyPad.add(b6);

keyPad.add(b7);

keyPad.add(b8);

keyPad.add(b9);

keyPad.add(b0);

keyPad.add(bp);

keyPad.add(com);

keyPad.add(add);

keyPad.add(sub);

keyPad.add(mul);

keyPad.add(div);

keyPad.add(mod);

  

calWin.getContentPane().add(keyPad);

calWin.setSize(300, 300);

calWin.setVisible(true);

  

b0.addActionListener(this);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

bp.addActionListener(this);

add.addActionListener(this);

sub.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

mod.addActionListener(this);

com.addActionListener(this);

clr.addActionListener(this);

}

  

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == b0)

{

show.setText(show.getText() + "0");

}

if(e.getSource() == b1)

{

show.setText(show.getText() + "1");

}

if(e.getSource() == b2)

{

show.setText(show.getText() + "2");

}

if(e.getSource() == b3)

{

show.setText(show.getText() + "3");

}

if(e.getSource() == b4)

{

show.setText(show.getText() + "4");

}

if(e.getSource() == b5)

{

show.setText(show.getText() + "5");

}

if(e.getSource() == b6)

{

show.setText(show.getText() + "6");

}

if(e.getSource() == b7)

{

show.setText(show.getText() + "7");

}

if(e.getSource() == b8)

{

show.setText(show.getText() + "8");

}

if(e.getSource() == b9)

{

show.setText(show.getText() + "9");

}

if(e.getSource() == bp)

{

show.setText(show.getText() + ".");

}

if(e.getSource() == add)

{

o1 = Double.parseDouble(show.getText());

op = '+';

show.setText("");

}

if(e.getSource() == sub)

{

if(show.getText().trim().equals(""))

{

show.setText("-");

}

else

{

o1 = Double.parseDouble(show.getText());

op = '-';

show.setText("");

}

}

if(e.getSource() == mul)

{

o1 = Double.parseDouble(show.getText());

op = '*';

show.setText("");

}

if(e.getSource() == div)

{

o1 = Double.parseDouble(show.getText());

op = '/';

show.setText("");

}

if(e.getSource() == mod)

{

o1 = Double.parseDouble(show.getText());

op = '%';

show.setText("");

}

if(e.getSource() == com)

{

o2 = Double.parseDouble(show.getText());

calculate();

}

if(e.getSource() == clr)

{

show.setText("");

clean();

}

}

  

public void calculate()

{

switch(op)

{

case '+': show.setText(new Double(o1 + o2).toString());

break;

case '-': show.setText(new Double(o1 - o2).toString());

break;

case '*': show.setText(new Double(o1 * o2).toString());

break;

case '/': show.setText(new Double(o1 / o2).toString());

break;   

case '%': show.setText(new Double(o1 % o2).toString());

break;

}

}

}

--------------------------------------

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

public class Calculator extends BasicCalculator
{
//attributes
protected JButton sqr, pct, log, pow;
  
//constructors
public Calculator()
{
super();
}
  
//methods
public void createUserInterface()
{
super.createUserInterface();
  
pow = new JButton("^");
sqr = new JButton("sqr");
pct = new JButton("pct");
log = new JButton("log");
  
keyPad.add(pow);
keyPad.add(sqr);
keyPad.add(pct);
keyPad.add(log);
  
pow.addActionListener(this);
sqr.addActionListener(this);
pct.addActionListener(this);
log.addActionListener(this);
  
clr.setBackground(Color.RED);
bp.setBackground(Color.LIGHT_GRAY);
b0.setBackground(Color.LIGHT_GRAY);
b1.setBackground(Color.LIGHT_GRAY);
b2.setBackground(Color.LIGHT_GRAY);
b3.setBackground(Color.LIGHT_GRAY);
b4.setBackground(Color.LIGHT_GRAY);
b5.setBackground(Color.LIGHT_GRAY);
b6.setBackground(Color.LIGHT_GRAY);
b7.setBackground(Color.LIGHT_GRAY);
b8.setBackground(Color.LIGHT_GRAY);
b9.setBackground(Color.LIGHT_GRAY);
pow.setBackground(Color.PINK);
add.setBackground(Color.PINK);
sub.setBackground(Color.PINK);
mul.setBackground(Color.PINK);
div.setBackground(Color.PINK);
mod.setBackground(Color.PINK);
sqr.setBackground(Color.ORANGE);
pct.setBackground(Color.ORANGE);
log.setBackground(Color.ORANGE);
com.setBackground(Color.GREEN);
show.setBackground(Color.YELLOW);
  
show.setHorizontalAlignment(JTextField.RIGHT);
}
  
public void actionPerformed(ActionEvent e)
{
super.actionPerformed(e);
  
if(e.getSource() == pow)
{
o1 = Double.parseDouble(show.getText());
op = '^';
show.setText("");
}
if(e.getSource() == sqr)
{
o1 = Double.parseDouble(show.getText());
show.setText(new Double(Math.sqrt(o1)).toString());
}
if(e.getSource() == pct)
{
o1 = Double.parseDouble(show.getText());
show.setText(new Double(o1 * 100).toString());
}
if(e.getSource() == log)
{
o1 = Double.parseDouble(show.getText());
show.setText(new Double(Math.log(o1)).toString());
}
}
  
public void calculate()
{
super.calculate();
  
if(op == '^')
{
show.setText(new Double(Math.pow(o1, o2)).toString());
}
}
}

--------------------------------------

public class Driver
{
//attributes
  
//constructors
  
//methods
public static void main(String[] args)
{
Calculator c;
c = new Calculator();
c.createUserInterface();
}
}

Explanation / Answer

As, you said, you want the TextField of your calculator to be yellow for input & green for output.

For the input, the color yellow is already set in the createUserInterface() method, in the statement :
show.setBackground(Color.YELLOW);

Now, we shall see how & when we can set the color to green. As we want the color to be green at the time of output, we need to set the color to green, while setting the output. You need to add code in the calculate() method.

public void calculate()

{

switch(op)

{

case '+': show.setText(new Double(o1 + o2).toString());
      show.setBackground(Color.GREEN);

break;

case '-': show.setText(new Double(o1 - o2).toString());
     show.setBackground(Color.GREEN);

break;

case '*': show.setText(new Double(o1 * o2).toString());
     show.setBackground(Color.GREEN);

break;

case '/': show.setText(new Double(o1 / o2).toString());
      show.setBackground(Color.GREEN);

break;   

case '%': show.setText(new Double(o1 % o2).toString());
      show.setBackground(Color.GREEN);

break;

}

}