Search code examples
javaoopinstance-variables

how to update value of instance variable JAVA


I'm new to JAVA and OOP, I'm trying to do some OOP exercises but stuck on updating the value of the instance variable of supper class.

I have a super-class called Print

public class Print {
    private String _color;
    private int _paper;
    
    public Print(int paper, String color) {
        this._color = color;
        this._paper = paper;
    }
    
    // getter 
    public String getColor() {
        return this._color;
    }
    
    public int getPaper() {
        return this._paper;
    }
    
    // getter
    public void setColor(String color) {
        this._color = color;
    }
    
    public void setPaper(int paper) {
        this._paper = paper;
        System.out.println("current amount of paper: " + this._paper);
    }
    
    // runPrint
    public void runPrint(int paper) {
        System.out.println("this is demo!");
        return;
    }
        
    // addPaper
    public void addPaper(int paper) {
        System.out.println("this is demo!");
    }
}

and a child class ColorPrint

public class ColorPrint extends Print {
    private String _color;
    private int _paper;
    
    public ColorPrint(int paper, String color) {
        super(paper, color);
    }
    
    // runPrint
    @Override
    public void runPrint(int paper) {
        int temp = 0;
        if(super.getPaper() - paper < 0) {
            paper -= super.getPaper();
            System.out.println(super.getColor() + " paper needs " + paper + " more!");
        } else { 
            System.out.println(super.getColor() + " " + super.getPaper() + " is printed.");
            temp = super.getPaper();
            temp -= paper;
            System.out.println(super.getColor() + " is remains for " + temp);
        }
        return;
    }
    
    // addPaper
    @Override
    public void addPaper(int paper) {
        System.out.println(paper + " is added.");
        int currPaper = super.getPaper() + paper;
        super.setPaper(currPaper);
    }
    
    @Override 
    public String toString() {
        return super.getColor() + ": " + "current paper is " + super.getPaper();  
    }
}

and the main function

public static void main(String[] args) {
        Print[] p = {
                        new ColorPrint(100, "Color")
                    };
        
        
        // print out the available 100 papers
        // after printed the current paper now is zero. 
        p[1].runPrint(100); 
        // then I add 50 papers 
        // the current paper now must be 50.
        // But it prints out 150. I'm stuck on this.
        p[1].addPaper(50);
        
    }

My question is how can I update the value of instance variable paper in the supper class after subtracted and added?

Thank you.


Solution

  • The parent class is ok, but there are some issues with the child and the main function.

    1. It is a bad practice to declare child class variables similar to the parent, since it creates confusion. But, it is still legal since parent variables are hidden from child class. So, it is better to remove:
    private String _color; //not used in child class
    private int _paper; //not used in child class
    
    1. The runPrint method
    @override
    public void runPrint(int paper) {
        if(paper > super.getPaper()) {
            int requiredPapers = paper - super.getPaper());
            System.out.println(super.getColor() + " paper needs " + requiredPapers + " more!");
        } else { 
            System.out.println(super.getColor() + " " + super.getPaper() + " is printed.");
            super.setPaper(super.getPaper() - paper); // HERE use setter/mutator method
            System.out.println(super.getColor() + " is remains for " + super.getPaper());
        }
    }
    
    1. And since you are dealing with an array of Print, loops are convinient.
    public static void main(String[] args) {
        Print[] p = { new ColorPrint(100, "Color") };
        
        for(int i = 0; i < p.length; i++) { 
            // print out the available 100 papers
            // after printed the current paper now is zero. 
            p[i].runPrint(100); 
            
            // then I add 50 papers 
            // the current paper now must be 50.
            // But it prints out 150. I'm stuck on this.
            p[i].addPaper(50);
        }
        
    }