codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  How to make an investment calculator  mrnewbiw at 00:55 on Sunday, November 06, 2005
 

Hi,
I've started working on a college assignment but I am hoping to get some help along the way on my journey. I thought if someone could add to my program, then I could go off and work on it then if I get stuck again I could repost the revised code.

This is some of the instruction I have been given:
There are formulae to calculate the accumulated investment value of a series of regular payments and if you are familiar with these you may use them. However, since this program is to be written in an object oriented language (Java) it is sensible to consider any stream of regular annual payments as the sum of a sequence of single payments.

Here is what I have done so far:



import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
* USERS INPUTS:
1. The amount of the investment (decimal)
2. The duration of the investment in years (integer)
3. The interest rate for the projection (as a decimal representing a percentage, eg 3.5 for 3.5%)
4. Whether the investment is for a single initial payment or a regular annual payment.

PROGRAM OUTPUTS:
1. A table of the accumulated investment value at the end of each year (to be provided in a scrolling text area)
2. A graph of the accumulated investment at the end of each year.

ADDITIONAL SOFTWARE REQUIREMENTS:
1. The user should be able to select from a file menu to export the table of accumulated investment value to a file of their choosing so that they can analyse the investment in a spreadsheet or other application if they so desire.
.
*
* @author ()
* @version (version number 03)
*/

// These will be the questions:
// ("How much do you want to invest?");
// ("How many years to you want to invest your money for?");
// ("What is the interest rate?");
// ("Is you investment is for a single initial payment or a regular annual payment?");


// JPanel radioButtonPanel = new JPanel();
// radioButtonPanel.setLayout(new GridLayout(3, 1));
// radioButton.setBorder(new TitledBorder(
// new EtchedBorder(), "size"));
// radioButtonPanel.add(smallbutton);
// radioButtonPanel.add(mediumbutton);
// radioButtonPanel.add(largebutton);



/**
.
*/
public class InvestCalc extends JFrame
{


/**
Constructs the frame.
*/

public InvestCalc()
{
{
// construct text sample
sampleField = new JLabel("Investment Calculator ");
getContentPane().add(sampleField, BorderLayout.CENTER);

// this listener is shared among all components
class ChoiceListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
setSampleFont();
}
}

listener = new ChoiceListener();

createControlPanel();
setSampleFont();
pack();
}

// construct menu

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

menuBar.add(createFileMenu());
// menuBar.add(createEditMenu());
}

/**
Creates the File menu.
@return the menu
*/
public JMenu createFileMenu()
{
JMenu menu = new JMenu("File");
// menu.add(createFileNewItem());
menu.add(createFileExitItem());
return menu;
}

// public JMenu createEditMenu()
// {
// JMenu menu = new JMenu("Edit");
// menu.add(createMoveMenu());
// menu.add(createEditRandomizeItem());
// return menu;
// }
/**
Creates the File->New menu item and sets its action listener.
@return the menu item
*/
// public JMenuItem createFileNewItem()
// {
// JMenuItem item = new JMenuItem("New");
// class MenuItemListener implements ActionListener
// {
// public void actionPerformed(ActionEvent event)
// {
// frame.reset();
// }
// }
// ActionListener listener = new MenuItemListener();
// item.addActionListener(listener);
// return item;
// }
/**
Creates the File->Exit menu item and sets its action listener.
@return the menu item
*/
public JMenuItem createFileExitItem()
{
JMenuItem item = new JMenuItem("Exit");
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);
return item;
}
/**
.
*/
public void createControlPanel()
{
JPanel whetherPanel = createComboBox(); //this used to be faceGroupPanel
JPanel amountGroupPanel = createCheckBoxes(); //this used to be sizeGroupPanel
JPanel styleGroupPanel = createRadioButtons(); //this used to be styleGroupPanel
JPanel interestGroupPanel = createRadioButtons();


// line up component panels

JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(5, 2));
controlPanel.add(whetherPanel);
controlPanel.add(amountGroupPanel);
controlPanel.add(styleGroupPanel);
controlPanel.add(interestGroupPanel);

// add panels to content pane

getContentPane().add(controlPanel, BorderLayout.SOUTH);
controlPanel.setBorder
(new TitledBorder(new EtchedBorder(), "Is the investment is for a single initial payment or a regular annual payment."));
}

