(Basic Theory of Information Processing) Java (eclipse ) Hello World! eclipse Java 1
3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World"); http://www.oracle.com/technetwork/java/javase/downloads/index.html Java Platform (JDK) 8u( ) Path (HelloWorld.class ) javac HelloWorld.java java HelloWorld 2
3.2 Eclipse IBM (IDE, Integrated Development Environment) ( ( ) ) Eclipse Public License Java eclipse Java, FORTRAN, C, C++, perl, PHP,... ( ) eclipse 3
3.3 Eclipse (Windows) http://mergedoc.sourceforge.jp Eclipse 3.6 Helios Full All in One (JRE ) Java Download ( 4.3 3.6) eclipse eclipse.exe eclipse eclipse eclipse.ini ( K2E ) UTF-8 -Dfile.encoding=utf-8 Linux (UTF-8 ) Mac ( ) 4
4 Java 4.1 Eclipse Hello World 1. eclipse 2. Java 3. HelloWorld 4. HelloWorld 5. 6. src 7. HelloWorld 8. HelloWorld.java 9. 10. 11. Java 12. HelloWorld 5
4.2 HelloWorld.java public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World"); { ( ) class HelloWorld { HelloWorld static public void main(string[] args) { main System.out.println("Hello World") System.out.println " " 6
4.3 ( ) ( ) // 1 /* */ /** */ javadoc html 7
// package src2; /* */ /* system.out.println("( o ;)") */ 8
/** * Javadoc * html */ public class Comment { /** main * @param args */ public static void main(string args[]) { // System.out.println(" "); /* * * */ System.out.println(" "); // 9
4.4 ( ) ( ) 10
4.5 System.out.println() System.out.println( ) System.out.println(10); 10 + "x " + 3 + " " "x 3 " (Println.java) System.out.println("x " + 3 + " "); x 3 11
4.6 x = 2; y = x + 3; y 5 System.out.println( ) x = 2; y = x + 3; System.out.println("x " + x + " y " + y + " "); x 2 y 5 12
4.7 ( ) import java.util.scanner; public class KeyInput { public static void main(string args[]){ Scanner stdin = new Scanner(System.in); System.out.println(" "); String instr = stdin.next(); // stdin.close(); System.out.println(inStr + " "); instr 13
import java.util.scanner; public class KeyAdd { public static void main(string args[]){ Scanner stdin = new Scanner(System.in); System.out.println("x + y x "); int x = stdin.nextint(); // System.out.println("y "); int y = stdin.nextint(); // stdin.close(); System.out.println(x + " + " + y + " = " + (x + y)); x y 14
4.8 (if else ) 1 10 sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; sum = 0; sum = sum + 1; sum = sum + 2; sum = sum + 3; sum = sum + 4; sum = sum + 5; sum = sum + 6; sum = sum + 7; sum = sum + 8; sum = sum + 9; sum = sum + 10; 1 10 15
1000 sum = sum + x; x 1 1000 for sum = 0; for (x = 1 ; x <= 1000 ; x = x + 1) { sum = sum + x; 1 prime ndivideded ( 2 ) diverder prime % divider prime divider diverder 1 prime prime diverder 2 prime 16
public class Prime { public static void main(string args[]){ int ndivided, divider; Scanner stdin = new Scanner(System.in); System.out.println(" "); int prime = stdin.nextint(); // ndivided = 0; for (divider = 1 ; divider <= prime ; divider = divider + 1) { if (prime % divider == 0) { System.out.println(prime +" " + divider + " "); ndivided = ndivided + 1; if (ndivided == 2) System.out.println(prime + " "); else System.out.println(prime + " "); 17
4.9 1 chkprime(int prime) true false // prime public static boolean chkprime(int prime) { int divider, ndivided; ndivided = 0; for (divider = 1 ; divider <= prime ; divider = divider + 1) { if (prime % divider == 0) { ndivided = ndivided + 1; if (ndivided == 2) return true; return false; 18
chkprime(int prime) 2 100 public class PrimeMethod { public static void main(string args[]){ int prime; for (prime = 2 ; prime <= 1000 ; prime = prime + 1) { if (chkprime(prime)) { System.out.println(prime + " "); // chkprime() 19
4.10 a[2] b[3][8] a[i] = 3 b[i][j] = 5 public class IntArray { public static void main(string args[]){ int i, j; int a[] = new int[5]; for (i = 0 ; i <= 4 ; i = i + 1) { a[i] = i * i * i; for (j = 0 ; j <= 4 ; j = j + 1) { System.out.println("a[" + j + "] = " + a[j]); a = (0, 1, 8, 27, 64) 20
4.11 = + (StudentData) ( ) StdNo name pjapa pmath pengl pavel ( ) print() calave() ( ) 21
public class StudentData { // ( ) int stdno; String name; int pjapa, pmath, pengl; double pave; // ( ) StudentData(int stdno, String name, int pjapa, int pmath, int pengl) { this.stdno = stdno; this.name = name; this.pjapa = pjapa; this.pmath = pmath; this.pengl = pengl; 22
// void print() { System.out.println(stdNo + " " + name + " " + pjapa + " " + pmath + " " + pengl + " " + pave + " "); // void calave() { pave = (pjapa + pmath + pengl) / 3.0; 23
StudentData public class StudentEx { public static void main(string args[]){ StudentData yamashita = new StudentData(2, " ", 80, 90, 70); StudentData kawakami = new StudentData(3, " ", 90, 70, 90); yamashita.print(); kawakami.print(); yamashita.calave(); yamashita.print(); 24
4.12 KeyInput KeyAdd 1 10 ( 2 ) System.out.println("1 + 2 = " + 1 + 2) System.out.println("1 + 2 = " + (1 + 2)) 4.13 Excel 25