Download 09 April 9

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSE 1720 03 Building
Interactive Systems
April 7, 2009
NB: Installation 1 extended to April 23
Debugging
Process of reducing the number of bugs (errors) in your
code.
Trial and error process.
Basic strategy
Find bug (often very difficult)
Remove it (often easier than finding it)
Some guidelines
Change one thing at a time
Well designed print statements can identify bugs.
Many languages support ‘debuggers’ (Java included)
If a change has no effect, undo it.
Use a change maintenance system (e.g. svn)
Documentation
Write ‘good’ documentation (internal and external).
Good is
effective
readable
succinct
Java documentation -> javadoc
UML
Graphical notation to describe the relationship between
classes/instances
We talked a bit about these earlier
UML
Unified modelling language (UML)
A language to aid in the visualization of relationships
between classes
We talked about UML earlier...
Static (Utility) Class
<< utility>>
type::lib::Rectangle2
width: int
height: int
getArea() : int
UML for a non-static class
type::lib::Rectangle3
width: int
height: int
getArea() : int
Instances
type::lib::Rectangle3
width: int
height: int
getArea() : int
instance of
q :Rectangle3
z :Rectangle3
width = 8
height = 3
width = 5
height = 9
...
...
Relationships between
classes
Class A uses B
A
B
Class A has a B
A
B
Class A is-a B
A
B
Character
Apllication
String
uses
Application
uses
main(String[] args) : void
is-a
java.lang.String
+ length() : int
....
java.lang.Object
+ equals(Object o) : boolean
....
UML..
As more Java syntax is covered in 1030 more UML
syntax will be introduced.
See chapters 8 & 9 in the text.
Putting it all together
Formal problem specification
Good design
Knowledge of appropriate programming
constructs
Solid implementation
Effective testing
Good documentation
A not so Toy Example
Pig Latin interpreter
igpay atinlay interpreterway
Words that start with a vowel (a,e,i,o,u) have way
appended to the end of the word
Words that start with a consonant have all letters up to
the first vowel moved to the end of the word and “ay”
appended
Requirements
What *exactly* is required
Process lines of text
Apply pig latin rules
Lines are separated words
What about punctuation? numbers?
Not all problems are well
specified
Basic pig latin rules don’t say what to do with words
like ff or gg.
After talking to the user, they say ‘just add ay”
Implementation
In Java
Built out of existing classes
Scanner for input
Strings to represent text
While loop for lines, while loop for words
int a = word.indexOf('a');
int e = word.indexOf('e');
int i = word.indexOf('i');
int o = word.indexOf('o');
int u = word.indexOf('u');
if((a == -1)&&(e == -1) && (i == -1) &&
(o == -1) && (u == -1))
{
word = word + "ay";
} else {
if((a == 0)||(e == 0)||(i == 0) || (o == 0) ||
(u == 0))
{
word = word + "way";
} else {
int n = word.length();
if((a >= 0) && (a < n))
n = a;
if((e >= 0) && (e < n))
n = e;
if((i >= 0) && (i < n))
n = i;
if((o >= 0) && (o < n))
n = o;
if((u >= 0) && (u < n))
n = u;
word = word.substring(n) +
word.substring(0, n) + "ay";
}
}
System.out.print(word + " ");
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine())
{
String line = sc.nextLine().trim().toLowerCase();
while(line.length() > 0) {
int blank = line.indexOf(' ');
String word = "";
if(blank > 0) {
word = line.substring(0, blank);
line = line.substring(blank).trim();
} else {
word = line;
line = "";
}
}
System.out.println();
}
}
}
Deployment
Documentation
Maintenance
Use of StringTokenizer
import java.util.Scanner;
import java.util.StringTokenizer;
public class PigLatin2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine())
{
String line = sc.nextLine().toLowerCase();
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens()) {
String word = st.nextToken();
int a = word.indexOf('a');
int e = word.indexOf('e');
int i = word.indexOf('i');
int o = word.indexOf('o');
int u = word.indexOf('u');
if((a == -1)&&(e == -1) && (i == -1) && (o == -1) && (u == -1))
{
word = word + "ay";
} else {
if((a == 0)||(e == 0)||(i == 0) || (o == 0) || (u == 0))
{
word = word + "way";
} else {
int n = word.length();
if(a >= 0)
n = Math.min(a, n);
if(e >= 0)
n = Math.min(e, n);
if(i >= 0)
n = Math.min(i, n);
if(o >= 0)
n = Math.min(o, n);
if(u >= 0)
n = Math.min(u, n);
word = word.substring(n) + word.substring(0, n) + "ay";
}
}
System.out.print(word + " ");
}
System.out.println();
}
}
}
And it goes into production
And a week later, it is decided that you should
generate ‘Swedish Chef’ rather than pig latin
import java.util.Scanner;
import java.util.StringTokenizer;
public class SweedishChef
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine())
{
String line = sc.nextLine().toLowerCase();
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens()) {
String word = st.nextToken();
if(word.equals("an"))
word = "un";
else if(word.equals("au"))
word = "oo";
else {
word = word.replaceAll("o", "u");
word = word.replaceFirst("i", "ee");
word = word.replaceAll("f", "ff");
word = word.replaceAll("ir", "ur");
word = word.replaceAll("ow", "ou");
word = word.replace("the", "zee");
word = word.replace("v", "w");
word = word.replace("f", "v");
}
System.out.print(word + " ");
}
}
}
}
System.out.println("bork bork bork");
20 programming exercises
1. Read in an integer (positive) and convert it to
binary (base 2).
2. Read in an integer (positive) and convert it to
roman (e.g. xvi, xvii, xviii)
3. As above but do xix not xviiii
4. Given an input paragraph (as a String), print it as a
formatted paragraph in an 80 column wide display
5. Write a simple Eliza (good test of regex)
6. Write a more complex Eliza (remember some
common events - and use those to prompt more
input)
7. Moderate a tic-tac-toe game between players.
8. Load and deconstruct a web service (e.g. date,
weather) - weatheroffice.ec.gc.ca
9. Read in a string and determine if it is a valid java
variable name
10. As above but identify reserved words
11. Is an input line a palindrome?
12. Read in an input string of 0’s and 1’s and convert
this base 2 number to base 10.
There are only 10 kinds of people who understand
binary, those who can and those who can’t.
13. Read in an input string in base 16 (Hex) and
convert this base 16 number to base 10.
14. Implement the ‘wc’ program (word count). Count
number of words, lines and characters in the input.
15. Write a program that inserts ‘boiler plate’
comments into your java program. (At the start of the
class and before the main.) Insert in javadoc format.
Create an output file that looks like the input but
with text appropriately added.
16. Write a program that reads in lines of text and
performs proper capitalization (after periods, colons).
Can you be clever about Dr., Mr. Prof. etc?
17. Generate a spinning cursor ‘-’, ‘/’, ‘|’, ‘\’
use backspace character to overwrite
Can you do a Cylon Warrior Eye?
18. Print out headers from your mailbox
/var/spool/mail/name
Lines start with ‘Subject:’
19. Write a program that prints out the number of
days before the 1020 final
and minutes, and seconds
20. Write a Haiku parser
3 lines
first line 5 syllables
second line 7 syllables
last line 5 syllables