C 8 12 6 6
8 Java (3) 1 8.1 8 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8.2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8.2.1 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8.2.2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 8.2.3 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 8.2.4 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 8.3 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 8.4 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 8.5 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 11 {i{
8 Java (3) 8.1 8 Java 8 8.2 8.2.1 class class \{" \" < > class { < > < > < > (modier) class abstract... abstract final public... final... public 1
8.2.2 < > <= > public... protected... private... int x public int y protected int r private double c Graphics gc public JFrame myframe // // // // // Graphics // myframe public static (class eld) 1 1 static int [] data = {9, 7, 6 public static double E=2.7182 // 8.2.3 (constructor) 1 2
Java 8.2.4 (method) C ( < > ( ) { return( ) return void void ( ) public, protected, private static (static method) (class method) static Java. ( ) Java (Overload) 3
// Rectangle.java public class Rectangle { protected double width, height // public Rectangle(double w, double h) { // 1 width = w height = h public Rectangle() { // 2 width = 3 height = 3 // double area() { // area() return width*height public double circumference() { // circumference() return 2*(width + height) public static void main(string [] args){ // main() Rectangle r1 = new Rectangle(10,5) System.out.println("Rectangle 1:") System.out.println("width is " + r1.width) System.out.println("height is " + r1.height) System.out.println("area is " + r1.area()) System.out.println("circumference is " + r1.circumference()) System.out.println("--------------------------") System.out.println("Rectangle 2:") Rectangle r2 = new Rectangle() System.out.println("width is " + r2.width) System.out.println("height is " + r2.height) System.out.println("area is " + r2.area()) System.out.println("circumference is " + r2.circumference()) double 2 2 Rectangle.java $ javac Recatngle.java $ java Rectangle Rectangle 1: width is 10.0 height is 5.0 area is 50.0 circumference is 30.0 -------------------------- Rectangle 2: width is 3.0 height is 3.0 area is 9.0 circumference is 12.0 4
8-1 (Circle) x,y r 1 main() (x=10, y=5, r=4) 1 x 7 y 10 Java x! 0! = 1 x! =x (x ; 1)! // Factorial.java class Factorial { public static void main(string [] args) { int n = 10 System.out.println("n = " + n) System.out.println("Factorial(" + n +")=" + fact(n)) public static int fact(int n) { // if (n==0) { return(1) else { return(n * fact(n-1)) Factorial.java $ javac Factorial.java $ java Factorial n = 10 Factorial(10)=3628800 fact(int n) 5
8.2 B 8 ) [1,6,15,12,7,9,23,2,10,4,20] 8.3 Java 1 new = new ( ) = new ( ) // MyObjects.java import java.util.date class MyObjects { public static void main(string [] args) { Date d d = new Date() System.out.println("Date : " + d) Rectangle r = new Rectangle(30,40) System.out.print("My rectangle is "+ r.width + "cm x ") System.out.print(r.height + "cm area=" + r.area() + "cm^2 ") System.out.println("circumference=" + r.circumference() + "cm") String firstname = new String("Ichiro") String lastname lastname = "Suzuki" System.out.println("Object Name : " + firstname + " " + lastname) MyObjects.java 6
$ javac MyObjects.java $ java MyObjects Now Date : Thu May 31 19:38:45 JST 2001 My rectangle is 30.0cm x 40.0cm area=1200.0cm^2 circumference=140.0cm Object Name : Ichiro Suzuki 8.4 6 1-(a) (Stack) ( 8.1) push/pop push 1 pop 1 capacityincrement capacity = 5 push(string) String pop() count = 2 "Nobu" "Kenji" itemlist = String[capacity] 8.1: 7
// Stack.java public class Stack { // private int count // private int capacity // private int capacityincrement // String [] itemlist // // public Stack() { count = 0 capacity = 5 capacityincrement = 2 itemlist = new String[capacity] // public Stack(String [] list) { count = list.length capacity = list.length capacityincrement = 5 itemlist = list // push public void push (String obj){ // // capacityincrement if (count == capacity) { capacity += capacityincrement String [] templist = new String[capacity] for (int i = 0 i < count i++){ templist[i]=itemlist[i] itemlist = templist // // count 1 itemlist[count] = obj count++ // pop public String pop (){ if (count == 0){ // return null //. else { // count-- // return itemlist[count] // // end pop() ( ) 8
// public void printitems(){ for (int i=0 i < count i++){ System.out.print(itemList[i] + ",") System.out.println(" ") // // Stack.java // StackTest.java public class StackTest { public static void main (String [] args) { String [] items = {"Kenji","Nobu" Stack mystack = new Stack(items) // mystack System.out.println("Initial data :") mystack.printitems() // mystack System.out.println(" ") System.out.println("-----------") mystack.push("taro") // System.out.println("Data after push Taro :") mystack.printitems() System.out.println(" ") System.out.println("-----------") String popobject1 = mystack.pop() // System.out.println("Data after pop object " + popobject1 + " :") mystack.printitems() System.out.println(" ") System.out.println("-----------") String popobject2 = mystack.pop() System.out.println("Data after pop object " + popobject2 + " :") mystack.printitems() System.out.println(" ") System.out.println("-----------") StackTest.java Stack.java 9
$ javac StackTest.java $ java StackTest Initial data : Kenji,Nobu, ----------- Data after push Taro : Kenji,Nobu,Taro, ----------- Data after pop object Taro : Kenji,Nobu, ----------- Data after pop object Nobu : Kenji, ----------- 8-3 (Queue) push/shift Komachi, Yamabiko, Tsubasa, Nasuno Komachi, Yamabiko 10
8.5 2 1. (Circle) x,y r 1 main() (x=10, y=5, r=4) 1 x 7 y 10 2. B 8 [1,6,15,12,7,9,23,2,10,4,20] 3. (Queue) push/shift Komachi, Yamabiko, Tsubasa, Nasuno Komachi, Yamabiko 2 1. // Cell.java public class Cell { int item // Cell next 2. [1,6,15,12,7,9,23,2,10,4,20] 3. 1) Java, 3, David Flanagan 2),, 3) 3 Java,, 4) JAVA,, Patrick Niemeyer & Joshua Peck, 5) Java,, Paul Tyma, Gabriel Torok, & Troy Downing, 11