Free Essay

Calculator Java Program

In:

Submitted By morrisca66
Words 917
Pages 4
Calculator Program:

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

public class Calculator extends JFrame { JTextField display;// Display text field. JPanel buttonPanel;// Panel for buttons. JButton buttons[];// Array to create button objects.

Calculator() { super("Calculator");// Gives the frame the title of Calculator.

display = new JTextField();// Creates and stores the object of the text // field in the display. buttons = new JButton[16];// Array made up of 16 variables to signify // buttons. buttons[0] = new JButton("7");// Seven button. buttons[1] = new JButton("8");// Eight button. buttons[2] = new JButton("9");// Nine button. buttons[3] = new JButton("/");// Divide button. buttons[4] = new JButton("4");// Four button. buttons[5] = new JButton("5");// Five button. buttons[6] = new JButton("6");// Six button. buttons[7] = new JButton("*");// Multiply button. buttons[8] = new JButton("1");// One button. buttons[9] = new JButton("2");// Two button. buttons[10] = new JButton("3");// Three button. buttons[11] = new JButton("-");// Subtract button. buttons[12] = new JButton("0");// Zero button. buttons[13] = new JButton(".");// Dot button. buttons[14] = new JButton("=");// Equals button. buttons[15] = new JButton("+");// Add button.

buttonPanel = new JPanel();// Creates panel for buttons. buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));// Sets the layout of // the buttons in a // grid format.

for (int i = 0, j = 0; i < 16; i++)// For statement to place buttons, // set font, set background and // foreground of buttons. { buttons[i].setFont(new Font("SanSerif", Font.BOLD, 16));// Sets the // font to // SanSerif, // BOLD and // to size // 16. if (i % 4 == 0) j++; if (j++ % 2 == 0) { buttons[i].setBackground(Color.GREEN);// Sets the background of // the 8, /, 4, 6, 2, -, // 0, = buttons to // green. buttons[i].setForeground(Color.BLACK);// Sets the // foreground(numbers) // of the 8, /, 4, 6, 2, // -, 0, = buttons to // black. } else { buttons[i].setBackground(Color.BLACK);// Sets the background of // the 7, 9, 5,*, 1, 3,. // , + buttons to black. buttons[i].setForeground(Color.GREEN);// Sets the foreground of // the 7, 9, 5,*, 1, 3,. // , + buttons to green. }

buttonPanel.add(buttons[i]); } add(display, BorderLayout.NORTH);// Sets the display(text field) at the // top of the frame. add(buttonPanel, BorderLayout.CENTER);// Sets the buttons in the center // of the frame to fill the // space below with buttons. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Allows the program to // close when the X // button is pressed. setSize(250, 250);// Sets the size of the frame to 250 x 250 pixels. setVisible(true);// Allows the user to see the calculator frame. }

public static void main(String[] args) { Calculator calculator = new Calculator(); }
}

Alignment GUI Program:

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

