I am having a really hard time with this lab. It deals with constructors and inh
ID: 3560322 • Letter: I
Question
I am having a really hard time with this lab. It deals with constructors and inheritance, but if you're a comfortable programmer it should be really easy.
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
Explanation / Answer
he problem comes with this piece of code:
Imagine that every element of the array A was initialized with the value N+1. Since the function call Arrays.fill(countersArray, currentMax) has a time complexity of O(N) then overall your algorithm will have a time complexity O(M * N). A way to fix this, I think, instead of explicitly updating the whole array A when the max_counter operation is called you may keep the value of last update as a variable. When first operation (incrementation) is called you just see if the value you try to increment is larger than the last_update. If it is you just update the value with 1 otherwise you initialize it to last_update + 1. When the second operation is called you just update last_update to current_max. And finally, when you are finished and try to return the final values you again compare each value to last_update. If it is greater you just keep the value otherwise you return last_update
given integer N = 5 and array A such that:
the values of the counters after each consecutive operation will be:
The goal is to calculate the value of every counter after all operations.
Write a function:
that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.
The sequence should be returned as:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.