Results 1 to 8 of 8
  1. #1
    lcrakel87 is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default Help With My First Class

    I just started getting in the object oriented part of Java. When I look at an example I feel like I understand whats going on but I'm completely lost when I try to write my own class. I need to write a class that contains an account number, loan amount, loan period, and interest rate. I need an accessor method for each instance variable. I also have to payment methods, but those are provided. Anyone have an pointers on where to start?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you've read the book, what you have done so far. Show your attempt first here. Then we can discuss about it and do the rest.

  3. #3
    lcrakel87 is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    Ok this is what I have so far. Also if anyone would let me know of any good online tutorials for writing classes, I would appreciate it.

    Java Code:
    public class Mortgage
    {
    	private static int acct_num = 37450000;
    	private int loan_amount = 100000;
    	private int loan_period = 30;
    	private double interest_rate = .075;	
    	
    	//------------
    	//constructors
    	//------------
    	
    	public Mortgage (int loanAmount,  int loanPeriod, double interestRate)
    	{
    		acct_num = acct_num + 1;
    		loan_period = loanPeriod;
    		loan_amount = loanAmount;
    		interest_rate = interestRate;
    		
    	}
    	
    	public Mortgage (int loanAmount, int loanPeriod)
    	{
    		acct_num = acct_num + 1;
    		loan_amount = loanAmount;
    		loan_period = loanPeriod;
    		interest_rate = .075;
    		
    	}
    	
    	public Mortgage (int loanAmount)
    	{
    		acct_num = acct_num + 1;
    		loan_amount = loanAmount;
    		loan_period = 30;
    		interest_rate = .075;
    	
    	}
    	
    	public Mortgage ()
    	{
    		acct_num = acct_num +1;
    		loan_amount = 100000;
    		loan_period = 30;
    		interest_rate = .075;
    	
    	}
    	//----------------
    	//Accessor methods
    	//----------------
    	
    	public int getLoanAmonut()
    	{
    		return loan_amount;
    	}
    	public int getLoanPeriod()
    	{
    		return loan_period;
    	}
    	public double getInterestRate()
    	{
    		return interest_rate;
    	}
    	public  int getAccountNumber()
    	{
    		return acct_num;
    	}
    	public String toString()
    	{
    		return "Loan Amount:       " +loan_amount   +"\n" +
    			   "Loan Period:       " +loan_period   +"\n" +
    			   "Interest rate:     " +interest_rate +"\n" +
    			   "Account number:    " +acct_num      +"\n";
    	}
    	
    	public void setLoanAmount (int loanAmount)
    	{
    		loan_amount = loanAmount;
    	}
    	
    	public void setLoanPeriod (int loanPeriod)
    	{
    		loan_period = loanPeriod;
    	}
    	
    	public void setInterstRate (double interestRate)
    	{
    		interest_rate = interestRate;
    	}
    	
    		
    }
    Last edited by lcrakel87; 03-19-2009 at 10:12 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    lcrakel87 is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Ok I can see you've added accesor methods there. So what's your question?
    I'm just not sure where to go from here. I have to get the loan amount/loan period, etc.. from the user. Do I add this into the mortgage class, or from the actual program?

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    do you know what static means?
    in your implementation, all Mortgage objects with the same acct_num

    i suggest you add 1 more variable in the class to store the real_acct_num

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you can create Mortgages object in the main class,

    Mortgage m1 = new Mortgage(xxx);
    System.out.println(m1. getLoanPeriod());

  8. #8
    lcrakel87 is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    I am using static so that it will automatically increment the account number each time a new account is created. I just haven't made it work yet though.

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  2. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  3. Replies: 0
    Last Post: 05-13-2008, 07:16 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  5. Replies: 0
    Last Post: 04-02-2008, 08:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •