Ask Question
Today, 00:01

Write a class definition of a class named 'Value' with the following: a constructor accepting a single integer parameter a constructor with no parameters a method 'setVal' that accepts a single parameter, a boolean method, 'wasModified' that returns true if setVal was ever called for the object. a method 'getVal' that returns an integer value as follows: if setVal has ever been called, it getVal returns the last value passed to setVal. Otherwise if the "single int parameter" constructor was used to create the object, getVal returns the value passed to that constructor. Otherwise getVal returns 0.

+3
Answers (1)
  1. Today, 03:14
    0
    The answer to this question can be given as:

    Class definition:

    public class Value / /define class.

    {

    private boolean modified = false; / /define the boolean variable and assign value.

    private int y; / / define integer variable.

    public Value (int x) / /define parameterized constructor

    {

    y = x; / /holding value in variable y.

    }

    public Value () / /define default constructor.

    {

    }

    public int getVal () / /define function getVal.

    {

    return y; / /return value.

    }

    public void setVal (int x) / /define function setVal.

    {

    y = x; / /hold parameter value.

    modified = true;.//hold boolean value.

    }

    public boolean wasModified () / /define function wasModified.

    {

    return modified; / /return value.

    }

    }

    Explanation:

    In the above class definition firstly we define a class that is "Value". In this class we constructor, methods and variables that can be described as:

    In the class we define a variable that is modified and y both variable is private but the modified variable is boolean type that is used for hold only true or false value and variable y is an integer variable. Then we define constructors. In this class, we define parameterized constructor and default constructor. In the default constructor, we do write anything but in the parameterized constructor we use the private variable y for the hold parameter value. Then we define a function getVal () and setVal (). The getVal () function is used to return private variable (y) value and setVal () function is used to set the value of y. and we also change the modified variable value that is "True". At the last we define a wasModified () function. In this function, we return the modified variable value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a class definition of a class named 'Value' with the following: a constructor accepting a single integer parameter a constructor with ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers