Select Menu

Combined Post

Mag Posts

Theme images by konradlew. Powered by Blogger.

Blogger templates

Blogger news

Facebook

Advertising

Blogroll

Popular Posts

Combined Posts 2

Mag Post 2

2 Column Post

Simple Post

Simple Post 2

New Carousel

Video Posts

» » Java code for a GetChange program!


SLVIKI 9:15 AM 0

Here is the output of the program.





Here is the java Code of above program


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

/**
 * This application lets you type in an amount of
 * cents in a textfield and see a report of how to make that amount
 * with the fewest number of pennies, dimes, nickels and quarters.
 */
public class GetChange extends JFrame {
    private JTextField amountField = new JTextField(12);
    private Document amountText = amountField.getDocument();
    private JTextArea report = new JTextArea(20, 80);

    /**
     * Constructor for GetChange, laying out the frame and registering
     * listeners
     */
    public GetChange() {

        // Lay out the components in the window.
        JPanel topPanel = new JPanel();
        topPanel.add(new JLabel("Amount:"));
        topPanel.add(amountField);
        getContentPane().add(topPanel, BorderLayout.NORTH);
        getContentPane().add(new JScrollPane(report), BorderLayout.CENTER);

        // Set some properties of the frame and its components
        setBackground(Color.LIGHT_GRAY);
        report.setEditable(false);

        // Ensure the text changes in response to button presses.
        amountText.addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                updateReport();
            }
            public void insertUpdate(DocumentEvent e) {
                updateReport();
            }
            public void removeUpdate(DocumentEvent e) {
                updateReport();
            }
        });
    }

    /**
     * Writes the correct amount of coins in the report window.
     */
    void updateReport() {
        try {
            int amount = Integer.parseInt(
                    amountText.getText(0, amountText.getLength()));
            report.setText("\tTo make " + amount + " Rupees, use\n\t");
     report.append(amount/1000 + " \t1000 notes\n\t");
            amount %= 1000;
     report.append(amount/500 + " \t500 notes\n\t");
     amount %= 500;
            report.append(amount / 100 + " \tHundread notes\n\t");
            amount %= 100;
            report.append(amount / 50 + " \tFifty notes\n\t");
            amount %= 50;
            report.append(amount / 20 + " \tTwenty notes\n\t");
            amount %= 20;
            report.append(amount / 10 + " \tTen notes\n\t");
     amount %= 10;
     report.append(amount / 5 + " \tFive Coins\n\t");
     amount %= 5;
     report.append(amount / 2 + " \tTwo coins\n\t");
     amount %= 2;
     report.append(amount + " \tOne coins\n\t");
 } catch (NumberFormatException e) {
            report.setText("Not an integer or out of range");
        } catch (Exception e) {
            report.setText(e.toString());
        }
    }

    /**
     * Runs a changer as an application.
     */
    public static void main(String[] args) {
        JFrame frame = new GetChange();
        frame.setTitle("GetChange");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


Enjoy!

«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply