Write a class definition of a class named ‘Value ‘ with the following: a boolean instance variable named ‘modified’, initialized to false an integer instance variable named ‘val’

Write a class definition of a class named ‘Value ‘ with the following: a boolean instance variable named ‘modified’, initialized to false an integer instance variable named ‘val’

LANGUAGE: JAVA

CHALLENGE:

Write a class definition of a class named ‘Value ‘ with the following:
a boolean instance variable named ‘modified’, initialized to false
an integer instance variable named ‘val’
a constructor accepting a single parameter whose value is assigned to the instance variable ‘val’
a method ‘getVal’ that returns the current value of the instance variable ‘val’
a method ‘setVal’ that accepts a single parameter , assigns its value to ‘val’, and sets the ‘modified’ instance variable to true , and
a boolean method , ‘wasModified’ that returns true if setVal was ever called.

SOLUTION:

public class Value{
    private boolean modified = false;
    private int val;
    public Value(int theVal){
        val = theVal;
    }
    public int getVal(){
        return val;
    }
    public void setVal(int theVal){
        val = theVal;
        modified = true;
    }
    public boolean wasModified(){
        return modified;
    }
}

Add comment