I hope someone can help me.. I'm taking a course in programming with java and I have an assignment which is just killing me. I've been visiting the Blackboard page for the class and looking at other questions asked for help from other students in the class having similar issues, but the answers just points students back to 'lecture notes' which are incomplete and full of notations referencing "explain in further detail during class lecture"

Well, this is an online only course, because the school decided to drop the actual classroom section and make us all take it over the internet. So there is no class lecture.

The assignment deals with inheritance, as well as reading data from an external file then printing the output to another external file.

There are five java files to create, plus 2 external txt files.

  • Loan.java
  • LoanConstants.java
  • BusinessLoan.java
  • PersonalLoan.java
  • CreateLoans.java
  • loan.txt
  • LoanResult.txt

I've been struggling with this for over a week, and I'm running out of time as the assignment is coming up due shortly. :(

I'm beyond frustrated. This was NOT the class I signed up for, but after they dropped the in-class option I was stuck with this one.. its required and they only offer it once a year so here I am.. apparently I paid tuition and fees to be in a class where I get to teach myself.

Every time I try to compile the CreateLoans.java file, it gives me this error:

CreateLoans.java:57: non-static method toString() cannot be referenced from a static context
		output.print(toString());
		             ^

Now I think the issue is that I need to create a loan object in CreateLoans.java in order to be able to use the toString() method.

Problem is, Loan.java is an abstract class and I can't create an object with an abstract class. And I can't figure out how to get it to create the object so that it can tell which loan subclass to use for the data results - whether it should be a business loan or a personal loan.

Lots of code here:

Loan.java

/**
	The Loan class is an abstract class that implements the Loan Constants interface.
*/

import java.util.*;

public abstract class Loan implements LoanConstants
{
	String name;				// Customer Last Name
	double amount;		   	// Loan amount
	double term;				// Loan Term
	int type;					// Loan Type
	double intRate;			// Interest Rate
	double busIntRate;		// business Interest Rate
	double perIntRate;		// personal interest rate
	double total;				// Total amount
	
	/**
		The Constructor sets the customer's last name, loan amount, loan term
		and interest rate.
		@param n						Customer's last name
		@param a							Loan Amount
		@param t							Loan term
	*/
	
	
	protected Loan(String n, double a, double t)
	{
		name = n;
		amount = a;
		term = t;
	}
	
	
	/**
		The setLoanTerm method sets the term of the loan
	*/
	
	public double setLoanTerm(double term)
	{
		if (term != LONGTERM && term != SHORTTERM && term != MEDIUMTERM)
			term = SHORTTERM;
			
		return term;
	}
	
	/**
		The setIntRate method sets the interest rate for the loan
	*/
	
	public double setIntRate(int type)
	{
		double intRate;
		
		if(type == 1)
		{
			intRate = busIntRate;
		}
		
		else
		{
			intRate = perIntRate;
		}
			
	   return intRate;
	}
			
	
	/**
		The toString() method sets a string for output
		@return 		Returns a string
	*/
	
	public String toString()
	{
		String str;
		
		str = "Customer Name:  " + name + "\n" +
		      "Loan Amount:  " + amount + "\n" +
				"Loan Type:  " + type + "\n" +
				"Interest Rate:  " + intRate + "\n" +
				"Term:  " + term + "\n" +
				"Total Loan Amount:  " + total;
		
		return str;
	}
	

}

LoanConstants.java

nal int SHORTTERM = 1;
		final int MEDIUMTERM = 3;
		final int LONGTERM = 5;
		
		final int PERSONAL = 1;
		final int BUSINESS = 2;
	
}

BusinessLoan.java

/**
   The BusinessLoan class is a public class that extends Loan.
*/

public class BusinessLoan extends Loan 
{
	double primeInterestRate;
	
	/**
		Constructor sets loan info
		@param n Customer last name
		@param a Loan amount
		@param t Loan term
	*/
	

	public BusinessLoan(String n, double a, double t)
	{
		super(n, a, t);
		double busIntRate;
	}
	
	/**
		The setIntRate method sets the interest rate
	*/
	
	public double setIntRate(double primeInterestRate)
	{
		busIntRate = primeInterestRate + .01;
		
		return busIntRate;
	}
	
		
	/**
		The setLoanTotal method sets the total amount of the loan
	*/
	
	public double setLoanTotal(double amount, double intRate)
	{
		double total = (amount * busIntRate) + amount;
		
		return total;
	}

	/**
		The setLoanAmount method sets the max loan amount
	*/
	
	public double setLoanAmount(double amount, double MAXLOANAMOUNT)
		throws IllegalArgumentException
	{
		if (amount <= MAXLOANAMOUNT)
			amount = amount;
			
		else
			throw new IllegalArgumentException("Invalid loan amount.");
		
		return amount;
	
	}

}