/**
Creates the combo box.
@return the panel containing the combo box
*/
public JPanel createComboBox()
{
whetherCombo = new JComboBox();
whetherCombo.addItem("single initial payment");
whetherCombo.addItem("regular annual payment");
whetherCombo.setEditable(false);
whetherCombo.addActionListener(listener);

JPanel panel = new JPanel();
panel.add(whetherCombo);


return panel;
}

/**
Creates the check boxes
@return the panel containing the check boxes
*/
public JPanel createCheckBoxes()
{
italicCheckBox = new JCheckBox("there will be");
italicCheckBox.addActionListener(listener);

boldCheckBox = new JCheckBox("a text field here soon");
boldCheckBox.addActionListener(listener);

JPanel panel = new JPanel();
panel.add(italicCheckBox);
panel.add(boldCheckBox);
panel.setBorder
(new TitledBorder(new EtchedBorder(), "How much money would you like to invest?"));

return panel;
}

/**
Creates the radio buttons
@return the panel containing the radio buttons
*/
public JPanel createRadioButtons()
{
smallButton = new JRadioButton("Small");
smallButton.addActionListener(listener);

mediumButton = new JRadioButton("Medium");
mediumButton.addActionListener(listener);

largeButton = new JRadioButton("Large");
largeButton.addActionListener(listener);
largeButton.setSelected(true);

// add radio buttons to button group

ButtonGroup group = new ButtonGroup();
group.add(smallButton);
group.add(mediumButton);
group.add(largeButton);

JPanel panel = new JPanel();
panel.add(smallButton);
panel.add(mediumButton);
panel.add(largeButton);
panel.setBorder
(new TitledBorder(new EtchedBorder(), "What is the duration of the investment in years?"));

return panel;
}

/**
Gets user choice .
*/
public void setSampleFont()
{ // get font name

String whether
= (String)whetherCombo.getSelectedItem();

// get font style

int style = 0;
if (italicCheckBox.isSelected())
style = style + Font.ITALIC;
if (boldCheckBox.isSelected())
style = style + Font.BOLD;

// get font size

int size = 0;

final int SMALL_SIZE = 24;
final int MEDIUM_SIZE = 36;
final int LARGE_SIZE = 48;

if (smallButton.isSelected())
size = SMALL_SIZE;
else if (mediumButton.isSelected())
size = MEDIUM_SIZE;
else if (largeButton.isSelected())
size = LARGE_SIZE;

// set font of text field

sampleField.setFont(new Font(whether, style, size));
sampleField.repaint();
}

// private RectanglePanel panel;
private JLabel sampleField;
private JCheckBox italicCheckBox;
private JCheckBox boldCheckBox;
private JRadioButton smallButton;
private JRadioButton mediumButton;
private JRadioButton largeButton;
private JComboBox whetherCombo;
private ActionListener listener;
private JComboBox facenameCombo;
}




import javax.swing.JFrame;

/**
This program tests the ChoiceFrame.
*/
public class InvesetCalcTest
{
public static void main(String[] args)
{
JFrame frame = new InvestCalc();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}



