60 likes | 153 Views
CPSC 233 Tutorial. Xin Liu 2010/01/19. Basic stuffs. Variable Declare variables before using Build-in types: byte, short int , long, float, double, char, boolean , String eg . int x ; int x = 8; Constants final int SIZE = 100; Output
E N D
CPSC 233 Tutorial Xin Liu 2010/01/19
Basic stuffs • Variable • Declare variables before using • Build-in types: byte, short int, long, float, double, char, boolean, String • eg. intx; intx = 8; • Constants • final int SIZE = 100; • Output • System.out.print(<string or variable name one> + <string or variable name two>..); • System.out.println(<string or variable name one> + <string or variable name two>..); • Escape sequence: \t, \r, \n, \”, \\ • Input • import java.util.Scanner; • Scanner in = new Scanner (System.in); • intx = in .nextInt (); • Mostly commonly used methods • nextInt(), nextLong(), nextFloat(), nextDouble(), nextLine()
Basic structures • Conditional • if, if else, nested if • switch case default • Iteration • for, while, do while
Make program run • Source code/Java program [compiler]byte-code / object program [interpreter] machine code • Step 1: Code • Use any text editor • Eclipse is a famous Integrated Development Environment (IDE) program • Step 2: Compile • javacexample.javaexample.class • Step 3: Run • java example
A hello world program • A very simple example public class HelloWorld{ public static void main(String[] args) { System.out.println("Hello world."); } }
A sorting program • Algorithm: • ☐☐☐☐☐☐☐☐ • do it in n – 1 pass for n numbers • for the i pass, make the ith number the smallest among the numbers [i … n]