0

I am new to java and I am still trying to learn the basics, I'm guessing I've probably made a very simple error. I am trying to create a code where two objects of the class savingsAccount are incremented by a monthly interest. Here is the code that I wrote:

class SavingsAccount {


    //main
    public static void main(String args[]){
        savingsAccount saver1 = new savingsAccount(2000.0);
        savingsAccount saver2 = new savingsAccount(3000.0);
        savingsAccount.modifyAnnualInterestRate(0.04f);

        System.out.println("S1: " + saver1);
        System.out.println("S2: " + saver2);

        savingsAccount.modifyAnnualInterestRate(0.05f);
        System.out.println("S1: " + saver1);
        System.out.println("S2: " + saver2);
    }
    //end main
    
    
    static double annualInterestRate;
    private double savingsBalance;
    
    public void savingsAccount(double balance){
        savingsBalance = balance;
    }

    public double calculateMonthlyInterest(){
        return (savingsBalance * annualInterestRate)/12;
    }
    
    public static void modifyInterestRate(double rate){
        annualInterestRate = rate;
    }
    
    public double getAnnualInterestRate(){
        return annualInterestRate;
    }
    
    public double getSavingsBalance(){
        return savingsBalance;
    }

}

When I attempt to run the code, these errors are returned:

SavingsAccount.java:29: error: cannot find symbol
                savingsAccount saver1 = new savingsAccount(2000.0);
                ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:29: error: cannot find symbol
                savingsAccount saver1 = new savingsAccount(2000.0);
                                            ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:30: error: cannot find symbol
                savingsAccount saver2 = new savingsAccount(3000.0);
                ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:30: error: cannot find symbol
                savingsAccount saver2 = new savingsAccount(3000.0);
                                            ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:31: error: cannot find symbol
                savingsAccount.modifyAnnualInterestRate(0.04f);
                ^
  symbol:   variable savingsAccount
  location: class SavingsAccount
SavingsAccount.java:36: error: cannot find symbol
                savingsAccount.modifyAnnualInterestRate(0.05f);
                ^
  symbol:   variable savingsAccount
  location: class SavingsAccount
6 errors

If anyone could help me with this, I would be very grateful. Thank you!

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • When you use a 'new' operator you are running a contructor method, and constructor method has to have the same name as the class (in this case `SavingsAccount` not `savingsAccount`) and there should be no void keyword used when declaring contructor – Mateusz Dryzek Oct 16 '20 at 18:30
  • Your class is named `SavingsAccount`, with an initial capital. You are pretty consistently trying to refer to it as `savingsAccount`, with an initial lowercase. – John Bollinger Oct 16 '20 at 18:30
  • @JohnBollinger I tried to change the case, but the error is still consistent. –  Oct 16 '20 at 18:37
  • @ZaynSeverance. The error messages you present show the incorrect case usage directly in the messages. If you had corrected the names in the source code being compiled, then *at minimum* that would be reflected in differences in that aspect of the error messages. – John Bollinger Oct 16 '20 at 18:40
  • Regarding the edit (now rolled back): if you have a new question then pose it *as* a new question. Do not modify an existing question to ask something different. Note too that the behavior you claimed in your edit to observe is inconsistent with the behavior you claim in the original question to observe, but you seemed to assert that you observe *both*. – John Bollinger Oct 16 '20 at 18:48

4 Answers4

1

You didn't create a constructor for the class.

public class SavingsAccount {


//main
public static void main(String args[]){
    SavingsAccount saver1 = new SavingsAccount(2000.0);
    SavingsAccount saver2 = new SavingsAccount(3000.0);
    saver1.modifyInterestRate(0.04f);
    saver1.calculateMonthlyInterest();
    System.out.println("S1: " + saver1.calculateMonthlyInterest());
    System.out.println("S2: " + saver2.calculateMonthlyInterest());

    saver1.modifyInterestRate(0.05f);
    saver1.calculateMonthlyInterest();
    System.out.println("S1: " + saver1.calculateMonthlyInterest());
    System.out.println("S2: " + saver2.calculateMonthlyInterest());
}
//end main


private double annualInterestRate;
private double savingsBalance;

//Constructor
public SavingsAccount(double balance){
    savingsBalance = balance;
}

public double calculateMonthlyInterest(){
    return (savingsBalance * annualInterestRate)/12;
}

public void modifyInterestRate(double rate){
    annualInterestRate = rate;
}

public double getAnnualInterestRate(){
    return annualInterestRate;
}

}

