Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming (Chapter 1). Java Programming Classes, Types, and Objects.

Similar presentations


Presentation on theme: "Java Programming (Chapter 1). Java Programming Classes, Types, and Objects."— Presentation transcript:

1 Java Programming (Chapter 1)

2 Java Programming

3 Classes, Types, and Objects

4

5 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++;} }

6

7

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

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26 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 ’ to compile your program. 5. Using ‘java ’ to run your program.

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

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

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


Download ppt "Java Programming (Chapter 1). Java Programming Classes, Types, and Objects."

Similar presentations


Ads by Google