1 / 22

Appendix A. JAVA Primer Basics Exception Handling Inheritance Interface and Abstract Classes

Appendix A. JAVA Primer Basics Exception Handling Inheritance Interface and Abstract Classes Applications and Applets. Basics. A Simple Program /* * 첫 번째의 간단한 프로그램 * / Public class First { public static void main(String args[]) { // 메시지를 스크린에 출력한다 .

teddy
Download Presentation

Appendix A. JAVA Primer Basics Exception Handling Inheritance Interface and Abstract Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Appendix A. JAVA Primer • Basics • Exception Handling • Inheritance • Interface and Abstract Classes • Applications and Applets

  2. Basics • A Simple Program /* * 첫 번째의 간단한 프로그램 */ Public class First { public static void main(String args[]) { // 메시지를 스크린에 출력한다. System.out.println(“Java Primer Now Brewing!”); } } • Compilation & Execution – Java Development Kit (JDK) • Javac First.java -> First.class • Java First

  3. Basics • 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 • Arithmetic: + - * / & ++ -- += • Logical: && || • Relational: == != < <= >= > • No Operator Overloading • Exception: System.out.println(“Can you hear ” + “me?”);

  4. Basics – Statements • for for(initialization; test; increment) { // loop body } int i; for(i = 1; i <= 25; i++) { System.out.println(“i = ” + i); } • while while(Boolean condition is true) { // loop body } do { // loop body } while(Boolean condition is true);

  5. Basics – Data Types • if if(Boolean condition is true) { // 문장(들) } else if(Boolean condition is true) { // 다른 문장(들) } else { // 또 다른 문장(들) } • Data Types • 기본형 – boolean, char, byte, short, int, long, float, double • 참조형 – 객체 및 배열

  6. Basics – Classes & Objects (1/4) • Point Class class Point { // 객체를 초기화하기 위한 생성자 public Point(int i, int j) { xCoord = i; yCoord = j; } // xCoord와 yCoord의 값을 교환한다. public void swap() { int tmp; tmp = xCoord; xCoord = yCoord; yCoord = tmp; } public void printIt() { System.out.println(“X coordinate = “ + xCoord); System.out.println(“Y coordinate = “ + yCoord); } // 클래스 데이터 private int xCoord, yCoord; // X & Y 좌표 }

  7. Basics – Classes & Objects (2/4) • Object Instantiation Point ptA = new Point(0, 15); • Undefined Constructor Point ptA = new Point(); • Method Overloading public Point() { xCoord = 0; yCoord = 0; } • Method Calls – object-name.method ptA.swap(); ptA.printIt(); • Protection private, public, protected • Constants public static final double PI = 3.14159;

  8. Basics – Classes & Objects (3/4) • Reference public class Third { public static void main(String args[]) { // 두 개의 점을 생성하여 초기화한다. Point ptA = new Point(5,10); Point ptB = new Point(-15, -25); // 그들의 값을 출력한다. ptA.printIt(); printB.printIt(); // 이제 첫 번째 점의 값들을 교환한다. ptA.swap(); ptA.printIt(); // ptC는 ptB에 대한 참조이다. Point ptC = ptB; // ptC의 값들을 교환한다. ptC.swap(); // 값을 출력한다. ptB.printIt(); ptC.printIt(); } } xCoord = 5 yCoord = 10 ptA xCoord = -15 yCoord = -25 ptB ptC

  9. Basics – Classes & Objects (4/4) • Parameter Passing – Call by Reference public static void chage(Point tmp) { tmp.swap(); } Point ptD = new Point(0, 1); change(ptD); ptD.printIt();

  10. Basics – Arrays • Array of 10 bytes bype[] numbers = new byte[10]; • Initialization int[] primeNums = {3, 5, 7, 11, 13}; • Array of Objects Point[] pts = new Point[5]; for(int i = 0; i < 5; i++) { pts[i] = new Point(i, i); pts[i].printIt(); }

  11. Basics – Packages • Usage java.<package name>.<class.name> • Examples java.lang.String java.lang.Thread java.util.Vector items = new java.util.Vector(); import java.util.Vector; Vector items = new Vector(); import java.util.*; http://www.javasoft.com

  12. Exception Handling public final void wait() throws InterruptedException; try { // 예외를 유발할 수 있는 어떤 method를 부른다. } catch(theException e) { // 이제 예외를 처리한다. } finally { // 예외가 발생하든 않든 간에 이를 수행한다. }

  13. Exception Handling – Examples public class TestExcept { public static void main(String args[]) { int num, recip; // 무작위수 0 또는 1을 생성한다. num = (int) (Math.random() * 2); try { recip = 1 / num; System.out.println(“The reciprocal is “ + recip); } catch (ArithmeticException e) { // 예외 메시지를 출력한다. System.out.println(e); } finally { System.out.println(“The number was “ + num); } } } int[] num = {5, 10, 15, 20, 25}; for(int i = 0; I < 6; i++) System.out.println(num[i]);

  14. public class Person { public Person(String n, Strings) { name = n; age = a; ssn = s; } public void printPerson() { System.out.println( “Name: “ + name); System.out.println( “Age: “ + age); System.out.println( “Sociral 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){ // 부모 (Person) 클래스 내의 // 생성자 (constructor)를 호출한다. super(n, a, s); major = m; GPA = g; } public void studentInfo() { // 부모 클래스 내의 인쇄 method를 // 호출한다. printPerson(); System.out.println( “Major: “ + major); System.out.println(“GPA: “ + GPA); } private String major; private double GPA; } Inheritance

  15. Inheritance – java.util.Vector addElement(Object obj); String first = “The first Element”; String second = “The second element”; String third = “The third element”; Vector item = new Vector(); items.addElement(first); items.addElement(second); items.addElement(thrid); String element; element = (String) items.elementAt(0); System.out.println(element); element = (String) items.elementAt(1); System.out.println(element); element = (String) items.elementAt(2); System.out.println(element);

  16. Interface (1/3) • 객체 동작의 구현을 명세하지 않고도 객체 동작을 정의할 수 있다. public interface Shape { public double area(); public double circumference(); public static final double PI = 3.14159; }

  17. public class Circle implements Shape { // 원의 반경을 초기화한다. public Circle(double r) { radius = r; } // 원의 면적을 계산한다. public double area() { return PI * radius * radius; } // 원의 둘레를 계산한다. public double circumference() { return 2 * PI * radius; } private double radius; } public class Rectangle implements Shape { public Rectangle(double h, double w) { height = h; width = w; } // 사각형의 면적을 계산한다. public double area() { return height * width; } // 사각형의 둘레를 계산한다. public double circumference() { return 2 * (height + width); } private double height; private double width; } Interface (2/3)

  18. Interface (3/3) 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); } }

  19. Abstract Classes (1/3) public abstract class Employee { public Empolyee(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; protected double salary; }

  20. 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 (2/3)

  21. Abstract Classes (3/3) 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”, 2600, 8); for(int i = 0; i < 3; i++) { worker[i].computeRaise(); worker[i].printInfo(); }

  22. import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void init() { // 초기화 코드 } public void paint(Graphics g) { g.drawString( “Java Primer Now Brewing!”, 15, 15); } } <applet code = FirstApplet.class width = 400 height = 200> </applet> appletviewer FirstApplet.html Applications & Applets

More Related