Question

In: Computer Science

Part Two: Download VendingChange.java (above). Run it and become familiar with the output. Essentially, you need...

Part Two: Download VendingChange.java (above). Run it and become familiar with the output. Essentially, you need to create a user friendly GUI app using the code parameters of VendingChange. Turn VendingChange.java into a GUI app.

1) Your program must display the input dialog.
2) You must check that the data entered by the user follows the required input. If not, your program must display an error dialog and exit.

3) If the input is valid, then your program must display a message dialog similar to the one shown in listing 2.12, but the message must pertain to VendingChange and not ChangeMaker. Your message dialog must show the number of pennies, nickels, dimes and quarters.
4) Your program must exit when the user closes the output message dialog.
5) Comment and document your program.

Complete the programming problems such that they produce output that looks similar to that shown in the text or upcoming class presentations (in-class and Announcements). Tips and formulas (if any) will be discussed in class. Additional details will be provided in class and Announcements.

add comments to code please.

import java.util.Scanner;

public class VendingChange
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter price of item");
        System.out.println
                ("(from 25 cents to a dollar, in 5-cent increments)");
        int originalAmount = keyboard.nextInt();
        int change = 100 - originalAmount;

        int quarters = change/25;
        change = change%25;// remaining change after deducting quarters
        int dimes = change/10;
        change = change%10;// remaining change after deducting dimes, too
        int nickels = change/5;
        change = change%5;
        int pennies = change;

        //The grammar will be incorrect if any of the values are 1
        //because the required program statements to handle that
        //situation have not been covered, yet.   
        System.out.println
                ("You bought an item for " + originalAmount
                + " and gave me a dollar,");
        System.out.println("so your change is");
        System.out.println(quarters + " quarters,");
        System.out.println(dimes + " dimes, and");
        System.out.println(nickels + " nickel.");
    }
}

Solutions

Expert Solution

O U T P U T

Message Box Result window:

J A V A C O D E

//Package declared is vendChange

package vendChange;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class VendingChangeGUI extends JFrame {

   private JPanel contentPane;
   private JTextField textFieldPrice;

   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       //Creating jFrame for GUI
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
                   VendingChangeGUI frame = new VendingChangeGUI();
                   //Setting maximize to false
                   frame.setResizable(false);
                   //setting location of app. to mid position
                   frame.setLocationRelativeTo(null);
                   //Setting look and feel(GUI) to current system look n feel
                   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                   frame.setVisible(true);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the frame.
   */
   //declaring the default constructor
   public VendingChangeGUI() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 376, 195);
       contentPane = new JPanel();
       contentPane.setBackground(SystemColor.inactiveCaptionBorder);
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       contentPane.setLayout(null);
      
       //Setting welcome label
       JLabel lblNewLabel = new JLabel("Welcome to Vending Change GUI App.");
       lblNewLabel.setFont(new Font("Stencil", Font.PLAIN, 15));
       lblNewLabel.setBounds(39, 11, 321, 25);
       contentPane.add(lblNewLabel);
      
       //setting Price label
       JLabel lblNewLabel_1 = new JLabel("Enter Price of item:");
       lblNewLabel_1.setBackground(SystemColor.controlShadow);
       lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
       lblNewLabel_1.setBounds(64, 47, 128, 25);
       contentPane.add(lblNewLabel_1);
      
       //Creating textfield to enter values
       textFieldPrice = new JTextField();
       textFieldPrice.setBounds(212, 47, 64, 23);
       contentPane.add(textFieldPrice);
       textFieldPrice.setColumns(10);
      
       //textPane to display direction under price lable
       JTextPane txtpnfromCents = new JTextPane();
       txtpnfromCents.setFont(new Font("Tahoma", Font.ITALIC, 11));
       txtpnfromCents.setBackground(SystemColor.control);
       txtpnfromCents.setEditable(false);
       txtpnfromCents.setText("(from 25 cents to a dollar, in 5-cent increments)");
       txtpnfromCents.setBounds(64, 72, 140, 39);
       contentPane.add(txtpnfromCents);
      
       //Creating buy button
       JButton btnNewButton = new JButton("Buy");
      
       //Setting action listener to buy button
       btnNewButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
              
               int originalAmount=0;
               //parsing the value of textbox Price
               try
               {
               originalAmount = Integer.parseInt(textFieldPrice.getText());
              
                  
               }
               catch(Exception e)
               {
                   JOptionPane.showMessageDialog(null, "Please provide a valid input","Error",JOptionPane.WARNING_MESSAGE);
                   dispose();
               }
               //Calculating change
       int change = 100 - originalAmount;

       int quarters = change/25;
       change = change%25;// remaining change after deducting quarters
       int dimes = change/10;
       change = change%10;// remaining change after deducting dimes, too
       int nickels = change/5;
       change = change%5;
       int pennies = change;
      
       //Displaying result to message box
       JFrame frame = new JFrame("Message");
       JOptionPane.showMessageDialog(frame,
               "You bought an item for "+ originalAmount + " and gave me a dollar, "
               + "so your change is "+quarters + " quarters, "+dimes + " dimes, and "+nickels + " nickel.");
       dispose();

              
           }
       });
       //adding buy button to content pane
       btnNewButton.setBounds(212, 81, 64, 23);
       contentPane.add(btnNewButton);
   }
}


