Sie sind auf Seite 1von 22

Java Basics Strings and the Math Class

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 reference type


String word3; // Creates a variable called word3 to be used to contain the memory address of a string object word3=Malindi; //Creates a string object in memory and inserts its memory address in variable word3 String word4=word3; //word4 points at same string as word3

String Methods

Length Method
String constants: "Carl" String variables: String name = "Carl"; String length: int n = name.length();

Converting Between Strings and Numbers


String str = 1432, x=8623;

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)

Simple Input Dialogue


String input = JOptionPane.showInputDialog("Your weight:"); int count = Integer.parseInt(input);//Throws exception System.out.println(count); System.exit(0); //For program to work, you need to import javax.swing.JOptionPane class

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;

word1==word2 //False word1.equals(word2) //True

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

Summary String Methods

Math Class

Static Import
Using the static import language feature, you don't have to write Math in front of every math function:

import static java.lang.Math.*;


This allows you to invoke the Math class methods by their simple names. For example:

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

WHILE ( A>=1) C=A+B WRITE C END WHILE

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

Das könnte Ihnen auch gefallen