  Re: How to make an investment calculator  crwood at 06:20 on Sunday, November 06, 2005
 


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

public class IC extends JFrame
{
// private RectanglePanel panel;
private JLabel sampleField;
private JCheckBox italicCheckBox;
private JCheckBox boldCheckBox;
private JRadioButton smallButton;
private JRadioButton mediumButton;
private JRadioButton largeButton;
private JComboBox whetherCombo;
private JComboBox facenameCombo;

public IC()
{
MenuItemListener menuItemListener = new MenuItemListener();
RadioButtonListener radioButtonListener = new RadioButtonListener(this);
CheckBoxListener checkBoxListener = new CheckBoxListener(this);
// construct components
sampleField = new JLabel("Investment Calculator ");
JPanel controlPanel = getControlPanel(radioButtonListener,
checkBoxListener);

// JFrame code
JMenuBar menuBar = new JMenuBar();
menuBar.add(createFileMenu(menuItemListener));
menuBar.add(createEditMenu());
setJMenuBar(menuBar);
getContentPane().add(sampleField, BorderLayout.NORTH);
getContentPane().add(controlPanel, BorderLayout.CENTER);
pack();
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

/**
* Creates the File menu.
* @return the menu
*/
public JMenu createFileMenu(ActionListener l)
{
JMenu menu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
JMenuItem exportItem = new JMenuItem("Export");
JMenuItem exitItem = new JMenuItem("Exit");
newItem.setActionCommand("new");
exportItem.setActionCommand("export");
exitItem.setActionCommand("exit");
newItem.addActionListener(l);
exportItem.addActionListener(l);
exitItem.addActionListener(l);
menu.add(newItem);
menu.add(exportItem);
menu.add(exitItem);
return menu;
}

public JMenu createEditMenu()
{
JMenu menu = new JMenu("Edit");
return menu;
}

public JPanel getControlPanel(ActionListener rbl, ActionListener cbl)
{
JPanel whetherPanel = createComboBox();
JPanel amountGroupPanel = createCheckBoxes(cbl);
JPanel styleGroupPanel = createRadioButtons(rbl);
JPanel interestGroupPanel = createRadioButtons(rbl);
// layout
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(0, 1)); // single column
controlPanel.add(whetherPanel);
controlPanel.add(amountGroupPanel);
controlPanel.add(styleGroupPanel);
controlPanel.add(interestGroupPanel);
controlPanel.setBorder(new TitledBorder(
new EtchedBorder(),
"Is the investment is for a single initial " +
"payment or a regular annual payment."));
setSampleFont();
return controlPanel;
}

/**
* Creates the combo box.
* @return the panel containing the combo box
*/
public JPanel createComboBox()
{
whetherCombo = new JComboBox();
whetherCombo.addItem("single initial payment");
whetherCombo.addItem("regular annual payment");
whetherCombo.setEditable(false);
whetherCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
String s = (String)cb.getSelectedItem();
System.out.println("s = " + s);
}
});

JPanel panel = new JPanel();
panel.add(whetherCombo);
return panel;
}

/**
* Creates the check boxes
* @return the panel containing the check boxes
*/
public JPanel createCheckBoxes(ActionListener l)
{
italicCheckBox = new JCheckBox("there will be");
italicCheckBox.addActionListener(l);

boldCheckBox = new JCheckBox("a text field here soon");
boldCheckBox.addActionListener(l);

JPanel panel = new JPanel();
panel.add(italicCheckBox);
panel.add(boldCheckBox);
panel.setBorder(new TitledBorder(
new EtchedBorder(),
"How much money would you like to invest?"));
return panel;
}

/**
* Creates the radio buttons
* @return the panel containing the radio buttons
*/
public JPanel createRadioButtons(ActionListener l)
{
smallButton = new JRadioButton("Small");
smallButton.addActionListener(l);

mediumButton = new JRadioButton("Medium");
mediumButton.addActionListener(l);

largeButton = new JRadioButton("Large");
largeButton.addActionListener(l);
largeButton.setSelected(true);

// add radio buttons to button group
ButtonGroup group = new ButtonGroup();
group.add(smallButton);
group.add(mediumButton);
group.add(largeButton);

JPanel panel = new JPanel();
panel.add(smallButton);
panel.add(mediumButton);
panel.add(largeButton);
panel.setBorder(new TitledBorder(
new EtchedBorder(),
"What is the duration of the investment in years?"));
return panel;
}

/**
* Gets user choice .
*
*/
public void setSampleFont()
{
// get font name
String whether = (String)whetherCombo.getSelectedItem();
// get font style
int style = 0;
if (italicCheckBox.isSelected())
style = style + Font.ITALIC;
if (boldCheckBox.isSelected())
style = style + Font.BOLD;
// get font size
int size = 0;
final int SMALL_SIZE = 24;
final int MEDIUM_SIZE = 36;
final int LARGE_SIZE = 48;

if (smallButton.isSelected())
size = SMALL_SIZE;
else if (mediumButton.isSelected())
size = MEDIUM_SIZE;
else if (largeButton.isSelected())
size = LARGE_SIZE;

// set font of text field
sampleField.setFont(new Font(whether, style, size));
}

public static void main(String[] args)
{
new IC();
}
}

