3 p.1 3 Java Java Java try catch C Java if for while C 3.1 boolean Java if C if ( ) 1 if ( ) 1 else 2 1 1 2 2 1, 2 { boolean true false 2 boolean Graphics draw3drect fill3drect C int C OK while (1)... 3.1.1 int boolean............... switch case C
3 p.2 3 Java 3.1.2 AddTime.java public class AddTime extends JApplet { int hour1, minute1, hour2, minute2; public void init() { hour1 = Integer.parseInt(getParameter("Hour1")); minute1 = Integer.parseInt(getParameter("Minute1")); hour2 = Integer.parseInt(getParameter("Hour2")); minute2 = Integer.parseInt(getParameter("Minute2")); int hour, minute; // hour = hour1+hour2; minute = minute1+minute2; if (minute>=60) { // hour++; minute-=60; // g.drawstring(" "+hour+" "+minute+" ", 30, 25); 2 45 1 25 3 70 60 3.2 String Java + String String String int String : System.out.println("2+2 " + (2+2)); System.out.println("2+3 " + (2+3) + " "); JDK 5.0 C printf sprintf drawstring String.format g.drawstring(string.format(" %d %d ", hour, minute), 30, 25);
3.3. Java 3 p.3 : printf Generics Autoboxing Varargs API public static String format(string format, Object... args)... format java.lang.string Integer.parseInt java.lang.integer public static int parseint(string s) String equals String == == true equals true java.lang.string public boolean equals(object s) public boolean equalsignorecase(string s) 3.3 Java try catch try 1 catch ( ) 2 1 1 catch 2 try catch try catch catch ( ) catch catch finally finally finally
3 p.4 3 Java 0 ArithmeticException TryCatchTest.java public class TryCatchTest { public static void main(string[] args) { int i; for (i=-3; i<=3; i++) { try { System.out.println(10/i); catch (ArithmeticException e) { System.out.println(" : "+e.tostring()); -3-5 -10 : java.lang.arithmeticexception: / by zero 10 5 3 i 0 catch ArithmeticException java.lang java.lang import import NullPointerException NumberFormatException ArrayIndexOutOfBoundsException null Integer.parseInt null C NULL 2.5 getparameter null try catch java.io.ioexception 3.4 throw throw throw ; Exception
3.5. for, while 3 p.5 main args 0 break TryCatchTest2.java public class TryCatchTest2 { public static void main(string[] args) { int i, m=1; try { for (i=0; i<args.length; i++) { m *= foo(args[i]); catch (Exception e) { m = 0; System.out.println(" " + m + " "); public static int foo(string arg) throws Exception { int a = Integer.parseInt(arg); if (a==0) throw new Exception("zero"); return a; java TryCatchTest2 1 2 0 3 4 5 6 3 0 3, 4, 5, 6 foo try catch throws, 3.5 for, while while ( 1 ) 1 for ( 1 ; 2 ; 3 ) 1 for ( : ) 1 while 1 1 1 for 1 2 1 3 2 for JDK5.0 for-each each Iterable : for do while C 3.5.1 n n
3 p.6 3 Java N gon.java import static java.lang.math.*; public class N_gon extends JApplet { int numpoints; int sc = 100; public void init() { numpoints = Integer.parseInt(getParameter("NumPoints")); int i; double theta1, theta2; for(i=0; i<numpoints; i++) { // theta1 = PI*2*i/numPoints; // 360*i/n theta2 = PI*2*(i+1)/numPoints; // 360*(i+1)/n g.drawline((int)(sc*(1.1+cos(theta1))), (int)(sc*(1.1+sin(theta1))), (int)(sc*(1.1+cos(theta2))), (int)(sc*(1.1+sin(theta2)))); Math.PI π =3.1415... Math.sin, Math.cos 3.5.2 sin, cos : (JDKDIR)/docs/ja/api/java.lang.Math.html 3.5.3 n 3.5.4 2 2 1
3.5. for, while 3 p.7 1 Gradation1.java public class Gradation1 extends JApplet { int scale = 4; int i; for (i=0; i<64; i++) { g.setcolor(new Color(i*4, 0, 255-i*4)); g.fillrect(i*scale, 0, scale, scale*10); Othello.java 3.5.5 Graph.java public class Graph extends JApplet { int[] is = {10, 4, 6, 2, 9, 1; Color[] cs = {Color.RED, Color.BLUE; int scale = 15; int i, n = is.length; // for (i=0; i<n; i++) { g.setcolor(cs[i%2]); // % g.fillrect(0, i*scale, is[i]*scale, scale); length (?) C for i 0 n-1
3 p.8 3 Java 3.6 String split 3.6.1 HTML param Graph.java Graph.html <html> <head></head> <body> <applet code="graph.class" width="200" height="200"> <param name="args" value="10 4 6 2 9 1"> <!-- --> </applet> </body> </html> Graph.java public class Graph extends JApplet {... public void init() { String[] args = getparameter("args").split(" "); int i; int n = args.length; is = new int[n]; for(i=0; i<n; i++) { is[i] = Integer.parseInt(args[i]);... // // String split java.lang.string public String[] split(string regex) (regex) Integer.parseInt init split "," "\\s+" 2 Java java.util.regex.pattern (JDKDIR)/docs/ja/api/java/util/regex/Pattern.html
3.7. 3 p.9 3.7 new new int[n] n int int C n 0 null new int[] {... int 3.7.1 HTML param ArrayIndexOutOfBoundsException : n n-1
3 p.10 3 Java 3.7.2 9:45 12:35 4:42 AddTime2.java public class AddTime2 extends JApplet { int[] t = {0,0; // 0 0 int[] addtime(int[] t1, int[] t2) { // int[] t3 = new int[2]; // 2 t3[0] = t1[0]+t2[0]; t3[1] = t1[1]+t2[1]; if (t3[1]>=60) { // t3[0]++; t3[1]-=60; return t3; // public void init() { String[] args=getparameter("args").split("\\s+"); for (String s : args) { String[] stime = s.split(":"); int[] time = new int[] { Integer.parseInt(stime[0]), Integer.parseInt(stime[1]) ; t=addtime(t, time); // addtime t time GC // g.drawstring(" "+t[0]+" "+t[1]+" ", 30, 25); AddTime2.java addtime paint init return C,... {... public C addtime new C malloc init addtime Garbage Collection, GC
3.8. 3 p.11 C free GC init for for-each 3.8 3.8.1 int 8 8 1 2 Othello.java public class Othello extends JApplet { int scale = 40; int space = 3; int[][] state = {{0,1,2,0,1,2,0,1, {2,0,1,2,0,1,2,0, {1,2,0,1,2,0,1,2, {0,1,2,0,1,2,0,1, {2,0,1,2,0,1,2,0, {1,2,0,1,2,0,1,2, {0,1,2,0,1,2,0,1, {2,0,1,2,0,1,2,0; int i,j; for (i=0; i<8; i++) { for (j=0; j<8; j++) { g.setcolor(color.green); g.fillrect(i*scale, j*scale, scale, scale); g.setcolor(color.black); g.drawrect(i*scale, j*scale, scale, scale); if (state[i][j]==1) { g.setcolor(color.white); g.filloval(i*scale+space, j*scale+space, scale-space*2, scale-space*2); else if (state[i][j]==2) { g.setcolor(color.black); g.filloval(i*scale+space, j*scale+space, scale-space*2, scale-space*2); 2 [] 2 3 C C state state[0][1] 0 {0,1,2,0,1,2,0,1 1 1 0 1 : Java 2 C 2 Java Java C 2
3 p.12 3 Java int[][] xss = {{1, {1,2, {1,2,3; 3.9 generic class JDK5.0 ArrayList, HashMap, LinkedList < > ArrayList ArrayList String ArrayList ArrayList<String> ArrayList<String> arr1 = new ArrayList<String>(); // ArrayList arr1.add("aaa"); arr1.add("bbb"); arr1.add("ccc"); // String s = arr1.get(1); // add get int, double Integer, Double int Integer ArrayList<Integer> arr2 = new ArrayList<Integer>(); // ArrayList arr2.add(123); arr2.add(456); arr2.add(789); // int i = arr2.get(1); // ArrayList<String> int add ArrayList<Integer> String get ArrayList<String> arr1 = new ArrayList<String> (); arr1.add(333); // ArrayList<Integer> arr2 = new ArrayList<Integer> ();... String t = arr2.get(2) // API E java.util.arraylist<e> : public boolean add(e e) e public E get(int index) index
3.9. 3 p.13 3.9.1 ArrayList init paint ArrayList paint for for-each Tree.java import java.util.arraylist; import static java.lang.math.*; public class Tree extends JApplet { ArrayList<int[]> data = new ArrayList<int[]>(); public void drawtree(int d, double x, double y, double r, double t) { /* d --- (x, y) --- * * r --- t --- */ double r1; if (d==0) return; data.add(new int[] {(int)x, (int)y, (int)(x+r*cos(t)), (int)(y+r*sin(t))); r1 = r; drawtree(d-1, x+r1*cos(t), y+r1*sin(t), 0.5*r, t+0.2); r1 = 0.55*r; drawtree(d-1, x+r1*cos(t), y+r1*sin(t), 0.5*r, t+1.25); r1 = 0.45*r; drawtree(d-1, x+r1*cos(t), y+r1*sin(t), 0.5*r, t-1.3); public void init() { drawtree(6, 128, 255, 128, -PI/2); g.setcolor(color.green); for(int[] pts : data) { // for-each g.drawline(pts[0], pts[1], pts[2], pts[3]);
3 p.14 3 Java 3.9.2 HashMap int String HashMap 2 1 2 HashMap<String, Color> String Color put get java.util.hashmap<k,v> : public V put(k key, V value) value key public V get(object key) key Object java.lang.object Java ColorName.java import javax.swing.japplet; import java.util.hashmap; public class ColorName extends JApplet { HashMap<String, Color> hm; String color1, color2, color3; public void init() { // http://www.colordic.org/w/ hm = new HashMap<String, Color>(); hm.put(" ", new Color(0xed1941)); hm.put(" ", new Color(0xffd400)); hm.put(" ", new Color(0x45b97c)); hm.put(" ", new Color(0x009ad6)); hm.put(" ", new Color(0x8552a1));... color1 = getparameter("color1"); color2 = getparameter("color2"); color3 = getparameter("color3"); g.setfont(new Font("Sans", Font.BOLD, 64)); g.setcolor(hm.get(color1)); g.drawstring(color1, 10, 70); g.setcolor(hm.get(color2)); g.drawstring(color2, 90, 70); g.setcolor(hm.get(color3)); g.drawstring(color3, 170, 70); 3.9.3 LinkedList if, if else, boolean, Integer.parseInt, while, for, for-each,, length, split, static, Math,,, ArrayList, HashMap, LinkedList