Docsity

Recursion Supplement - Lecture Slides | CMPU 101, Study notes of Computer Science

Toggle sidebar
bg1
Recursion Supplement
CMPU-101: Problem-Solving and
Abstraction
Jennifer Walter
Vassar College
Fall 2008
/**
* An Ilist is either empty or a construction consisting of the
* first element and the rest of the list
*/
public interface IList {
/**
* Interfaces can contain only method signatures and named
* constants. All methods defined must be overridden in subclasses
*/
public int length();
public boolean contains(int x);
public boolean areAllOdd();
public int sumAll();
public int productAll();
}
The IList Union of Classes
public class MTList implements IList {
public MTList() { }
public int length() { public String toString(){
return 0; return "()";
} }
public boolean contains(int x) { }
}
public boolean areAllOdd() {
}
public int sumAll() {
}
public int productAll() {
}
The MTList Class
Fill in the
empty
methods
hint: all are
one-liners
public class ConsList implements IList {
/**
* A list of ints contains a first element and a reference
* (rest) to sublists contained in this list
*/
private int first;
private IList rest;
/**
* Constructor needed to set instance variables. In order to
* create new ConsList, need integer and existing IList
*/
public ConsList(int first, IList rest) {
this.first = first;
this.rest = rest;
}
/**
* returns 1 plus the length of the rest of this list
*/
public int length() {
return this.rest.length() + 1;
}
The ConsList Class
/**
* returns true if x = this.first or if x is in this.rest
*/
public boolean contains(int x) {
}
/**
* returns true only iff all numbers in list are odd
*/
public boolean areAllOdd() {
}
The ConsList Class
/**
* returns sum of all integers in this ConsList
*/
public int sumAll() {
}
/**
* returns product of all integers in this ConsList
*/
public int productAll() {
}
/**
* returns String representation of this ConsList
*/
public String toString() {
return "("+this.first+", "+this.rest.toString()+")";
}
}
The ConsList Class
pf2

Partial preview of the text

Download Recursion Supplement - Lecture Slides | CMPU 101 and more Study notes Computer Science in PDF only on Docsity!

Recursion Supplement

CMPU-101: Problem-Solving and

Abstraction

Jennifer Walter

Vassar College

Fall 2008

* An Ilist is either empty or a construction consisting of the

* first element and the rest of the list

public interface IList {

* Interfaces can contain only method signatures and named

* constants. All methods defined must be overridden in subclasses

public int length();

public boolean contains(int x);

public boolean areAllOdd();

public int sumAll();

public int productAll();

The IList Union of Classes

public class MTList implements IList {

public MTList() { }

public int length() { public String toString(){

return 0; return "()";

public boolean contains(int x) { }

public boolean areAllOdd() {

public int sumAll() {

public int productAll() {

The MTList Class

Fill in the

empty

methods

hint : all are

one-liners

public class ConsList implements IList {

* A list of ints contains a first element and a reference

* (rest) to sublists contained in this list

private int first;

private IList rest;

* Constructor needed to set instance variables. In order to

* create new ConsList, need integer and existing IList

public ConsList(int first, IList rest) {

this.first = first;

this.rest = rest;

* returns 1 plus the length of the rest of this list

public int length() {

return this.rest.length() + 1;

The ConsList Class

* returns true if x = this.first or if x is in this.rest

public boolean contains(int x) {

* returns true only iff all numbers in list are odd

public boolean areAllOdd() {

The ConsList Class

* returns sum of all integers in this ConsList

public int sumAll() {

* returns product of all integers in this ConsList

public int productAll() {

* returns String representation of this ConsList

public String toString() {

return "("+this.first+", "+this.rest.toString()+")";

The ConsList Class

IList Methods that return ILists

Write a method doubleList( ) that returns a new

IList that is the equivalent of the receiving IList, but

with each integer multiplied by 2.

Write a method onlyEvens( ) that returns a new

IList containing only the even integers in the

receiving IList.

IList Methods that return ILists

Write a method called getIth(int i) to return

the ith element of an IList (assume the n elements

of the list are indexed 0…n-1 and that the MTList

version prints "no such element", after which -1 is

returned).

Write a method addToEnd(int x) that is called on

an IList and that returns a new IList with x

appended to the end as the rightmost integer in

the list.

IList Methods that return ILists

Write a method called removeX(int x) to return a new

IList, excluding the first occurrence of x (assume the

MTList version returns the empty list).

import javax.swing.; // for JOptionPane class public class TestIList { / Stand-alone class to test IList union / public static void main(String[] args) { / A list of natural numbers from 1 to 6 / IList naturals = new ConsList(1, new ConsList(2, new ConsList(3, new ConsList(4, new ConsList(5, new ConsList(6, new MTList())))))); IList mt = new MTList(); // empty list / A repetitive list of numbers */ IList repeatList = new ConsList(1, new ConsList(1, new new ConsList ConsList (1,(1, new ConsList (1, new MTList())))));

The TestIList Class

/* Print original lists / System.out.println(" naturals = " + naturals); System.out.println(" mt = "+ mt); System.out.println(" repeatList = "+ repeatList); / Test the recursive methods on our lists */ System.out.println("\nTesting recursive methods..."); System.out.println(" naturals.length() should be 6 and is " + naturals.length()); System.out.println(" mt.length should be 0 and is "+ mt.length()); System.out.println("\n naturals.contains(4) should be true and is "+ naturals.contains(4)); System.out.println(" naturals.contains(7) should be false and is "+ naturals.contains(7)); System.out.println(" mt.contains(4) should be false and is "+ mt.contains(4)); }

The TestIList Class

  • create a new IList that contains all the numbers from 1 to n in
  • ascending order. Note the new ConsList tempList replaces old tempList. / public static IList makeList(int n) { IList tempList = new MTList(); for (int i = n; i >=1; i--) { tempList = new ConsList(i,tempList); } return tempList; } /*
  • create a new IList that contains numbers read from the user */ public static IList readList() { IList tempList = new MTList(); int n = 0; while ((n = (^) (Integer.JOptionPaneparseInt.showInputDialog (null,"(Enter pos int or 0 to quit): "))) > 0) { tempList = new ConsList(n,tempList); } return tempList; } }

The TestIList Class