{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1620alec8 - ITEC 1620A Lecture 8 Fall 2010 York University...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ITEC 1620A Lecture 8 Fall 2010 York University
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Classes and objects Classes Each class is a data type Objects An object is an instance of a class An object to its class is just like a literal to its primitive data type e.g., 'a' to char, 100 to int, 12.34 to double, true to boolean If Student is a class, then each particular student is its object String is a standard Java class "This is a string" is an object of String "x", "" Single quotes for char
Background image of page 2
3 String class String is user-defined class, not primitive Represents character strings All string literals in Java are instances of this class String name = new String ("Tom Sawyer");
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Operator new Scanner scan = new Scanner(System.in); Random generator = new Random(); DecimalFormat fmt = new DecimalFormat("0.###"); String title = new String(“Java Software Solutions"); Strings are so fundamental, shortcut notation: String city = “London"; Without using operator new, this is only for class String Variables of a class type can be called: Instance variables, object variables, object reference variables
Background image of page 4
5 String methods Constructor String (String str) char charAt (int index) int compareTo(String str) String concat(String str) boolean equals (String str) boolean equalsIgnoreCase(String str) int length() String replace (int oldChar, char newChar) String substring (int offset, int endIndex) String toLowerCase() String toUpperCase()
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 An example on class String public class StringMutation{ public static void main (String[] args){ String phrase = "Change is inevitable"; String mutation1, mutation2, mutation3, mutation4; System.out.println ("Original string: \"" + phrase + "\""); System.out.println ("Length of string: " + phrase.length()); mutation1 = phrase.concat (", except from vending machines."); mutation2 = mutation1.toUpperCase(); mutation3 = mutation2.replace ('E', 'X'); mutation4 = mutation3.substring (3, 30); //The first char in a string has index 0 // Print each mutated string System.out.println ("Mutation #1: " + mutation1); System.out.println ("Mutation #2: " + mutation2); System.out.println ("Mutation #3: " + mutation3); System.out.println ("Mutation #4: " + mutation4); //From index 3 inclusive up to index 30 exclsive System.out.println ("Mutated length: " + mutation4.length()); } }
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}