Solutions to midterm 2, W05

In the following we show the java code for the classes that were involved in the two questions. You can sort out the parts that you needed to write.

Q1.

/**
FILE: BankAccount.java
PURPOSE: implement methods for the Bank account example
    separate the class from the testing method
DATE: Jan 15, 2001
SOURCE: Horstmann, p. 110, p. 120
*/

class BankAccount {

    // instance variables
    private double balance ;

    // constructors
    public BankAccount() {
      balance = 0 ;
    }
    public BankAccount(double InitialBalance) {
	balance = InitialBalance ;
    }

    // methods
    public void deposit(double amount) {
	balance += amount ;
    }

    public void withdraw(double amount) {
	balance -= amount ;
    }

    public void monthlyFee() {
	final double MONTHLY_FEE = 10.00 ;
	// note the implicit parameter for monthlyFee is also given to withdraw
	withdraw(MONTHLY_FEE) ;  
    }

    public double getBalance() {
	return balance ;
    }

    public boolean equals(Object other)
    {
	if (other == null) return false ;
	if(!(other instanceof BankAccount)) return false ;
	if (getClass() != other.getClass()) return false ;
	BankAccount ba = (BankAccount) other ;
	return ba.balance == balance ;
    }

    public String toString()
    {
	return "BankAccount[balance = " + balance + "]" ;
    }

    public Object clone()
    {
	return  new BankAccount(balance) ;
    }
    
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BankAccountTest
{
    public static void main(String[] args)
    {
	BankAccountFrame frame = new BankAccountFrame();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.pack();
	frame.setVisible(true);
    }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BankAccountFrame extends JFrame
{
    private BankAccount ba;
    public BankAccountFrame()
    {
	ba = new BankAccount();

	/*make the two JButtons and two textfields and one JLabel*/
	JLabel balanceLabel = new JLabel("Balance");
	JButton depositButton = new JButton("Deposit");
	final JTextField amountField = new JTextField(5);
       	final JTextField balanceField = new JTextField(5);
	JButton withdrawButton = new JButton("Withdraw");

	/* Design listener for deposit button )*/
	class DepositListener implements ActionListener
	{
	    public void actionPerformed(ActionEvent ae)
	    {
		double amt = Double.parseDouble(amountField.getText());
		ba.deposit(amt);
		balanceField.setText(ba.getBalance()+"");
	    }
	}

	class withdrawListener implements ActionListener
	{
	    public void actionPerformed(ActionEvent ae)
	    {
		double amt = Double.parseDouble(amountField.getText());
		ba.withdraw(amt);
		balanceField.setText(ba.getBalance()+"");
	    }
	}
	/* Install listener in the deosit button */
	depositButton.addActionListener(new DepositListener());
	withdrawButton.addActionListener(new withdrawListener());

	/* For brevity we omit similar code for withdraw */
	/* put the components in thie fram in a layout that makes it appear as shown */
	JPanel panel1 = new JPanel();
	panel1.add(balanceLabel);
	panel1.add(balanceField);
	getContentPane().add(panel1, BorderLayout.CENTER);
	
	JPanel panel2 = new JPanel();
	panel2.add(depositButton);
	panel2.add(amountField);
	panel2.add(withdrawButton);
	getContentPane().add(panel2, BorderLayout.SOUTH);
    }//constructor
}//class



Q2.
/**
   File: Find.java  
   Class: Find
   Description: prints all the lines of given files that contain a
      given keyword
   Input: keyword followed by file list
   Output: lines from files containing the keyword
   Usage: java Find keyword f1 f2 ... fn     where fi are file names
 */

import java.io.* ;

public class Find
{
    public static void main(String[] args)
    {
	if (args.length < 2)
	    usage() ;

	//get keyword
	String keyword = args[0] ;

	//check all given files
	for (int i = 1 ; i < args.length ; i++) {
	    //Catch exceptions
	    try {
		FileReader fr = new FileReader(args[i]) ;
		BufferedReader br = new BufferedReader(fr) ;
		String line ;
		//check all lines of this file for keyword
		while ( (line = br.readLine()) != null) {
		    if (line.indexOf(keyword) > 0)
			System.out.println(args[i] + ": " +
					   line + "\n") ;
		}
	    }
	    catch (IOException e) {
		System.out.println("IOException on file: " + 
				   args[i] + ": " + e) ;
	    }
	}
    }
    /**
       Displays how to use the program
     */
    public static void usage() 
    {
	System.out.println("USAGE: java Find keyword file1 file2 ...file_n") ;
	System.exit(0) ;
    }
}


Q3
  1. e
  2. a
  3. c
  4. d
  5. c
  6. b
  7. a
  8. e
  9. c
  10. c
  11. b