public class Align { private JFrame frame; private JButton okJButton; private JButton cancelJButton; private JButton helpJButton; private JTextField xJTextField; private JTextField yJTextField; private JCheckBox snapJCheckBox; private JCheckBox showJCheckBox; private JLabel xJLabel; private JLabel yJLabel; private JPanel checkJPanel; private JPanel buttonJPanel; private JPanel fieldJPanel1; private JPanel fieldJPanel2; private JPanel fieldJPanel;

// Constructor creates the GUI public Align() { // Builds the checkJPanel specific to check boxes. snapJCheckBox = new JCheckBox("Snap to Grid"); // Creates the // "Snap to Grid" check // box. showJCheckBox = new JCheckBox("Show Grid"); // Creates the "Show Grid" // check box. checkJPanel = new JPanel();// Creates the panel for the check boxes. checkJPanel.setLayout(new GridLayout(2, 1)); // uses layoutManager(grid // layout) checkJPanel.add(snapJCheckBox); // Adds "Snap to Grid" check box. checkJPanel.add(showJCheckBox); // Adds the "Show Grid" check box.

// Builds field panel1 specific to X label and text field. xJLabel = new JLabel("X: "); xJTextField = new JTextField("", 3); // Sets the width of the text // field. fieldJPanel1 = new JPanel();// Creates panel for the X label and X text // field. fieldJPanel1.setLayout(new FlowLayout()); // Uses flow layout layout // manager fieldJPanel1.add(xJLabel); // Adds the X label. fieldJPanel1.add(xJTextField); // Adds the X text field.

// Builds field panel2 specific to Y label and text field. yJLabel = new JLabel("Y: "); yJTextField = new JTextField("", 3); // Sets the width of the text // field. fieldJPanel2 = new JPanel();// Creates panel for the Y label and Y text // field. fieldJPanel2.setLayout(new FlowLayout()); // Uses flow layout layout // manager fieldJPanel2.add(yJLabel); // Adds the Y label. fieldJPanel2.add(yJTextField); // Adds the Y text field.

// Build field panel. fieldJPanel = new JPanel(); fieldJPanel.setLayout(new BorderLayout()); // Uses border layout to set // locations of the field // panel 1 and 2. fieldJPanel.add(fieldJPanel1, BorderLayout.NORTH);// Sets field panel 1 // to the north(or // top). fieldJPanel.add(fieldJPanel2, BorderLayout.SOUTH);// Sets field panel 2 // to the // south(bottom).

// Builds the button panel. okJButton = new JButton("Ok"); // Creates the OK button. cancelJButton = new JButton("Cancel"); // Creates the Cancel Button. helpJButton = new JButton("Help");// Creates the Help button. buttonJPanel = new JPanel();// Creates the panel for the buttons used. buttonJPanel.setLayout(new GridLayout(3, 1, 10, 5));// Uses a grid // layout to place // the buttons in // position. buttonJPanel.add(okJButton); // Adds the OK button in the grid layout. buttonJPanel.add(cancelJButton);// Adds the Cancel button in the grid // layout. buttonJPanel.add(helpJButton);// Adds the Help button in the grid // layout.

// Uses flow layout for center-aligned and add components frame = new JFrame("Align");// Gives the title of the frame and sets it // to "Align." frame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));// Sets the // layout to // center // and sets // spacing // to 10 X // 5. frame.add(checkJPanel);// Adds the check box panel to the frame. frame.add(fieldJPanel);// Adds the text field panel to the frame. frame.add(buttonJPanel);// Adds the button panel to the frame.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Closes the frame // by pressing // the X button. frame.pack(); frame.setVisible(true);// Allows the frame to be visible to the user. }

public static void main(String[] args) { Align align = new Align(); }
}

Similar Documents

Free Essay

Technology

...Java applets Slide 1 How applets work An applet is a special type of application that can be downloaded from an Internet or intranet server and run on the client’s computer within a web browser. The Java Plug-in is included as part of the JRE. It is a browser plug-in that allows a web browser to use a version of the JRE that’s newer than the one that’s included with the browser. For a client machine to run applets, a current version of the JRE and Java Plug-in must be installed on the client. If a user attempts to run an applet without first installing the current version of the Java Plug-in, a prompt will be displayed indicating that the Java Plug-in must be downloaded. Slide 2 Applet security issues To prevent applets from damaging a client system or from making it possible to damage a client system, security restrictions limit what an applet can do. To overcome these security restrictions, you can create a signed applet. This indicates that the applet comes from a trusted source. Then, you can add rights to the signed applet. Slide 3 What an applet can’t do Read, write, or delete files or databases on the client system. Access information about the files or databases on the client system. Run programs on the client system. Access system properties for the client system except the Java version, the name and version of the operating system, and the characters used to separate directories, paths, and lines. Make network connections to other servers available...

Words: 884 - Pages: 4

Premium Essay

History of Computers

