CSIS 10A Assignment 7 SOLUTIONS

Similar documents
JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Chapter 7: A First Look at GUI Applications

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Introduction. Introduction

CSIS 10A Assignment 3 Due: 2/21 at midnight

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

JAVA NOTES GRAPHICAL USER INTERFACES

AP CS Unit 11: Graphics and Events

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Window Interfaces Using Swing Objects

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Graphic User Interfaces. - GUI concepts - Swing - AWT

Systems Programming Graphical User Interfaces

Layouts and Components Exam

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

GUI Forms and Events, Part II

Graphical User Interfaces. Comp 152

H212 Introduction to Software Systems Honors

User interfaces and Swing

Window Interfaces Using Swing Objects

MIT AITI Swing Event Model Lecture 17

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CS Exam 3 - Spring 2010

Attempt FOUR questions Marking Scheme Time: 120 mins

RAIK 183H Examination 2 Solution. November 11, 2013

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Graphical User Interfaces

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Dr. Hikmat A. M. AbdelJaber

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

DM503 Programming B. Peter Schneider-Kamp.

Chapter 13 Lab Advanced GUI Applications

Adding Buttons to StyleOptions.java

Java. GUI building with the AWT

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Part I: Learn Common Graphics Components

SINGLE EVENT HANDLING

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Introduction to the JAVA UI classes Advanced HCI IAT351

COMP16121 Sample Code Lecture 1

Java Programming Lecture 6

We are on the GUI fast track path

Programming Languages and Techniques (CIS120)

SampleApp.java. Page 1

Graphics User Defined Forms, Part I

CS Exam 3 - Spring 2010

Agenda. Container and Component

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Swing from A to Z Some Simple Components. Preface

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Swing Programming Example Number 2

Java Help Files. by Peter Lavin. May 22, 2004

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

CSCI 201L Midterm Written Summer % of course grade

encompass a group of features for building Graphical User Interfaces (GUI).

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

JLayeredPane. Depth Constants in JLayeredPane

2110: GUIS: Graphical User Interfaces

INTRODUCTION TO (GUIS)

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Using Several Components

RAIK 183H Examination 2 Solution. November 10, 2014

Window Interfaces Using Swing. Chapter 12

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Programming Language Concepts: Lecture 8

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Advanced Swing GUI Applications

Project 1. LibraryTest.java. Yuji Shimojo CMSC 335

FirstSwingFrame.java Page 1 of 1

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

First Name: AITI 2004: Exam 2 July 19, 2004

Together, the appearance and how user interacts with the program are known as the program look and feel.

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

Transcription:

CSIS 10A Assignment 7 SOLUTIONS Read: Chapter 7 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment, then export JAR file and upload to the server using your pass code. You may do more than 10 points of work but the max award will be 11 points. Part I: Exam Prep Exercises--7 points total To be answered in the Exercises file Find the Error: in the Exercise file Find the Errors -- PUT an XXX by any errors you find and either correct them or explain what the error is. 2. The following is an inner class that will be registered as an action listener for a JButton component: private class ButtonListener implements ActionListener public void actionperformed() XXXXX needs to have ActionEvent e or any other variable inside the () Code appears here.

4. The panel variable references a JPanel governed by a BorderLayout manager. The following statement attempts to add the button component to the north region of panel: panel.add(button, NORTH); XXXX should be panel.add(button, BorderLayout.NORTH); 5. The panel variable references a JPanel object. The intention of the following statement is to create a titled border around panel: panel.setborder(new BorderFactory("Choices")); XXXXX should be panel.setborder(borderfactory.createtitledborder("choices")); Algorithm Workbench 1. mywindow.setsize(500, 250); 2. mywindow.setdefaultcloseoperation(jframe.exit_on_close); 7. panel.add(button, BorderLayout.WEST); 8. Create three radio buttons JRadioButton radio1 = new JRadioButton("Option 1", true); JRadioButton radio2 = new JRadioButton("Option 2"); JRadioButton radio3 = new JRadioButton("Option 3"); Create a ButtonGroup Object. ButtonGroup group = new ButtonGroup(); Add the radio buttons to the ButtonGroup object. group.add(radio1); group.add(radio2); group.add(radio3);

Part 2: Programming Challenges 3 points each 1) Create class P1MonthlySalesTax and solve the following problem: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.decimalformat; * The P1MonthlySalesTax GUI class creates the GUI for the Monthly Sales Tax report. * The user enters the total sales in a text field. The GUI calculates and displays: * The amount of county sales tax * The amount of states sales tax * The total sales tax (county plus state) * * The county sales tax is (0.02). * The state sales tas is (0.04). * * Dana A. DeVost * 23 March 2012 public class P1MonthlySalesTax extends JFrame private JPanel panel; To reference a panel private JLabel messagelabel; To reference a label private JTextField totalsalesfield; To reference a text field private JButton buttonpanel; To hold the buttons private JButton calcbutton; To calculate the cost private final int WINDOW_WIDTH = 310; Window width private final int WINDOW_HEIGHT = 200; Window height private final double COUNTY_SALES_TAX = 0.02; County Sales Tax private final double STATE_SALES_TAX = 0.04; County Sales Tax * Constructor public P1MonthlySalesTax() Display a title. settitle ("Monthly Sales Tax Calculator");