class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JMenuItem item = (JMenuItem)event.getSource();
String ac = item.getActionCommand();
System.out.println("menu ac = " + ac);
if(ac.equals("new"))
;
if(ac.equals("export"))
;
if(ac.equals("exit"))
System.exit(0);
}
}

class RadioButtonListener implements ActionListener
{
IC ic;

public RadioButtonListener(IC ic)
{
this.ic = ic;
}

public void actionPerformed(ActionEvent e)
{
JRadioButton rb = (JRadioButton)e.getSource();
String ac = rb.getActionCommand();
System.out.println("rb ac = " + ac);
ic.setSampleFont();
}
}

class CheckBoxListener implements ActionListener
{
IC ic;

public CheckBoxListener(IC ic)
{
this.ic = ic;
}

public void actionPerformed(ActionEvent e)
{
JCheckBox cb = (JCheckBox)e.getSource();
String ac = cb.getActionCommand();
System.out.println("cb ac = " + ac);
ic.setSampleFont();
}
}


  Re: How to make an investment calculator  mrnewbiw at 08:44 on Wednesday, November 09, 2005
 

Hi CR Wood,
thanks so much for that. I've made some progress but got stuck again. Here is my current code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class IC extends JFrame
{
// private RectanglePanel panel;
private JLabel sampleField;
private JCheckBox italicCheckBox;
private JCheckBox boldCheckBox;
private JRadioButton smallButton;
private JRadioButton mediumButton;
private JRadioButton largeButton;
private JComboBox whetherCombo;
private JComboBox facenameCombo;

public IC()
{
MenuItemListener menuItemListener = new MenuItemListener();
RadioButtonListener radioButtonListener = new RadioButtonListener(this);
CheckBoxListener checkBoxListener = new CheckBoxListener(this);
// construct components
sampleField = new JLabel("Investment Calculator ");
JPanel controlPanel = getControlPanel(radioButtonListener,
checkBoxListener);

// JFrame code
JMenuBar menuBar = new JMenuBar();
menuBar.add(createFileMenu(menuItemListener));
menuBar.add(createEditMenu());
setJMenuBar(menuBar);
getContentPane().add(sampleField, BorderLayout.NORTH);
getContentPane().add(controlPanel, BorderLayout.CENTER);
pack();
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

// public static void main(String[] args)
// {
// Payment p1 = new Payment(1000, 0);
// Payment p2 = new Payment(1000, 1);
// Payment p3 = new Payment(1000, 2);
// PaymentSeries ps = new PaymentSeries();
// //more code to add the payments to the PaymentSeries
// System.out.println("Value is " + p3.getValue(7, 0.08));
// JFrame appFrame = new InvestorAppFrame();
// appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// appFrame.setVisible(true);
// }


/**
* Creates the File menu.
* @return the menu
*/
public JMenu createFileMenu(ActionListener l)
{
JMenu menu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
JMenuItem exportItem = new JMenuItem("Export");
JMenuItem exitItem = new JMenuItem("Exit");
newItem.setActionCommand("new");
exportItem.setActionCommand("export");
exitItem.setActionCommand("exit");
newItem.addActionListener(l);
exportItem.addActionListener(l);
exitItem.addActionListener(l);
menu.add(newItem);
menu.add(exportItem);
menu.add(exitItem);
return menu;
}

public JMenu createEditMenu()
{
JMenu menu = new JMenu("Edit");
return menu;
}

public JPanel getControlPanel(ActionListener rbl, ActionListener cbl)
{
JPanel whetherPanel = createComboBox();
JPanel amountGroupPanel = createTextField(cbl);
JPanel amountGroupPanel = createTextField02(cbl);
// layout
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(0, 1)); // single column
controlPanel.add(whetherPanel);
controlPanel.add(amountGroupPanel);
controlPanel.add(interestGroupPanel);
controlPanel.setBorder(new TitledBorder(
new EtchedBorder(),
"Is the investment is for a single initial " +
"payment or a regular annual payment."));

return controlPanel;
}

/**
* Creates the combo box.
* @return the panel containing the combo box
*/
public JPanel createComboBox()
{
whetherCombo = new JComboBox();
whetherCombo.addItem("single initial payment");
whetherCombo.addItem("regular annual payment");
whetherCombo.setEditable(false);
whetherCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
String s = (String)cb.getSelectedItem();
System.out.println("s = " + s);
}
});

