290 likes | 309 Views
Java Programming (Chapter 1). Java Programming. Classes, Types, and Objects. public class Factorial3 { Static long[] table = new long[21]; Static {table[0] = 1;} //factorial of 0 is 1 static int last = 0; public static long factorial(int x) { while (last < x) {
E N D
Java Programming (Chapter 1)
public class Factorial3 { • Static long[] table = new long[21]; • Static {table[0] = 1;} //factorial of 0 is 1 • static int last = 0; • public static long factorial(int x) { • while (last < x) { • table [last + 1] = table[last]*(last + 1); • last++;} • } • }
Package A package is a group of classes and interfaces. You can assign the classes and interfaces in a source file to a particular package by using a package statement with the following syntax: package <packageName> Example: package engineering; package engineering.electrical.signals; • If a package statement is used, it must appear as the first statement in that source file. • If a source file does not contain a package statement, its classes and interface are • placed in a default package – normally, the current fold.
About how to create Java program Choice 1: 1. Using NotePad to produce a .java file. 2. Invoke ‘Commad Prompt’. 3. Using ‘cd’ command to go to the fold where you put your program. 4. Using ‘javac <file name>’ to compile your program. 5. Using ‘java <file name without .java>’ to run your program.
public class a1q2 { /** * main method * * prompts input and calls the recursive method */ public static void main(String[] args) { int m = Integer.parseInt(args[0]); System.out.println("result of fn(" + m + ") is " + computeFN(m)); } /** * computeFN - the definition of the function * * input: int * output: int * calculate f(N) = 3*f(N-3) + 4*f(N-2) - 5*f(N-1) where f(0) = 1, f(1) = 1, f(2) = 3. */ public static int computeFN(int n) { if(n<=0) return 1; else if(n==1) return 1; else if(n==2) return 3; else return 3 * computeFN(n-3) + 4 * computeFN(n-2) - 5 * computeFN(n-1); } }
About how to create Java programs Choice 2: 1. Using ‘JCreator’ to create Java code, compile it and then run it. 2. If your program is to run with an input of integers, you need arrange a statement in the main method, before any other statements as below: int m = (int)new Integer(JOptionPane.showInputDialog("Enter an integer")); (see the following example.)
import javax.swing.JOptionPane; public class a1q2 { /** * main method * * prompts input and calls the recursive method */ public static void main(String[] args) { //input dialog int m = (int)new Integer(JOptionPane.showInputDialog("Enter an integer")); //calculate f(N) = 3*f(N-3) + 4*f(N-2) - 5*f(N-1) where f(0) = 1, f(1) = 1, f(2) = 3. System.out.println("result of fn(" + m + ") is " + computeFN(m)); } /** * computeFN - the definition of the function * * input: int * output: int */ public static int computeFN(int n) { if(n<=0) return 1; else if(n==1) return 1; else if(n==2) return 3; else return 3 * computeFN(n-3) + 4 * computeFN(n-2) - 5 * computeFN(n-1); } } Not forget these two statements.