40.4k views
0 votes
Write a class definition of a class named Value with the following: a boolean instance variable named modified, initialized to false an int 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 a boolean method, wasModified that returns true if setVal was ever called.

1 Answer

7 votes

Answer:

The class definition to this question can be given as:

Class definition:

public class Value //define class value.

{

private boolean modified = false; //define variable modified.

private int val; //define variable.

Value(int i) //parameterized constructor

{

val = i; //holds parameter value.

}

public int getVal() //define method getVal.

{

return val;

}

public void setVal(int i) //define method setVal

{

val = i; //holds parameter value

modified = true; //change boolean variable value.

}

public boolean wasModified() //define method wasModified.

{

return modified; //return value.

}

}

Explanation:

The description of the above class definition as follows:

  • Firstly we define a class that is "value" inside a class we define two variable that is "modified and val". Where modified is a boolean variable that holds boolean value true or false and val is an integer variable that holds an integer value.
  • Inside the class, we define parameterized constructor in this constructor we pass an integer parameter that is "i" inside this constructor we use the val variable for hold i variable value.
  • Then we define three functions that are "getVal, setVal, and wasModified".
  • The getVal() function is used for return value. The setVal() function uses an integer parameter that is "i" this function holds value in variable i in val variable and change value of the modified variable that is "true". The wasModified() function is used to return modified variable value.

User Oleg Vikoultsev
by
8.1k points