Java error in creating multiple new objects?
-
Write two classes: TestAccount and Account to represent an account. The class contains: a) An int data field named id for the account with value 100. b) A double data field named balance for the account with value 1000. c) A double data field named annualInterestRate that stores the current interest rate with value 0.1 d) The accessor method for id, balance, and annual InterestRate. Account.java public class Account { //attributes double id; double bal; double air; //default constructor public Account() { id = 0.0; bal = 0.0; air = 0.0; } //parameterized constructor (3 parameters) public Account (double id, double bal, double air) { Identity = id; Balance = bal; AnnualInterestRate = air; } public double Identity() { return id; } public double Balance() { return bal; } public double AnnualInterestRate() { return air; } } ERROR: Account.java:19: error: cannot find symbol Identity = id; ^ symbol: variable Identity location: class Account Account.java:20: error: cannot find symbol Balance = bal; ^ symbol: variable Balance location: class Account Account.java:21: error: cannot find symbol AnnualInterestRate = air; ^ symbol: variable AnnualInterestRate location: class Account 3 errors TestAccount.java public class TestAccount { public static void main(String [] args) { //create a new object Identity id = new Identity(100); Balance bal = new Balance(1000); AnnualInterestRate air = new AnnualInterestRate(0.1); //Three variables that retrieve values from the account object's attributes double id = get.id(); double bal = get.bal(); double air = get.air(); double id * bal * air = monthlyinterestrate; } } ERROR: TestAccount.java:15: error: illegal start of expression double id; * bal * air = monthlyinterestrate; ^ 1 error
-
Answer:
You have not declared the variables you are setting in your constructor. It should be like this : public class Account { //attributes double id; double bal; double air; //default constructor public Account() { id = 0.0; bal = 0.0; air = 0.0; } //parameterized constructor (3 parameters) public Account (double id, double bal, double air) { Id = this.id; bal = this.bal; air = this.air; }
T.A.K. at Yahoo! Answers Visit the source
Other answers
You tried to set variables called Identity, Balance and AnnualInterestRate to certain values without having ever declared those variables anywhere. This is incorrect coding, the compiler can't know what you mean when you do that and so it just gives an error. You need to declare your variables properly.
green meklar
Related Q & A:
- How to save new objects into an arrayList?Best solution by stackoverflow.com
- how to create a new syntax in java?Best solution by Stack Overflow
- How many objects are eligible for Garbage collection in Java?Best solution by stackoverflow.com
- How to return multiple objects of a specific library in R?Best solution by Stack Overflow
- Can you change your yahoo email address without creating a new account?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.