given the class \'ReadOnly\' with the following behavior: A (protected) integer
ID: 3651398 • Letter: G
Question
given the class 'ReadOnly' with the following behavior:A (protected) integer instance variable named 'val'.
A constructor that accepts an integer and assigns the value of the parameter to the instance variable 'val'.
A method name 'getVal' that returns the value of 'val'.
Write a subclass named 'ReadWrite' with the following additional behavior:
Any necessary constructors.
a method named 'setVal' that accepts an integer parameter and assigns it the the 'val' instance variable.
a method 'isDirty' that returns true if the setVal method was used to override the value of the 'val' variable.
Explanation / Answer
public class ReadWrite extends ReadOnly
{
private boolean modified=false;
public ReadWrite(int value)
{
super(value);
}
public void setVal(int nVal)
{
val=nVal;
modified=true;
}
public boolean isDirty()
{
if(modified==true) return true;
else return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.