Question
CSE21 Lab 9: Inheritance Task 1: Counter.java Counter class represents a counter that can be initialized and reset to 0, incremented by 1, and asked
CSE21 Lab 9: Inheritance
Task 1: Counter.java
Counter class represents a counter that can be initialized and reset to 0, incremented by 1, and asked for its current value.
public class Counter { private int myCount = 0; public void increment ( ) { myCount++; } public int value ( ) { return myCount; } public void reset ( ) { myCount = 0; } }
It doesn't have a main method; we'll use the class Runner to run and manipulate it.
Using the provided Runner class, along with calls to Counter methods, create a counter whose value is 3 (This should go inside testCounter). Create another counter that uses exactly seven calls to the reset and increment methods to end up with a value of 3 (This should go inside testCounter7Statements).
Task 2: ModNCounter.java
"Mod N" counters count up to a specified amount (the "N"), and then cycle back to zero, for example: 0, 1, 2, 0, 1, 2, 0, ... (This is a "mod N" counter with N = 3.)
A mod N counter has an extra instance variablea good name for it is myNand is initialized with a 1-argument constructor whose argument is the intended myN value. Thus the code
ModNCounter c = new ModNCounter (2); System.out.println (c.value ( )); c.increment ( ); System.out.println (c.value ( )); c.increment ( ); System.out.println (c.value ( )); c.increment ( );
should print 0, then 1, then 0. Fill in ModNCounter so it has the behavior described here. You should only override the increment method and making no other changes to produce the desired behavior.
Task 3: ModNCounter2.java
Implement a method to test ModNCounter2 in Runner so you know it's working. Be sure this new code is included in what you submit for Runner
Task 4: DecrementableCounter.java
One might wish for a counter that allows decrementing its value as well as incrementing it. Create a class DecrementableCounter that inherits from Counter and provides a decrement method. If the counter's value is 0, a call to decrement should have no effect. Otherwise, it should reduce the counter's value by 1. Don't change the Counter class to implement decrement.
Task 5: SeasonCounter.java
Complete a SeasonCounter class that cycles through the four seasons. It will inherit from ModNCounter, overriding the toString method to return "spring", "summer", "fall", or "winter", depending on whether the current value is 0, 1, 2, or 3.
The Counter class doesn't have a constructor. Why doesn't it need one?
The Counter class doesn't have a main. Why doesn't it need one?
How do we know ModNCounter inherits from Counter (what is the keyword)?
Which method is the constructor inside ModNCounter class?
Is cycleLength variable visible to the parent Counter class?
What happens when we call value and reset methods for ModNCounter since it is not defined in ModNCounter?
Create this class so it inherits from Counter. For this one, you should only override the value method and making no other changes to produce the desired behavior. For the class that's creating and using ModNCounter2, it should appear to be exactly the same but the implementation is quite different. Be sure to include the line "package oop" at the top of this file.
What happens when we call increment and reset methods for ModNCounter since it is not defined in ModNCounter2?
Does decrement exists inside Counter?
Does increment's behavior change for DecrementableCounter?
Where is toString being inherited from?
package oop;
public class Counter { Counter(){} private int myCount = 0; public void increment ( ) { myCount++; } public int value ( ) { return myCount; } public void reset ( ) { myCount = 0; }
}
package oop;
/** * A counter class that cycles its internal value back to 0 when it * reaches a maximum value. * * e.g., A (new) ModNCounter with a cycleLength (maximum) of 5 will, * when increment() is called 7 times, have an internal value of 2 */ public class ModNCounter extends Counter {
private int cycleLength;
public ModNCounter (int n) { cycleLength = n; }
public void increment ( ) { // fill in this method such that value() will return // the correct value } }
package oop;
public class Runner {
public static void main(String[] args) { testCounter();
testCounter7Statements();
// testModNCounter(); }
public static void testCounter() { System.out.println("-- testing Counter");
Counter c = new Counter(); c.increment(); System.out.println(c.value());
}
// Use this method to complete exercise 1c public static void testCounter7Statements() { Counter c = new Counter(); System.out.println("-- testing 7 statements"); // include exactly 7 increment() and reset() statements below
System.out.println("current value: " + c.value()); }
}
package oop;
public class SeasonCounter extends ModNCounter {
private static String[] names = {"spring", "summer", "fall", "winter"}; public SeasonCounter() { // fill-in by creating an object with 4 divisor }
@Override public String toString() { // fill-in so it returns the season name } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started