Stata Center Photo

MIT Computer Science Labs.

LNK2LRN™ 2010/11

 

AP Comp Sci A

 

October 7 to 20. 

Ch.4: Using the Java

Char and String

Classes.

               Weekly Plans and Assignments:

1. Thursday(10/07): Encrypting a statement (Prog4_1), and Decrypting

a statement (Prog4_2). HW: Study Website Notes and Work on

Programming Assignments not finished in class.

2. Friday(10/08): Prog4_03 (Separate names, "Joe,Mary,Bill,Sam"),

Prog4_04 (Reverse a string, "wow mom radar"), and Prog4_05 (Triangulate

a string, "My Java Code"). HW: Study Website Notes and Work on

Programming Assignments not finished in class.

Homecoming Week Starts Now. Show your Bobcat Spirit!

3. Monday(10/11): (Dress Caveman)Prog4_06 (Separate a phone

number, "5612415916", with hyphens), and Prog4_07 (Remove hyphens

 from SSNo, "163-25-6681"). HW: Study Website Notes and Work on

Programming Assignments not finished in class.

4. Tuesday(10/12): (Dress Greek/Toga)Prog4_08 (Count words in

input string, "Are we having fun yet?"), and Prog4_09 (Scan an input

string, "What is your name?", look for "name", if found, respond with

"My name is Tobor."). HW: Study Website Notes and Work on

Programming Assignments not finished in class.

5. Wednesday(10/13): (Dress Cowboy/Western)Prog4_10 (Scan an

input string, "How are you? I hope you are doing well.", look for "How

are you", if found,respond with random "I am (a) "fine.", (b)"happy.", or

(c) "sad."). HW: Study Website Notes and Work on Programming

Assignments not finished in class. Try the 8-Queens Problem.

6. Thursday(10/14): (Dress '60's/Hippie)Prog4_11 (AI Dialog Project).

HW: Study Website Notes and Work on Programming Assignments not

finished in class.

7. Monday(10/18): Finish the Prog4_11 (AI Dialog Project). HW: Study

 Website Notes and Work on Programming Assignments not finished in

class.

4. Tuesday(10/19): Review Ch.4 - The Java Char and String Classes.

HW: Complete Review handout.

5. Wednesday(10/20): Test on Ch.4 - The Java Char and String Classes.

HW: Go to website for notes and plans for Ch.5: Arrays in Java.

Very Important: If you have any questions or were absent from class,

see me before school (8:00 - 8:30 AM), during Lunch, 7th hour, or

after school. Best to send an email to rpersin@fau.edu.

 

Website Notes: Ch.4 - The Java Char and String Classes.

1. Character class static methods.

Character Class Methods
Character class is used mostly for static methods to test char values.
b =  Character.isDigit(c) true if c is digit character.
b =  Character.isLetter(c) true if c is letter character.
b =  Character.isLetterOrDigit(c) true if c is letter or digit.
b =  Character.isLowerCase(c) true if c is lowercase char.
b =  Character.isUpperCase(c) true if c is uppercase char.
b =  Character.isWhitespace(c) true if c is space, tab, ....
c =  Character.toLowerCase(c) Lowercase version of c.
c =  Character.toUpperCase(c) Uppercase version of c.

// This program reads a single character from the keyboard and displays it
public class DemonstrateRead1
{
public static void main(String[] args)
{
char entry;

// Prompt for a keyboard input and read it
System.out.print("Enter a character: ");
entry = Keyboard.readChar();

// Display the keyboard character typed
System.out.println();
System.out.println("You typed " + entry);
}
}

2. Strings and Concatenation.

The + operator joins two strings together. If either operand is String, the other is converted to String and concatenated with it. This is the most common way to convert numbers to Strings.

If a non-String object is part of the concatenation, its string representation is produced by calling its toString() method. When you write a class that might have a sensible representation as a string, it's often useful for debugging purposes to define a toString() method. If no such method is defined, the default version from Object is used.

"abc" + "def" "abcdef"
"abc" + 4 "abc4"
"1" + 2 "12"
"xyz" + (2+2 == 4) "xyztrue"
1 + "2.5" "12.5"

//Here is the standard way of getting text.

public class Echo3 {
public static void main (String[] args)
{

String message; // Creates a varible called message for input

System.out.print ("Enter the message : ");
message = Keyboard.readString();

System.out.print("You ");
System.out.println("entered : " + message);

} // method main
} // end of public class Echo

 

3. String methods.

Below are some of the most common String methods that we will be using.

In all of these prototypes, i and j are int indexes into a string, s and t are

Strings, and c is a char.

Length
i =  s.length() length of the string s.
Comparison (note: use these instead of == and !=)
i =  s.compareTo(t) compares to s. returns <0 if s<t, 0 if ==, >0 if s>t
i =  s.compareToIgnoreCase(t) same as above, but upper and lower case are same
b =  s.equals(t) true if the two strings have equal values
b =  s.equalsIgnoreCase(t) same as above ignoring case
b =  s.startsWith(t) true if s starts with t
b =  s.startsWith(t, i) true if t occurs starting at index i
b =  s.endsWith(t) true if s ends with t
Searching -- Note: All "indexOf" methods return -1 if the string/char is not found
i =  s.indexOf(t) index of the first occurrence of String t in s.
i =  s.indexOf(t, i) index of String t at or after position i in s.
i =  s.indexOf(c) index of the first occurrence of char c in s.
i =  s.indexOf(c, i) index of char c at or after position i in s.
i =  s.lastIndexOf(c) index of last occurrence of c in s.
i =  s.lastIndexOf(c, i) index of last occurrence of c on or before i in s.
i =  s.lastIndexOf(t) index of last occurrence of t in s.
i =  s.lastIndexOf(t, i) index of last occurrence of t on or before i in s.
Getting parts
c =  s.charAt(i) char at position i in s.
s1 =  s.substring(i) substring from index i to the end of s.
s1 =  s.substring(i, j) substring from index i to BEFORE index j of s.
Creating a new string from the original
s1 =  s.toLowerCase() new String with all chars lowercase
s1 =  s.toUpperCase() new String with all chars uppercase
s1 =  s.trim() new String with whitespace deleted from front and back
s1 =  s.replace(c1, c2) new String with all c2s replaced by c1s.
Static Methods for Converting to String
s =  String.valueOf(x) Converts x to String, where x is any type value (primitive or object).

