Linux JAVA 1. http://java.sun.com/j2se/1.4.2/download.html J2SE 1.4.2 SDK 2. Linux RPM ( 9 ) 3. sh j2sdk-1_4_2_07-linux-i586-rpm.bin 4. rpm Uvh j2sdk-1_4_2_07-linux-i586-rpm 5. PATH JAVA 1. vi.bash_profile 2. PATH /usr/java/j2sdk1.4.2_07/bin 3...bash_profile ( ) 4. javac
Java ~ Java program: main() class class» public static void main(string args[])» First.java (main class ) /* The first simple program */ public class First public static void main(string args[]) //output a message to the screen System.out.println( Java Primer Now Brewing ); : $ javac First.java» First.class : $ java First
Java ~ (Methods) public class Second public static void printit(string message) System.out.println(message); public static void main(string args[]) printit( Java Primer Now Brewing ); (Operators)» : + - * / ++(autoincrement) --(autodecrement)» : ==(equality)!=(inequality) &&(and) (or)» : +(concatenate) (Statements): C» for» while» if» primitive data types: boolean, char, byte, short, int, long, float, double» reference data types: objects, arrays
Java ~ (Classes and Objects) class Point // constructor to initialize the object public Point(int i, int j) xcoord = i; ycoord = j; public Point() xcoord = 0; ycoord = 0; // exchange the values of xcoord and ycoord public void swap() int temp; temp = xcoord; xcoord = ycoord; ycoord = temp; public void printit() System.out.println( X coordinate = + xcoord); System.out.println( Y coordinate = + ycoord); // class data private int xcoord; //X coordinate private int ycoord; //Y coordinate
Java ~ Point pta» constructor Point no return value» ( ) Point pta = new Point(0, 15); method overloading: (, )» ( ) Point pta = new Point(); static method instance method» static: class Second printit();» instance: instance pta.swap(); pta.printit(); public private» public: class constant static final public static final double PI = 3.14159;» private: class By reference» instance reference( )» reference» this: (self-referential object)
Java ~ Objects as references Object parameters are passed by reference public class Third public static void change(point tmp) tmp.swap(); public static void main(string args[]) //create 2 points and initialize them Point pta = new Point(5, 10); Point ptb = new Point(-15, -25); Point ptd = new Point(0,1); // output their values change(ptd); pta.printit(); ptd.printit(); ptb.printit(); // now exchange values for first point pta.swap(); pta.printit(); //ptc is a reference to ptb; Point ptc = ptb; //exchange the values for ptc ptc.swap(); //output the values ptb.printit(); ptc.printit();
Java ~ (Arrays)» 10 byte[] numbers = new byte[10];» int[] primenums = 3,4,7,11,13;» : new new 5 references Point[] pts = new Point[5]; reference for (int i = 0; I < 5; I++) pts[i] = new Point(i,i); pts[i].printit(); Java default initialization» primitive numeric data: zero» arrays of objects: null
Java (Packages): class» core Java application programming interface(api) http://www.javasoft.com http://java.sun.com/j2se/1.4.2/docs/api/ core java package : java.<package name>.<class name> java.lang» java.lang.string, java.lang.thread» class full name java.util.vector items = new java.util.vector();» import java.util.vector; Vector items = new Vector();» import java.util.*;
(Exception Handling) Public final void wait() throws public class TestExcept InterruptedException;» wait() public static void main(string args[]) InterruptedException int num,recip; //generate a random number 0 or 1 try-catch //random() returns a double,type-cast to int try num=(int)(math.random()*2); try //call some method(s) that recip=1/num; //may result in an exception. System.out.println( The reciprocal is +recip); catch(theexception e) catch(arithmeticexception e) //now handle the exception //output the exception message System.out.println(e); finally finally //perform this whether the System.out.println( The number was +num); //excepiton occurred or not.
Inheritance class derived-class extends base-class public class Person public Person(String n,int a,string s) name=n; age=a; ssn=s; public void printperson() System.out.println( Name: + name); System.out.println( Age: +age); System.out.println( Social Security: +ssn); private String name; private int age; private String ssn; public class Student extends Person public Student(String n,int a,string s, String m,double g) //call the constructor in the //parent (Person) class super(n,a,s); major=m; GPA=g; public void studentinfo() //call this method in the parent class printperson(); System.out.println( Major: +major); System.out.println( GPA: +GPA); private String major; private double GPA;
Class Object (java.lang.object ) class Object Public class Object» Constructor : public Object()» Method Summary protected Object clone() : Creates and returns a copy of this object. boolean equals(object obj) :Indicates whether some other object is "equal to" this one. protected void finalize() : Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. Class getclass() : Returns the runtime class of an object. int hashcode() : Returns a hash code value for the object. void notify() : Wakes up a single thread that is waiting on this object's monitor. void notifyall() : Wakes up all threads that are waiting on this object's monitor. String tostring() : Returns a string representation of the object. void wait() : Causes current thread to wait until another thread invokes the notify() method or the notifyall() method for this object. void wait(long timeout) : Causes current thread to wait until either another thread invokes the notify() method or the notifyall() method for this object, or a specified amount of time has elapsed. void wait(long timeout, int nanos) : Causes current thread to wait until another thread invokes the notify() method or the notifyall() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Interfaces Interface : class» method method Polymorphism: Polymorphic reference public class Circle implements Shape //initialize the radius of the circle public interface Shape public Circle(double r) radius=r; public double area(); public double circumference(); public static final double PI=3.14159; //calculate the area of a circle public double area() return PI*radius*radius; //calculate the circumference of a circle public double circumference() return 2*PI*radius; private double radius;
Interfaces public class Rectangle implements Shape public Rectangle(double h,double w) height=h; width=w; //calculate the area of a rectangle public double area()return height*width; //calculate the circumference of a rectangle public double circumference() return 2*(height + width); private double height; private double width; Public class TestShapes public static void display(shape figure) System.out.println( The area is + figure.area()); System.out.println( The circumference is figure.circumference()); public static void main(string args[]) Shape figone=new Circle(3.5); display(figone); figone=new Rectangle(3,4); display(figone);
Abstract Classes Shape Interface method (abstract) ( )» abstract : optional, implied Abstract class: (abstract method) (defined method) Interface abstract class instance Public abstract class Employee public Employee(String n,string t,double s) name=n; title=t; salary=s; public void printinfo() System.out.println( Name: +name); System.out.println( Title: +title); System.out.println( Salary : $ +salary); public abstract void computeraise(); private String name; private String title;» private : class protected double salary;» protected : class» public : class (derived) sub class
Abstract Classes A manager A developer public class Manager extends Employee public Manager(String n, String t, double s) super(n,t,s); Public void computeraise() salary+=salary *.05+BONUS; private static final double BONUS=2500; Public class Developer extends Employee public Developer(String n, String t, double s, int np) super(n,t,s); numofprograms=np; public void computeraise() salary+=salary *.05 +numofprograms*25; private int numofprograms;
Abstract Classes Manager class Developer class public static void main(string arg[]) Employee[] worker = new Employee[3]; worker[0] = new Manager( Pat, Supervisor,30000); worker[1] = new Developer( Tom, Java Tech,28000,20); worker[2] = new Developer( Jay, Java Intern,26000,8); for(int i = 0; i < 3; i++) worker[i].computeraise(); worker[i].printinfo();
Applet standalone application : applet : web page» No main()» Constructor: init() AppletViewer FirstApplet.html FirstApplet.java Import java.applet.*; Import java.awt.*; public class FirstApplet extends Applet public void init() //initialization code goes here FirstApplet.html <applet Code = FirstApplet.class Width = 400 Height = 200> </applet> public void paint(graphics g) g.drawstring( Java Primer Now Brewing!,15,15);
P918 java.lang.arrayindexoutofboundsexception TestArrayException.java P920 Vector 3 TestVector.java Unix 211.119.245.68 ~mysung/2004os/class?/.