week-06-handout

week-06-handout - COMP 202 Week 6 COMP 202 Introduction to...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 COMP 202 – Introduction to Computing 1 COMP 202 - Week 6 1 COMP 202 – Week 6 Organizing large programs Learning outcome : • How to organize big programs into manageable units? • How to solve a big problem in terms of small, well-defined black-boxes? • Know about Methods or sub-programs • Object oriented programming: Think in terms of Classes and Objects COMP 202 - Week 6 2 Looking back • So far, have been writing programs for small, well-defined problems. • Example: – Take a word as input and print it in reverse order. – Take a word as input and print it in all -c a p i t a l form (using Character.toUpperCase() and not using String.toUpperCase() ). COMP 202 - Week 6 3 Reverse.java class Reverse { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println(“Enter a word”); String word = kb.next(); /* using for loop */ for (int i = word.length()-1; i >= 0; i--){ System.out.print(word.charAt(i)); } System.out.println(); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 COMP 202 – Introduction to Computing 1 COMP 202 - Week 6 4 Capitalize.java class Capitalize { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println(“Enter a word”); String word = kb.next(); for (int i = 0; i < word.length(); i++){ System.out.print(Character.toUpperCase(word.charAt(i))); } System.out.println(); } } COMP 202 - Week 6 5 Bigger programs • Think about the following problem – • Write a program that take a sentence as input and print out the sentence with each individual words reversed and capitalized. e.g. – input : How are you output : WOH ERA UOY • What if the program keeps doing the previous job as new sentences are given as input, and finish doing it when the user enters an empty sentence? COMP 202 - Week 6 6 Bigger programs • What are the steps involved in the whole problem? • Possibly – Do the following as long as sentence is not an empty one Read sentence as long as more words in sentence reverse the word capitalize the reversed word print the word
Background image of page 2
3 COMP 202 – Introduction to Computing 1 COMP 202 - Week 6 7 BigProgram.java class BigProgram { public static void main(String[] args){ Scanner kb = new Scanner(System.in); String sentence = “” ; do { System.out.println(“Enter a sentence”); sentence = kb.nextLine(); sentence = sentence.trim(); Scanner wordScanner = new Scanner(sentence); while (wordScanner.hasNext()){ String word = wordScanner.next(); /* reverse the word */ /* capitalize the word */ /* print the new word */ } }while (sentence.length() != 0); } } COMP 202 - Week 6 8 BigProgram.java class BigProgram{ public static void main(String[] args){ Scanner kb = new Scanner(System.in); String sentence = “” ; do { System.out.println(“Enter a sentence”); sentence = kb.nextLine(); sentence = sentence.trim(); Scanner wordScanner = new Scanner(sentence); while (wordScanner.hasNext()){ String word = wordScanner.next(); String revWord = reverse ( word ); String capRevWord = capitalize ( revWord ); System.out.print(capRevWord); } System.out.println(); }while (sentence.length() != ); } } method invocation COMP 202 - Week 6 9 BigProgram.java class BigProgram { public static void main(String[] args){ /* stuff inside the main program */ } /* helper method/sub-program 1 */ static String reverse ( String s ) {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/28/2008 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 22

week-06-handout - COMP 202 Week 6 COMP 202 Introduction to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online