Potentially simple java methods HW

  • Context: Comp Sci 
  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around a homework assignment involving the implementation of Java classes, specifically a BankAccount class and a Rectangle class. Participants express confusion regarding the requirements and expectations of the assignment, as well as the lack of sample outputs or detailed instructions from the professor.

Discussion Character

  • Homework-related
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant questions the purpose of writing methods that may not perform any actions beyond returning values, expressing uncertainty about the assignment's requirements.
  • Another participant suggests that the BankAccount class should include checks for insufficient funds during withdrawals and whether negative or zero deposits should be allowed.
  • A participant proposes a method for withdrawal that includes a condition to check for sufficient funds before allowing the transaction.
  • Some participants discuss the importance of understanding Java's syntax and methods, expressing concerns about the professor's teaching approach and the lack of detailed instruction.
  • One participant reflects on their self-learning approach and questions whether it is necessary to understand the underlying concepts of Java, given its abstraction.
  • Another participant seeks clarification on what is meant by "what goes on behind the scenes" in programming and the relevance of abstraction in app development.

Areas of Agreement / Disagreement

Participants express varying levels of agreement on the necessity of certain checks in the BankAccount class methods, with some proposing additional functionality while others remain uncertain about the assignment's expectations. The discussion reflects multiple competing views on the importance of theoretical understanding versus practical application in programming.

Contextual Notes

Participants note the absence of sample inputs/outputs and detailed instructions from the professor, which contributes to the confusion regarding the assignment's requirements. There are also unresolved questions about the implementation details and the necessity of understanding underlying concepts in programming.

Arnoldjavs3
Messages
191
Reaction score
3

Homework Statement


[/B]
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)

Homework Equations



"similar example code" http://pastebin.com/1n6bhHtT

The Attempt at a Solution


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:
Physics news on Phys.org
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.
 
Arnoldjavs3 said:

Homework Statement


[/B]
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)

Homework Equations



"similar example code" http://pastebin.com/1n6bhHtT

The Attempt at a Solution



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 + "$";
    
        }

}
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:
Java:
some java code
 
  • Like
Likes   Reactions: Arnoldjavs3
Arnoldjavs3 said:
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.
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.
 
Mark44 said:
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:
Java:
some java code
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 into 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.)
 
Here's what I had in mind for one of the methods:
Java:
public void withdraw(double amount) {
   if (balance - amount .> 0.0)
       balance -= amount;  
   else
      System.out.println("Insufficient funds for withdrawal amount");          
}
 
  • Like
Likes   Reactions: 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?
 
Arnoldjavs3 said:
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 don't understand what you mean by "what goes on behind the scenes." Can you clarify what you mean by this?
Arnoldjavs3 said:
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?
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.
 
Hmm, tbh I don't actually quite understand myself either. But for that matter, maybe this is an example.
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
Arnoldjavs3 said:
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)
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.
Arnoldjavs3 said:
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.
As you get more practice writing programs, you'll learn how to do things with less trial and error.
 
  • Like
Likes   Reactions: Arnoldjavs3

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K