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?