JPanel panel = new JPanel();
panel.add(whetherCombo);
return panel;
}

/**
* Creates a text field
* @return the panel containing the text field
*/
public JPanel createTextField(ActionListener l)
{

final JTextField xField = new JTextField(5);
xField.addActionListener(l);
JLabel xLabel = new Jlabel("$");


JPanel panel = new JPanel();
panel.add(xLabel);
panel.add(xField);
panel.setBorder(new TitledBorder(
new EtchedBorder(),
"How much money would you like to invest?"));
return panel;
}

/**
* Creates a text field
* @return the panel containing the text field
*/
public JPanel createTextField02(ActionListener 2)
{

final JTextField yField = new JTextField(5);
yField.addActionListener(l);
JLabel yLabel = new Jlabel("years")


JPanel panel = new JPanel();
panel.add(yField);
panel.add(yLabel);
panel.setBorder(new TitledBorder(
new EtchedBorder(),
"What is the duration of the investment in years?"));
return panel;
}

/**
* Gets user choice .
*
*/


public static void main(String[] args)
{
new IC();
}
}

class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JMenuItem item = (JMenuItem)event.getSource();
String ac = item.getActionCommand();
System.out.println("menu ac = " + ac);
if(ac.equals("new"))
;
if(ac.equals("export"))
;
if(ac.equals("exit"))
System.exit(0);
}
}

class RadioButtonListener implements ActionListener
{
IC ic;

public RadioButtonListener(IC ic)
{
this.ic = ic;
}

public void actionPerformed(ActionEvent e)
{
JRadioButton rb = (JRadioButton)e.getSource();
String ac = rb.getActionCommand();
System.out.println("rb ac = " + ac);

}
}

class CheckBoxListener implements ActionListener
{
IC ic;

public CheckBoxListener(IC ic)
{
this.ic = ic;
}

public void actionPerformed(ActionEvent e)
{
JCheckBox cb = (JCheckBox)e.getSource();
String ac = cb.getActionCommand();
System.out.println("cb ac = " + ac);

}
}



  Re: How to make an investment calculator  crwood at 19:14 on Wednesday, November 09, 2005
 

Most of the compile errors were typos. One component ('amountGroupPanel') was listed twice instead of a declaration/instantiation for 'interestGroupPanel'. In addition to what is wrong the compile errors usually give the line number of the error in your source file. You can often count down from the top to find ther trouble. Or set a marker, eg, "/index.html", in the general area and compile to get the line number in the console.
About this code in the extra 'main' method:

public static void main(String[] args)
{
Payment p1 = new Payment(1000, 0);
Payment p2 = new Payment(1000, 1);
Payment p3 = new Payment(1000, 2);

When you do everything on the console then you usually put the control statements (that direct the application) inside the main method. When you use a gui you put this kind of control-statement code in your event listeners/event code. This is so the user can initiate and control the activities in the app. Once you get the gui set up you can set up the listeners and then start putting the event code together.
See what you think of this

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

public class IC1 extends JFrame
{
private JTextField amountField;
private JTextField interestField;
private JTextField termField;
private JTextArea resultsArea;

public IC1()
{
super("Investment Calculator");
MenuItemListener menuItemListener = new MenuItemListener();
RadioButtonListener radioButtonListener = new RadioButtonListener(this);
// construct components
JPanel entryPanel = getEntryPanel(radioButtonListener);
resultsArea = getResultsArea();

// JFrame code
JMenuBar menuBar = new JMenuBar();
menuBar.add(createFileMenu(menuItemListener));
menuBar.add(createEditMenu());
setJMenuBar(menuBar);
getContentPane().add(entryPanel, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(resultsArea), BorderLayout.CENTER);
pack();
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

/**
* Creates the File menu.
* @return the menu
*/
private JMenu createFileMenu(ActionListener l)
{
JMenu menu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
JMenuItem exportItem = new JMenuItem("Export");
JMenuItem exitItem = new JMenuItem("Exit");
newItem.setActionCommand("new");
exportItem.setActionCommand("export");
exitItem.setActionCommand("exit");
newItem.addActionListener(l);
exportItem.addActionListener(l);
exitItem.addActionListener(l);
menu.add(newItem);
menu.add(exportItem);
menu.add(exitItem);
return menu;
}

private JMenu createEditMenu()
{
JMenu menu = new JMenu("Edit");
return menu;
}

private JPanel getEntryPanel(ActionListener l)
{
amountField = new JTextField(8);
interestField = new JTextField(4);
termField = new JTextField(4);
String[] ids = { "single", "annual" };
// layout
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 2)); // 2 columns
// row 1
JPanel p = new JPanel();
p.add(new JLabel("amount: "));
p.add(amountField);
panel.add(p);
// radio buttons
p = new JPanel(); // reuse reference p
p.add(new JLabel("payments: "));
ButtonGroup group = new ButtonGroup();
for(int j = 0; j < ids.length; j++)
{
JRadioButton rb = new JRadioButton(ids[j]);
rb.setActionCommand(ids[j]);
rb.addActionListener(l);
group.add(rb);
p.add(rb);
}
panel.add(p);
// row 2
p = new JPanel();
p.add(new JLabel("interest: "));
p.add(interestField);
panel.add(p);
p = new JPanel();
p.add(new JLabel("years: "));
p.add(termField);
panel.add(p);
return panel;
}

private JTextArea getResultsArea()
{
resultsArea = new JTextArea(8,30);
resultsArea.setMargin(new Insets(2,5,2,5));
return resultsArea;
}

public static void main(String[] args)
{
new IC1();
}
}

