Need comments added to program please import javax.swing.*; import java.awt.*; i
ID: 3667678 • Letter: N
Question
Need comments added to program please
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField DisText = new JTextField(30);
private JButton[] Btn = new JButton[16];
private String[] Keys =
{
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
private String Num1 = "";
private String Num2 = "";
private char Operater;
private boolean Input1 = true;
public Calculator()
{
setTitle("My Calculator");
setSize(230, 200);
Container MyPane = getContentPane();
MyPane.setLayout(null);
DisText.setSize(200, 30);
DisText.setLocation(10, 10);
MyPane.add(DisText);
int X, Y;
X = 10;
Y = 40;
for(int ind = 0; ind < 16; ind++)
{
Btn[ind] = new JButton(Keys[ind]);
Btn[ind].addActionListener(this);
Btn[ind].setSize(50, 30);
Btn[ind].setLocation(X, Y);
MyPane.add(Btn[ind]);
X = Y + 50;
if((ind + 1) % 4 == 0)
{
Y = 10;
Y = Y + 30;
}
}
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String resultStr; //Step 1
String str = String.valueOf(e.getActionCommand()); //Steps 1 and 2
char CH = str.charAt(0); //Steps 1 and 3
switch (CH) //Step 4
{
case '0': case '1': case '2': //Step 4a
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9':
if (Input1)
{
Num1 = Num1 + CH;
DisText.setText(Num1);
}
else
{
Num2 = Num2 + CH;
DisText.setText(Num2);
}
break;
case '+': case '-': case '*': //Step 4b
case '/': Operater = CH;
Input1 = false;
break;
case '=': resultStr = evaluate(); //Step 4c
DisText.setText(resultStr);
Num1 = resultStr;
Num2 = "";
Input1 = false;
break;
case 'C': DisText.setText(""); //Step 4c
Num1 = "";
Num2 = "";
Input1 = true;
}
}
private String evaluate()
{
final char beep = 'u0007';
try
{
int num1 = Integer.parseInt(Num1);
int num2 = Integer.parseInt(Num2);
int result = 0;
switch (Operater)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/': result = num1 / num2;
}
return String.valueOf(result);
}
catch(ArithmeticException e)
{
System.out.print(beep);
return "E R R O R: " + e.getMessage();
}
catch(NumberFormatException e)
{
System.out.print(beep);
if(Num1.equals(""))
return "E R R O R: Invalid First Number";
else
return "E R R O R: Invalid Second Number";
}
catch(Exception e)
{
System.out.print(beep);
return "E R R O R";
}
}
public static void main(String[] args)
{
Calculator C = new Calculator();
}
}
Explanation / Answer
Below is the code with appropriate comments:
import javax.swing.*; // it is class to implement graphical interface in java program
import java.awt.*; // class to use some graphical elements like buttons etc
import java.awt.event.*; // class to add events on buttons or textboxes like button press, button release, mouse over etc
import java.io.*; // used for input output
public class Calculator extends JFrame implements ActionListener // calculator class will use JFrame(graphical window) which is part of swing package
// and will use events which is part of AWT package
{
private JTextField DisText = new JTextField(30); //.. this will create textbox of lengt 30 characters
private JButton[] Btn = new JButton[16]; // this will create button array of 16 buttons
private String[] Keys = // this will create array of keys to place over 16 buttons
{
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
private String Num1 = "";
private String Num2 = "";
private char Operater;
private boolean Input1 = true;
public Calculator() // constructor
{
setTitle("My Calculator"); // will set title of calculator window frame
setSize(230, 200); //set size of window(X and Y coordinate)
Container MyPane = getContentPane(); //this is panel inside frame
MyPane.setLayout(null);
DisText.setSize(200, 30); // setting size of textbox created above(length and breadth)
DisText.setLocation(10, 10); // setting location of text box inside panel
MyPane.add(DisText); //adding textbox to panel finally
int X, Y;
X = 10;
Y = 40;
for(int ind = 0; ind < 16; ind++)
{
Btn[ind] = new JButton(Keys[ind]); // initializing every button with its name which comes from keys array
Btn[ind].addActionListener(this); // adding action listen on buttons if button is pressed event will generate and we can handel that.
Btn[ind].setSize(50, 30); // setting size of button created above(length and breadth)
Btn[ind].setLocation(X, Y); // setting location of button inside panel
MyPane.add(Btn[ind]); //adding button to panel finally
X = Y + 50; // change x and y values otherwise every button will add on same location one over other
if((ind + 1) % 4 == 0)
{
Y = 10;
Y = Y + 30;
}
}
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) // this is for if someone close the window program will end.
{
System.exit(0);
}
});
setVisible(true); // this is used to show window or not.
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) // if button is pressed and event is generated then this part will handle that event. This part will perform whole
// functioning of calculator.
{
String resultStr; //Step 1
String str = String.valueOf(e.getActionCommand()); //Steps 1 and 2 // str will contain value of button which is pressed
char CH = str.charAt(0); //Steps 1 and 3
switch (CH) //Step 4 // on the basis of button value Below switch case will decide what needs to be done. Example if
// 1 is press and then 2 is press and then + is press. It will add both values and show it in text box.
// Similarly for other values different case will execute.
{
case '0': case '1': case '2': //Step 4a
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9':
if (Input1)
{
Num1 = Num1 + CH;
DisText.setText(Num1);
}
else
{
Num2 = Num2 + CH;
DisText.setText(Num2);
}
break;
case '+': case '-': case '*': //Step 4b
case '/': Operater = CH;
Input1 = false;
break;
case '=': resultStr = evaluate(); //Step 4c
DisText.setText(resultStr);
Num1 = resultStr;
Num2 = "";
Input1 = false;
break;
case 'C': DisText.setText(""); //Step 4c
Num1 = "";
Num2 = "";
Input1 = true;
}
}
private String evaluate()
{
final char beep = 'u0007';
try
{
int num1 = Integer.parseInt(Num1);
int num2 = Integer.parseInt(Num2);
int result = 0;
switch (Operater)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/': result = num1 / num2;
}
return String.valueOf(result);
}
catch(ArithmeticException e) // these are the catch block to handle possible exception which can be generated at run time by user mistake.
// like if divide by 0 case comes then it is handle by (ArithmeticException block. Similarly if
// user press invalid type button then NumberFormatException block will handle and so on
{
System.out.print(beep);
return "E R R O R: " + e.getMessage();
}
catch(NumberFormatException e)
{
System.out.print(beep);
if(Num1.equals(""))
return "E R R O R: Invalid First Number";
else
return "E R R O R: Invalid Second Number";
}
catch(Exception e)
{
System.out.print(beep);
return "E R R O R";
}
}
public static void main(String[] args) // Main method to first call constructor by creating class object.
{
Calculator C = new Calculator();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.