Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.

Similar presentations


Presentation on theme: "JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked."— Presentation transcript:

1 JAVA PROGRAMMING BASICS CHAPTER 2

2 History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked for Sun Microsystems. The purpose is to draft and plan for the future computer program that can be executed on any machine. Begin with the name Oak programming language that was developed in the early 90’s by the Sun Micro system. In Mac 1995, Java was distributed to public freely for testing and then Java Development Kit was launched in Mei 1995 and was downloaded 10,000 times/day.

3 Java Development Environment Java programming consists of the compiler and interpreter. Java source file is compiled into byte code and then interpreted to enable it to execute at command prompt, applet viewer or web browser. Java Compiler Java Source CodeJava byte code Pentium Java Interpreter Power PC Java Interpreter SPARC Java Interpreter

4 Basic Component of Java Application Program Java program may contain the following basic component:  Comments  Blocks {...}  Classes  Methods  The main( ) method  Statements  Reserved word  Modifiers

5 Example //My first Java program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } class block method block comment main method

6 Output Statement Output statement is the statement used to display information on computer screen. In Java, the most common Java package used to handle much different input and output activity is java.io. The most commonly used IO objects are:  System.in (input stream)  System.out (output stream for normal results)  System.err (output stream for error messages)

7 Output Statement Generally System.in is connected to the keyboard and inputs while System.out and System.err are connected to the monitor and output character data. The Standard Output Stream: System.out

8 Output Statement Example:  System.out.println(“Welcome to Java class”);  System.out.println(“She says \”Learning Java is fun\””);  String Name=”Hamidah”;  int Age=20;  char ch=’a’;  System.out.println(Age);  System.out.println(++ch);  System.out.println(“Age is ” + Age + “\nName is “ + Name);

9 JOptionPane To produce a dialogue box that enable user to display output. Syntax: JOptionPane.showMessageDialog(null, "Hello World !");

10 Input Statement (Keyboard) There are a number of ways to accept input from the user. We will look at three of them:  Buffered Reader  Scanner  JOptionPane

11 Buffered Reader Buffered Reader (java.io) In the System class, the object, System.in, represents the keyboard (unless you specify otherwise), and is used to read user data (entered using the keyboard) into your program. The class BufferedReader reads data from standard input (System.in, the keyboard).

12 Buffered Reader Step 1: Create an InputStreamReader object Step 2: Create a BufferedReader object Step 3: Get input from user Step 4: Convert character data to numeric data (if required)

13 Scanner Read input using Scanner Declare and Create Scanner Object Scanner input=new Scanner(System.in); Read data from keyboard into specific type of variable  int Number=input.nextInt( );  float Price=input.nextFloat( );  string name=input.nextLine( );  double Dec=input.nextDouble( );

14 JOptionPane To produce a dialogue box that enable user to input a value, we can include codes such as String x=JOptionPane.showInputDialog("Type your age");

15 Packages The use of packaging is to avoid class name collision especially when the class name that we use is the same as another. In java, some classes are created in package. For an example: java.util is the name of a package that contains a class named Vector (java.util.Vector). Therefore there shouldn’t be any conflict if we create another Vector class in our program.

16 Packages The purpose of creating package is: Easy to mantain/manage files Easy to organize classes Easy for collaboration among developers

17 How to create package? Create a directory for a package In a new source file, write declaration of package on top of source file. Define the class to include in the package Save it in the directory. Packages

18 Example package Shape; public class Triangle { public static void main(String[ ] args) { System.out.println("This is a class named Triangle"); } Note: This class Triangle is saved as Triangle.java in folder C:\Shape

19 Setting up the CLASSPATH CLASSPATH is set to point to.(dot) and C:\directory C:\jdk1.7.0\bin>set CLASSPATH=.;c:\; Compile file Triangle.java C:\jdk1.7.0\bin> javac c:\Shape\Triangle.java When running a package class file we need to specify the class name by including its directory and separate them with a dot. Example: C:\Shape>java Shape.Triangle

20 Create a class named Triangle. On top of class, write package Shape. Save the file in a folder named Shape. Compile the file normally. Run the file as follows: c:\Shape>java Shape.Triangle Setting up the CLASSPATH


Download ppt "JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked."

Similar presentations


Ads by Google