1 Replies - 554 Views - Last Post: 12 May 2008 - 08:43 AM Rate Topic: -----

#1 blake11  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 20-February 08

Advise on proper OOP

Posted 12 May 2008 - 06:54 AM

So, I am working on an assignment that requires me to make an investment calculator. So far, I have created three classes: Driver, Investment, and Interest. I guess I want to make sure that I am thinking about this the right way, and if there is a better way of doing this, I would really appreciate advice on how to clean it up.

Here is my driver so far:

public class Driver extends javax.swing.JFrame {
	
	Investment inv = new Investment();
	Interest inter = new Interest();
	
	String[] columnNames = {"Date", "Total", "Interest Earned"};
	Object[][] rowData;
		
	DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);
	
	DecimalFormat fmt1 = new DecimalFormat("0.00");
	DecimalFormat fmt2 = new DecimalFormat("$,##0.00");
	   
	/** Creates new form Driver */
	public Driver() {
		initComponents();
		
		jTable1.setModel(tableModel);
		
		this.setLocationRelativeTo(null);
		
		Calendar cal = new GregorianCalendar();
		cal.set(2008, 2, 0, 0, 00);
				
		Investment invest = new Investment("College Fund", 5000, 1000, 2.3, cal, 1);
		   
		nameDisplay.setText(invest.getName());
		rateDisplay.setText(fmt1.format(invest.getRate()));
		addInvestDisplay.setText(fmt2.format(invest.getAddI()));
		inInvestDisplay.setText(fmt2.format(invest.getIniI()));
		startDisplay.setText(cal.get(Calendar.MONTH) + "/" + 
				cal.get(Calendar.YEAR));
		termDisplay.setText(Integer.toString(invest.getTerm()));
		
		tableModel.addRow(new Object[] {cal.get(Calendar.MONTH) + "/" + 
				cal.get(Calendar.YEAR),
				fmt2.format(invest.getIniI()), fmt2.format(115.0), true});
		
		inter.annualInvestment();
	}
	
	public void addNewRow(Interest o)
	{
		tableModel.addRow(new Object[]{o.getDate(), o.getTempT(), o.getIntE()});
	}



The object that I created is just for testing to make sure everything displays properly. There is a "New Investment" button on the frame to add a new investment.

Next, I have a class called Investment that is just used to create an Investment object.

public class Investment 
{
	private String name;
	private double iniI;
	private double rate;
	private double addI;
	private int term;
	private Calendar date;
   
	public Investment()
	{
		//No-arg Constructor//
	}
	
	public Investment
			(String n, double iI,double aI, double r, Calendar dt, int t)
	{
		name = n;
		iniI = iI;
		addI = aI;
		rate = r;
		date = dt;
		term = t;
	}
	
	public void setName(String n)
	{
		name = n;
	}
	
	public void setIniI(double iI)
	{
		iniI = iI;
	}
	
	public void setAddI(double aI)
	{
		addI = aI;
	}
	
	public void setRate(double r)
	{
		rate = r;
	}
	
	public void setDate(Calendar dt)
	{
		date = dt;
	}
	
	public void setTerm(int t)
	{
		term = t;
	}
	
	public String getName()
	{
		return name;
	}
	
	public double getIniI()
	{
		return iniI;
	}
	
	public double getAddI()
	{
		return addI;
	}
	
	public double getRate()
	{
		return rate;
	}
	
	public Calendar getDate()
	{
		return date;
	}
	
	public int getTerm()
	{
		return term;
	}
}



And the last class is Interest. I was planning on this class to handle calculating the interest earned and pushing it back to Driver to display it in the jTable. I am having an issue with that right now, so if anyone can enlighten me as to way I keep getting "java.lang.OutOfMemoryError: Java heap space" when I run my program.

public class Interest
{
	private Calendar date;	  //Date used to set loan start and for display//
	private double tempT;	   //Temporary investement total for display//
	private double tempI;	   //Used to display interest earned in a loop//
	private double intE;		//Interest earned//
	private double invT;		//Total investement return//
	private double intI;		//Interest for the first year to display in JTable//
	private double startTot;	//Used to start the total in the for loop//
	
	public Interest()
	{
		//No-arg Constructor//
	}
	
	public Interest(Calendar d, double total, double iE)
	{
		date = d;
		tempT = total;
		intE = iE;
	}
	
	public double getTempT()
	{
		return tempT;
	}
	
	public double getIntE()
	{
		return intE;
	}
	
	public Calendar getDate()
	{
		return date;
	}
				
	public void annualInvestment()
	{
		Driver dr = new Driver();
		
		Interest int2 = new Interest(date, 2000, 200);
		
		dr.addNewRow(int2);
	}



In the annualInvestment() method, I have it setup like this right now just trying to get it to work. You'll see in the Driver class there is method that is supposed to take the object created in Interest class and place it in a new row on the jTable. It's not working right now and I have no idea as to why. Thanks in advance for any advice someone can throw my way.

Is This A Good Question/Topic? 0
  • +

Replies To: Advise on proper OOP

#2 blake11  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 20-February 08

Re: Advise on proper OOP

Posted 12 May 2008 - 08:43 AM

Well, I fixed the problem of passing objects from my Interest class to my Driver class by making my Interest class an inner class of Driver. For the life of me, I cannot figure out how to pass objects correctly between classes to display in jTables, arrayLists, and arrays.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1