Docsity

Arrays and Strings in CMPU-101: Implementations and 2-Dimensional Arrays - Prof. Marc L. S, Exams of Computer Science

Toggle sidebar
bg1
Chapter 14
Arrays – Part II
CMPU-101: Problem-Solving and Abstraction
Marc Smith
Vassar College
Spring 2007
Prog1417
public MyString substring ( int first, int lastPlusOne )
{
int n = lastPlusOne - first;
char[] result = new char[n];
for ( int i = 0 ; i < n ; i++ )
{
result[i] = a[i+first];
}
return new MyString(result);
}
Draw a
picture
Prog1418
public MyString trim()
{
int firstNonBlank = this.firstNonBlank();
if ( firstNonBlank == -1 ) { // all blanks?
return new MyString(new char[0]); // empty string
}
int lastNonBlank = this.lastNonBlank();
int length = lastNonBlank - firstNonBlank + 1;
char[] newA = new char[length]; // size of trimmed string
for ( int i = 0 ; i < length ; i++ ) {
newA[i] = a[i+firstNonBlank];
}
return new MyString(newA);
}
Prog1419
// is this MyString a prefix of that MyString?
public boolean isPrefixOf ( MyString that )
{
char[] a1 = this.a;
int n1 = a1.length;
char[] a2 = that.a;
int n2 = a2.length;
if ( n1 > n2 ) {
return false;
}
for ( int i = 0 ; i < n1 ; i++ ) {
if ( a1[i] != a2[i] ) {
return false;
}
}
return true;
}
Section — Using an Array of
Students
Change the implementation,
but don’t change the interface.
Use a partially full array a
and an int variable used
which tells how many slots are in use.
Prog1420
Class Section
// These 3 fields instead of the filename…
private Student [] a; // array of Students
private int used; // how many in a[]
private static final int INIT_SIZE = 20; // max possible in a[]
public Section(String) // read from file, fill array
public void print(PrintStream) // just print from array
public int howMany(); // just return number used
public double average(); // loop through array
pf3
pf4

Partial preview of the text

Download Arrays and Strings in CMPU-101: Implementations and 2-Dimensional Arrays - Prof. Marc L. S and more Exams Computer Science in PDF only on Docsity!

CMPU- 101 A: PrCrroabhleyamsp-^ S–teo^ lvPrin^1 ag 4 rat IInd Abstraction

Va S^ Mspasrarincrg^ SC 2 mo 0 lleit 0 h 7 ge

p {u i c fnbohlictr a n(r [Myin=] rtle aSi ss=turt inPlt 0 lu g=; s isn O<ueP bnwners t; cr - oi+hin fira+ggrs [)(tn^1 ;i]n;^4 t f^1 irs^7 t, int lastPlusOne )

}^ {^ }^ re^ rteusrnu^ ltn[ei]^ w=^ Ma[yi+Sftirrsintg];(result);^ Draw a^ picture

public { i if ( } i int firstNonBlanknt lastNonBlanknt return new lfength =irstNonBlank MyString lastNonBlank Mtrim()yString === = -1 )this.this.(flnew char[0]);irstNonBlankastNonBlank P{- firstNonBlankrog^1 (( ););^4 1 // all blanks?/+/ empty string 1;^8

}^ char[]^ for (^ }^ return new^ newA^ int^ n^ ewA[i^ i] = a[i+= 0 ; i < length ; i++ ) {^ M^ =yString^ new char[length];firstNonBlank(newA);];^ //^ size of trimmed string Prog^1419

/ p {/ (^) ui c i c i isnnfbhh ltt(tia (^) a hcnnnrr i[ (^1) [ 21 sb]] oa=a=>Mo (^1 2) aanyl eS= (^1) = 22 a (^). (^). tntltl)reehhi nnn{iiassgggt.P. (^) attaahhr;; (^) e;;pfrixeOfixf (o fM tyhSatt (^) rMinygS tthraint g)? }^ }^ f^ }^ roe^ rr^ i^ }tfe^ u^ ((^ rt^ r^ uienanrt^1 ntut^ r[^ iru^ ifn=]ae^ !lf;^0 =sae^ l;as;^ i^2 e^ <[;i]^ n)^1 {^ ;^ i++^ )^ {

Ch S baun e tg d c eo t nth’ i te c o imh n apn^ leg^ S— me t et^ h uU net d a s int e io in te n nr g ,f t a^ c as e n.^ Array^ of

U a w Pnrshdeoic gaha 1 n tp 4 eina 2 llsr 0 ttia h llyvoaw rf iaumllb aalenr yrua ssyloe adts are in use.^ // These 3 fields instead of the filename^ private Student [] a;^ private^ private static final^ public Section(String) public void print( public public double average(); i^ nt howManyint^ used;^ PC^ irintStreamnt(^ l);^ aI^ NIT_SIZE = 20;s s^ )S e/// read from file, fill array// just print from array// loop through array/ just return number usedct^ i/…/// array of Students/ how many in a[]/ max possible in a[]on

m inta[]tr[]ic ae^2 ;s-Dimensional^ Arrays

a = new int[ 2 ][ 3 ];^ Declares^ of an array of Allocates with 2 rows, 3 cols; of an array of (draw picture!)^ “an array “a^ intsn array ints” ”

a a a a[[[[ 0001 ]]]][[[[ 0120 ]]]] ==== 7341 ;;;; For^ example

a a[[ 11 ]][[ 12 ]] == 63 ;; 01 We can draw^ A better picture^ On the board! 071 136

f {or f {o P(r S in(yr tsiin ntreot ticmwon .=lo =g u 0 t^0. ;pw ; rrcoiniowthtl( <a< 2 32 N[r ;o; ewcroo]s[wlc+te+o+l]+ ) d +) "L\t"o);ops

}^ }^ System.out.println();^71 36

c[i][j] = a[i][j] + b[i][j];Matrix^ Addition

i d s tdriyaeamnngsmtoiptnyeoatsrliecMatrix^ Properties ' Im a'Gg e<r a o'fy L.-'iSn cc<oal n'l eb:y^ ' ImL e<o an'g fHe'as rm<^ & o'n^ cN'h a< r'sM'

c {la p p psrrrsiiivvv GUIaaaMttteeeyP iiinnn^ a—tttn [[n]]e ;xyRegular Polygonsl ;;^ Prog1428extends JPanel^ — p {u t x y i inbhn ==ttlicis xy.nn^ nCCMyee ee=wwnn Pn ttiinneea;ttrrn[[ nn==e]]l ;; 11 ( 00 in 00 t;;^ n^ )

int radius = 50 ; f {or x y (^) [[(i]i] i n==t xy ( (i (^) CrCr=aa (^) eded (^0) nniuiu ;tt (^) eessi (^) r (^) r <** + (^) + MMn (( (^) aaii;nn (^) tti+hhtt))..+cs (^) oin)s((ii 22 .. 00 **MMaatthh..PPII//nn))));; }

p { }u s pbualipgcee vr..dopridaa iwnptPaCionoltymCgopomonn(pxeo,nynt,e(npn)a;t g( eG);raphics page )

foCr f oh(r i fi e n(( t ci n(rrk poto acewwogr +=le^ c=.B of 0 ill 0 o)l;R %;ar eoc 2 rcwo dt!l( = <r^ < o— 0 8 w 8 ) ;*^ ; S PrcoIoDrwl+Eo++,+g ))^1429

c S SoIIDDl*SEEI,)D;E,