Potentially simple java methods HW

  • Comp Sci
  • Thread starter Arnoldjavs3
  • Start date
  • Tags
    Java
In summary: USD".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
  • #1
Arnoldjavs3
191
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
  • #2
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
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:
[code=java]
some java code
[/code]
 
  • Like
Likes Arnoldjavs3
  • #4
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.
 
  • #5
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:
[code=java]
some java code
[/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.)
 
  • #6
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 Arnoldjavs3
  • #7
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
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.
 
  • #9
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 Arnoldjavs3

Related to Potentially simple java methods HW

1. What is a potentially simple java method?

A potentially simple java method is a function or routine that performs a specific task or operation in a java program. It is considered "potentially simple" because it may require additional complexity based on the specific needs of the program.

2. How do I create a potentially simple java method?

To create a potentially simple java method, you will need to define the method by giving it a name, specifying its parameters (if any), and writing the code to perform the desired task. This can be done within a class in your java program.

3. What is the purpose of using potentially simple java methods?

The purpose of using potentially simple java methods is to break down complex tasks into smaller, more manageable pieces of code. This makes it easier to read, understand, and maintain your java program.

4. Can a potentially simple java method be reused in different programs?

Yes, potentially simple java methods can be reused in different programs as long as the method is defined in a separate class or package and can be imported into the new program. This promotes code reusability and saves time and effort in writing new code.

5. How do I call a potentially simple java method?

To call a potentially simple java method, you will need to use the method's name followed by parentheses and any required arguments or parameters. This can be done within the main method or within another method in your java program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top