I need to preface this with I am not allowed to use an IDE in class, I must use TextPad to compile and run. Any help would be greatly appreciated.
There are two files at play here, one called Account and the other called Lab4B. The assignment is to create and use the following files:
Lab4B.java = source code for the driver code class (Lab4B) for the assignments
Account.java = source code for the Account class
The instructions are:
Design a class named Account (that contains
- A private int data field named id for the account (default 0).
- A private double data field named balance for the account (default 0).
- A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
- A private Date data field named dateCreated that stores the date when the account was created.
- A no-arg constructor that creates a default account.
- A constructor that creates an account with the specified id and initial balance.
- The accessor and mutator methods for id, balance, and annualInterestRate.
- The accessor method for dateCreated.
- A method named getMonthlyInterestRate() that returns the monthly interest rate.
- A method named withdraw that withdraws a specified amount from the account.
- A method named deposit that deposits a specified amount from the account.
I am getting the following errors:
The errors are when I compile the “Lab4B” file.
F:\Java\Lab 4b\Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
F:\Java\Lab 4b\Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
2 errors
The code for “Account” is below:
import java.util.Scanner;
import java.util.Date;
public class Account
{
int id = 0;
double balance = 0;
double annualInterestRate = 0;
Date dateCreated;
public Account()
{
}
public Account(int id, double balance, double annualInterestRate)
{
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated = new Date();
}
public void setID (int id)
{
this.id = id;
}
public int getID()
{
return this.id;
}
public void setBalance (double balance)
{
this.balance = balance;
}
public double getbalance()
{
return this.balance;
}
public void setAnnualInterestrate (double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
public double getAnnualInterestrate()
{
return this.annualInterestRate;
}
public Date getDateCreated()
{
return this.dateCreated;
}
public double getMonthlyInterestRate()
{
return (this.annualInterestRate) / 12;
}
public void withdraw(double amount)
{
this.balance -= amount;
System.out.println ("After withdrawing $"+amount +" the balance in your account is: $ " +this.balance +"\n");
}
public void deposit(double amount)
{
this.balance += amount;
System.out.println(" After making a deposit of $"+amount +" the balance in your account is: $" +this.balance + "\n");
}
}
The code for the Lab4B file is below:
public class Lab4B
{
public static void main(String[] args)
{
// Creates an instance of Account
Account account = new Account(5544, 45000, 0.036);
// Sets default amounts
account.setID(5544);
account.setBalance(45000);
account.setAnnualInterestRate(0.036);
account.withdraw(3700);
account.deposit(2200);
// Prints output
System.out.println ("Account number: \n"+ +account.getID());
System.out.println ("The account balance is: $ \n" + +account.getbalance());
System.out.println ("The monthly interest earned is: $ \n" + +account.getbalance() * account.annualInterestRate());
System.out.println ("The account was created on: \n" + +account.getdateCreated());
}
}
Here are the errors I am getting from the javac command line compiler as suggested:
C:\>javac Lab4B.java
Lab4B.java:5: error: package mypackage does not exist
import mypackage.Account;
^
Lab4B.java:15: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
Lab4B.java:15: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
3 errors
Here is the javac -verbose output:
C:\Users\kschultz>cd\
C:\>javac -verbose Lab4B.java
[parsing started RegularFileObject[Lab4B.java]]
[parsing completed 12ms]
[search path for source files: .C:\Program Files (x86)\Java\jdk1.7.0_02\lib,C:\P
rogram Files (x86)\Java\jre6\lib\ext\QTJava.zip]
[search path for class files: C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\re
sources.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\rt.jar,C:\Program Fi
les (x86)\Java\jdk1.7.0_02\jre\lib\sunrsasign.jar,C:\Program Files (x86)\Java\jd
k1.7.0_02\jre\lib\jsse.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\jce.j
ar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\charsets.jar,C:\Program Files
(x86)\Java\jdk1.7.0_02\jre\classes,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\
lib\ext\dnsns.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\ext\localedata
.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\ext\sunec.jar,C:\Program Fi
les (x86)\Java\jdk1.7.0_02\jre\lib\ext\sunjce_provider.jar,C:\Program Files (x86
)\Java\jdk1.7.0_02\jre\lib\ext\sunmscapi.jar,C:\Program Files (x86)\Java\jdk1.7.
0_02\jre\lib\ext\sunpkcs11.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\e
xt\zipfs.jar,.C:\Program Files (x86)\Java\jdk1.7.0_02\lib,C:\Program Files (x86)
\Java\jre6\lib\ext\QTJava.zip]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/String.class)]]
[checking Lab4B]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]
Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/System.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/io/PrintStream.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/io/FilterOutputStream.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/io/OutputStream.class)]]
[total 228ms]
2 errors
Both of your errors are related to the same problem.
Problem:
Solution:
Package declaration syntax
The statement order is as follows.
Package Import syntax
There are 3 ways you can specify package imports.
The JOptionPane class is in the swing package, which is located in the javax package. The wildcard character (*) is used to specify that all classes with that package are available to your program. This is the most common programming style.
Classes can be specified explicitly on import instead of using the wildcard character.
Alternately we can the fully qualified class name without an import.
You can read more about packages here