1 / 29

Java Programming (Chapter 1)

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) {

rivenbark
Download Presentation

Java Programming (Chapter 1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming (Chapter 1)

  2. Java Programming

  3. Classes, Types, and Objects

  4. 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++;} • } • }

  5. 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.

  6. 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.

  7. 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); } }

  8. 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.)

  9. 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.

More Related