12 Palindrome

12 Palindrome - The Palindrome function a starting The...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: The Palindrome function: a starting The Palindrome function: a starting point void setup() { String lines = loadStrings("palindrome.txt"); String println("there are " + lines.length + " lines"); println("there for (int i=0; i < lines.length; i++) { if(palindrome(lines[i])==true) if( { println(lines[i] + " IS a palidrome."); println(lines[i] } else else { println(lines[i] + " is NOT a palidrome."); println(lines[i] } } } boolean palindrome(String word) { return } true; true 1 The Palindrome function The Palindrome function void setup() { String lines = loadStrings("palindrome.txt"); String println("there are " + lines.length + " lines"); println("there for (int i=0; i < lines.length; i++) { if(palindrome(lines[i])==true) if(palindrome(lines[i])==true) { println(lines[i] + " IS a palidrome."); println(lines[i] } else else { println(lines[i] + " is NOT a palidrome."); println(lines[i] } } } boolean palindrome(String word) { return true; return } 2 The Palindrome function The Palindrome function void setup() { String lines = loadStrings("palindrome.txt"); String println("there are " + lines.length + " lines"); println("there for (int i=0; i < lines.length; i++) { if(palindrome(lines[i])==true) if(palindrome(lines[i])==true) { println(lines[i] + " IS a palidrome."); println(lines[i] } else else { println(lines[i] + " is NOT a palidrome."); println(lines[i] } } } boolean palindrome(String text) { return } false; false; 3 The Palindrome function The Palindrome function void setup() { String lines = loadStrings("palindrome.txt"); String println("there are " + lines.length + " lines"); println("there for (int i=0; i < lines.length; i++) { if(palindrome(lines[i])==true) if(palindrome(lines[i])==true) { println(lines[i] + " IS a palidrome."); println(lines[i] } else else { } println(lines[i] + " is NOT a palidrome."); println(lines[i] } } boolean palindrome(String text) { return false; return } 4 The Palindrome Algorithm The Palindrome Algorithm An Algorithm is the steps you take to solve a problem What steps would you take to see if the following word is a palindrome? rewriter rewriter 5 The Palindrome Algorithm The Palindrome Algorithm rewriter 6 The Palindrome Algorithm The Palindrome Algorithm rewriter 7 The Palindrome Algorithm The Palindrome Algorithm rewriter 8 The Palindrome Algorithm The Palindrome Algorithm Start by comparing the first and last letters If they are the same, does that mean the word is a palindrome? If they are different, does that mean the word is NOT a palindrome? rewriter rewriter 01234567 01234567 9 The Palindrome Algorithm The Palindrome Algorithm Start by comparing the first and last letters If they are the same, does that mean the word is a palindrome? If they are different, does that mean the word is NOT a palindrome? rewriter rewriter 01234567 01234567 int first = 0; int last = word.length() – 1; if(word.charAt(first)!= word.charAt(last)) word.charAt(last)) ???? ???? 10 The Palindrome Algorithm The Palindrome Algorithm Start by comparing the first and last letters If they are the same, does that mean the word is a palindrome? If they are different, does that mean the word is NOT a palindrome? rewriter rewriter 01234567 01234567 int first = 0; int last = word.length() – 1; if(word.charAt(first)!= word.charAt(last)) word.charAt(last)) { return false; return } 11 The Palindrome Algorithm The Palindrome Algorithm rewriter rewriter 01234567 01234567 int first = 0; int last = word.length() – 1; if(word.charAt(first)!= word.charAt(last)) word.charAt(last)) { return false; return } else ??? ??? 12 The Palindrome Algorithm The Palindrome Algorithm rewriter rewriter 01234567 01234567 int first = 0; int last = word.length() – 1; while(???) { if(word.charAt(first)!= if(word.charAt(first)!= word.charAt(last)) word.charAt(last)) ???; ???; else else ??? ??? } ??? 13 loadStrings() from the API loadStrings() This loop will display each line of input on a line by itself for (int i=0; i < list.length; i++) for { println(list[i]); println(list[i]); } 14 Now that we know the program is Now that we know the program is working, we can begin to modify it Our assignment is to write a program that converts Fahrenheit temperatures to Celsius temperatures We’ll have a text file with three integers: 212 32 -40 15 Converting a String to an int Converting a to an loadStrings() always returns an array of Strings This is a little awkward if you want to work with integers or decimals int fahrenheit = lines[i]; //error! 16 the int() function the The problem is that the types don’t match String lines = loadStrings("list.txt"); lines //lots of java not shown int fahrenheit = lines[i]; 17 the int() function the the int() function fixes the problem String lines = loadStrings("list.txt"); lines //lots of java not shown int fahrenheit = int(lines[i]); It converts a String to an int, for example "6" with 6 18 Practice quiz question: If we use the Practice quiz question: If we use the following data file, what will be the output? String lines println("there ("there 1 = loadStrings("list.txt"); are " + lines.length + " lines"); lines.length for (int i= ; i < lines.length; lines.length; { println(lines[i]); println } i=i+3) i=i+3 19 ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online