Chapter 5: Preparing Java Programs 1 Chapter 5 Preparing Java Programs
Chapter 5Program development2 EDIT Understanding the problem Writing an algorithm Converting to code COMPILE RUN errors Result Verifying the algorithm
Chapter 5Program development3 EDIT COMPILE RUN errors Result Source code pico prog.java javac prog.java java prog
Chapter 5High-level languages4 qMachine language: computer’s native language. Add two numbers: qAssembly language: uses mnemonics. Add two numbers: ADD R1 R2 An assembly code needs to be translated to machine code.
Chapter 5High-level languages5 qHigh-level programming languages: bridging the gap between machine language and natural languages. qExamples: COBOL, Fortran, Algol, C Add two numbers (in C): Z = A + B; qCompilation: translation to machine code.
Chapter 5A Java program6 qA sample Java program // A Java program to read a number and compute and display its square. import java.io.*; class square { public static void main(String arg[]) throws IOException { int n; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); System.out.print ("Enter the number to be square: "); n = Integer.parseInt (stdin.readLine()); System.out.println ("The square of " + n + " is " + n*n); }
Chapter 5Compilation in Java7 qUse the javac command to compile: javac prog.java qProduces the object file with extension.class (eg: prog.class) qTo run object file, use the java command: java prog
Chapter 5Errors8 qCompilation errors: occur during compilation. uReason: syntax errors. uEasy to rectify. qRun-time errors: occur during execution. uReasons: logic errors, data errors, computation errors. uHarder to rectify.
Chapter 5Homework9