Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class definition of a class named \'Value\' with the following: a constr

ID: 3655148 • Letter: W

Question

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.

Explanation / Answer

public class value{

//the number that will be set or returned
public int theNum;

//the boolean valueto be returned
public boolean wasCalled;
wasCalled = false;//set to false initially

//constructor with one parameter
public value(int inNum){
//theNum is set to the specified value
theNum = inNum;
}

//the defaut contructor
public value(){
//if it has not been set, it will be set to 0 by default
theNum = 0;
}

//method used to set theNum's value
public setVal(int newNum){
//theNum is set to a new, specified number
theNum = newNum;
//a boolean value is set to true because this method was called
wasCalled = true;
}

//returns true or false depending on whether or not theNum was set using setVal
public boolean wasModified(){
//return true if it was modified
return wasCalled;
}

//returns theNum's value
public int getVal(){
//return theNum
return theNum;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote