214k views
2 votes
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.

1 Answer

2 votes

Answer:

The following program are:

public class Value { // Define a class named Value which is public type

private boolean modified = false; // a Boolean instance variable named 'modified',

private int val;

public Value(int i) { val = i; }

public int getVal() { return val; }

public void setVal(int i) { val = i; modified = true; }

public boolean wasModified() { return modified; }

}

Step-by-step explanation:

Firstly, a class defines "Value" and then take a Boolean variable "modified" initialize the value false and then take an integer variable "val" and after that we create a constructor taking a parameter in which variable is assigned to the "val" and then we create an integer type function "getVal()" which returns the value of the "val", and then we create a void type function "setVal()" which takes parameter assign to the value to the "val" and we set the value in the "modified" is true and after all we create a Boolean type function, "wasModified()" which give an output true, if the function "setVal()" was called

User CJ Yetman
by
6.2k points