class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JMenuItem item = (JMenuItem)event.getSource();
String ac = item.getActionCommand();
System.out.println("menu ac = " + ac);
if(ac.equals("new"))
;
if(ac.equals("export"))
;
if(ac.equals("exit"))
System.exit(0);
}
}

class RadioButtonListener implements ActionListener
{
IC1 ic;

public RadioButtonListener(IC1 ic)
{
this.ic = ic;
}

public void actionPerformed(ActionEvent e)
{
JRadioButton rb = (JRadioButton)e.getSource();
String ac = rb.getActionCommand();
System.out.println("rb ac = " + ac);
if(ac.equals("single"))
;
if(ac.equals("annual"))
;
}
}

Once the user enters the necessary data they will need a way(s) to get the app to do something with it. One option is to add some buttons along the south section of the JFrame. When the user presses a button the buttons event listener can call a method that will collect the data from the entry panel and process it...

  Re: How to make an investment calculator  mrnewbiw at 08:07 on Friday, November 18, 2005
 

Hi CR,
thanks a heap for that. I will upload a zip file of my final project onto my website next week for you to see. In the mean time I have a couple of questions.
Are these number types in the correct order?
long, short, float, int, double, byte

Do you know what this would be in regular maths?
(Math.sqrt(25.0)* b – b / 2)/4;
I'm thinking the answer would be 5. Because Math.sqrt(25.0) is 5 x 10 = 50 -10 = 40 / 2 = 20 /4 = 5

You have written a method called returnLadderHeightUpWall(double d) that takes a double parameter representing the distance in metres that the base of a 5 metre ladder is set from a wall and returns the height that the ladder will reach up the wall. You want to test this method. List a positive test case, a boundary test case and a negative test case that you would use to test your method.

Is it possible to convert a superclass reference into a subclass reference? If so, give an example. If not, explain why not.

  Re: How to make an investment calculator  crwood at 22:22 on Friday, November 18, 2005
 

If you order them by size it could be
byte, short, integer, long, float, double
See the page Variables and the page that follows for details.

(Math.sqrt(25.0)* b – b / 2)/4; with b = 10

"Math.sqrt(25.0)" returns a double
(5.0 * 10 - 10/2)/4 = (50.0 - 5)/4 = 45.0/4 = 11.25
if Math.sqrt(25.0) returned an int
(5 * 10 - 10/2)/4 = (50 - 5)/4 = 45/4 = 11
You can add these expressions to an existing app or make up a small test app to check it.

public class Test
{
public static void main(String[] args)
{
int b = 10;
System.out.println(((Math.sqrt(25.0)* b - b / 2)/4));
// positive test case
double positive = getLadderHeightUpWall(2.0);
// negative test case
double negative = getLadderHeightUpWall(-2.0);
// boundry test case
double boundry = getLadderHeightUpWall(5.0);
System.out.println("positive = " + positive + "\n" +
"negative = " + negative + "\n" +
"boundry = " + boundry);
}

private static double getLadderHeightUpWall(double base)
{
double length = 5.0;
// length^2 = base^2 + height^2
double height = Math.sqrt(length*length - base*base);
return height;
}
}

