CSC212 Homework 2 Solutions 2014
--D. Thiebaut (talk) 07:38, 28 September 2014 (EDT)
Problem #1
PythonList.java
// put your PythonList.java file here
public class PythonList {
public int[] array = null;
private int maxdim;
private int end = 0;
PythonList(int m) {
maxdim = m;
end = 0;
array = new int[maxdim];
}
PythonList() {
this(100);
}
private boolean isValid( int i ) {
return ( i >= 0 && i < end );
}
public void setAt( int i, int n ) {
if ( ! isValid( i ) )
return;
array[i] = n;
}
public int index( int n ) {
for ( int i=0; i < end; i++ ) {
//if ( array[i]==n )
if ( at(i) == n )
return i;
}
return -1;
}
public int rindex( int n ) {
for ( int i=end-1; i >= 0; i-- )
if ( array[i]==n )
return i;
return -1;
}
public void append(int n) {
if (end <= array.length - 1) {
array[end++] = n;
return;
}
int[] temp = new int[maxdim * 2];
for (int i = 0; i < array.length; i++)
temp[i] = array[i];
array = temp;
array[end++] = n;
maxdim = maxdim * 2;
}
public int length() {
return end;
}
public int at(int index) {
if (index >= 0 && index < array.length)
return array[index];
// this is bad... we'll fix it later...
else
return -1;
}
public static void main(String[] args) {
// --- Test List ---
PythonList L = new PythonList(5);
for (int i = 0; i < 11; i++)
L.append(i*2);
L.append( 2 );
for (int i = 0; i < L.length(); i++)
System.out.println(L.at(i));
System.out.println( " index("+ 2 +") = " + L.index(2) );
System.out.println( " rindex("+ 2 +") = " + L.rindex(2) );
}
}
PascalList.java
// Put your PascalList.java file here
public class PascalList {
static private final int ROW = 9;
public PascalList() {
}
static public void main( String[] args ) {
PythonList row = new PythonList( 3 );
row.append( 1 );
for ( int i=1; i<ROW; i++ )
row.append( 0 );
//row.array[0] = 1;
for ( int i=1; i<ROW; i++ ) {
for ( int k=ROW-1; k>0; k-- )
row.setAt(k, row.at(k-1) + row.at(k) );
}
for ( int i=0; i<row.length(); i++ )
System.out.println( row.at(i) + " " );
System.out.println();
}
}
Problem #2
PythonList2.java
// place your code here...
public class PythonList2 {
private String[] array = null;
private int maxdim;
private int end = 0;
PythonList2(int m) {
maxdim = m;
end = 0;
array = new String[maxdim];
}
PythonList2() {
this(100);
}
private boolean isValid( int i ) {
return ( i >= 0 && i < end );
}
public void setAt( int i, String n ) {
if ( ! isValid( i ) )
return;
array[i] = n;
}
public int index( String n ) {
for ( int i=0; i < end; i++ ) {
if ( at(i) == n )
return i;
}
return -1;
}
public int rindex( String n ) {
for ( int i=end-1; i >= 0; i-- )
if ( array[i]==n )
return i;
return -1;
}
public void append(String n) {
if (end <= array.length - 1) {
array[end++] = n;
return;
}
String[] temp = new String[maxdim * 2];
for (int i = 0; i < array.length; i++)
temp[i] = array[i];
array = temp;
array[end++] = n;
maxdim = maxdim * 2;
}
public int length() {
return end;
}
public String at(int index) {
if (index >= 0 && index < array.length)
return array[index];
else
return null;
}
public static void main(String[] args) {
// --- Test List ---
PythonList2 dwarves = new PythonList2(5);
dwarves.append("Blick2");
dwarves.append("Flick2");
dwarves.append("Glick2");
dwarves.append("Plick2");
dwarves.append("Quee2");
dwarves.append("Snick2");
dwarves.append("Whick2");
System.out.println("Snow White's friends:");
for (int i = 0; i < dwarves.length(); i++)
System.out.println(dwarves.at(i));
String dwarf;
dwarf = dwarves.at(-1);
if (dwarf != null)
System.out.println(dwarf);
dwarf = dwarves.at(20);
if (dwarf != null)
System.out.println(dwarf);
int n = dwarves.index( "Quee" );
if ( n != -1 )
System.out.println( "Quee found at Index: " + n );
else
System.out.println( "Quee not found" );
n = dwarves.rindex( "Quee" );
if ( n != -1 )
System.out.println( "Quee found at Index: " + n );
else
System.out.println( "Quee not found" );
n = dwarves.index( "Queen" );
if ( n != -1 )
System.out.println( "Queen found at Index: " + n );
else
System.out.println( "Queen not found" );
}
}