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

CSE21 Lab 9: Inheritance Task 1: Counter.java Counter class represents a counter

ID: 3817969 • Letter: C

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 for its current value.

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 variable—a good name for it is myN—and is initialized with a 1-argument constructor whose argument is the intended myN value. Thus the code

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
   }
  
  
}

Explanation / Answer

------------------------------

Runner.java

------------------------------

public class Runner {

   public static void main(String[] args) {
       testCounter();

       testCounter7Statements();

   }

  
   public static void testCounter() {
       System.out.println("-- testing Counter");

       Counter c = new Counter();
       c.increment();
       c.increment();
       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");
  c.increment();
       c.increment();
       c.increment();
       c.reset();
       c.increment();
       c.increment();
       c.increment();

       System.out.println("current value: " + c.value());
   }

}

-------------------------------

ModCounter.java

-------------------------------

public class ModNCounter extends Counter {


private int cycleLength;

public ModNCounter (int n) {
   cycleLength = n;
}

  
public void increment ( ) {
   if(super.value()==cycleLength-1){
           super.reset();
       }else{
           super.increment();
       }
}
}

------------------------------

ModCounter2.java

------------------------------

public class ModCounter2 {

   public void testModCounter() {
       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 ( );
   }
   public static void main(String[] args) {
       ModCounter2 m = new ModCounter2();
       System.out.println("ModCounter for myN=2");
       m.testModCounter();
   }

}

----------------------------------------

SeasonCounter.java

----------------------------------------

public class SeasonCounter extends ModNCounter {

   private static String[] names = { "spring", "summer", "fall", "winter" };

   public SeasonCounter() {
       super(4);
   }

   @Override
   public String toString() {
       return names[super.value()];
   }
}

--------------------------------------------

RunnerSeasonCounter.java

--------------------------------------------

public class RunnerSeasonCounter {

   public static void main(String[] args) {
       SeasonCounter s = new SeasonCounter();
       System.out.println(s.toString());
       s.increment();
       System.out.println(s.toString());
   }

}

----------------------------------------------------

DecrementableCounter.java

------------------------------------------

public class DecrementableCounter extends Counter {

   public DecrementableCounter(int n) {
       for (int i = 0; i < n; i++) {
           super.increment();
       }
   }

   public int decrement() {
       if (super.value() == 0) {
           return 0;
       } else {
           int currentValue = super.value();
           super.reset();
           for (int i = 0; i < currentValue - 1; i++) {
               super.increment();
           }
           return super.value();
       }
   }

}

-----------------------------------------------

DecCounterTest.java

-------------------------------------------------

public class DecCounterTest {

   public static void main(String[] args) {
       DecrementableCounter d = new DecrementableCounter(4);
       System.out.println(d.value());
       d.decrement();
       System.out.println(d.value());
   }

}

The Counter class doesn't have a constructor. Why doesn't it need one?

Because we are initializing private variable to 0 at declaration time and java provides default constructor automatically.

The Counter class doesn't have a main. Why doesn't it need one?

Becase the Counter Class is not a runner class and will be used as parent class of other counters.

How do we know ModNCounter inherits from Counter (what is the keyword)?

Keyword 'extends' shows inheritance of class which is present after the class name.

Which method is the constructor inside ModNCounter class?

Constructors are always with the name of the class itself. ModNCounter(int n) is constructor.

Is cycleLength variable visible to the parent Counter class?

No, Parent class never knows anything about child class.

What happens when we call value and reset methods for ModNCounter since it is not defined in ModNCounter?

These methods of its parent class will be called as it is inherited.

What happens when we call increment and reset methods for ModNCounter since it is not defined in ModNCounter2?

ModNCounter2 is just a runner class it doesn't has any of these methods.

Does decrement exists inside Counter?

No, this method is only available in child class of Counter.

Does increment's behavior change for DecrementableCounter?

No, the implementation of this behaviour is not changed.

Where is toString being inherited from?

This method is inherited from Object class which is the Parent class of Counter class.