week-06 - C O M P 2 2 – I n t r o d u c t i o n t o C o m...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 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 C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 6 2 Looking back • So far, have been writing programs for small, well-defined problems. • E x a m p l e : – Take a word as input and print it in reverse order. – Take a word as input and print it in all-capital form (using Character.toUpperCase() and not using String.toUpperCase() ). C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 1 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(); } } C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 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(); } } C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 1 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? C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 1 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 C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 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); } } C O M P 2 2 – I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 6 8 BigProgram.java class BigProgram{...
View Full 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 / 66

week-06 - C O M P 2 2 – I n t r o d u c t i o n t o C o m...

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

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