B)(i)Create a class file BankAccount.java under c:\myjava folder. Declare a String member type variable called accountType and a double member type variable called balance. There are three account type values allowed: "student", "basic" and "advanced" . (ii)Create a method called minimumBalance which takes one argument of type String (acccType) and which would return a double type . Inside this method write if else statements to return different numbers depending on different values of accType. (iii) Create another method called findInterest which outputs interest for 5 years for the current account balance using a while loop.
Then save and compile by giving the javac BankAccount.java command on command prompt.

This is what I have:

class BankAccount {


public static void main () {

String accountType;


 double balance;

 //minimumBalance 

 accountType = "student";
  accountType = "basic";
  accountType = "advanced";



   if(accountType.equals("student"))
    {
  return 100;
       }
    else if (accountType.equals("basic"))
     {
    return 600;
    }



    }             

Recommended Answers

All 20 Replies

is that what you have, or is that what you've been given? this sounds pretty much like an assignment to me.
what actual questions do you have?

WEll the code is what I have and is coming back errors. The

"B)(i)Create a class file BankAccount.java under c:\myjava folder. Declare a String member type variable called accountType and a double member type variable called balance. There are three account type values allowed: "student", "basic" and "advanced" . (ii)Create a method called minimumBalance which takes one argument of type String (acccType) and which would return a double type . Inside this method write if else statements to return different numbers depending on different values of accType. (iii) Create another method called findInterest which outputs interest for 5 years for the current account balance using a while loop"

is the assignment. I need a little help can't figure this out. Any help would be great thanks!

is coming back errors.

Can you post the full text of the error messages so the current code can be cleaned up.

can't figure this out.

Can you explain where you are having problems?

`BankAccount.java:24: error: <identifier> expected
   accountType = "student";
             ^
    BankAccount.java:25: error: <identifier> expected
    accountType = "basic";
           ^
BankAccount.java:26: error: <identifier> expected
accountType = "advanced";
           ^
BankAccount.java:30: error: illegal start of type
if(accountType.equals("student"))
^
BankAccount.java:30: error: <identifier> expected
if(accountType.equals("student"))
                     ^
BankAccount.java:30: error: ';' expected
if(accountType.equals("student"))
                      ^
BankAccount.java:30: error: illegal start of type
if(accountType.equals("student"))
                               ^
BankAccount.java:30: error: <identifier> expected
if(accountType.equals("student"))
                                ^
BankAccount.java:30: error: ';' expected
if(accountType.equals("student"))
                                 ^
BankAccount.java:32: error: illegal start of type
return 100;
^
BankAccount.java:32: error: <identifier> expected
return 100;
      ^
BankAccount.java:34: error: class, interface, or enum expected
else if (accountType.equals("basic"))
^
BankAccount.java:37: error: class, interface, or enum expected
}
^
13 errors

`

Having problems with it all organization and where to start!!

class BankAccount {

public static void main (String args []) {

String accountType;

double balance;

balance= 1000;

acountType student = accountType();
acountType basic = accountType();
acountType advanced = accountType();



//minimumBalance 

if(accountType.equals("student"))
{
return 100;
}
else if (accountType.equals("basic"))
{
return 600;
}

//findinterest
i = 0;
while(i < 5)
{
double interest = balance * 0.05;
System.out.println(i+ " year interest:"+interest);
balance = balance + interest;

} 
}

This is the error in Command prompt after compiled.

C:\myjava>javac BankAccount.java  BankAccount.java:47: error: reached end of file while parsing}
       ^
1 error                                                

Check that all the beginning brackets: '{'s have matching ending brackets: '}'s.

It makes the code easier to read and understand if it is properly formatted.
Indent nested logic 3-4 spaces.
Align the ending '}' vertically beneath the start of the line with the '{'

The statements in the posted code all start in the first column which makes it hard to read.

you can't return values from your main method, it's declared void.

also:

accountType = "student";
accountType = "basic";
accountType = "advanced";

here you are overwriting the value of accountType, so it will always have the value "advanced"

acountType student = accountType();
acountType basic = accountType();
acountType advanced = accountType();

here, well ... the same, and unless you actually have a method accountType that returns a String it 'll cause trouble. for the rest, indeed, check your brackets, you've probably got a closing bracket too many (or too soon)

I re-wrote the code to this:

public class BankAccount {

public double balance;
public String accountType;

//The function takes a string EX. "student" and returns a number if
//it matches in the if statements. The else is if there are no matches.
//The program will not compile if that last else was not in place because
//the function says it will return a double.
public double minimumBalance(String accType){

if(accType.equals("student")){
return 100;
}
else if(accType.equals("basic")){
return 500;
}
else if(accType.equals("advanced")){
return 700;
}
else{
return 0;
}
}

//This one takes the balance that was set in the class and calulates interest
//for five years. We store the current year balance in the double interestAndBalance
//variable so that we don't change the class's amount. The amount is then saved back as
//a running total so we can calculate the next years amount.
public void findInterest()
{
int year=0;
double interestAndBalance=balance;
while(year<5)
{

}
double interest = interestAndBalance * 0.05;
System.out.println(year+" year inerest:"+interest);
interestAndBalance+=balance+interest;
year++;



BankAccount ba;
        ba = new BankAccount();
ba.balance=500;
ba.findInterest();
}

Everything is great until the last line where I get a parsing error. No matter how i re-arrange the brackets I get the same error.
After compiled I get this error.