...“HISTORY OF COMPUTERS” CIS 110 March 13, 2010 The history through Computer and its timeline defines the beginning for where it all began. Let’s start in the year 1939 where David Packard and Bill Hewlett first started there company there first product was called the HP 200A Audio Oscillator which was a popular piece of test equipment for the engineers Walt Disney Pictures was their first customer and ordered eight of the 200B model to use as sound effects generators for the movie “Fantasia” in the 1940s the first (CNC) that was build was call “The Complex Number Calculator” designed by researcher George Stibitz and demonstrated in New York at the Dartmouth College. This demonstration showed that this was to consider to be the first remote access computing using a teletype connected via special telephone lines through this timeline.( 2006 Computer History Museum) it will show that from the moment Computers was thought of and how the build went through a concept of steps before fully calling a (PC) an Personal Computer a lot of what I have read and seen through the timeline of the history of are computers for every new year a new chapter starts in building a fully functional computer but over time is where it took for where it all began as different components that different Engineers build. To get or build a fully functional computer from the components that make it you will start with a Case, Power Supply, Motherboard, Microprocessor, Memory, Drive Controllers, Hard disk...

Words: 1191 - Pages: 5

Free Essay

Pool Calculator User Manual

...Swimming Pool Volume Calculator June 20, 2014 CIS355A Course Project Swimming Pool Volume Calculator The Swimming Pool Volume Calculator (SPVC) is a simple and easy to use application that calculates the volume of a swimming pool or hot tub. The SPVC also includes a Temperature Calculator that converts Fahrenheit to Celsius or vice versa, a Length Calculator that coverts various measurements both metric and standard, holds customer information and contractor information. This extremely lightweight application that is portable and flexible that can run on any Operating System that has Java platform installed. General Tab The General Tab is the first tab you see when you first start the program. It show the current date and gives the user the opportunity to exit the application. Options Tab The Options Tab gives you the ability to customize your application with your company name at the top of the window. You just put your company name in the text field and click the Set New Name button, but you have to restart the program for the changes to take effect. This tab also gives the user the opportunity to exit the program. Customer Tab The Customer Tab allows you to keep track of your customer information and keeps it only a click away. To add a customer simply click the Add Customer button, a popup window will appear with a simple form to fill out. Fill in all fields and press the Add Customer button. Once Customer...

Words: 482 - Pages: 2

Premium Essay

Genesis to Revelation

