COSC 1173, Fall 2005 Java Programming Laboratory, Lab 7 c© Chung-Chih Li In this lab, we will use NetBeans IDE to compile two user defined classes. You will define a method of the class and understand the GUI components including JButton, JLabel, JTextField, and JPanel. Special Note: You should bring your textbook and all previous lab handouts to the lab. 1. Review Lab 2 handout and use NetBeans to create a new project named CS1Lab7 . You don’t have to new a program at this point. This time, you will not type in the most of the programs, you just modify them. 2. Go to http://hal.lamar.edu/∼licc/COSC1173Lab and click on Lab Handouts, then under Lab 7, click on Demo. You will see a Java applet in a page, and the contents in the applet are what you will create in a JFrame in this lab. Play with the applet to see what is it. 3. Back to the Lab Handouts page. Download the three Java programs (lab7.java, Account.java, and ATMPanel.jave) and save them under folder src of this project. (Right click the files and use save the target as). 4. Afterwards, go back to the NetBeans IDE and you should find the three files under src. 5. Compile your programs and run the project to see what you get. 6. Modify the programs to make them work like the applet in the Demo page. You should check what are missing and figure how to add those missing components to atm, which is a JFrame. Read the comments in the programs to get some hints. Or, you may go through the following questions first, that may help you to get some clues 7. Prepare your answers to the following questions in file Lab7Answers.txt (a) What GUI components are missing before you modify the programs? (b) Before modification, how many methods in class Account? (c) Can a user like ATMPanel use the variables (owner and balance) defined in class Account? (d) How does class Account provide values of variables owner and balance to its users? (e) Uncomment line 21 of program ATMPanel.java and run the project. What will happen? (f) Refer to Figure 1, what do lines 8, 9, and 11 of program lab7.java do? (g) Refer to Figure 3, what do lines 9, 13, and 15 of program ATMPanel.java do? (h) Refer to Figure 3, what does line 20 of program ATMPanel.java do? (i) Refer to Figure 3, what does line 27 of program ATMPanel.java do? (j) What is the purpose of the private class ATMListener in ARMPanel? (k) In class ATMListener, what does the if statement do? (l) Check folder build of this project. How many classes are there? List their names. 8. Create a folder named CS1Lab7 in your floppy disk # 1. Copy Lab6Answers.txt , lab7.java , Account.java and ATMPanel.java to your floppy disk # 1 under folder CS1Lab7. We will run and compile your programs. COSC 1173 Java Programming Laboratory Lab 7 Figure 1: Java Program for lab7.java 01 /* lab7.java */ 02 /* Chung-Chih Li Created on November 15, 2005, 11:42 PM */ 03 04 import javax.swing.JFrame; 05 06 public class lab7 { 07 public static void main(String[] args) { 08 JFrame atm = new JFrame("ATM"); 09 Account roger = new Account("Roger Clemens"); 10 atm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 11 atm.getContentPane().add(new ATMPanel(roger)); 12 atm.pack(); 13 atm.setVisible(true); 14 } 15 } 16 Figure 2: Java Program for Account.java 01 /* Account.java */ 02 /* Chung-Chih Li Created on November 15, 2005, 11:44 PM */ 03 04 public class Account { 05 private String owner; 06 private double balance; 07 08 public Account(String name) { 09 owner = name; 10 balance=0; 11 } 12 13 public void deposit(double amount) { 14 balance += amount; 15 } 16 17 public double withdraw(double amount) { 18 if (balance >= amount) 19 balance -= amount; 20 else { 21 amount = balance; 22 balance = 0; 23 } 24 return amount; 25 } 26 27 public String getName() { 28 return owner; 29 } 30 /******* Add your code from here *****/ 31 } 32 c© Chung-Chih Li P-2/3 COSC 1173 Java Programming Laboratory Lab 7 Figure 3: Java Program for ATMPanel.java 01 /* ATMPanel.java */ 02 /* Chung-Chih Li Created on November 16, 2005, 12:11 AM */ 03 import java.awt.*; 04 import java.awt.event.*; 05 import javax.swing.*; 06 07 public class ATMPanel extends JPanel{ 08 09 private JButton Deposit = new JButton("Deposit"); 10 /*************************************/ 11 /* Create a new Jbutton for withdraw */ 12 /*************************************/ 13 private JTextField Amount = new JTextField(10); 14 private JLabel Welcome = new JLabel(); 15 private JLabel Balance = new JLabel(); 16 Account owner; 17 18 public ATMPanel(Account newowner) { 19 setLayout (new BorderLayout()); 20 Deposit.addActionListener(new ATMListener()); 21 // Withdraw.addActionListener(new ATMListener()); 22 owner = newowner; 23 Welcome.setText("Welcome! "+ owner.getName() + "!"); 24 Balance.setText(" Your balance is $"); /* You may need to modify this */ 25 add(Welcome, BorderLayout.NORTH); 26 add(Balance, BorderLayout.SOUTH); 27 add(Deposit, BorderLayout.EAST); 28 add(Amount, BorderLayout.CENTER); 29 /*****************************************/ 30 /* Add your Jbutton to BorderLayout.WEST */ 31 /*****************************************/ 32 } 33 34 private class ATMListener implements ActionListener { 35 public void actionPerformed(ActionEvent whathappen) 36 { 37 double a = Double.parseDouble(Amount.getText()); 38 if (whathappen.getSource() == Deposit) 39 { 40 owner.deposit(a); 41 } 42 /*************************************/ 43 /****** add your code from here ******/ 44 /*************************************/ 45 Balance.setText(" Your balance is $"); /* You may need to modify this */ 46 } 47 } 48 } 49 c© Chung-Chih Li P-3/3