ࡱ > W Y V j bjbj "* j j j l & p p p p | , & L 2 6
6
6
6
6
6
6
K K K K K K K $ ;M [O L K 6
6
6
6
6
K 6
6
K 6
R 6
6
K 6
K N F K 6
-2& J p
XH 0 K K 0 L H O O K & & 95713 Lecture Notes
Review of Java Compile Cycle
Reference: Chapter 2 of textbook
Applications
Compile Java source files
Execute the class file that contains the main function
Applets
Compile Java source files
Create an HTML file for loading the applet
Load the HTML file in web browser or in applet viewer.
A minimal HTML tag for loading the applets:
A typical HTML tag
Eclipse features
Reference: Eclipse help and online help at HYPERLINK "http://www.eclipse.org" www.eclipse.org
Eclipse is an integrated development environment.
Workspace is where you have your files.
Perspective is a known layout an arrangement of certain views and actions.
Workspace, perspective, views and editors are very customizable.
Java Development Basics:
Creating Java projects
Adding Java classes
Editing Java classes
Running Java applications
Using debugger to toggle breakpoints and to execute code step by step.
Javadoc comments
Export to zip files
Export to jar file - later
Turn on and off the automatic building
Expand and collapse definitions
Accessing Java API documentation with javadoc bar, focus and navigate
Using Content Assist: Ctrl-Space
Create constructors
Examine overridden methods
Add import statements
Code formatting
Add Java comments
Generate getter/setter methods
Externalize strings
Refactoring
Command-line arguments
Working sets and filters
Showing additional windows/views
Preferences
Lots of plug-ins
See HYPERLINK "http://www.andrew.cmu.edu/user/syucel/95713/examples/v1/v1ch2/Welcome/Welcome.java" Welcome.java
See HYPERLINK "http://www.andrew.cmu.edu/user/syucel/95713/examples/v1/v1ch2/WelcomeApplet/" WelcomeApplet/
Strings
See at HYPERLINK "http://java.sun.com/j2se/1.5.0/docs/api/index.html" http://java.sun.com/j2se/1.5.0/docs/api/index.html
String s = "John";
String s1 = new String(); // no characters and the length is 0.
String s1 = new String (s2); // copy of s.
Methods
s1.length(); // returns length
s1.charAt(I); // returns character at position I+1.
s1.equals(s2); // compares two strings, returns true or false.
s1.equalsIgnoreCase(s2); // compares two strings ignoring the case.
s1.compareTo(s2); // compares two strings. 0 if equal, negative if s1 < s2, positive if s1 > s2
Dont use == for comparing strings. Use one of the equals methods.
s1.regionMatches(int toffset, String other, int offset, int len);
s1.regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len);
s1.indexOf(c, i); // locates the first occurrence of character c in s1 starting at i and returns the index of the character if it is found, otherwise -1.
s1.concat(s2); // adds the characters of s2 to s1 and returns a new String. s1 and s2 remain the same.
s1.replace(c1, c2); // generates a new String replacing c1's with c2 in s1.
s1.upperCase(); // returns a new String that is uppercase of s1.
s1.lowerCase();
Arrays
An array is collection of variables all of the same type. Java doesn't allow access to outside the limits of an array.
int a[]; // declares the array
a = new int[10]; // allocates the array
int a[] = new int[10];
Initialization: int a[] = {0, 1, 2, 3, 4 };
Unlike C++, the number of elements is not specified in array's brackets.
int a[10]; // is syntax error
Using Arrays
a.length returns the length of the array. a[5] refers to 6th element of the array.
To access the elements using the for loop
for (int i = 0; i < a.length; i++) sum += a[i];
or with Java 5
for (int element : a) sum += element
2-dimentional Arrays
int a[][] = new int[3][3];
Initialization int a[][] = { {1], {1, 2}, {1, 2, 3},{1,2,3,4} };
Using
for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) System.out.println(a[i][j]); }
For sorting arrays, use java.util.Arrays.sort() method.
For copying arrays, use System.arraycopy() method.
Constant members
Constant members are defined with the final keyword.
public static final int k = 10;
It is common to define constant members as static.
Static members
A static variable is also called class variable and there is only one copy of it shared by all the instantiated objects. The static variables can be used even if the class does not have any objects.
A static method cannot refer to instance variables of its class, and cannot have this reference. The static variables and the static methods are independent of any object of a class. However, an instance method can refer to the class variables.
Math class methods
See at HYPERLINK "http://java.sun.com/j2se/1.5.0/docs/api/index.html" http://java.sun.com/j2se/1.5.0/docs/api/index.html
Reading Input
Use a Scanner object to read input from the console. See at HYPERLINK "http://java.sun.com/j2se/1.5.0/docs/api/index.html" http://java.sun.com/j2se/1.5.0/docs/api/index.html
See HYPERLINK "http://www.andrew.cmu.edu/user/syucel/95713/examples/v1/v1ch3/InputTest/InputTest.java" InputTest.java
Formatting Output
Use the print and println methods of the System.out for simple output. Use the printf method for formatted output. See page 61 of the CoreJava1.
You can also use MessageFormat class like below:
Scanner scan = new Scanner(System.in);
System.out.print("What is your name? ");
String name = scan.next();
System.out.print("How old are you? ");
int age = scan.nextInt();
String msgPattern = "Hi {0}. You are {1} years old.";
String msg = MessageFormat.format(msgPattern, name, age);
System.out.println(msg);
Z [ i j z { s z
) " ' R S 2 3 A B g j CJ aJ jG Uj U6] 5\ 6 B*ph j U0J PJ 0J j U j U C 2 S ` z 5 6 b & ` j { 9
&