Set the size of the windwo. setsize(window_width, WINDOW_HEIGHT); Specify an action for the close button. setdefaultcloseoperation(jframe.exit_on_close); Build the panel and add it to the frame buildpanel(); Add the panel to the frame's content pane. add(panel); Display the window. setvisible(true); * The buildpanel method adds a label, text field and buttons to a panel. private void buildpanel() Create a label to display the instructions messagelabel = new JLabel("Enter the amount of total sales for the month."); Create a text field 10 characters wide. totalsalesfield = new JTextField(15); Create a button with the caption "Calculate". calcbutton = new JButton("Calculate"); Add an action listner to the button. calcbutton.addactionlistener(new CalcButtonListener()); Create a JPanel object and let the panel field reference it. panel = new JPanel(); Add the label, text field and button components to the panel. panel.add(messagelabel); panel.add(totalsalesfield); panel.add(calcbutton); * CalcButtonListener is an action listner for the * Calculate button private class CalcButtonListener implements ActionListener * The actionperformed method executes when the user * clicks on the calculate button. * @ param e The event object. public void actionperformed(actionevent e) String input; To hold the user's input

double countysalestax; To hold the county sales tax double statesalestax; To hold the state sales tax double totalsalestax; To hold the total sales tax (county + state) Get the text entered by the user into the text field. input = totalsalesfield.gettext(); input = input.replaceall (",", ""); Convert the input to county sales tax. countysalestax = Double.parseDouble(input) * 0.02; Convert the input to state sales tax. statesalestax = Double.parseDouble(input) * 0.04; Create a DecimalFormat object to format the output. DecimalFormat dollar = new DecimalFormat("0.00"); Display the total sales tax. totalsalestax = (countysalestax + statesalestax); Display the resutls. JOptionPane.showMessageDialog(null, "\n" + "Total monthly Sales $" + dollar.format (Double.parseDouble(input)) + "\n" + "County sales tax is $" + dollar.format(countysalestax) + "\n" + "State sales tax is $" + dollar.format(statesalestax) + "\n" + "Total sales tax is $" + dollar.format(totalsalestax)); End of the CalcButtonListener class * The main method creates an instance of the * P1MonthlySalesTax class, which displays its window * on the screen. public static void main(string[] arges) new P1MonthlySalesTax();

2) Create a new class called P2LongDistance that solves the following problem: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.decimalformat; * Write a description of class P2LongDistance here. * * @author (Andrew Harasta) * @version (April 3rd, 2012) public class P2LongDistance extends JFrame Private variable declarations. private final int WINDOW_WIDTH = 500; private final int WINDOW_HEIGHT = 300; private JPanel panel; private JButton displaycharge; private JRadioButton Daytime, Evening, offpeak; private JTextField field; private JLabel labelmessage; private ButtonGroup RadioButtonGroup; To group radio buttons private double rate, totalminutes, totalcharge; * Constructor for objects of class P2LongDistance public P2LongDistance() setsize(window_width, WINDOW_HEIGHT); Set the size of the window Specify an action for the close button. setdefaultcloseoperation(jframe.exit_on_close); settitle("programming Challenge #2"); Set the title. setlayout(new BorderLayout()); Add a BorderLayout to the content pane. buildpanel(); Calls the buildpanel method. add(panel); Adds panel built to the content pane. setvisible(true); Allows GUI window to be visible. private void buildpanel()

panel = new JPanel(); Creates a new JPanel object. Creates a new JLabel object. labelmessage = new JLabel("Please enter the total minutes of the call:"); Adds the JLabel object to the JPanel. panel.add(labelmessage, BorderLayout.CENTER); field = new JTextField(10); Creates a new JTextField object. panel.add(field); Adds the JTextField object to the JPanel. Defines and labels each radio button. Daytime = new JRadioButton("Daytime (6:00A.M. to 5:59P.M.), $0.07 per minute."); Evening = new JRadioButton("Evening (6:00P.M. to 11:59P.M.), $0.12 per minute."); offpeak = new JRadioButton("Off-Peak (12A.M. to 5:59A.M.), $0.05 per minute."); Adds action listener for each radio button. Daytime.addActionListener(new RadioButtonListener()); Evening.addActionListener(new RadioButtonListener()); offpeak.addactionlistener(new RadioButtonListener()); Adds buttons to the same group. RadioButtonGroup = new ButtonGroup(); RadioButtonGroup.add(Daytime); RadioButtonGroup.add(Evening); RadioButtonGroup.add(offPeak); Adds each button to the panel. panel.add(daytime); panel.add(evening); panel.add(offpeak); displaycharge = new JButton("Display Charge"); Creates a new JButton object. Creates the Action Listener for the JButton. displaycharge.addactionlistener(new ButtonListener()); panel.add(displaycharge); Adds the JButton to the JPanel. public static void main(string[] args) Creates a new instance of the constructor method. P2LongDistance mylongdistance = new P2LongDistance(); private class ButtonListener implements ActionListener public void actionperformed(actionevent e) String input; Creates a Decimal Format object. DecimalFormat formatter = new DecimalFormat("#0.00"); Assigns String variable the same memory address as the JTextField. input = field.gettext(); Converts the string of the JTextField into a double.

totalminutes = Double.parseDouble(input); totalcharge = totalminutes * rate; Calculates the total charge. Creates the message dialog box containing the call's charge information. JOptionPane.showMessageDialog(null, "The charge for this call is $" + formatter.format(totalcharge) + ". Thank you!"); private class RadioButtonListener implements ActionListener Assigns corresponding rate values to radio buttons. public void actionperformed(actionevent e) if(e.getsource() == Daytime) rate =.07; else if(e.getsource() == Evening) rate =.12; else if(e.getsource() == offpeak) rate =.05;