The Wings Road

UncategorizedOctober 4, 2007 6:34 pm

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectInputOutputStream {
   
    public static void main(String[] args) throws IOException, ClassNotFoundException {
       
        //writing object to a file
        ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream("objout.txt"));
       
        Student s = new Student();
        s.setId(100);
        s.setName("James Go");
        s.setCourse("Information Technology");
        s.setAge(18);
       
        oout.writeObject(s);
        oout.flush();
       
        //reading object from a file
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("objout.txt"));
       
        Student student = (Student) in.readObject();
        System.out.println("Student ID :" + s.getId());
        System.out.println("Name :" + s.getName());
        System.out.println("Age :" + s.getAge());
        System.out.println("Course :" + s.getCourse());
       
    }
   

}

#########################################

import java.io.Serializable;

public class Student implements Serializable {
   private int id;
   private String name;
   private String course;
   private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

Uncategorized 6:33 pm

import java.io.*;

public class DataInputOutputStream {
   
    public static void main(String[] args) throws IOException {
       
        // write the data out
        DataOutputStream out = new DataOutputStream(new
                FileOutputStream("invoice"));

       
        double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
        int[] units = { 12, 8, 13, 29, 50 };
        String[] descs = { "Java T-shirt",
        "Java Mug",
        "Duke Juggling Dolls",
        "Java Pin",
        "Java Key Chain" };
       
        for (int i = 0; i < prices.length; i ++) {
            out.writeDouble(prices[i]);
            out.writeChar(’\t’);
            out.writeInt(units[i]);
            out.writeChar(’\t’);
            out.writeChars(descs[i]);
            out.writeChar(’\n’);

        }
        out.close();
       
        // read it in again
        DataInputStream in = new DataInputStream(new
                FileInputStream("invoice"));

       
        double price;
        int unit;
        StringBuffer desc;
        double total = 0.0;
       
        String lineSepString = System.getProperty("line.separator");
        char lineSep = lineSepString.charAt(lineSepString.length()-1);
       
        try {
            while (true) {
                price = in.readDouble();
                in.readChar();       // throws out the tab
                unit = in.readInt();
                in.readChar();       // throws out the tab
                char chr;
                desc = new StringBuffer(20);
                while ((chr = in.readChar()) != lineSep)
                    desc.append(chr);
                System.out.println("You’ve ordered " +
                        unit + " units of " +
                        desc + " at $" + price);

                total = total + unit * price;
            }
        } catch (EOFException e) {
        }
        System.out.println("For a TOTAL of: $" + total);
        in.close();
    }
}

UncategorizedOctober 2, 2007 6:22 pm

/*
 * FileReaderWriter.java
 *
 * Created on October 2, 2007, 6:16 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.io.*;
/**
 *
 * @author Admin
 */
public class FileReaderWriter {
   
    /** Creates a new instance of FileReaderWriter */
    public FileReaderWriter() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
                File inputFile = new File("farrago.txt");
        File outputFile = new File("outagain.txt");
       
        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        int c;
       
        while ((c = in.read()) != -1){
            System.out.println(c);
            out.write(c);
        }
       
        System.out.println("FileReader is used to read a file and FileWriter is used for writing.");
       
        in.close();
        out.close();

    }
   
}

Uncategorized 6:14 pm

/*
 * FileInputOutputStream.java
 *
 * Created on October 2, 2007, 6:03 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.io.*;
/**
 *
 * @author Admin
 */
public class FileInputOutputStream {
   
    /** Creates a new instance of FileInputOutputStream */
    public FileInputOutputStream() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        File inputFile = new File("farrago.txt");
        File outputFile = new File("outagain.txt");
       
        FileInputStream in = new FileInputStream(inputFile);
        FileOutputStream out = new FileOutputStream(outputFile);
        int c;
       
        while ((c = in.read()) != -1){
            System.out.println(c);
            out.write(c);
                    }
       
        System.out.println("FileInputStream is used to read a file and FileOutPutStream is used for writing.");
       
        in.close();
        out.close();

    }
   
}

Uncategorized 11:00 am

#include <iostream>
#include <conio.h>
using namespace std;

struct node
{
    int num;
    node *lLink;
    node *rLink;
};
void createNode(node *&root);
void connectNode(node *newNode,node *current);
void preOrder(node *current);
void menu(int& choice);

main()
{
    int choice;
    int num;
    node *root;            
    node *parent;          
    node *current;
    node *sNode;
    node *temp;            
    root = NULL;
    do
    {
        menu(choice);
        switch(choice)
        {
            case 1:
                createNode(root);
                getch();
                break;
            case 2:
                cout<<"Pre-order Traversal"<<endl<<endl;
                preOrder(root);
                getch();
                break;
        }
       
    }while(choice!=24);
}
void menu(int& choice)
{
    system("cls");
    cout<<"[1] Add New Node"<<endl
        <<"[2] Display"<<endl;
     cout<<"Enter your choice:";
    cin>>choice;
}
void createNode(node* &root)
{
    node *newNode;
    int num;
   
    newNode=new node;
    newNode->lLink=NULL;
    newNode->rLink=NULL;
    cout<<"Enter number:";
    cin>>num;
    newNode->num=num;
   
    if(root==NULL){root=newNode;cout<<"root";}
    else connectNode(newNode,root);
}
void connectNode(node *newNode,node *current)
{
    if(newNode->num > current->num)
    {
        if(current->rLink!=NULL)
        {
            cout<<"Move to the right of "<<current->num<<endl;
            connectNode(newNode,current->rLink);
        }
        else
        {
            current->rLink=newNode;
            cout<<newNode->num<<" is connected to the right of "<<current->num<<endl;
        }
    }
    else
    {
        if(current->lLink!=NULL)
        {
            cout<<"Move to the left of "<<current->num<<endl;
            connectNode(newNode,current->lLink);
        }   
        else
        {
            current->lLink=newNode;
            cout<<newNode->num<<" is connected to the left of "<<current->num<<endl;
        }   
    }
}
void preOrder(node* current)
{
    if(current->lLink!=NULL)
        cout<<current->num<<" has a left child of "<<current->lLink->num<<endl;
    else cout<<current->num<<" has no left child."<<endl;
   
    if(current->rLink!=NULL)
        cout<<current->num<<" has a right child of "<<current->rLink->num<<endl;
    else cout<<current->num<<" has no right child."<<endl;
    //cout<<current->num <<" ";
   
   
    if(current->lLink!=NULL)preOrder(current->lLink);
    if(current->rLink!=NULL)preOrder(current->rLink);
}