Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
dbm041905
pg.712,713,714 -working on Java- database
this is on final
look at some java program and try to understand them and demo them in the class.
if you can run it you get point
-Reading data in java is more difficult than C or C++
-they are both identical
-even the manin program is inside the applet
-applet(little application for internet) and application (C++ main function) are two ---important parts in JAVA
-class is a prototype or template
-object is exactly what you use.
-In Java you should make your own class(cin and cout) that is why makes it more difficult and not friendly
- in Java you hahve two steps – it is an interpretive language- it should be translated and then run- Java program should be compiled (line by line- statement by statement )and then interpret it(class)
If the name of the programe is database.java
Compile it database.class
Then java database.class- that interpret and run it for you.
Search, add, delete, insert are the most important functions
import java.io.*;
public class Database
{ // beginning the applet
private static final int MAX = 100;
private Cin cin = new Cin();
private String name[] = new String[MAX];
private double hourlyrate[] = new double[MAX];
private double hoursworked[] = new double[MAX];
private double grosspay[] = new double[MAX];
private String searchName;
private String fileName;
private int n = 0;
public void load() throws IOException{ //beginning of LOAD (the first function)
try{ Reader fi = new BufferedReader( // you have to make your own cin
new InputStreamReader(
new FileInputStream("employee.dat")));
StreamTokenizer fin = new StreamTokenizer(fi);
int tokenType;
tokenType = fin.nextToken();
while((tokenType != StreamTokenizer.TT_EOF)){
name[n] = fin.sval;
fin.nextToken();
hourlyrate[n] = fin.nval;
fin.nextToken();
hoursworked[n] = fin.nval;
grosspay[n] = hoursworked[n] * hourlyrate[n];
tokenType = fin.nextToken();
n++; }//WHILE
}//TRY
catch(IOException ex){ System.out.println(ex); }//CATCH
}//LOAD // end of LOAD (the first function) – get the data and store it in array – make us able to do all the changes to it.
public void insert(){
System.out.print("Enter the employee name: "); //insert the database- gets the employee name
name[n] = cin.readString();
System.out.print("What is employee hourly rate? ");
hourlyrate[n] = cin.readDouble();
System.out.print("How many hours did the employee work? ");
hoursworked[n] = cin.readDouble();
grosspay[n] = hoursworked[n] * hourlyrate[n];
n++; }//INSERT
public void display(){
for ( int i = 0; i < n; i++ ){
if(name[i]!=""){
System.out.println( name[i] + " " + grosspay[i] );}//IF //just displays name and gross pay
}//FOR
}//DISPLAY
public boolean search(){
what strategy is there in array when you remove a data? The information is deleted but not the storage.it will be blank
System.out.print("Enter the search name: ");//beginning the search function
String searchName = cin.readString();
for(int i = 0; i < n; i++){
if ( searchName.equalsIgnoreCase(name[i])){
System.out.println("Found: "+name[i])
System.out.println("Grosspay: "+grosspay[i]);
//when you type a name you get the name and grosspay
return true; }//IF
}//FOR
System.out.println("Name not found");
return true;
}//SEARCH
public boolean modify(){ // beginning the modify function
System.out.print("Enter the search name: ");
String searchName = cin.readString();
for(int i = 0; i < n; i++){
if ( searchName.equalsIgnoreCase(name[i])){
System.out.print("What is employee hourly rate? ");
hourlyrate[i] = cin.readDouble();
System.out.print("How many hours did the employee work?");
hoursworked[i] = cin.readDouble();
grosspay[i] = hoursworked[i] * hourlyrate[i];
return true; }//IF
}//FOR
System.out.println("Name not found");
return false;
}//MODIFY
public boolean remove(){ // beginning of the remove function
System.out.print("Enter the search name: ");
String searchName = cin.readString();
for(int i = 0; i < n; i++){
if ( searchName.equalsIgnoreCase(name[i])){
name[i] = "";
return true; }//IF
}//FOR
System.out.println("Name not found");
return false;
}//REMOVE
public void quit() throws IOException{
this.store();
System.out.println("Thank you");
System.exit(0);
}//QUIT
public void store() throws IOException{
try{
FileOutputStream out = new FileOutputStream("employee.dat");
PrintStream ps = new PrintStream(out);
for(int i = 0; i < n; i++){
if ( name[i] != "" )
ps.println( name[i] + "\t" + hourlyrate[i] +
"\t" + hoursworked[i] );
}//FOR
out.close(); }//TRY
catch( IOException ex ){
System.out.println(ex); }//CATCH
}//STORE
public static void main( String args[] ) throws IOException{
Database db = new Database();
Cin read = new Cin();
db.load();
int choice = 0;
do{System.out.println("\t1-Insert"); // print order
System.out.println("\t2-Display");
System.out.println("\t3-Search");
System.out.println("\t4-Modify");
System.out.println("\t5-Remove");
System.out.println("\t6-Quit");
choice = read.readInt(); //cin in C++- you should do it ourselves manually
if( choice == 1 ) db.insert(); // this is the beginning of the menue
else if( choice == 2 ) db.display();// it means: if choice is 2 call display
else if( choice == 3 ) db.search();
else if( choice == 4 ) db.modify();
else if ( choice == 5 ) db.remove();
else if ( choice == 6 ) db.quit();
else System.out.println("Enter correct number");
} while(true);
}//MAIN
}//DATABASE // end of applet
This is a good site to learn Java:
http://www.ibiblio.org/javafaq/javatutorial.html#xtocid45973
// This is the Hello World program in Java
class HelloWorld {
public static void main (String args[]) {
/* Now let's print the line Hello World */
System.out.println("Hello World");
// This is the Hello Rusty program in Java
class HelloRusty {
public static void main (String args[]) {
// You may feel free to replace "Rusty" with your own name
String name = "Rusty";
/* Now let's say hello */
System.out.print("Hello ");
System.out.println(name);
}
}
There is no main in applet- there is no memorization too.
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length > 0) {
System.out.println(args[0]);
}
}
}
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length > 0) {
System.out.println(args[0]);
}
if (args.length <= 0) {
System.out.println("whoever you are");
}
}
}
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length == 0) {
System.out.print("whoever you are");
}
else if (args.length == 1) {
System.out.println(args[0]);
}
else if (args.length == 2) {
System.out.print(args[0]);
System.out.print(" ");
System.out.print(args[1]);
}
else if (args.length == 3) {
System.out.print(args[0]);
System.out.print(" ");
System.out.print(args[1]);
System.out.print(" ");
System.out.print(args[2]);
}
else if (args.length == 4) {
System.out.print(args[0]);
System.out.print(" ");
System.out.print(args[1]);
System.out.print(" ");
System.out.print(args[2]);
System.out.print(" ");
System.out.print(args[3]);
}
else {
System.out.print(args[0]);
System.out.print(" ");
System.out.print(args[1]);
System.out.print(" ");
System.out.print(args[2]);
System.out.print(" ");
System.out.print(args[3]);
System.out.print(" and all the rest!");
}
System.out.println();
}
}
// Print a Fahrenheit to Celsius table
class FahrToCelsius {
public static void main (String args[]) {
int fahr, celsius;
int lower, upper, step;
lower = 0; // lower limit of temperature table
upper = 300; // upper limit of temperature table
step = 20; // step size
fahr = lower;
while (fahr <= upper) { // while loop begins here
celsius = 5 * (fahr-32) / 9;
System.out.print(fahr);
System.out.print(" ");
System.out.println(celsius);
fahr = fahr + step;
} // while loop ends here
} // main ends here
} //FahrToCelsius ends here
// Print a more accurate Fahrenheit to Celsius table
class FahrToCelsius {
public static void main (String args[]) {
double fahr, celsius;
double lower, upper, step;
lower = 0.0; // lower limit of temperature table
upper = 300.0; // upper limit of temperature table
step = 20.0; // step size
fahr = lower;
while (fahr <= upper) { // while loop begins here
celsius = 5.0 * (fahr-32.0) / 9.0;
System.out.print(fahr);
System.out.print(" ");
System.out.println(celsius);
fahr = fahr + step;
} // while loop ends here
} // main ends here
} //FahrToCelsius ends here
Why people use a lot of applet? Because it runs on internet.
\