13.5k views
1 vote
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.

1 Answer

5 votes

Answer:

I believe you want a subclass so here it is!

public class ReadWrite extends ReadOnly {

public ReadWrite(int initialValue){

super(initialValue);

}

private boolean modified = false;

public void setVal(int x) {

val = x;

modified = true;

}

public boolean isDirty() {

return modified;

}

}

Step-by-step explanation:

I might be wrong, just check through it in case

Hope this helped

:)

User Taskinoor
by
5.3k points