





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






ModNCounter ctr = new ModNCounter (2); for (int k=1; k<=5; k++) { ctr.increment ( ); } System.out.println (ctr);
public class Counter { protected int myCount; public Counter ( ) { myCount = 0; } public void increment ( ) { myCount++; } public void reset ( ) { myCount = 0; } public int value ( ) { return myCount; } public String toString ( ) { return "" + value ( ); } }
public class ModNCounter extends Counter { private int myN; public ModNCounter (int n) { myN = n; } public int value ( ) { return myCount % myN; } }
program segment desired output
SeasonProgression s = new SeasonProgression ( ); s.reset ( ); System.out.println (s); for (int k=1; k<=5; k++) { s.increment ( ); System.out.println (s); }
spring summer autumn winter spring summer
public class SeqArray { private IntSequence [ ] mySeqs; // Return the next integer in the input (assuming integers are provided one per line). private static int nextInt (BufferedReader in) throws Exception { return Integer.parseInt (in.readLine ( )); } // Initialize this SeqArray by reading from the given input source. public SeqArray (BufferedReader in) throws Exception {
int arrayLen = nextInt (in);
for (int k1=0; k1<arrayLen; k1++)
int seqLen = nextInt (in)
for (int k2=0; k2<seqLen; k2++) {
int x = nextInt (in);
public class ElemEnumeration { private int index; private IntSequence.IntEnumeration enum; // Initialize an iterator for all integers in all the sequences. public ElemEnumeration ( ) { index = 0; if (hasMoreElements ( )) { enum = mySeqs[0].elements ( ); } } // Return true if there are more integers left to return in any of the sequences; // return false if we’ve gone through all the integers in all the sequences. public boolean hasMoreElements ( ) { return index < mySeqs.length; } // Return the next integer. // If we run out of integers in one sequence, go on to the next. // Precondition: hasMoreElements ( ). public int nextElement ( ) { int x = enum.nextElement ( ); if (!enum.hasMoreElements ( )) { index++; if (hasMoreElements ( )) { enum = mySeqs[index].elements ( ); } } return x; } }
public class SchemeList { private ConsNode myHead; // pointer to the first node in a nonempty list private ConsNode myTail; // pointer to the last node in a nonempty list private class ConsNode { public Object myCar; public ConsNode myCdr; } public void rotate (int k) {
k represented list before rotation represented list after rotation
0 (A B C D) (A B C D)
2 (A B C D) (C D A B)
3 (A B C D) (D A B C)
public class SeqArray { private IntSequence [ ] mySeqs; // Initialize this SeqArray by reading from the given input source. public SeqArray (BufferedReader in) throws Exception { ... } // Return the next integer in the input (assuming integers are provided one per line). private static int nextInt (BufferedReader in) throws Exception { return Integer.parseInt (in.readLine ( )); } // Return an enumeration of all the elements of all the stored sequences. public ElemEnumeration allElems ( ) { return new ElemEnumeration ( ); } public class ElemEnumeration { ... // Initialize an iterator for all integers in all the sequences. public ElemEnumeration ( ) { ... } // Return true if there are more integers left to return in any of the sequences; // return false if we’ve gone through all the integers in all the sequences. public boolean hasMoreElements ( ) { ... } // Return the next integer. // If we run out of integers in one sequence, go on to the next. // Precondition: hasMoreElements ( ). public int nextElement ( ) { ... } } }