Related Solutions

In this section you will interpret output that is provided (you do NOT need to run...
In this section you will interpret output that is provided (you do NOT need to run anything for this scenario). I am interested in testing whether the section of SOC390 that a participant was in (Variable name = Class; 1= Miller, 2 = Field, 3= Smith) influences their exam scores (Variable name = exam_scores). I also think that gender might also influence exam scores (Variable name = Gender; 0 = female, 1 = male). To test this, I conducted a...
You are required to become familiar with the workings of the Rat in a Maze program...
You are required to become familiar with the workings of the Rat in a Maze program and make certain adjustments to the program. To become familiar with the program I suggest that you run it several times, using a different maze and maybe a different food location. Also trace by hand to ensure you understand how each step works. The adjustments to be made are: 1. Allow the RAT to move in a diagonal direction, so now there are a...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str -- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string...
Part 1: netstat Run the 'netstat' command and review the output. Note that the first part...
Part 1: netstat Run the 'netstat' command and review the output. Note that the first part of the output is a list of active Internet connections, and the second part is a list of active sockets. The format of each part is different (i.e. different number of columns per line, and different column headers). 1. Write a bash command line that will display the number of active Internet connections as reported by 'netstat'. Your output should be a single number....
you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that...
you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that contains counts for characters in a text (see submission folder on iCollege) and put it into the root of related Eclipse project folder. To view your project folder through system file explorer, right-click on ‘src’ folder in the Eclipse project explorer and choose ‘Show In->System Explorer’. Consider the following Java code, that reads a .csv file: BufferedReader csvReader = new BufferedReader(new FileReader("letter_count.csv")); String currentRow...
Download everything.c. You can compile and run it with the usual commands. You know that putting...
Download everything.c. You can compile and run it with the usual commands. You know that putting all your code into a single file is bad form. Therefore, your goal is to refactor everything.c so that the code is in multiple source (.c and .h) files, as well as write a Makefile to compile and run the program. Here are your constraints: There should be no code duplication Each .c and .h file must only #include header files that it actually...
Linux-Practise1: Run the following commands (Print working directory) pwd. What is the output of the above...
Linux-Practise1: Run the following commands (Print working directory) pwd. What is the output of the above command Create a file named Linux1 nano Linux1 Type the following in the file Hello world Save the file Create a copy of the file cp Linux1 Linux2 view the listing of the files in the directory            ls What is the output of the above command rename the file mv Linux2 Linux3 view the listing of the files in the directory            ls...
Part A: How acidic would the external pH need to become in order to make it...
Part A: How acidic would the external pH need to become in order to make it possible to synthesize one ATP molecule from the energy delivered when one proton enters the mitochondrial matrix? (Assume ATP formation requires 52.0 kJ/mol, 37 degrees celsius, membrane potential -0.12V, and matrix pH 7.2) Part B: Comment on whether this pH is feasible, BE SPECIFIC!
Over the past few weeks, you have become more familiar with your staff and are beginning...
Over the past few weeks, you have become more familiar with your staff and are beginning to feel like part of their family. To that end, you have recently accepted some "friend requests" on Facebook from some of the staff. You were at home last night, getting caught up on Facebook when you came across a post from one of the employees that said: " I hate my new boss at the clinic. A power hungry jerk making too many...
Assess the configuration of your local airport or some airport with which you can become familiar?...
Assess the configuration of your local airport or some airport with which you can become familiar? How would you describe the configuration? To what extent does it appear to meet the needs of the principal stakeholders? How flexible does it appear to be, to meet the requirements of plausible future traffic?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT