Cannot find class in same path.
I am working on a project (it isn't complete yet so some of the code is not done) and for some reason it isn't recognizing the Loan constructer from the class in the same directory. It could very well be that I am missing something ridiculously simple but I've wasted enough time on this.
This is the client, when I compile it says it cannot find the Loan() constructor in Client1... I've compiled the Loan class prior to compiling this class so I am drawing a blank. Everything works fine if I can get the Loan instantiation to work. Please Help.
Code:
/**
* This program is designed to calculate the value of an investment
* based on the amount, the length in years and the interest rate
* based on a 12 month compounded APR.
*
* @author Geoff Berl
* @version 1.00
*/
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Client1 extends JFrame implements ActionListener
{
private JButton jbtCalculate = new JButton("Calculate");
private JButton jbtCancel = new JButton("Clear");
private JTextField jtxtAmount = new JTextField(8);
private JTextField jtxtYears = new JTextField(8);
private JTextField jtxtRate = new JTextField(8);
private JTextArea jta = new JTextArea();
private JMenuBar jMenuBar = new JMenuBar();
private JMenuItem jmiCalculate;
private JMenuItem jmiClear;
private JMenuItem jmiExit;
private JMenuItem jmiAbout;
Loan newLoan;
//private JOptionPane aboutWindow = new JOptionPane();
DecimalFormat output = new DecimalFormat("0.00");
// IO streams
private ObjectOutputStream toServer;
private DataInputStream fromServer;
//Main method
public static void main(String[] args)
{
Client1 window = new Client1();
}
/**
This is the InvestCalc method
@postcondition Generates the GUI for the investment calculator
*/
public Client1()
{
//Create panel(s)
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//Set the window title and size
setTitle("Investment Calculator");
setSize(300, 300);
//use FlowLayout to align components
setLayout(new BorderLayout());
panel1.setLayout(new GridLayout(4, 2, 10, 0));
panel2.setLayout(new FlowLayout());
//Menu stuff
setJMenuBar(jMenuBar);
JMenu fileMenu = new JMenu("Operation");
JMenu helpMenu = new JMenu("Help");
jMenuBar.add(fileMenu);
jMenuBar.add(helpMenu);
fileMenu.add(jmiCalculate = new JMenuItem("Calculate",'L'));
fileMenu.add(jmiClear = new JMenuItem("Clear", 'R'));
fileMenu.addSeparator();
fileMenu.add(jmiExit = new JMenuItem("Exit",'E'));
helpMenu.add(jmiAbout = new JMenuItem("About",'T'));
// Set keyboard accelerators
jmiCalculate.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
jmiClear.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
jmiExit.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
jmiAbout.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
//Add components
add(panel1, BorderLayout.NORTH);
panel1.add(new JLabel("Investment Amount"));
panel1.add(jtxtAmount);
panel1.add(new JLabel("Years"));
panel1.add(jtxtYears);
panel1.add(new JLabel("Annual Interest Rate"));
panel1.add(jtxtRate);
//panel1.add(new JLabel("Future Value"));
add(jta, BorderLayout.CENTER);
panel2.add(jbtCalculate);
panel2.add(jbtCancel);
add(panel2, BorderLayout.SOUTH);
//misc operations
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta.setEditable(false);
//Center the window on the screen
setLocationRelativeTo(null);
setVisible(true);
//Register listeners
jbtCalculate.addActionListener(this);
jbtCancel.addActionListener(this);
jmiCalculate.addActionListener(this);
jmiExit.addActionListener(this);
jmiAbout.addActionListener(this);
jmiClear.addActionListener(this);
try
{
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
//Create loan object
double radius = Double.parseDouble(jtxtRate.getText().trim());
int years = Integer.parseInt(jtxtYears.getText().trim());
double amount = Double.parseDouble(jtxtAmount.getText().trim());
newLoan = Loan();
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
jta.append(ex.toString() + '\n');
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
/** actionPerformed is used to handle ActionEvents from menu items and buttons*/
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
//if it's a menu item this statement will handle it
if (e.getSource() instanceof JMenuItem)
{
//System.out.println("Yay");
if ("Calculate".equals(actionCommand))
performComputation();
else if ("Clear".equals(actionCommand))
clear();
else if ("About".equals(actionCommand))
respondToAbout();
else if ("Exit".equals(actionCommand))
System.exit(0);
}
//Otherwise, check it it was a button
if(e.getSource() == jbtCalculate)
performComputation();
if(e.getSource() == jbtCancel)
clear();
} //end actionPerformed
/**
This is the respondToAbout method
@postcondition JOptionPane is displayed with information about the program.
*/
public void respondToAbout()
{
JOptionPane.showMessageDialog(null, "Compute Future Investment Value", "About This Program", JOptionPane.INFORMATION_MESSAGE);
}
/**
This is the performComputation method
@postcondition The value textfield displays the result of the computation based on the data provided
@return result of the investment
*/
public void performComputation()
{
try {
// Get the radius from the text field
double radius = Double.parseDouble(jtxtAmount.getText().trim());
// Send the radius to the server
toServer.writeObject(newLoan);
toServer.flush();
// Get area from the server
double value = fromServer.readDouble();
// Display to the text area
jta.append("The value returned from the server was "
+ value + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
}
/**
This is the clear method
@postcondition all four text fields are emptied
*/
public void clear()
{
jtxtAmount.setText("");
jtxtYears.setText("");
jtxtRate.setText("");
jta.setText("");
}
}// End of main class
this is a Loan object
Code:
import java.io.*;
public class Loan implements Serializable
{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
/**
* Construct a loan with specified annual interest rate, number of years, and loan amount.
* add the current date for the loan
*
* @param annualInterestRate The annual interest rate in percent
* @param numberOfYears The number of years of the loan
* @param loanAmount The initial amount of the loan
*/
public Loan(double annualInterestRate, int numberOfYears, double loanAmount)
{
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** Return annualInterestRate */
public double getAnnualInterestRate() {
return annualInterestRate;
}
/** Set a new annualInterestRate */
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
/** Return numberOfYears */
public int getNumberOfYears() {
return numberOfYears;
}
/** Set a new numberOfYears */
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
/** Return loanAmount */
public double getLoanAmount() {
return loanAmount;
}
/** Set a newloanAmount */
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
/** Return loan date */
public java.util.Date getLoanDate() {
return loanDate;
}
/**
* Find monthly payment
*
* @returns The monthly payment of a Loan object
*/
public double monthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
return loanAmount * monthlyInterestRate / (1 -
(Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
}
/** Find total payment */
public double totalPayment() {
return monthlyPayment() * numberOfYears * 12;
}
/**
* Display the information of a Loan object
*
* @returns A string that represents a Loan object
*/
public String toString()
{
String returnString = "";
returnString += "Loan Amount: " + loanAmount + "; Loan Date: " +
loanDate + "; Annual Interest Rate: " +
annualInterestRate + "; Years of loan: " + numberOfYears + "\n";
return returnString;
}
}
You shouldn't need the server because the error occurs at compile time for the client class.