PersonalLoan.java

/**
   The PersonalLoan class is a public class that extends Loan.
*/

public class PersonalLoan extends Loan
{
	double primeInterestRate;
	
	/**
		Constructor sets loan info
		@param n Customer last name
		@param a Loan amount
		@param t Loan term
	*/
	
	public PersonalLoan(String n, double a, double t)
	{
		super(n, a, t);
		double perIntRate;
	}
	
	/**
		The setIntRate method sets the interest rate
	*/
	
	public double setIntRate(double primeInterestRate)
	{
		perIntRate = primeInterestRate + .02;
		
		return perIntRate;
	}
	
		
	/**
		The setLoanTotal method sets the total amount of the loan
	*/
	
	public double setLoanTotal(double amount, double intRate)
	{
		double total = (amount * perIntRate) + amount;
		
		return total;
	}

	/**
		The setLoanAmount method sets the max loan amount
	*/
	
	public double setLoanAmount(double amount, double MAXLOANAMOUNT)
		throws IllegalArgumentException
	{
		if (amount <= MAXLOANAMOUNT)
			amount = amount;
			
		else
			throw new IllegalArgumentException("Invalid loan amount.");
		
		return amount;
	
	}


}

CreateLoans.java

/**
   The CreateLoans class is an application that creates a loan object by
   reading from an external file named Loan.txt to find the current prime
	interest rate information and loan type, then display the loan object
	information in an external file named LoanResult.txt.
*/
import java.io.*;
import java.util.Scanner;


public class CreateLoans  
{
	public static void main(String[] args) throws Exception
	{
		// Create file object
		java.io.File file = new java.io.File("Loan.txt");
		
		// Create a Scanner and a PrintWriter
		Scanner input = new Scanner(file);
		
		// Create variables
		double primeInterestRate = 0;
		double intRate = 0;
		int type;
		int loanNum = 0;
		String name = null;
		double amount = 0;
		int term = 0;
		double total = 0;
		
		// Read data from the file
		while (input.hasNext())
		{
			primeInterestRate = input.nextDouble();
			type = input.nextInt();
			loanNum = input.nextInt();
			name = input.next();
			amount = input.nextDouble();
			term = input.nextInt();
		}
		
		// Close the files
		input.close();
	
		// Create output file object
		java.io.File result = new java.io.File("LoanResult.txt");
		if (result.exists())
		{
		 	System.out.println("File already exists!");
		 	System.exit(0);
		}
	
		// Create result file
		java.io.PrintWriter output = new java.io.PrintWriter(result);
		
		// Write to file
		output.print(toString());
				

		// Close the file
		output.close();

		
	}
		
}

I'm not asking someone to do it for me.. But I really can't figure out where I'm going wrong..

I can't figure out how to create a loan object for the CreateLoans.java file when the Loan class is abstract..

Please please please.. help?

You have to create an instance of one of its concrete subclasses, as in

Loan loan = new PersonalLoan(...) or
loan = new BusinessLoan (...)

You can use the same Loan variable for either type because they are both a "kind of" Loan.

I am stumped.

I created the loan objects using:

if (type == 1)
{
   Loan loan = new PersonalLoan();
}
else
{ 
   Loan loan = new BusinessLoan();
}

when I try and use the .toString() method to call up the information for the loan object, I get the following error.

 ----jGRASP exec: javac -g CreateLoans.java

CreateLoans.java:67: cannot find symbol
symbol  : variable loan
location: class CreateLoans
            output.print(loan.toString());
                         ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

Needless to say, at this point.. seriously frustrated. In fact, I've been up all night working on this.. started on it around 6 pm after dinner.. now its 1 pm the next day and I still can't get it straight.

I have no idea what I've got wrong

You declared Loan loan inside the if tests, so as soon as you got to the next } they ceased to exist. Just declare Loan loan in the class, but outside any methods. Basically, when you declare a variable it only exists within the {} brackets that immediately surround the declaration.

You have been SO much help with this.. :) I hate to ask one more question.. but..

I have the program almost working.. when I run CreateLoans.java it gives me all of the required output except for the calculated interest rate (which is supposed to be calculated by adding +1 or +2 to the value in the rate field); and the calculated loan total which is just adding the interest to the loan amount.

It doesn't seem like the method is working, or. . processing the math function?

I have confirmed that the subclasses for both BusinessLoan and PersonalLoan ARE getting the values for rate, and amount. I added a line to the .toString() method to print them out and the values display.

The 'getIntRate' method should be returning a value to the variable 'intRate'
The 'getLoanInterest" method should be returning a value to the variable 'interest'
The 'getLoanTotal' method should be returning a value to the variable 'total'

And yet..they don't. :-|


When I run the CreateLoans application, it returns the following string:

Customer Name:  Fryar
Loan Amount:  $100000.0
Loan Term:  3 years
Loan Type:  1
Prime Interest Rate:  0.0575
Interest Rate:  0.0
Total Loan Amount:  $0.0


AmountTestPL:  $100000.0
RateTestPL:  0.0575
PLRate:  0.02
PLInterest:  0.0

The second group of data (AmountTestPL, RateTestPL, PLRate, PLInterest,) are additional variables set into the .toString() method to verify whether or not any values are being passed. With the exception of PLInterest (which calls up 'interest') the information is there.

Why aren't the methods returning the values?

This is the complete code for the PersonalLoan.java file. (Its identical to the BusinessLoan.java file as they are both subclasses to Loan.java and do the same
thing with the difference being in the interest rate set.)

/**
   The PersonalLoan class is a public class that extends Loan.
*/

import java.util.*;


public class PersonalLoan extends Loan
{
	public double rate;
	public double intRate;
	public double total;
	public double plRate = .02;
	public int type;
	
	public double interest;

	
	public PersonalLoan(String n, double a, int t, double r, int ty)
	{
		super(n, a, t, r, ty);
		
		rate = r;
		name = n;
		amount = a;
		type = ty;
	}
	
	/**
		 The getLoanType method sets the loan type
		 @return type
	*/
	
	public int getLoanType(int ty)
	{
		return type;
	}
			
	/**
		The getIntRate method sets the interest rate
		@return intRate
	*/
	
	public double getLoanRate(double rate, double plRate)
	{
		intRate = rate + plRate;
		return intRate;
	}
	
	/**
		The getLoanInterest method calculations the interest on the loan
		@return interest
	*/
	
	public double setLoanInterest(double amount, double intRate)
	{
		interest = amount * intRate;
	
		return interest;
	}	
	
	/**
		The getLoanTotal method sets the loan total
		@return total
	*/
	
	public double getLoanTotal(double amount, double interest)
	{
		total = amount + interest;
		
		return total;
	}

	/**
		The getLoanTerm method sets the term of the loan
		@return term
	*/
	
	public int setLoanTerm(int term)
	{
		if (term != LONGTERM && term != SHORTTERM && term != MEDIUMTERM)
			term = SHORTTERM;
			
		return term;
	}
	
	/**
		The toString() method sents data to print
		@return String
	*/
	
	public String toString()
	{
		String str;
		
		str = super.toString() +
		"Prime Interest Rate:  " + rate + "\n" +
		"Interest Rate:  " + intRate + "\n" +
		"Total Loan Amount:  " + "$" + total + "\n"+ "\n" + "\n" +
		"AmountTestPL:  " + "$" + amount + "\n" +
		"RateTestPL:  " + rate + "\n" + 
		"PLRate:  " + plRate + "\n" +
		"PLInterest:  " + interest;
		
		return str;
	}

}

One other thing I tried as a test on this, I went into the file and set values for the 'intRate', 'total', and 'interest' variables at this point where they're declared in the beginning of the code. When I run CreateLoans.java with the values set in there, I get the value back.

i.e.:

public class PersonalLoan extends Loan
{
     public double rate;
     public double intRate = .0725;
     public double total = 12000;

...

I need the values to be passed by the methods.. and I can't figure out why it won't, the other methods in the file (getLoanType, getLoanTerm, toString()) all pass their values back correctly when called and to the best I can tell..the others should be as well.

One more desperate cry for help?

:)

No time now to read all that code, but it looks like you declare things like intRate and total in the superclass, but then declare them again in the subclass. This means that you have at least 2 versions of these variables floating around, with huge potential for confusion. As a general rule you should never re-declare variables without some very good and well thought out reason.
I would start by deleting every duplicated declaration of any variable, keeping only the one in the highest level class (or the widest scope in the case of re-declaring a variablein a method).

I have rewritten the Loan.java file

At the moment, it only references the variables needed to create a basic loan object:

name, term, amount, rate (prime interest rate), type, and MAXLOANAMOUNT

It has no reference or mention of the total or intRate variables in it. Those two are found only in the subclass files for PersonalLoan and BusinessLoan

The methods should process the math equations to determine what the interest rate is for either a PersonalLoan or BusinessLoan, then use that information to calculate the interest and then add up the total loan amount then pass those values to the declaration and then display when called by the toString() method which concatinates from the super.toString() and adds in the additional variables to print the end result.

Or.. that was the idea at any rate.

I know that the methods in both PersonalLoan and BusinessLoan are grabbing the basic information such as rate (prime), and amount. It should be calculating the rest and displaying it but... I'm still working on it

I figured it out! You were right.. I had a duplication in one of the variables that was causing the issue. :)

Thank you SOOOOO much for all of your help.

It is deeply appreciated.

can i see full coding? pls

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.