Class cannot be instantiated - java

I have created a class by the name of Account. I am then instantiating an object of the type class. All the code is saved in the file TestAccount. But the system gives me an error as shown below
Exception in thread "main" java.lang.ExceptionInInitializerError
at testaccount.TestAccount.main(TestAccount.java:8)
Caused by: java.lang.RuntimeException: Uncompilable source code - class Account is public, should be declared in a file named Account.java
at testaccount.Account.(TestAccount.java:20)
... 1 more
Java Result: 1
Below is my code:
package testaccount;
public class TestAccount
{
public static void main(String[] args)
{
Account Account1=new Account();
Account1.setId(1122);
Account1.setBalance(20000);
Account1.setAnnualInterestRate(4.5);
System.out.println("The monthly interest rate is " + Account1.getMonthlyInterestRate());
System.out.println("The balance after the withdrawal is "+ Account1.withdraw(2000));
System.out.println("The balabce after the deposit is " + Account1.deposit(3000));
}
}
public class Account
{
private int id;
private double balance;
private double annualInterestRate;
private static long dateCreated;
public Account()
{
id=0;
balance=0;
annualInterestRate=0;
dateCreated=System.currentTimeMillis();
}
public Account(int newId,double newBalance)
{
id=newId;
balance=newBalance;
}
public int getId()
{
return id;
}
public void setId(int newId)
{
id=newId;
}
public double getbalance()
{
return balance;
}
public void setBalance(double newBalance)
{
balance=newBalance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public void setAnnualInterestRate(double newAnnualInterestRate)
{
annualInterestRate=newAnnualInterestRate;
}
public static long getDateCreate()
{
return dateCreated;
}
public double getMonthlyInterestRate()
{
return (annualInterestRate/12);
}
public double withdraw(double newWithdraw)
{
return (balance-newWithdraw);
}
public double deposit(double deposit)
{
return (balance+deposit);
}
}
Can some one tell me what I am doing wrong?

You have to make a new file called Account.java and put your account class in there. this is because when you call account from another class, the jvm will go looking for Account.class and if your account class is in a file named TestAccount.class it wont be able to find it.
Otherwise the compiler wont compile your file
As long as both your classes are in the same package (folder) you don't have to do anything special to "link" the two.
Unless, of course, you want to nest the classes, in which case you put your Account class inside of your TestAccount class. Although I do not recommend this as it is very messy.

It's not a good pratice, and the solutions already present are better but you can move the Account class inside the TestAccount and make it static (put static in front of the class definition). That'll also work.

Related

When i am trying to execute getaccountnumber method,getting following error mentioned in the image [duplicate]

This question already has answers here:
method in class cannot be applied to given type
(2 answers)
Closed 2 years ago.
Method getbalance and getaccountnumber are not getting called when i am calling them from the Main class.
public String getaccountnumber(String accountnumber)
{
return accountnumber;
}
public void setaccountnumber(String accountnumber)
{
this.accountnumber = accountnumber;
}
public double getbalance(double balance)
{
return balance;
}
I am getting "method cannot be Applied to given types".
public static void main(String[] args)
{
System.out.println(sanjay.getaccountnumber());
System.out.println(sanjay.getbalance());
As others pointed out in the comments, you need to pass the parameters into the methods, it also looks like you need to create an instance of your class.
Assuming your class with the account methods is like the one below:
public class sanjay
{
public String getaccountnumber(String accountnumber)
{
return accountnumber;
}
public void setaccountnumber(String accountnumber)
{
this.accountnumber = accountnumber;
}
public double getbalance(double balance)
{
return balance;
}
}
You would need to create an instance of the class sanjay as follows:
sanjay mySanjayInstance = new sanjay();
Then you can call the methods with the parameters:
mySanjayInstance.getaccountnumber("myaccountnumber");
mySanjayInstance.setaccountnumber("myaccountnumber");
mySanjayInstance.getbalance(0.4);
As such your main method would look like this:
public static void main(String[] args)
{
sanjay mySanjayInstance = new sanjay();
System.out.println(mySanjayInstance.getaccountnumber("myaccountnumber"));
System.out.println(mySanjayInstance.getbalance(0.4));
}
Taking a guess it seems like you intended to create a class that you set details on, and then retrieve them (with getter and setter methods). As such you would want to use some class variables and remove the parameters from the getter methods.
public class sanjay
{
private String accountnumber;
private double balance;
public String getaccountnumber()
{
return accountnumber;
}
public void setaccountnumber(String accountnumber)
{
this.accountnumber = accountnumber;
}
public double getbalance()
{
return balance;
}
public void setbalance(double balance)
{
this.balance = balance;
}
}
Now your usage would look like this:
public static void main(String[] args)
{
// Create an instance of the `sanjay` class.
sanjay mySanjayInstance = new sanjay();
// Set the properties on the object we just created via the setters.
mySanjayInstance.setaccountnumber("myaccountnumber");
mySanjayInstance.setbalance(0.4);
// Retrieve the properties via the getters.
System.out.println(mySanjayInstance.getaccountnumber());
System.out.println(mySanjayInstance.getbalance());
}
Some observations that can make your life much easier in the long run;
Consider using SnakeCase for class names (so Sanjay instead of sanjay).
And snakeCase for methods and variable names (including parameters), so getAccountNumber instead of getaccountnumber.
public class Account
{
public String getaccountnumber()
{
return accountnumber;
}
public double getbalance()
{
return balance;
}
}
Here is the answer. This will work but it will not help you really understand what you are doing wrong. To really understand the concept of "Classes", "Objects" and "Parameters" I still recommend you follow an introductory tutorial for Java of at least 5 hours.

Check value in subclass before sending it to private superclass variable

I somehow need to check the value of a variable amount being received in the subclass before sending it to the superclass.
This is what I have for my superclass
//superclass
public class Account
{
private double balance;
public Account( double amount )
{
balance = amount;
}
public Account()
{
this(0.0);
}
public void deposit( double amount )
{
balance += amount;
}
public void withdraw( double amount )
{
balance -= amount;
}
public double getBalance()
{
return balance;
}
}
This is what I have for my subclass
public class SafeAccount extends Account
{
public SafeAccount(double amount)
{
// something to check if amount is positive before sending it to
super
// if it's not positive, use no argument constructor to set balance ==
0.0
}
}
I think I am supposed to use "this(amount)" to somehow check it, but I'm not exactly sure how that will work.
super() must be the first statement in a constructor body so you cannot perform checks before it.
Simple work around:
public class SafeAccount extends Account
{
public SafeAccount(double amount)
{
super(Math.max(0.0, amount));
}
}
If you really must use the no arg constructor a more complex work around would be:
public class Account {
Account() {
}
Account(double x) {
}
}
public class SafeAccount extends Account {
private SafeAccount() {
}
private SafeAccount(double amount) {
super(amount);
}
public static SafeAccount boo(double x) {
if (x < 0.0) {
return new SafeAccount();
}
return new SafeAccount(x);
}
}
Use private constructors in your subclass to prevent instantion from constructor and supply a factory method that does the checks you want.
You can use following statement
class SafeAccount extends Account {
public SafeAccount(int balance) {
super(balance > 0? balance: 0);
}
}
But in general such Inheritance hierarchy needs to be rechecked for correctness of design.

Trying to get default value from the data definition class

Please consider two classes :
Data Definition Class :
public class A {
private int amount = 1000;
public A(int amount){
this.amount = amount
}
public int getAmount(){
return amount ;
}
}
Main Class :
public class B {
public static void main (String arg[]){
A a = new A(2000);
System.out.println("Amount:"+a.getAmount());
}
}
Since I am passing 2000 to the constructor, I am getting 2000 in the output. But I would like to keep a option of if the user doesn't specifiy any amount, it should print
the default value which is 1000 as mentioned in the private variable in data definition class.
Is there a way I can accomplish my task using the constructor?
public class A {
private int amount;
public A() {
amount = 1000;
}
public A(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
}
You just need to create an empty constructor
public A(){
}
And in your main you will be able to do this:
A a = new A();
You can provide default constructor along with the one argument constructor you mentioned. This way user don't have to pass the amount if he don't want to while creating the object.
Create an empty constructor
public class A {
private int amount = 1000;
public A(){
}
public A(int amount){
this.amount = amount;
}
public int getAmount(){
return this.amount;
}
}

How can I create an array of instances accessible in a class?

I am writing a code that create an array of instances of Account object in another class (Bank).
I am initializing the array inside the main method, but it is not accessible inside the Bank class.
What I want to do is create 4 instances of the Account class and to be able to perform all tasks inside the Bank class methods. Is there a way that I can do this?
this is my code
Account.java
package question1;
import java.util.Date;
public class Account {
public int AccountNum;
public double BALANCE;
public Date OPENDATE;
public String OwnerName;
public Account() {
// TODO Auto-generated constructor stub
}
public Account(int accnum, double balance, Date opendate, String ownername) {
this.AccountNum = accnum;
this.BALANCE = balance;
this.OPENDATE = opendate;
this.OwnerName = ownername;
}
public int getAccountNum() {
return AccountNum;
}
public void setAccountNum(int accountNum) {
AccountNum = accountNum;
}
public double getBALANCE() {
return BALANCE;
}
public void setBALANCE(double bALANCE) {
BALANCE = bALANCE;
}
public Date getOPENDATE() {
return OPENDATE;
}
public void setOPENDATE(Date oPENDATE) {
OPENDATE = oPENDATE;
}
public String getOwnerName() {
return OwnerName;
}
public void setOwnerName(String ownerName) {
OwnerName = ownerName;
}
public double yearlyInterest(double balace) {
return balace;
}
}
Bank.java
package question1;
public class Bank {
public static void main(String[] args) {
Account[] acc = new Account[4];
for(int i = 0 ; i<acc.length; i++){
acc[i] = new Account();
System.out.println(acc[i].toString());
}
/// how to continue form here ??
}
}
Call the constructor of the Account class. Now you can set all arguments as desired. Afterwards, you can add the instance to any array or collection.
List<Account> accounts = new ArrayList<Account>(4);
Account myAccount = new Account(123, 100.5, new Date(), "dev leb");
accounts.add(myAccount);
You probably want to have a property of an Account array in your class.
You can set your property in the class body:
public class Bank {
//Set your property here.
private Account[] _acc;
//Initialize in ctor.
public Bank() {
_acc = new Account[4];
}
//....
//You can then use it as a property in your code.
//If needed outside the class, set up setter and getter methods,
//avoiding violating encapsulation.
public Account[] getAcc(){
return _acc;
}
public void setAcc(Account acc){
this._acc = acc;
}
//If you need this to be used inside main, then you must instantiate a
//Bank in main and then make all the appropriate operations there.
public static void main(String[] args) {
Bank bank = new Bank();
Account[] bankAccounts = bank.getAcc();
//....
}
}
My suggestion though is that you use an ArrayList instead:
Set the property:
private List<Account> acc;
And in constructor:
acc = new List();
In order to add an account in a class method:
acc.add(new Account());
In order to retrieve an element by indexing:
acc.get(0);
for more information, look at the ArrayList JavaDoc.
If you are not able to understand why your array that is defined inside the main is not accessible inside your Bank class, then I suggest you search more about Object-Oriented programming, Class definition, instantiation and properties accessibility and manipulation and static methods in Java.

Returning a String for a Super class' abstract method to display

The abstract method statement (in the super class) must be implemented to return a string representation of a statement.
So I've done the following:
public abstract String statement(); //The abstract method in my super class
..and the method in my subclass:
//#Override
public String statement()
{
return String.format("Account no %d has balance R%d and minimum balance R%d", accountNumber,balance,getMinBalance());
}
My main class just calls the Account class (the super class in question) as follows:
new SavingsAccount(Integer.toString(ao[i]),ao[i+1],ao[a]); //ao being the array which contains the values.
However, the console just terminates without displaying anything (I'm also not familiar with implementation).
Here's the full code:
Main:
public class AccountList
{
public static void main(String[] args)
{
int[] ao = {00000,0,0,12345,500,250,23456,230,-50,34567,340,500,45678,-320,-50,56789,-320,-500};
for(int i=0;i<ao.length;i=i+3)
{
int a = i+2;
if(ao[a]>=0)
{
new SavingsAccount(Integer.toString(ao[i]),ao[i+1],ao[a]);
}
if(ao[a]<=0)
{
new ChequeAccount(Integer.toString(ao[i]),ao[i+1],ao[a]);
}
}
}
}
Super class:
public abstract class Account implements InterestAccount
{
static String accountNumber;
int balance;
public Account()
{
accountNumber = "00000";
balance = 0;
//statement();
}
public Account(String accountNumber,int balance)
{
setAccountNum(accountNumber);
setBalance(balance);
}
public void setAccountNum(String accNum)
{
accountNumber = accNum;
}
public void setBalance(int balance)
{
this.balance = balance;
}
public String getAccountNumber()
{
return accountNumber;
}
public int getBalance()
{
return balance;
}
public abstract String statement();
}
One of the sub-classes:
public class SavingsAccount extends Account
{
int minBalance;
public SavingsAccount()
{
super();
minBalance = 0;
}
public SavingsAccount(String accountNum,int minBalance,int balance)
{
super(accountNum,balance);
setMinBalance(minBalance);
}
public void setMinBalance(int minBalance)
{
this.minBalance = minBalance;
}
public int getMinBalance()
{
return minBalance;
}
#Override
public int calculateInterest(int value) {
if(minBalance>balance)
{
return 0;
}
else
{
return (minBalance*balance)/100;
}
}
//#Override
public String statement()
{
return String.format("Account no %d has balance R%d and minimum balance R%d", accountNumber,balance,getMinBalance());
}
}
You never calls the method statement.
1) create a new SavingsAccount object
new SavingsAccount(Integer.toString(ao[i]),ao[i+1],ao[a]);
2) Constructor class got call by above statement
public SavingsAccount(String accountNum,int minBalance,int balance)
{
super(accountNum,balance);
setMinBalance(minBalance);
}
3) Subclass then call superclass method
public Account(String accountNumber,int balance)
{
setAccountNum(accountNumber);
setBalance(balance);
}
4) SetBalance is called
public void setBalance(int balance)
{
this.balance = balance;
}
5) setMinBalance called
public void setMinBalance(int minBalance)
{
this.minBalance = minBalance;
}
6) End of create object SavingsAccount
No single statement calling the method statement
It might help to know more of your code, but from what I see, there are some possible cases:
Your code runs and terminates without an error. Reason you don't see anything is, there is nothing written to the standard output stream (e.g. via System.out.println()).
Your code has some internal problems, which cause an exception, which again is not shown due to a broken logging mechanism.
In any case, use a debugger to step through you code and determine which parts run perfectly and if any fail. It might help to extract some local variables, give them proper meaningful names and watch their contents during execution.

Categories

Resources