Is it possible to convert a superclass reference into a subclass reference?

A subclass extends a superclass so it is everything that the superclass is. If it adds or changes anything, eg, adding new methods, overriding superclass methods, adding new member variables, then it is more than the superlass. And the superclass has no way of knowing about it. So no, the superclass is not the same as a subclass and cannot be converted to a subclass. A subclass can be converted to its superclass since it is all that its superclass is, and more.

public class ClassTest
{
public static void main(String[] args)
{
Superclass superclass = new Superclass();
Subclass sub1 = new Subclass();
Superclass super1 = (Superclass)sub1;
// Subclass sub2 = (Subclass)superclass;
System.out.println("superclass = " + superclass + "\n" +
"sub1 = " + sub1 + "\n" +
"super1 = " + super1);
}
}

class Superclass
{
String id;

public Superclass()
{
id = getClass().getName();
}

public String toString() { return id; }
}

class Subclass extends Superclass
{
String id;
String alt;

public Subclass()
{
id = getClass().getName();
alt = "extra";
}

public String toString() { return id + ", " + alt; }
}


  one last question  mrnewbiw at 23:28 on Friday, November 18, 2005
 

Question 1
Write an abstract class Computer with a constructor that takes the brandName as the parameter. Also define abstract accessor and mutator methods for the brandName instance field. Comment your class appropriately so that the javadoc utility could be used to create documentation for this class. Take care to explicity specify the access control of your methods and instance fields.

Question 2
Assuming that you have an abstract class Computer as defined in Question 1 above, write a child class Laptop that extends Computer. When you implement your accessor, return the brandName with the String “ (Laptop)†appended to it. Comment your code for this question and provide an additional attribute for the class (a sensible attribute, of your choosing). Also provide an appropriate constructor for Laptop.

Question 3
Write a main method that prompts a user to enter a positive integer into a JOptionPane and then prints the divisors of the number to the terminal window. Provide error checking and exception handling for incorrect user input. (You do not have to comment your code.)


  Re: How to make an investment calculator  crwood at 17:27 on Saturday, November 19, 2005
 


public class ComputerTest
{
public static void main(String[] args)
{
Laptop laptop = new Laptop("Dell");
System.out.println("laptop name = " + laptop.getName());
}
}

/**
* Computer is the abstract base class for all computers.
*/
abstract class Computer
{
/** The brand name of this computer. */
protected String brandName;

/**
* Sole constructor.
* @ param name brand name of this computer.
*/
public Computer(String name)
{
brandName = name;
}

/**
* Returns the brand name of this computer.
* @ return the name.
*/
public String getName()
{
return brandName;
}
}

/**
* A Laptop computer.
*/
class Laptop extends Computer
{
/** The screen size of this computer. */
int screenSize;

/**
* Sole constructor.
* @ param name brand name of this computer.
*/
public Laptop(String name)
{
super(name);
}

/**
* Returns the brand name of this computer with the laptop
* designation.
* @ return the name.
*/
public String getName()
{
return brandName + " (Laptop)";
}
}


import javax.swing.JOptionPane;

public class EntryTest
{
public static void main(String[] args)
{
String in = JOptionPane.showInputDialog(null, "enter an integer");
int n = 0;
try
{
n = Integer.parseInt(in);
}
catch(NumberFormatException nfe)
{
System.err.println("format error: " + nfe.getMessage());
System.exit(1);
}
int[] factors = getFactors(n);
System.out.println("factors of " + n);
for(int j = 0; j < factors.length; j++)
{
System.out.print(factors[j]);
if(j < factors.length-1)
System.out.print(", ");
}
System.exit(0);
}

private static int[] getFactors(int n)
{
int[] temp = new int[n];
int count = 0;
for(int j = 1; j <= n; j++)
if(n % j == 0)
temp[count++] = j;
int[] factors = new int[count];
System.arraycopy(temp, 0, factors, 0, count);
return factors;
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005