I recently submitted an assignment to my instructor, and i got a less than desirable grade and the jist of the marks were that iw as using hard coded variables so can someone explain to me how to use the getter method a little better? (i assume thats what i have to use to get the user information)

public class BankAccount {
        private String name = "JohnDoe";
        private int nextAccountNumber = 123456;
        private double balance = 5000;
        private String typeofaccount = "checking";


        public void setname(String name){
        this.name = name;

        }
        public String getName(){
            return this.name;
        }

        public void setBalance(double balance2){
            this.balance = balance2;
        }

        public double getBalance(){
            return this.balance;
        }


        public void setAccountNumber(int AccountNumber){
            this.nextAccountNumber = nextAccountNumber;
        }

        public int getAccountNumber(){
            return this.nextAccountNumber;
        }

        public void settypeofaccount(String typeofaccount){
            this.typeofaccount = typeofaccount;
        }

        public String getTypeOfAccount(){
            return this.typeofaccount;
        }

        public void displayAccountInfo(){

            System.out.println("Account Type: " + this.typeofaccount);
            System.out.println("Name: " + this.name);
            System.out.println("Account Number: " + this.nextAccountNumber);
            System.out.println("Balance: $" + this.balance);


        }
        public void displayInfo() {
            // TODO Auto-generated method stub

        }

}



import java.util.Scanner;

public class BankAccountDriver{ 
    private static  String JohnDoe = null;
    private static final String CheckingAccount = null;
    private static String name = JohnDoe;
    public static void main (String[] args){

        // create instance of Bankaccount
        BankAccount account = new BankAccount();

        String name = askName();
        account.setname(name);

        String type = askType();
        account.settypeofaccount(type);

        String accnum = askName();
        account.displayAccountInfo();

        double balance = askBalance();
        account.setBalance(balance);

        account.displayInfo();
        }
    private static double askBalance() {
        // TODO Auto-generated method stub
        return 0;
    }
    private static String askType() {
        // TODO Auto-generated method stub
        return CheckingAccount;
    }
    private static String askName() {
        // TODO Auto-generated method stub
        return JohnDoe;
    }
        }

Recommended Answers

All 10 Replies

so, what are the compiler errors you get? usually, the stacktrace tells you exactly what is going wrong.

Your getters & setters seem OK. Was he referring to lines 86, 90, 94? Those are hard-coded values where presumably you are expected to prompt the user to input a value? Have you covered the use of Scanners in your class yet?

my compiler returned null on 2 of the objects. and kind of but it didnt really make sense to me, ive read up on it alot and im still not really understanding it

your compiler returned null ? that statement itself makes no sense.
what is the exact error message/stacktrace you get?

private static  String JohnDoe = null;
private static String name = JohnDoe;
BankAccount account = new BankAccount();
String name = askName();

When you ask for the name here, it returns null because it hasn't been set. Also becareful you have name string object global and within main. You want to avoid that and I don't see why do you create and set those strings to null.

The reason why you get null is because you don't set those variables on object creation (In a constructor) you just have created a variable name which has "JohnDoe" value but its not set to the new object. Make a constructor. It is a method which has the same name as the class and it executes always when new object is created. Within the constructor set those variables that you want to any values you desire

1) line 26, setAccountNumber(int) does not set to anything but its own number? Shouldn't the argument be named 'nextAccountNumber' instead of 'AccountNumber'?
2) line 62~64, I smell fishy here. I am guessing you want to assign a String "JohnDoe" to line 64 instead of the variable named JohnDoe. I am also guessing that you first tried to compile without the line 62&63 and got an error of unknown symbols. Then you just added the line 62&63 to be able to successfully compile.
3) line 70~71, now you call askName() to get the value of JohnDoe which is null (from line 62). Then set your BankAccount.name to null.
4) line 73~74, same deal as line 70~71 that you call the method askType() which returns null value from variable CheckingAccount in line 63, and then set the type to BankAccount.typeofaccount.

im using the same bank account class up top, and decideed to scratch the driver and make a new one. so far im getting that "JohnDoe" and "checking Account" cant be resolved as a variable and for some reason theres an error at
" public static void main"

import java.util.Scanner;

public class BankAccountDriver{
    private String name = JohnDoe;
    private int nextAccountNumber = 123456;
    private String TypeOfAccount = CheckingAccount;
    private double Balance = 5000;
    public static void main (String[] args){
    }
 public String getName(){ 
 return name;
 }
 public int getnextAccountNumber(){
 return nextAccountNumber;
 }
}

"JohnDoe" and "CheckingAccount" are Strings, you have to enclose them in double quote marks.

ok ive editted that, where would i go from here? i finished the other 2 return types and a displayaccount info, but nothing is showing up in eclipse. does anyone have a code i could see for an example?

samuel, don't just look at "someones code". post the code as you have it now. show what is going wrong.

just saying "nothing is showing up" means nothing to us, since we don't completely know what the application is supposed to do, and we don't completely know what it is doing now.

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.