It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Help with programming assignment

Help with programming assignment

Postby SophmoreINhighschool on Wed Nov 22, 2006 8:00 pm

Hello, I am taking computer science in my high school. Right know we are working with the language of Java.

I have a programming excersize that I need help with, specifically P12.8 which comes from Cay horstmann's 2nd Edition Big Java Book.


"Write a graphical front end for a bank account class. Supply text fields and buttons for depositing and withdrawing money, and for displaying the current balance in a label."


If someone could help me out here, it would be appreciated.

THANKS,

Dan S





Here is my bank account class:


/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}

/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}

/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}

/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}

/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}

private double balance;
}



Here are some other classes to show you my ability and style:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
This program displays the growth of an investment.
*/
public class InvestmentViewer1
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

// The button to trigger the calculation
JButton button = new JButton("Add Interest");

// The application adds interest to this bank account
final BankAccount account = new BankAccount(INITIAL_BALANCE);

// The label for displaying the results
final JLabel label = new JLabel(
"balance=" + account.getBalance());

// The panel that holds the user interface components
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);

class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double interest = account.getBalance()
* INTEREST_RATE / 100;
account.deposit(interest);
label.setText(
"balance=" + account.getBalance());
}
}

ActionListener listener = new AddInterestListener();
button.addActionListener(listener);

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static final double INTEREST_RATE = 10;
private static final double INITIAL_BALANCE = 1000;

private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 100;
}


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
An action listener that prints a message.
*/
public class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("I was clicked.");
}
}



import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
This program demonstrates how to install an action listener.
*/
public class ButtonTester
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);

ActionListener listener = new ClickListener();
button.addActionListener(listener);

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static final int FRAME_WIDTH = 100;
private static final int FRAME_HEIGHT = 60;
}



THANKS :!:
SophmoreINhighschool
 
Posts: 0
Joined: Wed Nov 22, 2006 7:39 pm

Postby Grouchotron on Fri Dec 22, 2006 7:50 pm

I'm not one to be giving too much advice (I'm a first year java student as well), but if I were you I would replace the line's

double newBalance = balance + amount;
balance = newBalance;

in your deposit method with

balance += amount;


Also, with your withdraw method I would change

double newBalance = balance + amount;
balance = newBalance;

to

balance -= amount;


This makes no difference of course, except for to make code a little more concise, which seems important to some people. Also, tell me if that breaks any standards or rules, cause I can never be certain.
Grouchotron
 
Posts: 9
Joined: Fri Dec 22, 2006 2:40 am

Re: Help with programming assignment

Postby meongmania on Sat Dec 23, 2006 7:40 am

SophmoreINhighschool wrote://...

class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double interest = account.getBalance()
* INTEREST_RATE / 100;
account.deposit(interest);
label.setText(
"balance=" + account.getBalance());
}
}

ActionListener listener = new AddInterestListener();
button.addActionListener(listener);

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

//...
THANKS :!:


your Listener initializations are a bit long i think. It won't be a problem if your program only have few actionlisteners, but it will be confusing if you're working with large form with lots of ActionListeners, KeyListeners, and MouseListeners. Since you're writing one class for one event handler, imagine what would happen if your form consists of tons of buttons, item menus, fields, or other components?

and for the above, why don't you use inner class? instead of declaring new class just for handling one event. or why don't you group your coding so it will be much easier to read and modify. but as i see it, different people use different coding method regarding to event handling and gui. i used to write them based on the event handler not by the components. if it's implemeneted into your program, it should look like :

class SomeClass extends JFrame implements ActionListener, KeyListener{
//declare the components here...
//example :
// JButton startButton;
public SomeClass(){
initComponents();
initListeners();
}

//initialize the components here...
private void initComponents(){
//example :
//startButton = newJButton();
}

//initialize the listeners here...
private void initListeners(){
//example :
//startButton.addActionListener(this);
}

public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if(source == startButton){
//do something if the startButton is clicked...
}else
if(source ...
}

public void keyPressed(){}
public void keyReleased(){}
public void keyTyped(){}

}

even better if just separate the GUI class from the Listener class using inheritance. like this, your code'll be much much easier to read, and there won't be any problem if there are many components listed in the program.
meongmania
 
Posts: 16
Joined: Tue Dec 19, 2006 2:58 am

Postby meongmania on Sat Dec 23, 2006 7:47 am

and yeah, almost forgot. when solving problems for banking type or money, be carefull on your class and variable declarations. always remember to use encapsulation so there won't be any outsider classes that can access your engine. better yet, you should have hide the interest adder class inside a method so that nothing can access that class besides the method itself. java support inner classes, so use it : )
meongmania
 
Posts: 16
Joined: Tue Dec 19, 2006 2:58 am


Who is online

Users browsing this forum: No registered users and 0 guests