Here is another program to try:

public class StringInput
{
public static void main(String[] args)
{

int i1=-1;
String entry, idno, restof;

System.out.print("Input a string of 10 UPPERCASE characters: ");
entry = Keyboard.readString();
System.out.println(entry);
idno = entry.substring(0,5);
restof = entry.substring(5);
System.out.println(idno+" "+restof);
 

     for(int j=0; j<restof.length(); j++)
{
        for(char i=65; i<90; i++)
            { if(restof.charAt(j)==i)
            { i1=j; break;
             }
              } if(i1>0)break;
}
System.out.println(i1+" "+restof.substring(i1));
}
}

Here is an algebraic expression parser:

// Works with expressions like 3x^3-2x^2+5x-9

public class EquaProg {
public static void main (String[] args)
{

int count = 0, terms;
boolean found1=false;
String func;
String lookfor;
System.out.print ("Enter the function : ");
func = Keyboard.readString();

System.out.println();
System.out.println("You entered : " + func);
System.out.println();

for(int i=0;i<=func.length()-1;i++)
{lookfor = func.substring(i,i+1);
if(lookfor.equals("+")||lookfor.equals("-"))
{count++;
}
}
System.out.println("This function has "+(count+1)+" terms.");
System.out.println();

System.out.print("Coefficients are : ");
for(int i=0;i<=func.length()-1;i++)
{lookfor = func.substring(i,i+1);
if(lookfor.equals("x"))
{System.out.print(func.substring(i-1,i)+" ");
}
}
System.out.println();
System.out.println();

System.out.print("Exponents are : ");
for(int i=0;i<=func.length()-1;i++)
{lookfor = func.substring(i,i+1);
if(lookfor.equals("^"))
{System.out.print(func.substring(i+1,i+2)+" ");

}
if(func.substring(i,i+2).equals("x+")||func.substring(i,i+2).equals("x-"))
{found1=true;
}
if(found1) {System.out.print("1");break;}
}
System.out.println();
System.out.println();
terms = count+1;
count = 0;
System.out.println("The "+(terms)+" terms are:");
for(int i=0;i<=func.length()-1;i++)
{lookfor = func.substring(i,i+1);
if(lookfor.equals("+")||lookfor.equals("-"))
{ System.out.println();
count++;
}System.out.print(func.substring(i,i+1));
} System.out.println();
}
}

4. Convert from type X to type Y

              integer to String :

int i = 42;

String str = Integer.toString(i);

or

String str = "" + i

double to String :

String str = Double.toString(i);

long to String :

String str = Long.toString(l);

float to String :

String str = Float.toString(f);

String to integer :

str = "25";

int i = Integer.valueOf(str).intValue();

or

int i = Integer.parseInt(str);

String to double :

double d = Double.valueOf(str).doubleValue();

String to long :

long l = Long.valueOf(str).longValue();

or

long l = Long.parseLong(str);

String to float :

float f = Float.valueOf(str).floatValue();

decimal to binary :

int i = 42;

String binstr = Integer.toBinaryString(i);

decimal to hexadecimal :

int i = 42;

String hexstr = Integer.toString(i, 16);

or

String hexstr = Integer.toHexString(i);

hexadecimal (String) to integer :

int i = Integer.valueOf("B8DA3", 16).intValue();

or

int i = Integer.parseInt("B8DA3", 16);

ASCII code to String :

int i = 64;

String aChar = new Character((char)i).toString();

integer to ASCII code (byte) :

char c = 'A';

int i = (int) c; // i will have the value 65 decimal

To extract Ascii codes from a String :

String test = "ABCD";

for ( int i = 0; i < test.length(); ++i ) {

char c = test.charAt( i );

int i = (int) c;

System.out.println(i);

} 

integer to boolean :

b = (i != 0);

boolean to integer :

i = (b)?1:0;

 

5. Here is an idea for a String Program:

public class How4

{public static void main(String[] args){

int x=0;
String greeting="Hello, how are you feeling today?";
String reply="I am feeling ";
String tofind="how are you";
String lookfor;
boolean found=false;

x=(int)(3.0*Math.random());

System.out.println(greeting);
System.out.println();
System.out.print(reply);

for(int i=0;i<greeting.length()-11;i++)
{ lookfor=greeting.substring(i,i+11);

if(lookfor.equals(tofind))
{found=true; break;}}

if((x==0)&&found)
{System.out.println("fine.");}

if((x==1)&&found)
{System.out.println("happy.");}

if((x==2)&&found)
{System.out.println("sad.");}

System.out.println();
} }

Anti-Phishing Game

ASCII Table

 

 

animated open door gifARCHIVES:   CH.1   CH.2  CH.3 

0. MIT Artificial Intelligence

1. Sun Microsystems Java

2. Elements of Java

3. University of Virginia Oracle

4. Review of Java

5. Honda ASIMO