Download CHAPTER 2 PART 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CHAPTER 2
JAVA PROGRAMMING BASICS
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.
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.
Pentium Java Interpreter
Java Source Code
Java Compiler
Power PC Java Interpreter
Java byte code
SPARC Java Interpreter
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
Example
//My first Java program
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World");
}
method block
}
comment
main method
class block
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)
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
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);
JOptionPane
JOptionPane
• To produce a dialogue box that enable user to display
output.
• Syntax:
JOptionPane.showMessageDialog(null, "Hello World !");
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
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).
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)
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( );
JOptionPane
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");
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.
Packages
• The purpose of creating package is:
 Easy to mantain/manage files
 Easy to organize classes
 Easy for collaboration among developers
Packages
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.
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
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
Setting up the CLASSPATH
• 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