 C:\myjava>javac test.java
test.java:50: error: reached end of file while parsing
}
 ^
1 error

Have you checked that all the curly brackets are paired? The compiler could not find one or more ending '}'s.

Also your code is not properly formatted. The nested statements within pairs of brackets should be indented 3-4 spaces. Your code has all the statements start in the first column. With indentations you could easily see which '{' does not have a matching '}'.

Look at the code in this thread for an example of good formatting:
http://www.daniweb.com/software-development/java/threads/431720/keys-not-responding

Well The format is no issue, because even oracle states you can program it all in one line as you have the proper brackets and braces.... Just need to figure out how to fix it. Thanks!

The format is no issue,

The compiler is happy with any formatting. Most human programmers are NOT.
You code is a good example of how proper formatting would allow you to quickly see where the problem is.

Here is my code in format... Now it is giving me a line 47 parsing error

 public class BankAccount {
    public double balance;
    public String accountType;

//The function takes a string EX. "student" and returns a number if
//it matches in the if statements. The else is if there are no matches.
//The program will not compile if that last else was not in place because
//the function says it will return a double.
//miimumBalance
    public double minimumBalance(String accType) {

        if (accType.equals("student")) {
            return 100;
        } else if (accType.equals("basic")) {
            return 500;
        } else if (accType.equals("advanced")) {
            return 700;
        } else {
            return 0;
        }
    }

//This one takes the balance that was set in the class and calulates interest
//for five years. We store the current year balance in the double interestAndBalance
//variable so that we don't change the class's amount. The amount is then saved back as
//a running total so we can calculate the next years amount.
//findInterest
    public void findInterest() {
        int year = 0;
        double interestAndBalance = balance;
        while (year < 5) {


            double interest = interestAndBalance * 0.05;
            System.out.println(year + " year inerest:" + interest);
            interestAndBalance += balance + interest;
            year++;
        }



                BankAccount ba;
                ba = new BankAccount();
                ba.balance = 500;
                ba.findInterest();
            }

That looks a lot better.

Where is the ending '}' to pair with the '{' on line 1?

What '{' does the '}' on line 46 pair with?

Line 31 pairs with line 38.
What pairs with the '{' on line 28?

I got it to compile but its missing the public static main void and do not know where to put it. Here is the code

public class BankAccount{  
    public double balance;
    public String accountType;


//miimumBalance  
 public double minimumBalance(String accType) {
        if (accType.equals("student")) {
            return 100;
        } else if (accType.equals("basic")) {
            return 500;
        } else if (accType.equals("advanced")) {
            return 700;
        } else {
            return 0;
        }
    }


//findInterest
    public void findInterest() {
        int year = 0;
        double interestAndBalance = balance;
        while (year < 5) {


            double interest = interestAndBalance * 0.05;
            System.out.println(year + " year inerest:" + interest);
            interestAndBalance += balance + interest;
            year++;

              }



                BankAccount ba = new BankAccount();
                ba.balance = 500;
                ba.findInterest();
            }




 }

You can put the main() method anywhere in the class next to and outside of the other methods you've defined. Some people like the main() to be the first method and some like it last. It's up to you.

Not every class needs a main() method. You need one main() to execute the program. It will be where the program starts execution. Many programs have more than one class.
I add main() methods to many of my classes just to test the class by creating an instance and calling its methods to test them. Having extra methods doesn't hurt.

got it to work

public class BankAccount {

    public double balance;
    public String accountType;

    public static void main(String[] args) {

        BankAccount ba = new BankAccount();
        ba.balance = 1000;
        ba.findInterest();   


       }



    //minimumBalance

    public double minimumBalance(String accType) {
        if (accType.equals("student")) {
            return 200;
        } else if (accType.equals("basic")) {
            return 600;
        } else if (accType.equals("advanced")) {
            return 900;
        } else {
            return 0;
        }
    }
    //findInterest

    public void findInterest() {
        int year = 0;
        double interestAndBalance = balance;
        while (year <= 5) {
            double interest = interestAndBalance * 0.05;
            System.out.println(year + " year interest:" + interest);
            interestAndBalance += balance + interest;
            year++;




        }   

    }
}

Ok, what's next?

I will have more during the week... Just needed that to work first. Thanks with the help and guidance.

Ok, someone will be here if you need more help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.