*********** In Java complete the following GUI. The instructions are in the imag
ID: 644805 • Letter: #
Question
***********In Java complete the following GUI. The instructions are in the image below and the program should look like the following diagram below. Finish the // To Do comments to complete the program and fix any other errors. The program should look like the following diagram below. The // To Do comments will be bolded for you.***************
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
This program displays the growth of an investment.
*/
public class InvestmentFrame extends JFrame
{
private JRadioButton CheckingRadioButton;
private JRadioButton SavingsRadioButton;
private JPanel buttonPanel;
private JPanel depositpanel;
private JPanel resultPanel;
private JLabel amountLabel;
private JTextField amountField;
private JButton depositButton;
private JLabel balanceLabel;
private CheckingAccount cAccount;
private SavingsAccount sAccount;
//TODO: Instance fields for GUI components
private static final double DEFAULT_AMOUNT = 0;
private static final double INITIAL_BALANCE = 1000;
private static final double INITIAL_RATE = 5;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 200;
public InvestmentFrame()
{
cAccount = new CheckingAccount(INITIAL_BALANCE);
sAccount = new SavingsAccount(INITIAL_RATE );
// TODO: Initialize component instance fields
JLabel amountLabel = new JLabel("Amount");
JLabel balanceLabel = new JLabel("Balance =");
JTextField amountField = new JTextField(6);
JButton depositButton = new JButton("Deposit");
// Use helper methods
JPanel buttonPanel = createButtonPanel();
JPanel resultPanel = createResultPanel();
JPanel depositPanel = createDepositPanel();
// TODO: add panels to content pane
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public JPanel createButtonPanel()
{
// TODO: add statements here to create the radio buttons
JRadioButton CheckingsRadioButton = new JRadioButton("Checking");
JRadioButton SavingsRadioButton = new JRadioButton("Savings");
ButtonGroup group = new ButtonGroup();
group.add(CheckingsRadioButton);
group.add(SavingsRadioButton);
class AccountListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// TODO: add statements here to display the balance of the currently selected account
JRadioButton CheckingsRadioButton = new JRadioButton("Checking");
CheckingsRadioButton.setSelected(true);
if(CheckingsRadioButton.isSelected())
{
balanceLabel.setText("Balance = " + INITIAL_BALANCE);
}
JRadioButton SavingsRadioButton = new JRadioButton("Savings");
SavingsRadioButton.setSelected(true);
if(SavingsRadioButton.isSelected())
{
balanceLabel.setText("Balance = " + INITIAL_BALANCE);
}
}
}
// TODO: create instance of ActionListener for the buttons and attach to the buttons
JPanel buttonPanel = new JPanel();
// TODO: add buttons to the panel
buttonPanel.add(CheckingsRadioButton);
buttonPanel.add(SavingsRadioButton);
add(buttonPanel, BorderLayout.NORTH);
return buttonPanel;
}
public JPanel createResultPanel()
{
JPanel resultPanel = new JPanel();
// TODO: add component to the result panel
resultPanel.add(balanceLabel);
add(resultPanel, BorderLayout.CENTER);
return resultPanel;
}
public JPanel createDepositPanel()
{
final int FIELD_WIDTH = 10;
// TODO: initialize instance fields on the deposit panel
// TODO: create deposit button
class DepositListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// TODO: add statements here deposit the amount in the currently selected account and update balance
}
}
// TODO: create instance of ActionListener and attach to deposit button
JPanel depositPanel = new JPanel();
// TODO: add components to the deposit panel
depositPanel.add(amountLabel);
depositPanel.add(amountField);
depositPanel.add(depositButton);
return depositPanel;
}
}
//******************** New Class
import javax.swing.JFrame;
/**
This program tests the InvestmentFrame.
*/
public class InvestmentFrameViewer
{
public static void main(String[] args)
{
JFrame frame = new InvestmentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// ************************ New Class
/**
An account that earns interest at a fixed rate.
*/
public class SavingsAccount extends BankAccount
{
/**
Constructs a bank account with a given interest rate.
@param rate the interest rate
*/
public SavingsAccount(double rate)
{
interestRate = rate;
}
/**
Adds the earned interest to the account balance.
*/
public void addInterest()
{
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
private double interestRate;
}
// ********************************* New Class
/**
A checking account that charges transaction fees.
*/
public class CheckingAccount extends BankAccount
{
/**
Constructs a checking account with a given balance.
@param initialBalance the initial balance
*/
public CheckingAccount(double initialBalance)
{
// Construct superclass
super(initialBalance);
// Initialize transaction count
transactionCount = 0;
}
public void deposit(double amount)
{
transactionCount++;
// Now add amount to balance
super.deposit(amount);
}
public void withdraw(double amount)
{
transactionCount++;
// Now subtract amount from balance
super.withdraw(amount);
}
/**
Deducts the accumulated fees and resets the
transaction count.
*/
public void deductFees()
{
if (transactionCount > FREE_TRANSACTIONS)
{
double fees = TRANSACTION_FEE *
(transactionCount - FREE_TRANSACTIONS);
super.withdraw(fees);
}
transactionCount = 0;
}
private int transactionCount;
private static final int FREE_TRANSACTIONS = 3;
private static final double TRANSACTION_FEE = 2.0;
}
// ************ New 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)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Transfers money from the bank account to another account
@param amount the amount to transfer
@param other the other account
*/
public void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
private double balance;
}
Explanation / Answer
I am not getting actual error..but i found straight way variables declartion errors..As java compiles from top to bottom approach..in your program variables declared at end of class.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
This program displays the growth of an investment.
*/
public class InvestmentFrame extends JFrame
{
private JRadioButton CheckingRadioButton;
private JRadioButton SavingsRadioButton;
private JPanel buttonPanel;
private JPanel depositpanel;
private JPanel resultPanel;
private JLabel amountLabel;
private JTextField amountField;
private JButton depositButton;
private JLabel balanceLabel;
private CheckingAccount cAccount;
private SavingsAccount sAccount;
//TODO: Instance fields for GUI components
private static final double DEFAULT_AMOUNT = 0;
private static final double INITIAL_BALANCE = 1000;
private static final double INITIAL_RATE = 5;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 200;
public InvestmentFrame()
{
cAccount = new CheckingAccount(INITIAL_BALANCE);
sAccount = new SavingsAccount(INITIAL_RATE );
// TODO: Initialize component instance fields
JLabel amountLabel = new JLabel("Amount");
JLabel balanceLabel = new JLabel("Balance =");
JTextField amountField = new JTextField(6);
JButton depositButton = new JButton("Deposit");
// Use helper methods
JPanel buttonPanel = createButtonPanel();
JPanel resultPanel = createResultPanel();
JPanel depositPanel = createDepositPanel();
// TODO: add panels to content pane
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public JPanel createButtonPanel()
{
// TODO: add statements here to create the radio buttons
JRadioButton CheckingsRadioButton = new JRadioButton("Checking");
JRadioButton SavingsRadioButton = new JRadioButton("Savings");
ButtonGroup group = new ButtonGroup();
group.add(CheckingsRadioButton);
group.add(SavingsRadioButton);
class AccountListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// TODO: add statements here to display the balance of the currently selected account
JRadioButton CheckingsRadioButton = new JRadioButton("Checking");
CheckingsRadioButton.setSelected(true);
if(CheckingsRadioButton.isSelected())
{
balanceLabel.setText("Balance = " + INITIAL_BALANCE);
}
JRadioButton SavingsRadioButton = new JRadioButton("Savings");
SavingsRadioButton.setSelected(true);
if(SavingsRadioButton.isSelected())
{
balanceLabel.setText("Balance = " + INITIAL_BALANCE);
}
}
}
// TODO: create instance of ActionListener for the buttons and attach to the buttons
JPanel buttonPanel = new JPanel();
// TODO: add buttons to the panel
buttonPanel.add(CheckingsRadioButton);
buttonPanel.add(SavingsRadioButton);
add(buttonPanel, BorderLayout.NORTH);
return buttonPanel;
}
public JPanel createResultPanel()
{
JPanel resultPanel = new JPanel();
// TODO: add component to the result panel
resultPanel.add(balanceLabel);
add(resultPanel, BorderLayout.CENTER);
return resultPanel;
}
public JPanel createDepositPanel()
{
final int FIELD_WIDTH = 10;
// TODO: initialize instance fields on the deposit panel
// TODO: create deposit button
class DepositListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// TODO: add statements here deposit the amount in the currently selected account and update balance
}
}
// TODO: create instance of ActionListener and attach to deposit button
JPanel depositPanel = new JPanel();
// TODO: add components to the deposit panel
depositPanel.add(amountLabel);
depositPanel.add(amountField);
depositPanel.add(depositButton);
return depositPanel;
}
}
//******************** New Class
import javax.swing.JFrame;
/**
This program tests the InvestmentFrame.
*/
public class InvestmentFrameViewer
{
public static void main(String[] args)
{
JFrame frame = new InvestmentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// ************************ New Class
/**
An account that earns interest at a fixed rate.
*/
public class SavingsAccount extends BankAccount
{
/**
Constructs a bank account with a given interest rate.
@param rate the interest rate
*/
private double interestRate;
public SavingsAccount(double rate)
{
interestRate = rate;
}
/**
Adds the earned interest to the account balance.
*/
public void addInterest()
{
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
}
// ********************************* New Class
/**
A checking account that charges transaction fees.
*/
public class CheckingAccount extends BankAccount
{
/**
Constructs a checking account with a given balance.
@param initialBalance the initial balance
*/
public CheckingAccount(double initialBalance)
{
// Construct superclass
super(initialBalance);
// Initialize transaction count
transactionCount = 0;
}
public void deposit(double amount)
{
transactionCount++;
// Now add amount to balance
super.deposit(amount);
}
public void withdraw(double amount)
{
transactionCount++;
// Now subtract amount from balance
super.withdraw(amount);
}
/**
Deducts the accumulated fees and resets the
transaction count.
*/
public void deductFees()
{
if (transactionCount > FREE_TRANSACTIONS)
{
double fees = TRANSACTION_FEE *
(transactionCount - FREE_TRANSACTIONS);
super.withdraw(fees);
}
transactionCount = 0;
}
private int transactionCount;
private static final int FREE_TRANSACTIONS = 3;
private static final double TRANSACTION_FEE = 2.0;
}
// ************ New 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.
*/
private double 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)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Transfers money from the bank account to another account
@param amount the amount to transfer
@param other the other account
*/
public void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.