Professional Documents
Culture Documents
/*
My very first java program.
*/
public class HelloWorld {
public static void main(String[] args) {
// print message on screen
System.out.println(“Hello World”);
}
}
main method
– Called when java HelloWord is run
Argument: Array args hold command-line arguments
– java HelloWord is colorful
– args[0] = is
– args[1] = colorful
Methods
public class HelloWorld {
public static void printToScreen(String
message) {
System.out.println(message);
}
public static void main(String[] args) {
printToScreen(“Hello World”);
printToScreen(“My First Java Program”);
}
}
public : method can be called from outside this class
static : only one copy for all objects
void : return type (what the method returns)
printToScreen : name of method
String message : type and name of arguments passed in
Learning Through Errors
Insignificant errors
Compile time errors (syntax)
Run time errors (syntax)
Semantic errors (bugs)
Insignificant Errors
http://java.sun.com/docs/books/tutor
ial/java/index.html
Variables, Operators, Control
Statements …..
Primitive Reference
boolean (true, objects
false) arrays
char (‘a’, ‘z’) String (very basic)
int (1, 100, -8)
float[32bit]
(3.1415)
double[64bit]
(3.1415)
Others:
byte, short, long
Control Statements
while (condition) { do {
// body of loop // body of loop
} } while (condition);
int x = 0;
while (x < 5) {
System.out.println(“loop no = ” + x);
x++;
}
if statement
if (x == 1)
if (condition) { System.out.println(“x =
// statements 1”);
} else if (condition) { else if (x == 2) {
// more System.out.println(“x =
statements 2”);
} else { } else
// else statements System.out.println(“x >
2”);
}
switch statement
switch (variable) { int month;
case value1: switch (month) {
// body case 1:
break; System.out.println(“Jan”);
case value2: break;
// body case 2:
break; System.out.println(“Feb”);
default: break;
// body …
} default:
System.out.println(“N/A”);
}
Object Oriented Programming
Concepts with Java
An object is a software bundle of related variables and
methods. Software objects are often used to model real-
world objects you find in everyday life
Static methods
No need to be associated with an object
System.out.println(“a”);
Math.random();
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
}
}
Creating Packages
package graphics;
java.lang
String, Type wrappers and other classes and
interfaces that are fundamental to all java
programs.
Automatically imported to all programs
java.util
java.io
networking
Applets
AWT
…
Interfaces
Capturing similarities among unrelated classes
without artificially forcing a class relationship
String s1 = “hello”;
String s2 = s1.substring(1, 4);
System.out.println(s2); // “ell”
System.out.println(s1.length()); //5
// to check equality of strings
if (s1.equals(s2)) doTrue();
else doFalse();
Defined in java.util
Source/Destination
a file, memory, a socket
etc.
Reading
open a stream Character Stream and
while more information Byte Stream
read information
close the stream
Files and Filters
Wrapping a Stream
Writing
Tutorial
open a stream http://java.sun.com/docs/books/tut
while more information orial/essential/io/overview.html
write information
close the stream
Character Streams
Examples of I/O Streams
Type of I/O Streams Description
import java.io.*;
import java.util.*;
class InputDemo
{
public static void main(String args[])
throws IOException
{
String inputFile;
}
/* This program demonstrates
1. How to read input from the console
2. Reading data from an ASCII file
3. Writting an ASCII file*/
import java.io.*;
import java.util.*;
class ioExample
{
public static void main(String args[])
throws IOException
{
// The path must be set to the ASCII data file
FileReader fin;
FileWriter fout;
String inputFile, outputFile, s, insertString;
String month, year, name;
float percDA, DA, HRA, Deduction, Gross, Net;
int basic;
}catch(FileNotFoundException e)
{
System.out.println("Error opening input file!!");
return;
}
fout = new FileWriter(outputFile);
s = brin.readLine();
StringTokenizer st = new StringTokenizer(s);
month = st.nextToken();
year = st.nextToken();
percDA = Float.parseFloat(st.nextToken());
insertString = "Pay Slip for "+month+" "+year;
fout.write(insertString);
fin.close();
fout.close();
}catch(ArrayIndexOutOfBoundsException e)
{System.out.println("Exception"+e);
}
// While Loop
while((s = brin.readLine()) != null)
{
System.out.println(s);
StringTokenizer st1 = new StringTokenizer(s);
name = st1.nextToken();
basic = Integer.parseInt(st1.nextToken());
HRA = Float.parseFloat(st1.nextToken());
Deduction = Float.parseFloat(st1.nextToken());
DA = basic*percDA/100;
Gross =basic+DA+HRA;
Net = Gross - Deduction;
fout.write(insertString);
}
Sample Input File