I'm currently stuck on a project in my Intro to Java class and need any help I can get. I need to pass a parameter from my main method to a non-static method in another class and then print a double value from that class to the console.
When modeled as simply as possible, it looks like this:
public class UserAccount {
double accountBalance = 0.0;
public void addToBalance(double amountToAdd) {
accountBalance = accountBalance + amountToAdd;
}
public double getBalance() {
return accountBalance;
}
}
and...
public class Driver {
public static void main(String[] args) {
System.out.println("Enter an amount to add to your balance:");
Scanner sc = new Scanner(System.in);
double input = sc.nextDouble();
// I can't figure out these last two lines:
UserAccount.addToBalance(input);
System.out.println("Account Balance: " + UserAccount.getBalance());
}
}
I feel like those last two lines, in theory, should allow me to pass the scanned input to the addToBalance() method and then display the balance to the console via the main method. However, neither line will work because they are both calling non-static methods.
Last time this happened, I just added static modifiers to the methods being called. However, my professor literally deducted 50 points (out of 100) for this because the methods listed in the specification didn't include static modifiers. So I guess I can't do that for some reason.
I basically was just wondering if there is any way to accomplish all of this without using static methods? Keep in mind that this is an intro course, so any possible solutions shouldn't be too complicated (but I'll take literally anything at this point). If anybody can help me, it would be greatly appreciated as I am in dire need of help right now.