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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 ITEC 1620A Lecture 8 Fall 2010 York University 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 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"); 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 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() 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); System....
View Full Document

Page1 / 25

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online