Beruflich Dokumente
Kultur Dokumente
J M GITHEKO
Agenda
Javadoc documentation and usage The String reference type String methods Math Class Simple keyboard input Exercises
Javadoc
How to download Javadoc How to use Javadoc Adding the JDK Javadoc to the IDE
Choose Tools > Java Platforms from the main window. Select the platform to which you want to add Javadoc in the left panel of the dialog box. In the Javadoc tab, click Add ZIP/Folder and specify the location of the Javadoc files. Click Close.
String Methods
Length Method
String constants: "Carl" String variables: String name = "Carl"; String length: int n = name.length();
Convert to number: int n = Integer.parseInt(str); double x = Double.parseDouble(x); Convert to string: String str2; str2 = Integer.toString(n); //Where n is a number
Substring Method
"Nakuru".substring(2, 4) OR String ex1=Nakuru; ex1.substring(2, 4); //Returns ku Meaning: Return the substring between position 2 and position 4 (starts at position 0)
Exception Handling
try{ int count = Integer.parseInt(input);//Throws exception } catch(Exception e) { System.out.println(The following error occurred + e); System.exit(0); }
Extracting Characters
char: character type Represents a single Unicode character Character constants use single quotes: 'A', '\n', '\u00E9' 'A', is not the same as "A" charAt method gets character from a string "Hello".charAt(0) is 'H' Exercise: Use a for loop to print out the characters that make up the title of this slide.
Comparing Strings
String word1 = Nairobi; String word1 = Nairobi;
StringBuffer
String objects are immutable their contents can not be manipulated Example: If a word is misspelled and is stored in a String, you can change a single character in the string. You would have to replace the entire string StringBuffer class allows manipulation
StringBuffer contd
StringBuffer town=Nairubi; for (int i=o; i< town.length(); i++) { if (town.charAt(i)==u){ town.setCharAt(i, o);} } town.append( Metropolitan) //Appends word to town string variable
Math Class
Static Import
Using the static import language feature, you don't have to write Math in front of every math function:
cos(angle);
Exercises
1. Write a class that counts the number of characters in the text that a user types in from the keyboard. 2. Write a class that checks whether the user input has the character @ 3. Write a class that counts the number of words in the user input. 4. Write a part of a quiz class that asks the user for certain facts and checks if their answer is correct. Example: Who is the MP for Nakuru Town constituency?
Exercise
Write a program to generate the Fibonacci series: 1, 2, 3, 5, 8, 13, .89. 1. Problem analysis understand the problem 2. Design a solution [algorithm] 3. Implement the design 4. Test and verify that it works
Analysis
A=1 B=2
Nested Loops
A=10 B=0 WHILE (A>0) WHILE (B <10) WRITE $ B= B +1 END WHILE WRITE # A=A-1 END WHILE
Blueprint Openoffice Proofing tools spellcheckers Hard disk = diski ngumu Mouse = kipanya, panya, kinyoshi, \r\n