Sie sind auf Seite 1von 5

King Fahd University of Petroleum & Minerals Information and Computer Science Department

ICS102: Introdu tion to Com!uting Spring 2009 (Term 082)

Lab04: Java Strings


Objectives
Designing and implementing Java programs that deal with: 1. String Class Objects. 2. String Concatenation (+). 3. String Class Methods (length, equals, equalsIgnoreCase, toLowerCase, toUpperCase, trim, charAt, substring, indexOf, lastIndexOf, compareTo, compareToIgnoreCase). Refer to the String class
(page 1022) on Appendix 4 of the book, or to the JD !1"# Doc$%entation online or offline for %ore %ethods belonging to the String class"

4. String ndices 5. !scape Se"#ences (\, \ , \\, \n, \r, \t). 6. $he mp#table Characteristic o% the String Object.

Example
$he %ollowing e&ample generates a password %or a st#dent #sing his initials and age. ('ote: do not #se this %or (o#r act#al passwords).
!" #"#generates#a#password#for#a#student#using#his#initials#and#age #"!

public#class#$assword%a&er#' public#static#(oid#main)*tring+,#args-#' *tring#first.ame#/#0Amr01 *tring#middle.ame#/#0Ahmed01 *tring#last.ame#/#0Al23hamdi01 int#age#/#451

!"#$% &'S (epartment &'S+02: &ntr,-.cti,n t, ',mp.ting

2)*

Spring 2009 (Term 082) Lab04: Java Strings

!!#extract#initials *tring#initials#/#first.ame6substring)578-#+ ####middle.ame6substring)578-#+ ####last.ame6substring)578-1 ##### !!#append#age#after#changing#the#initials#to#lower#case *tring#password#/#initials6toLowerCase)-#+#age1

*9stem6out6println)0:our#$assword#/#0#+#password-1 ; ;

!"#$% &'S (epartment &'S+02: &ntr,-.cti,n t, ',mp.ting

/)*

Spring 2009 (Term 082) Lab04: Java Strings

Exercises
Exercise 1: (StringWelcome.java) Design and implement a Java program that will do the %ollowing operations to this string )Welcome! This is ICS102 Course*: Convert all alphabets to capital letters and print o#t the res#lt+ Convert all alphabets to lower,case letters and print o#t the res#lt+ and -rint o#t the length o% the string. -rint o#t the inde& o% the word Course.

Exercise 2:

(PasswordMaker2.java)

Modi%( the e&ample shown above in s#ch a wa( that instead o% ta.ing the name initials, it ta.es the middle letter %rom the %irst, middle and last name. $he obtained letters will be concatenated along with age which is m#ltipl( b( /00. NOT ! 1o#r program sho#ld wor. %or an( names 'O$ O'21 those names speci%ied in the e&ample.

Exercise 3:

(CharRemover.java)

Design then implement a Java program that remove the /st three occ#rrences o% the character "m# (capital or small) %rom I#m $ %$&$ 'rO()$*m r string the print the res#lting string to the screen. NOT ! 1o#r program sho#ld wor. %or an( string having at least 3 "m# characters 'O$ O'21 the giving string in the e&ercise.

Exercise 4:

(FamilyName.java)

4e have 5 st#dent names +li +l,+li and +hmed +l,+hmed. Design and implement a Java program that will e&change the last name o% the two st#dents in s#ch a wa( that the new names are going to be +li +l,+hmed and +hmed +l,+li. NOT ! 1o#r program sho#ld wor. %or an( names 'O$ O'21 these given names in the e&ercise.

!"#$% &'S (epartment &'S+02: &ntr,-.cti,n t, ',mp.ting

4)*

Spring 2009 (Term 082) Lab04: Java Strings

Exercise 5:

(Swa .java)

4e have the st#dent name $li $l,$li. Desing and implement a Java program that will change the %irst letter with the last letter and it will change the last letter with the %irst letter and the new name is going to be ili $l,$l$. NOT ! 1o#r program sho#ld wor. %or an( name 'O$ O'21 this given name in the e&ercise.

Exercise 6: !(Project!"ints!Series)

(#irline.java)

'ormall( %light availabilit( command in 6irline 7eservation S(stems has the %ollowing %ormat
99 6M6D!:S 6;6 26< 2 $1 , 6' 99 GA ;A HA E7 5 2=>3II CA BA ;F 2@ =I <0 E0 D876 / O71 S 0@30 0F>? 0D@3@ C0 G0 M0 3 2=>>5F CA BA ;A 2A =A <A EA D876 / CDE / 0F>? 0A?? 0D3/> CA GA 4A MA > 68/>>? CA B5 1A SA CA =A $A D876 5 CDE5D /05? //3? 0D6<3 GA ;A HA E7 Jan5>jan%rapar /:/0 /:/0 /:/? /5 $= 5>J6' 0000 /:/0 / 68/>?@ CA B5 1A SA CA =A $A D876 5 CDE5D 0@5? 0F3? 0D350

$he command is described as %ollows: 8irst two characters indicate the %#nction: which is a availabilit( normal. Second two digits indicate the da(, ne&t three characters indicate month, ne&t three characters indicate cit( code %or depart#re and the ne&t three characters indicate the arrival cit( code. e.g -$.13/e01eddmm- command means:
<unction=#an >a9=#8? %onth=#<eb >eparture=#@A> Arri(al=#>%%

4rite a java program that ta.e a normal availabilit( command and print the %#nction, da(,month (%irst letter in Capital), depart#re cit( in Capital and arrival cit( in capital regardless o% the inp#t case. 1o#r o#tp#t sho#ld be %ormatted as above. NOT ! 1o#r program sho#ld wor. %or an( command %ollowing the above %ormat Exercise 7: ($ tional) (Re lace.java)

6ss#me that (o# have the %ollowing string: )This is ICS102 2$0*. Design then implement a Java program that searches %or ICS102 and replaces it with CO ,200. NOT ! 1o#r program sho#ld wor. %or an( string 'O$ O'21 the giving string in the e&ercise.

!"#$% &'S (epartment &'S+02: &ntr,-.cti,n t, ',mp.ting

*)*

Spring 2009 (Term 082) Lab04: Java Strings

Exercise 8: ($ tional)!

(S%m.java)

Design then implement a Java program that s#ms the 6SC codes o% all the letters o% an( >, character string (e.g., Java, have, this, code ...). :se the charAt)- method to accomplish (o#r wor..

Das könnte Ihnen auch gefallen