Project 2 Project 2 is a continuation of the calculator graphical user interface
ID: 3857323 • Letter: P
Question
Project 2 Project 2 is a continuation of the calculator graphical user interface program that you wrote in project 1. Enhance the calculator program to accomplish the following 4 operations. 1. Perform addition function using the calculator graphical user interface. Write code to add a plus (+) button in calculator graphical interface, and write code so that two numbers can be added, when the plus button is pressed. 2. Perform subtraction function using the calculator graphical user interface. Write code to add a minus (-) button in calculator graphical interface, and write code so that you can subtract one number from the other, when the minus button is pressed. 3. Perform multiplication function using the calculator graphical user interface. Write code to add a multiplication (x) button in calculator graphical interface, and write code so that two numbers can be multiplied, when the multiplication button is pressed. 4. Perform division function using calculator graphical user interface. Write code to add a divide (/) button in calculator graphical interface, and write code so that two numbers can be divided, when the division button is pressed. Handle error condition raised, if divided by zero. In other words, the program should not terminate, or throw exception, if the user is trying to divide by zero. Instead, the calculator will say “Cannot divide by zero”. You can demonstrate the use of whole number in your program. You are not required to code for decimal values. You may use any programming language of your choice, examples are Java, or C++, or C# etc. It is typical that the students would use the same programming language for Project 2, which they used for Pproject1. Deliverables: Deliverables for this project include the following sections. Label the section appropriately, as noted below. Section 1- Approach Write up, or explanation on your approach. Write a paragraph, or two on this. Section 2- Assumptions Write up, or explanation on any assumption that you have made. Write a paragraph, or two on this. If you have not made any assumption, you may keep this section empty. Section 3- Not Implemented Write up, or explanation on functions that you were not able to implement. Explain challenges faced, and effort you made to resolve it. Write a paragraph, or two on this. Consider this your way to defend yourself and explain the effort made. If you have implemented all the functions, then keep this section empty. Section 4 – lessons Learned Write up on what lessons you have learned. Write a paragraph, or two on this. Section 5- Possible Improvements Write up on ways to improve your code. Improvement could be something you would have done different, but was not able to do because the scope, or time did not permit, functions that you were not able to implement and the reason for not doing, a different approach that you would have taken a different way of solving the problem etc. Write a paragraph, or two on this. Section 6 – Source Code Include the source code that you have written. Copy and paste it in the MS Word document. Section 7 – Test Plans Write up on the test conditions (test plans) that you have tested. Include the positive test (conditions that pass the program), and negative test (conditions that will make the program not perform), explain those scenarios. Section 8 – Test Runs with Screen Shot Include screen shots of user interfaces generated while you tested the program. For example, when you run the program, you will receive GUI output, or console output. Include screen shot of each test run of the GUI output to support successful running of the program. Use these screen shots to demonstrate that you have successfully run the program and tested it. What to submit: 1. A single MS Word (or PDF) file including the answers for the above 8 sections. In a single MS Word (or PDF) file, include the answers for the above 8 sections. Number each section, as noted above. Name the MS Word (or PDF) document as lastname_firstname_project1 and upload that single document in the submission area. 2. Zip file with the source code Upload a separate file (a zip file) containing all your source code in the in a single zip file. For example, if you wrote the code in Java, only include the .java files, do not include .class files. In other words, you will submit a single MS Word file, or a PDF (and yes, the document will include the copy and paste of the source code, as that is one of the sections, which is Section 6), and also a zip file with the actual source code. The course shell allows upload of multiple files, which is one MS Word (or PDF) in this case, plus a zip file (containing the source code). It may sound redundant for you to include the source code (copy and paste) ibn the document, and also in the zip file. But please follow so. Grading Rubric Your score may range between minimum and maximum for each attribute. Grading Attribute Meets Does not meet Functionality I will look at your documentation (containing copy and paste of code) and also the zip file (containing the source code) for this. Maximum possible points: 40 Writes a program that meets the functionality of plus, minus, multiply and divide Minimum possible points: 0 Does not meet the functionality. Output I will look at your documentation and source code for this. Maximum possible points: 30 Generates the expected GUI and the test results. Minimum possible points: 0 Does not generate the GUI and the expected test results. Documentation I will look at your documentation for this. Maximum possible points: 30 High quality of the documentation provided, including the 8 sections. Minimum possible points: 0 Documentation is not provided.
Here is my code from last assignment.
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RecursiveDescentParser
{
private int currentIndex;
private String[] data;
JFrame window = null;
public RecursiveDescentParser()
{
init();
}
public static void main(String[] args)
{
RecursiveDescentParser RecursiveDescentParser = new RecursiveDescentParser();
}
private String getGrammer() throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader("Calculator.txt"));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
}
private void init()
{
try
{
String grammer = getGrammer();
data = grammer.split(" ");
ArrayList<String> tokensList = getTokens(data[0]);
String component = tokensList.get(0);
if (component.equalsIgnoreCase("Window"))
{
try
{
String title = tokensList.get(1).replace(""", "");
int width = Integer.parseInt(tokensList.get(2).substring(1, tokensList.get(2).length() - 1));
int height = Integer.parseInt(tokensList.get(3).substring(0, tokensList.get(3).length() - 1));
LayoutManager layout = null;
if (tokensList.size() > 4)
{
if (tokensList.get(4).equalsIgnoreCase("Layout"))
{
layout = getLayout(tokensList.subList(5, tokensList.size()));
}
else
{
throw new Exception("Syntax error");
}
}
window = (createWindow(title, width, height, layout));
System.out.println(title + ", " + width + ", " + height);
}
catch (Exception e)
{
e.printStackTrace();
throw new Exception("Syntax error");
}
}
else
{
throw new Exception("No GUI found for this input");
}
for (currentIndex = 1; currentIndex < data.length; currentIndex++)
{
tokensList.clear();
tokensList = getTokens(data[currentIndex]);
if (tokensList.get(0).equalsIgnoreCase("Button"))
{
window.add(new JButton(getComponetLabel(tokensList.get(1))));
}
else if (tokensList.get(0).equalsIgnoreCase("Label"))
{
window.add(new JLabel(getComponetLabel(tokensList.get(1))));
}
//
else if (tokensList.get(0).equalsIgnoreCase("Textfield"))
{
String token = tokensList.get(1);
token = token.substring(0, token.length() - 1);
window.add(new JTextField(Integer.parseInt(token)));
}
window.validate();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private JPanel getPanel() throws Exception
{
JPanel panel = new JPanel();
ArrayList<String> tokensList = getTokens(data[currentIndex]);
if (tokensList.get(1).equalsIgnoreCase("Layout"))
{
panel.setLayout(getLayout(tokensList.subList(2, tokensList.size())));
}
else
{
throw new Exception("invalid Layout syntax at row " + currentIndex);
}
for (currentIndex = currentIndex + 1; currentIndex < data.length; currentIndex++)
{
tokensList.clear();
tokensList = getTokens(data[currentIndex]);
if (tokensList.get(0).equalsIgnoreCase("Button"))
{
panel.add(new JButton(getComponetLabel(tokensList.get(1))));
}
else if (tokensList.get(0).equalsIgnoreCase("Label"))
{
panel.add(new JLabel(getComponetLabel(tokensList.get(1))));
}
else if (tokensList.get(0).equalsIgnoreCase("Textfield"))
{
String token = tokensList.get(1);
token = token.substring(0, token.length() - 1);
panel.add(new JTextField(Integer.parseInt(token)));
}
else if (tokensList.get(0).equalsIgnoreCase("Panel"))
{
panel.add(getPanel());
}
else if (tokensList.get(0).equalsIgnoreCase("End;"))
{
return panel;
}
}
throw new Exception("No End tag forund");
}
private String getComponetLabel(String token)
{
if (token.length() > 3)
{
return token.substring(1, token.length() - 2);
}
else
{
return "";
}
}
private LayoutManager getLayout(java.util.List<String> layoutTokensList) throws Exception
{
if (layoutTokensList.get(0).equalsIgnoreCase("Flow:"))
{
return new FlowLayout();
}
else if (layoutTokensList.get(0).equalsIgnoreCase("Grid"))
{
int parm1 = Integer.parseInt(layoutTokensList.get(1).substring(1, layoutTokensList.get(1).length() - 1));
int parm2 = Integer.parseInt(layoutTokensList.get(2).substring(0, layoutTokensList.get(2).length() - 1));
int parm3 = Integer.parseInt(layoutTokensList.get(3).substring(0, layoutTokensList.get(3).length() - 1));
int parm4 = Integer.parseInt(layoutTokensList.get(4).substring(0, layoutTokensList.get(4).length() - 2));
System.out.println(parm1 + ", " + parm2 + ", " + parm3 + ", " + parm4);
return new GridLayout(parm1, parm2, parm3, parm4);
}
else
{
throw new Exception("Invalid Layout");
}
}
private JFrame createWindow(String title, int width, int height, LayoutManager layout)
{
JFrame frame = new JFrame(title);
if (layout != null)
{
frame.setLayout(layout);
}
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
return frame;
}
private ArrayList<String> getTokens(String grammer)
{
String[] tokens = grammer.trim().split(" ");
ArrayList<String> tokensList = new ArrayList<String>();
for (int i = 0; i < tokens.length; i++)
{
String token = tokens[i].trim();
if (token.equals(" ") || token.equals(""))
{
continue;
}
tokensList.add(token);
System.out.println(token);
}
return tokensList;
}
}
Explanation / Answer
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RecursiveDescentParser
{
private int currentIndex;
private String[] data;
JFrame window = null;
public RecursiveDescentParser()
{
init();
}
public static void main(String[] args)
{
RecursiveDescentParser RecursiveDescentParser = new RecursiveDescentParser();
}
private String getGrammer() throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader("Calculator.txt"));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
}
private void init()
{
try
{
String grammer = getGrammer();
data = grammer.split(" ");
ArrayList<String> tokensList = getTokens(data[0]);
String component = tokensList.get(0);
if (component.equalsIgnoreCase("Window"))
{
try
{
String title = tokensList.get(1).replace(""", "");
int width = Integer.parseInt(tokensList.get(2).substring(1, tokensList.get(2).length() - 1));
int height = Integer.parseInt(tokensList.get(3).substring(0, tokensList.get(3).length() - 1));
LayoutManager layout = null;
if (tokensList.size() > 4)
{
if (tokensList.get(4).equalsIgnoreCase("Layout"))
{
layout = getLayout(tokensList.subList(5, tokensList.size()));
}
else
{
throw new Exception("Syntax error");
}
}
window = (createWindow(title, width, height, layout));
System.out.println(title + ", " + width + ", " + height);
}
catch (Exception e)
{
e.printStackTrace();
throw new Exception("Syntax error");
}
}
else
{
throw new Exception("No GUI found for this input");
}
for (currentIndex = 1; currentIndex < data.length; currentIndex++)
{
tokensList.clear();
tokensList = getTokens(data[currentIndex]);
if (tokensList.get(0).equalsIgnoreCase("Button"))
{
window.add(new JButton(getComponetLabel(tokensList.get(1))));
}
else if (tokensList.get(0).equalsIgnoreCase("Label"))
{
window.add(new JLabel(getComponetLabel(tokensList.get(1))));
}
//
else if (tokensList.get(0).equalsIgnoreCase("Textfield"))
{
String token = tokensList.get(1);
token = token.substring(0, token.length() - 1);
window.add(new JTextField(Integer.parseInt(token)));
}
window.validate();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private JPanel getPanel() throws Exception
{
JPanel panel = new JPanel();
ArrayList<String> tokensList = getTokens(data[currentIndex]);
if (tokensList.get(1).equalsIgnoreCase("Layout"))
{
panel.setLayout(getLayout(tokensList.subList(2, tokensList.size())));
}
else
{
throw new Exception("invalid Layout syntax at row " + currentIndex);
}
for (currentIndex = currentIndex + 1; currentIndex < data.length; currentIndex++)
{
tokensList.clear();
tokensList = getTokens(data[currentIndex]);
if (tokensList.get(0).equalsIgnoreCase("Button"))
{
panel.add(new JButton(getComponetLabel(tokensList.get(1))));
}
else if (tokensList.get(0).equalsIgnoreCase("Label"))
{
panel.add(new JLabel(getComponetLabel(tokensList.get(1))));
}
else if (tokensList.get(0).equalsIgnoreCase("Textfield"))
{
String token = tokensList.get(1);
token = token.substring(0, token.length() - 1);
panel.add(new JTextField(Integer.parseInt(token)));
}
else if (tokensList.get(0).equalsIgnoreCase("Panel"))
{
panel.add(getPanel());
}
else if (tokensList.get(0).equalsIgnoreCase("End;"))
{
return panel;
}
}
throw new Exception("No End tag forund");
}
private String getComponetLabel(String token)
{
if (token.length() > 3)
{
return token.substring(1, token.length() - 2);
}
else
{
return "";
}
}
private LayoutManager getLayout(java.util.List<String> layoutTokensList) throws Exception
{
if (layoutTokensList.get(0).equalsIgnoreCase("Flow:"))
{
return new FlowLayout();
}
else if (layoutTokensList.get(0).equalsIgnoreCase("Grid"))
{
int parm1 = Integer.parseInt(layoutTokensList.get(1).substring(1, layoutTokensList.get(1).length() - 1));
int parm2 = Integer.parseInt(layoutTokensList.get(2).substring(0, layoutTokensList.get(2).length() - 1));
int parm3 = Integer.parseInt(layoutTokensList.get(3).substring(0, layoutTokensList.get(3).length() - 1));
int parm4 = Integer.parseInt(layoutTokensList.get(4).substring(0, layoutTokensList.get(4).length() - 2));
System.out.println(parm1 + ", " + parm2 + ", " + parm3 + ", " + parm4);
return new GridLayout(parm1, parm2, parm3, parm4);
}
else
{
throw new Exception("Invalid Layout");
}
}
private JFrame createWindow(String title, int width, int height, LayoutManager layout)
{
JFrame frame = new JFrame(title);
if (layout != null)
{
frame.setLayout(layout);
}
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
return frame;
}
private ArrayList<String> getTokens(String grammer)
{
String[] tokens = grammer.trim().split(" ");
ArrayList<String> tokensList = new ArrayList<String>();
for (int i = 0; i < tokens.length; i++)
{
String token = tokens[i].trim();
if (token.equals(" ") || token.equals(""))
{
continue;
}
tokensList.add(token);
System.out.println(token);
}
return tokensList;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.