-2

So I already have coded mostly everything but I need help with one part which is all the way at the bottom of the code. So I made an object with a saving of 2531.5 so i can print that out as the initial saving of Mr. Gob's but then you have to add 25,000 to Mr. Gob's savings and print it out again and it prints out the same thing as the first one which is 2531.5. The output needs to come out like this https://gyazo.com/631227169fa677fa389fd029db146ed4 . I just confused on how Im suppose to do task #6 and don't know what I'm doing wrong.

public class Gob
{
    //The following are IF/IV
    public String position;
    public int age;
    //task #1 create an Instance Field "saving" as type double
    public double saving;
     //your work here    
    public Gob(double d){
        //task #2 initialize the saving to user input
        
        
        saving = d;
        //your work here
        
        
        position = "Teacher";
        age = 55;
    }

    //task #2 create a method printPosition() to print out position
    //in the following format
    //    "My position is a _______"

    public void printPosition(){
        
        System.out.println("My position is a " + position);
    }
    
    //task #3 create a method to print out saving 
    //you need to create a whole new method

    public void printSaving(){
        System.out.println("My saving is "+ saving);
    }
    
    //task #4 complete the method below 
    //add the input amount to the saving you have 

    public void addSaving(double e){
       e = saving;
     } 
    
    public static void main(String args[]){
        //Create an new object with saving = 2531.50
        Gob h = new Gob(2531.50);
        h.printPosition(); //this is the name of the method
        //Task #5 print out the initial saving of Mr. Gob
        //your work here
       h.printSaving();
        
        
        //Task #6 add 25,000 to Mr. Gob saving and then
        //print the saving again
        //your work here
        h.printSaving();
        
    }
}

3 Answers3

2

Two things :

1 : correct the below method. You are not adding the variable to the class' reference variable (saving)

 public void addSaving(double e){
 //  e = saving;  **should be as below** 
 this.saving += e;
  
 } 

2 : you need to call addSaving(--some double value--) before calling the printSaving for the second time which will actually add up to the old saving. maybe h.addSaving(500.0); h.printSaving();

1
public void addSaving(double e){
   e = saving;
}

When you do this you are assigning the current value of the field you want to modify to the parameter you are passing. It should be the other way around, and you should add the values, not replace one with the other. So, like this:

public void addSaving(double e){
   this.saving = this.saving + e;
}

Also in your main() method I don't see you calling addSaving(). You'll need to do that too if you aren't already.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
1

Your code for public void addSaving(double e) won't add e to the running total of this.saving. You need to change public void addSaving(double e) to be

public void addSaving(double e) {

  this.saving += e;

}

and then call h.addSaving(25000.00); after which you can print the savings amount again using your print method.