In: Computer Science
*In JAVA and JavaFX please!!
CSE1322 – Assignment 8 – GUI
General User Interface
Assignment 8
Objectives
• Build a GUI application similar to a calculator.
• Create an application that acts as a simple calculator. Create
buttons for 0-9 and a text
field that displays the concatenation of the current digits as they
are clicked.
• Add buttons for operators “+”, “-“, “*”, and “/”.
• Add a final button for “=” that calculates the computed value
with the value entered in
the text field. For instance, clicking buttons 7, +, 8, = should
display 15.
Tasks
This assignment has two parts (one for the front-end and another
for the back-end):
1. Front-End: Building the window (Making the GUI buttons and the
Label).
2. Back-End: Making the variables to store the values and event
handlers that are
responsible for handling the actions of the Label and
Buttons.
Overall Objective: You will need create a calculator with the that
can add, subtract, divide, and
multiply numbers. This calculator should not rely on textboxes to
enter in values but should have
an array of buttons one through nine for the numbers and buttons
for the operations.
Logical Insight: How Should the Program Work?
When a numerical or operator button is clicked it should
concatenate its number or operator to
the current calculation. For example, it should show the value “98”
if the buttons “9” and “8”
were clicked. However, if an operator button was clicked between
two numbers then it should
show that operator between the values. For example, it should show
“9 / 8” if the buttons “9”,
then “/”, and then “8” were clicked. It is important to note that
if we click “9”, then “8”, then “/”,
and finally “8” we would get “98 / 8”.
The equal (=) button is special in that if it is clicked after a
valid operation occurs it should
replace the whole equation with the result of the calculation.
Therefore, we should never see the
“=” in the label. For instance, “98 / 8” evaluates to 12.25, so the
label should show “12.25” after
the equal sign button is clicked.
NOTE: Assume that the equal sign is only pressed after at least two
separate numbers and an
operator are entered and that the user will want to use that as
their first number for the next
operation. That means if a new number is pressed, then it will be
added to the end (as the last
digit) of the result from the last operation. Also, this should
work for at least two (2) digits, but
your calculations do not need to work for more than that.
Page 2 of 4
Front-End – Building the Window
Using the knowledge acquired in the Lab 8, construct a GUI window
with buttons for the
numbers zero (0) through nine (9), and buttons for addition (+),
subtraction (-), multiplication
(*), division (/) and finally equality (=). Also add a label object
in your window that will fill up
with the numbers/operator buttons being pressed (this text field
should show the complete
formula before pressing the “=” button). You should be familiar
with this as a result of doing Lab
8.
Back-End – Make the Event Handlers and the Variables to Store
Values
In Java you will need to create variables to store the values in
the Controller Class as well
as variables to store what will appear in the Label; Event Handlers
should also be created that
will take care of what happens when buttons are pressed. How to do
this is mentioned in detail in
the explanation sections in Lab 8.
In C# you will need to create variables that should store the
values for when the buttons
are pressed in the GUI in the Form1.cs file. In addition, Event
Handlers (in the form of methods)
should also be created to handle what these button presses do on a
per button by button bases.
Labels should be created and named in the GUI portion. How to do
this is mentioned in detail in
the explanation sections in Lab 8.
For example, for when the equality (=) is clicked, ideally one of
these variables will be
the result variable that will hold the final solution. There should
be an event handler (event and
event listener for C#) that is triggered by the clicking of the
equality (=) button that goes and
retrieves the value in the result variable and displays it on the
label instead of the previous
function that was being displayed.
Intentionally Left Blank
Below is some possible sample output (note: ∃ simply means, “there
exists”):
The last button pressed has a dark blue highlight around the
edges.
• Build a GUI application similar to a calculator.
import java.util.Scanner; public class Calculator { public static void main(String[] args) { double num1; double num2; double ans; char op; Scanner reader = new Scanner(System.in); System.out.print("Enter two numbers: "); num1 = reader.nextDouble(); num2 = reader.nextDouble(); System.out.print("\nEnter an operator (+, -, *, /): "); op = reader.next().charAt(0); switch(op) { case '+': ans = num1 + num2; break; case '-': ans = num1 - num2; break; case '*': ans = num1 * num2; break; case '/': ans = num1 / num2; break; default: System.out.printf("Error! Enter correct operator"); return; } System.out.print("\nThe result is given as follows:\n"); System.out.printf(num1 + " " + op + " " + num2 + " = " + ans); } }
If you need python code i wil provide also,Thank you