I think you are learning oop. I placed a better example for your study.

public class Main {

public static void main(String[] args) {
    
    //Create two instances for SavingsAccount and pass the savingsBalance to constructor
    SavingsAccount saver1 = new SavingsAccount(2000.0);
    SavingsAccount saver2 = new SavingsAccount(3000.0);
     //Set the value rate of the AnnualInterestRate
    saver1.setAnnualInterestRate(0.5);
    saver2.setAnnualInterestRate(0.7);
    
    //calculate the Monthly Interest and print it out
    System.out.println("S1: " + saver1.calculateMonthlyInterest());
    System.out.println("S2: " + saver2.calculateMonthlyInterest());
    
    //Change the rate
    saver1.setAnnualInterestRate(0.1);
    saver2.setAnnualInterestRate(0.2);
    System.out.println("S1: " + saver1.calculateMonthlyInterest());
    System.out.println("S2: " + saver2.calculateMonthlyInterest());
}


//Create a separated class for a better structure
static class SavingsAccount {
    
    private double savingsBalance = 0;
    
    private double annualInterestRate = 0;
    
    //Constructor
    public SavingsAccount(double savingsBalance){
        this.savingsBalance = savingsBalance;
    }
    
    //Function for calculat the eMonthly Interest
    public double calculateMonthlyInterest(){
        return (savingsBalance * annualInterestRate)/12f;
    }
    
    
    //Getter setter for the class value
    public double getAnnualInterestRate(){
        return annualInterestRate;
    }
    
    public void setAnnualInterestRate(double annualInterestRate){
        this.annualInterestRate = annualInterestRate;
    }
    
  
    public double getSavingsBalance(){
        return savingsBalance ;
    }
    

    public void setSavingsBalance(double savingsBalance){
        this.savingsBalance = savingsBalance;
    }

}

}
Capslock10
  • 796
  • 2
  • 16
  • 37
0

I don't know what you are trying to do but here is your compilable code

class SavingsAccount {


//main
public static void main(String args[]){
    SavingsAccount saver1 = new SavingsAccount();
    SavingsAccount saver2 = new SavingsAccount();
    
    saver1.savingsAccount(2000.0);
    saver2.savingsAccount(3000.0);
    
    SavingsAccount.modifyInterestRate(0.04);

    System.out.println("S1: " + saver1.calculateMonthlyInterest());
    System.out.println("S2: " + saver2.calculateMonthlyInterest());

    SavingsAccount.modifyInterestRate(0.05);
    System.out.println("S1: " + saver1.calculateMonthlyInterest());
    System.out.println("S2: " + saver2.calculateMonthlyInterest());
}
//end main


static double annualInterestRate;
private double savingsBalance;

public void savingsAccount(double balance){
    savingsBalance = balance;
}

public double calculateMonthlyInterest(){
    return (savingsBalance * annualInterestRate)/12;
}

public static void modifyInterestRate(double rate){
    annualInterestRate = rate;
}

public double getAnnualInterestRate(){
    return annualInterestRate;
}

public double getSavingsBalance(){
    return savingsBalance;
}

}

Anirudh Jadhav
  • 967
  • 1
  • 9
  • 23
0

Your class name is SavingsAccount, but when you create an object instance, you write savingsAccount;

Java is a case-sensitive language, so you need to change savingsAccount to SavingsAccount

And you are missing a constructor:

public SavingsAccount(double savingsBalance){
      this. savingsBalance = savingsBalance;
}
-1

Java is a Case-sensitive language. When creating objects you need to use the same name as the class name. In your case you have to use

SavingsAccount saver1 = new SavingsAccount(2000.0);
Abra
  • 19,142
  • 7
  • 29
  • 41
  • I have tried this, and it did nothing to fix the problem. It returns the same exact error. –  Oct 16 '20 at 18:40
  • This is not going to work; the code provided does not have a constructor `SavingsAccount(Double)`. Admittedly, the code provided has many, many other problems as well. – SiKing Oct 16 '20 at 18:41
  • @SiKing I tried to make a class for the lowercase function, but now I have an error I've never seen before. –  Oct 16 '20 at 18:43