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;