# Potentially simple java methods HW

1. Sep 20, 2016

### Arnoldjavs3

1. The problem statement, all variables and given/known data

So my professor has given this as a lab activity. He has not given any sample outputs, other than these simple instructions. I don't actually understand to what extent I'm to perform this activity to.
1. Write a class BankAccount that contains a double field balance, and the following methods: (a) double getBalance() that returns the current balance of that account (b) void deposit(double amount) that increases the balance by the given amount (c) void withdraw(double amount) that decreases the balance by the given amount. Last, write a proper toString method in your class.
Whatis this? Do i just write a bunch of methods that don't actually do anything aside from return values? He did give source code that is meant to be similar to this(but it doesn't look functional)

2. Relevant equations

"similar example code" http://pastebin.com/1n6bhHtT
3. The attempt at a solution
Code (Java):

public class BankAccount {
double balance;

public double getBalance() {
return balance;

}

public void deposit(double amount) {
balance += amount;

}

public void withdraw(double amount) {
balance -= amount;

}

public void toString() {
return balance + "$"; } } Last edited: Sep 20, 2016 2. Sep 20, 2016 ### Arnoldjavs3 If this helps... this is part 2 of the lab. I'm used to having sample inputs/outputs so I know what is expected of me but I was not given anything here. 1. Write a class Rectangle that has two double fields width and height, and a method void setDimensions(double w, double h) that assigns its parameter values to these fields. Then, write the accessor (“getter”) methods double getArea() and double getPerimeter() that compute and return the area and the perimeter length of the rectangle, and a mutator (“setter”) method void scale(double sf) that multiplies the width and the height of the rectangle by the scaling factor sf. Again, finish up by writing a proper toString method in your class. 3. Sep 20, 2016 ### Mark44 ### Staff: Mentor This is a good start, but it needs some work. Can you withdraw money from an account if the balance is 0? Should you be able to deposit a negative amount? Or even an amount of 0? Also, in your toString() method, the currency symbol goes before the amount, not after, as you have it. IOW, we write$5.62, not 5.62$And please use code tags. I have inserted them around your code. They look like this: [code=java] some java code [/code] 4. Sep 20, 2016 ### Mark44 ### Staff: Mentor You shouldn't need sample inputs/outputs. All this does is to set the dimensions of a rectangle, and get its area and perimeter, and a couple other things. Pick some reasonable values for the two dimensions, and you should be able to calculate the area and perimeter and compare those values to what you program displays. 5. Sep 20, 2016 ### Arnoldjavs3 Thanks for the response. Whilst it would be a relatively easy task for me to do this, the fact of the matter is the professor hasn't really gone in to detail... about anything. He hasn't taught any of java's syntax or methods or really anything aside from general theory. Which is why i'm unsure if I should further on my code(I've been playing wtih java using the free course on helsinki university so my knowledge comes from there.) 6. Sep 20, 2016 ### Mark44 ### Staff: Mentor Here's what I had in mind for one of the methods: Code (Java): public void withdraw(double amount) { if (balance - amount .> 0.0) balance -= amount; else System.out.println("Insufficient funds for withdrawal amount"); } 7. Sep 21, 2016 ### Arnoldjavs3 I see. That's relatively simple and I just hope he won't deduct marks for using syntax that wasn't demonstrated by him(He just briefly went over everything a beginner Java user should know by the end of the course). I think the main goal of this assignment is to simply return values from methods to the class Also, this is somewhat off-topic but I consider myself a self/independent/rely completely on internet learner. I tend to look at other examples and then I can make my own source and go from there. However, with this learning I don't bother much at all with what goes behind the scenes. I hope to make phone apps eventually(I want to release my own app by april next year), and I was wondering is it worth actually looking at this stuff? Java abstracts all of this for a reason - so is it necessary to understand them on a theoretical level? Not sure if i'm clarifying properly, but I want to make functional applications that the everyday person would use. Do I need to understand what's being abstracted? 8. Sep 21, 2016 ### Mark44 ### Staff: Mentor I don't understand what you mean by "what goes on behind the scenes." Can you clarify what you mean by this? What are you classifying as "abstraction"? If you're planning to write an app that is useful, it will need to be smart enough that an ordinary person can use it. That means that it will need to have a user interface that contains enough logic to anticipate all sorts of responses from the user of the app. 9. Sep 21, 2016 ### Arnoldjavs3 Hmm, tbh I don't actually quite understand myself either. But for that matter, maybe this is an example. Code (Java): /** * BankAccount class, foundational code. * * @daniel * @9/21/2016 */ public class BankAccount { double balance; /** * Constructor for objects of class BankAccount */ public double getBalance() { return balance; } /* * @param amount */ public void deposit(double amount) { if (amount > 0.0) balance += amount; else System.out.println("Please enter an amount that is more than$0.0.");
}

public void withdraw(double amount)
{
if (balance - amount > 0.0)
balance -= amount;
else
System.out.println("Insufficient funds.");
}

public void toString()
{
return "\$" + balance;

}
}

I think the main goal of this assignment like i said before is to return values from methods to the class. However, I don't know if I'm doing that in my code?(I don't even know if what I'm saying makes sense)
I'd say I could make clearly more complicated programs however I do so through trial and error. That's why I believe I don't understand what's going on as I'm just correcting the code until I achieve the output I want.

10. Sep 21, 2016

### Staff: Mentor

It makes sense, and you are more or less doing this.
Your deposit() and withdraw() methods are void methods -- they don't return anything, so they shouldn't have a return <something>; statement in them (and yours don't).
Your getBalance() method is supposed to return a double value, and yours does.
However, your toString() function is a void function, so it should not have the return statement you show. Do a web search for "java toString" to see many examples of how it should look.

Finally, you have a comment that says that getBalance() is a constructor for your BankAccount class - it's not. In post #1, the program requirements don't say anything about creating a constructor for the class, but the constructor's name is always the same as the name of the class. Look at some other example programs to see how to write a constructor. At minimum, your constructor should set the balance member to a reasonable value, say 0.0.
As you get more practice writing programs, you'll learn how to do things with less trial and error.