...has changed in time. The computer is an electronic device used for storing and processing data, typically in binary functions. Computers were built to solve problems that are impossible to human mind. Now computers almost do every work of a man. Genesis to Revelation of Computers John W. Mauchly and J. Presper Eckert first designed computer called as ENIAC (Electrical Numerical Integrator and Calculator) machine. This used nearly 18000 vacuum tubes, which require almost 1800 square feet area. It also had a punched card system. (Department of computer science, n.d.). The next level of computers is the EDVAC, in which processor stores data in numerical format. In the later ages, computers are designer based on their digital operation, stored program capability, their self-regulating capacity, and their reliance on electronics. (Davis, 1977). ENIAC could do the work done by 400,000 people with calculators. First, calculators work the same as computers. Calculators can compute in a range of limited numbers, and are limited to the size of memory and RAM. The ordinary calculator cannot be changed into different memory sizes. (PICCININI, 2008). But this was later ruled out by EDVAC. The change in computers then began to appear. (Ceruzzi, 1983) . By 1983, there was only one computer per 200 people. This became one per 100 people in late 2000’s. And now almost every home has a computer. Computers were invented to solve processes, which could be made easy. (Computers in the...

Words: 1139 - Pages: 5

Free Essay

Itt Technical Institute Pt1420 Unit 2

...David Camacho PT1420 Programming Lab 2.1 Display Welcome to the Fiber Optic Calculator Program. Enter company name. Enter the number of fiber optic cable needed. Multiply .87 times the feet needed for the total cost. Display the total cost of the fiber optic cable and the company name. Purpose of Variable | Variable Name | Stores cost of fiber | fiberCost | Stores the company name | firstChoice | Stores the number of feet to be installed | 50 | Stores the calculated cost of installed fiber | .87 | Display “Welcome to the Fiber Optic Calculator Program” Set fiberCost = .87 Display “What is the company name?” Input companyName Display “How many feet of fiber will be installed” Input feetInstalled Set totalCost = fiberCost * feetInstalled Display “For the company” , companyName, “the total cost will be $” , totalCost David Camacho PT1420 Lab 2.2 Flowchart David Camacho PT1420 Programming Lab 2.3 Visual Studios Program David Camacho Unit 2 HW Assignment 1 Short Answer 1.What does a professional programmer usually do first to gain an understanding of a problem? The programmer creates a design for the program, which starts by asking the customer what they need the program to accomplish. 2.What is pseudocode? Pseudocode is an informal language that has no syntax rules, and is not meant to be compiled or executed. 3.Computer programs typically perform what three steps? Step 1: Input is received. Step 2: Some...

Words: 718 - Pages: 3

Free Essay

Pseudocode

...have 25 minutes to complete the quiz. • It will be a written quiz (not using any computer). • It will be closed-book, closed-notes, and no calculator is allowed. • Answers must be neat and legible. We recommend that you use pencil and eraser. • The quiz will be based on the exercises you will find below. The quiz will ask you to write pseudocode for a particular problem. • We have provided previous semesters’ quizzes at the end. Take a look at them so you get an idea of the pseudocode we expect. The following exercises cover the material to be covered in Quiz #2. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with the TA and the instructor during office hours. Keep in mind that in the following exercises you are being asked to provide only pseudocode. 1. Write pseudocode for a program that computes the average of a set of values after the highest and lowest scores have been removed. 2. Write pseudocode for a program that reads a sequence of integer values and determines whether it is a decreasing sequence. A decreasing sequence is one where each value is greater than or equal to the next element in the sequence. The program will first read the number of values to process followed by the values in the sequence. The program will print the message "Decreasing" for a decreasing sequence and "Non-Decreasing" otherwise. For example, here are two decreasing sequences: ...

Words: 841 - Pages: 4

Free Essay

Class Date

...PRG 420 November 30, 2011 Class Date Assignment Introduction There are several different aspects to programming that we need to take into consideration as we learn more and more of the language. In Java, there is much to learn that will assist in becoming programmers. During this paper we will discuss several aspects of a program that are essential to make a program run properly. Inside every program there is a certain set of commands and formats that are required in order for the program to work like it needs to. Several of them will be listed later. Karen Fords two bullets Methods The definition of a method is, “two of the components of a method declaration comprise the method signature—the method's name and the parameter types.” ("Defining Methods", 2011). There are several different aspects of a method that we, as programmers, need to keep in mind as begin our journey into the programming world. Each method has six components that make work correctly. Each of the six components will have to be used in order to make the method work right. The six components are: 1. Modifiers a. Public – allows this method to be used in all areas of the program b. Private – can only be used when direction is given or a password is used 2. Return Type a. Void – means that the method does not return any value b. Double – means that the method returns a double value; for instance, 10...

Words: 728 - Pages: 3

Free Essay

Calculater

...[pic] CSE-151 TERM PAPER TOPIC: CALCULATOR SUBMITTED TO: SUBMITTED BY: Mr. Anand Kumar Kumar Gaurav Roll No.: 27 Dept. Of CSE Section: 257(G2) Regd. No.: 10805952 ACKNOWLEDGEMENT I would like to express my gratitude to all those who have helped me in the successful completion of my project. I want to thank the Department of CSE of Lovely Professional University for giving me permission to commence on this project. I would like to thank MR. ANAND KUMAR for helping me somehow and encouraged me to go ahead with my project. Especially, I would like to give my special thanks to my mother whose patient love enabled me to complete this work. TABLE OF CONTENTS 1. INTRODUCTION 2. INTRODUCTION OF PROJECT 3. PROPOSED SYSTEM (i) DESCRIPTION (ii) SYSTEM REQUIREMENTS 3. REQUIREMENTS ANALYSIS 4. USING FUNCTIONS 5. SOURCE CODE 6. FUTURE SCOPE OF PROJECT 7. BIBLIOGRAPHY Introduction: C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dannis Ritchie...

Words: 1832 - Pages: 8

Premium Essay

Emily 1

...Mobile based applications are compact software programs that perform specific tasks for the mobile user. There are two types of mobile apps: “The native app must be installed on the device; they either arrive pre-installed on the phone – these might include address book, calendar, calculator, games, maps and Web browser – or they can be downloaded from for free or a small fee from Web sites – today these sites are called app stores. Native apps are either written specifically for a type of handset – as many iPhone applications have been – so they can take more advantage of a phone’s functions, or as Java applications – this was the norm with download apps until recently – to run on many handsets. The Web app resides on server and is accessed via the Internet. It performs specified tasks – potentially all the same ones as a native application – for the mobile user, usually by downloading part of the application to the device for local processing each time it is used. The software is written as Web pages in HTML and CSS, with the interactive parts in Java. This means that the same application can be used by most devices that can surf the Web (regardless of the brand of phone)”, (www.eweek.com). Smartphone’s are extremely popular. Ninety-seven percent of all smartphone users use mobile applications. Fifty-eight percent of those smart phone users use location based applications. There were surveys done by a nonprofit global information security association called ISACA (previously...

Words: 318 - Pages: 2

Premium Essay

Individual: Programming Solution Problem

...Bhavik Patel Individual: Programming Solution Problem It was my first time using the Visual Logic Program to create a program. Comparing to another program I have used before, Visual Logic was the easiest program to use. It was very easy to learn, and the tutorial on YouTube was very helpful. For this assignment, I had to create two programs that show any calculation or some math calculation. I’ve created a calculator in HTML and Java, and that was very difficult. I had to work hard to create the calculator but when I used Visual Logic to create a program calculation it was fairly easy. The two programs I create for this assignment are hours and rate and triangle calculation program. To create this program, it was fairly easy because by watching the YouTube videos I learned lot on how to use the program. On hours and rate calculation program it was fairly easy because the calculation was very simple. In order to create this program first, you need to have an input for hours. It’s crucial that you do all the steps in order because when you write a real program you will need to go in order. You can never skip any steps because when you skip steps the whole program will not work. After you create the input for hours, then you need to create an input for rates. Then you need to create an assignment with the formula that you are going to use to get a solution for hours * rate. For the assignment, you will type in Pay = Hours * Rate this is the formula to get a pay for a person...

Words: 706 - Pages: 3

Free Essay

Secutiry in Informatics

...Istoria si evolutia securitatii in informatica Securitatea in informatica Importanta aspectelor de securitate în retelele de calculatoare a crescut odată cu extinderea prelucrărilor electronice de date si a transmiterii acestora prin intermediul retelelor. În cazul operării asupra unor informatii confidentiale, este important ca avantajele de partajare si comunicare aduse de retelele de calculatoare să fie sustinute de facilităti de securitate substantiale. Acest aspect este esential în conditiile în care retelele de calculatoare au ajuns să fie folosite inclusiv pentru realizarea de operatiuni bancare, cumpărături sau plata unor taxe. În urma implementării unor mecanisme de securitate într-o retea de calculatoare, informatiile nu vor putea fi accesate sau interceptate de persoane neautorizate (curioase sau, eventual, chiar rău intentionate) si se va împiedica falsificarea informatiilor transmise sau utilizarea clandestină a anumitor servicii destinate unor categorii specifice de utilizatori ai retelelor. Persoanele care atentează la securitatea retelelor pot apartine unor categorii diverse, comitând delicte mai mult sau mai putin grave: studenti care se amuză încercând să fure posta electronică a celorlalti, "hacker"-i care testează securitatea sistemelor sau urmăresc să obtină în mod clandestin anumite informatii, angajati care pretind că au atributii mai largi decât în realitate, accesând servicii care în mod normal le-ar fi interzise, sau fosti angajati care urmăresc...

Words: 5182 - Pages: 21

Premium Essay

My Penchant For Computer Programming

...Just before dawn, I managed to calculate the product of two 1000-digit numbers in my Pentium III processor. Tasks like these further grew into developing a complete Computerised Billing System by the time I finished my high school. This was a career-defining moment for me. My penchant for programming made me choose an undergraduate degree in Information Technology. Two years into the study, I could update my pet calculator code to run on a Hadoop cluster to multiply not just big integers, but also large multi-dimensional matrices. Challenges like these kept enticing me each day of my undergraduate study, which made problem-solving in varying scales, from assembly codes to high-level languages my...

Words: 1007 - Pages: 5

Premium Essay

Stored Program Concept

...Intro to CS: stored program concept Panos Hilaris Intro to CS: stored program concept 1 Stored program concept John von Neumann’s idea: Store programs, together with data, in the memory of the computer. Memory contains: (a) instructions, (b) data. Conceptually, programs and data seem very different. In fact the first computers, distinguished between them and stored them in different places. Another point of view: Treat programs as data. Intro to CS: stored program concept 2 Inside hardware The two hardware parts, that are ‘inside’ the computer: Processor or CPU (Central Processing Unit) Main memory (RAM: Random Access Memory) (Remember: the other part is input/ouput devices) Intro to CS: stored program concept 3 Components of CPU/memory arithmetic and logic unit (ALU): circuitry for arithmetic and logic operations (here the ‘real’ work is done) control unit (CU): responsible for running programs (here the ‘book-keeping’ job is done) • Fetch next instruction • Decode instruction • Execute instruction (activate relevant circuitry in ALU) registers (in ALU and CU): hold data for use by arithmetic and logical operations bus: wires connecting CPU and main memory Intro to CS: stored program concept 4 Review question What is the difference between a computer and a calculator? Intro to CS: stored program concept 5 Review question What is the difference between a computer and a calculator? Answer: The computer is programmable, and this is achieved through the control unit...

Words: 694 - Pages: 3

Free Essay

Baseline Proposal

...exercise. Because of everybody realize that exercise makes you healthy and cure the disease. Moreover, fat people choose working out instead of using diabetic medicine because of disadvantages from using medicine such as yoyo effect. BMI: Body Mass Index is ratio between the body weight and height. The value shows us how healthy we are. If the value less than 18.5, you are skinny. If the value is between 18.5 and 24.9, your weight is normal. If the value is between 25 and 29.9, you are overweight. If the value is between 30 and 39.9, you are obese. If the value is over 40, you are morbidly obese. The BMI value is well known for people who exercise frequently. In this project, we realize the importance of the health, so we create the program called “Mr.Fitness” which is the assistance for the people who love exercise to measure his/her BMI....

Words: 3043 - Pages: 13

Premium Essay

Data Communication and Networking

...SEMESTER III |SL. |COURSE CODE |COURSE TITLE |L |T |P |C | |NO | | | | | | | |THEORY | |1 |MC9231 |Computer Networks |3 |0 |0 |3 | |2 |MC9232 |Microprocessors and its |3 |0 |0 |3 | | | |Applications | | | | | |3 |MC9233 |Software Engineering |3 |0 |0 |3 | |4 |MC9234 |Computer Graphics |3 |0 |0 |3 | |5 |MC9235 |Web Programming |3 |0 |0 |3 | |PRACTICAL | |6 |MC9237 |Graphics Lab |0 |0 |3 |2 | |7 |MC9238 |Microprocessor Lab |0 |0 |3 ...

Words: 1592 - Pages: 7