You are on page 1of 35

Tutorial: Java Programming

with JDK

Jinhwa Kim
School of Business
Oklahoma State University
Table of Contents (1)
Part I: How to Start
• Sample Java programs(applications vs. Applets)
• How to install Java2(SDK)
• How to write, compile, and run Java programs

Part II: Languages in Java


• Program structure in Java
• Data Types
• Expressions and Operators
• Control structures in Java(selections & repetitions)
• Methods
• Arrays
Table of Contents (2)
III. Object-oriented programming
• Classes and object
• Inheritance

IV. Java Graphics


• MS J++
• Java Forte
• IBM Visual Age

V. More Helps
• Books
• Internet sites
Part I: How to Start
Sample Java Programs
• Applications vs. Applets

How to Install Java2


• go to http://java.sun.com
• download j2sdk-1_3_0_02-win.exe
• install above into your computer
• change path = c:\jdk1.3.0_02\bin
How to write, compile, and run Java programs
• Write programs using dos editor or note pad
ex) c:\edit Welcome.java
• Compile the programs
ex) c:\javac Welcome.java
Compile produces Welcome.class
• Run the programs
ex) c:\java Welcome
c:\appletviewer Welcome.html
Part II: Languages in Java

Program Structure in Java


• Declaring variables/ initializations
• Processing
- reading inputs
- process inputs
- show output
• Exit the program
Data Types
• String for strings of letters
ex) String LastName = “Kim”;
• char for characters
ex) char alphabet = ‘A’;
• byte, short, int, and long for integer numbers
ex) int number = 3;
• float & double for real numbers
ex) double pi = 3.14159
• boolean for logical
ex) boolean check = true;
Expressions and Operators
• Expressions
- ( ), * ,/ , +, -

• Operators
- && for AND
- || for OR
- ! For NOT
- == for equal
-!= for NOT equal
Selection Statements
If statement
ex) if (A>B)
System.out.println(“A>B”);
If else statement
ex) if (A>B)
System.out.println(“A>B”);
else
System.out.println(“A<=B”);
Switch Statement ex) switch (case)
case 1: {statement; break;}
case 2: {statement;break;}
….
default: {statement;break;}
Repetitions(while, do while, and for)

An example adding all numbers between 1 to 100

1. Using While
int sum =0;
int count =0;
while (count <=1000)
{ sum = sum + count;
count = count +1; //same as count++
}
2. Using do while

int sum =0;


int count =0;
do
{ sum = sum + count;
count = count +1; //same as count++
} while (count <=1000)
3. Using for

int sum =0;


For (int count =0; cout <=100; count++)
{ sum = sum +count;
}
Introducing Methods
Method Structure
A method is a
returnvaluetype
collection of modifier methodName parameter list

statements that are public static double readDouble()


{ double d = 0.0;
grouped together try
to perform an { String str = df.readLine();
st = new StringTokenizer (str);
operation. d = new Double (st.nextToken()).doubleValue();
} catch (IOException ex)
{ System.out.println(ex); }

return d;
}
Declaring Methods
int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
Passing Parameters
void nPrintln (String message, int n)
{
for (int i=0; i<n; i++)
System.out.println(message);
}
Arrays
• datatype[] arrayname;
Example:
int[] myList;

• datatype arrayname[];
Example:
int myList[];
Creating Arrays
arrayName = new datatype[arraySize];

Example:
myList = new double[10];
Declaring and Creating
in One Step
• datatype[] arrayname = new
datatype[arraySize];
double[] myList = new double[10];

• datatype arrayname[] = new


datatype[arraySize];
double myList[] = new double[10];
Initializing Arrays
• Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = (double)i;

• Declaring, creating, initializing in one step:


double[] myList = {1.9, 2.9, 3.4, 3.5};
III. Object-oriented programming

• Objects

• Classes
Class Declaration
class Circle
{
double radius = 1.0;

double findArea()
{
return radius*radius*3.14159;
}
}
Declaring Objects
ClassName objectName;

Example:
Circle myCircle;
Creating Objects
objectName = new ClassName();

Example:
myCircle = new Circle();
Declaring/Creating Objects
in a Single Step
ClassName objectName = new
ClassName();

Example:
Circle myCircle = new Circle();
Accessing Objects
• Referencing the object’s data:
objectName.data
myCircle.radius

• Referencing the object’s method:


objectName.method
myCircle.findArea()
Constructors
Circle(double r)
{
radius = r;
}

Circle()
{
radius = 1.0;
}

myCircle = new Circle(5.0);


Modifiers
By default, the class, variable, or data can be
accessed by any class in the same package.

• public
The class, data, or method is visible to any class in any package.
• private
The data or methods can be accessed only by the declaring class.
class TestCircle
{
public static void main(String[] args)
{
Circle myCircle = new Circle();
System.out.println("The area of the circle of radius "
+ myCircle.radius + " is " + myCircle.findArea());
}
}
 
class Circle
{
double radius = 1.0;
 
double findArea()
{
return radius*radius*3.14159;
}
}
Class Inheritance :
Superclasses and Subclasses
Creating a Subclass
Creating a subclass extends properties and
methods from the superclass. You can also:
 Add new properties
 Add new methods
 Override the methods of the superclass
Using the Keyword super
The keyword super refers to the superclass
of the class in which super appears. This
keyword can be used in two ways:
• To call a superclass constructor
• To call a superclass method
An Inheritance example
public class Cylinder extends Circle
{
private double length;
  //default constructor
public Cylinder()
{
super();
length = 1.0;
}
  //constructor
public Cylinder(double r, double l)
{
super(r);
length = l;
}
  //getter method
public double getLength()
{
return length;
}
  //find cylinder volume
public double findVolume()
{
return findArea()*length;
}
}
IV. Java Graphics
• Java 2 can build graphic programming
• More user oriented Java languages are:
- MS J++
- Java Forte
- IBM Visual Ages
V. More Help
Books
Java How to Program by Deitel & Deitel
• Introduction to Java Programming by Daniel
Liang
• An Introduction to Java programming with
Java by Thomas Wu
• Easier books can be found in course.com
Websites
- http://java.sun.com
– http://java.sun.com/applets/index.html
• Many resources and free applets
• Has demo applets from J2SDK
– http://java.sun.com/jdc35
• Free registration
• Tech support, training, articles, resources, links
– http://www.developer.com
• Java directory page
• Thousands of applets and resources
– http://www.gamelan.com
• All-around Java resource
• References, games, downloads, talk to experts...

You might also like