// This program demonstrate how to write multiple bojects to a file and reading multiple objects from
// a file using Object Serilization
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.OutputStream;
//import java.io.BinaryWriteHelper;
import java.util.Scanner;
// class Student
class Student implements Serializable
{
String name;
int age;
String clas;
String dob;
float cgp;
}
public class PS_Task3
{
static ArrayList al=new ArrayList();
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
//storeArrayList();
// This try block will write multiple objects to a file
ObjectOutputStream oo=null;
try
{
File file1 = new File ("student.bin");
if (!file1.exists())
{
FileOutputStream fo=new FileOutputStream("student.bin");
oo=new ObjectOutputStream(fo);
}
else
{
oo = new AppendableObjectOutputStream (new FileOutputStream ("student.bin", true));
//FileOutputStream fo=new FileOutputStream("student.bin",true);
//oo=new ObjectOutputStream(fo);
}
System.out.println("Please enter n how many student details you want to enter");
n=sc.nextInt();
Student s;
for (int i=0;i
Tuesday, 21 February 2012
Saturday, 11 February 2012
Task 1 Description:Create a class LIST which represents a generalized list (By a sequential implementation) where insertions and deletions can be done arbitrarily at any point in the list. Implement all basic operations on it. (Insert, Delete, Isempty, Display etc). Specialize this class to represent STACK data structure (By inheriting the List class) and implement all basic operations (push, pop, Isempty, Display), on it
import java.util.ArrayList;
import java.util.Scanner;
class LIST // class List
{
ArrayList Al;
LIST()
{
Al=new ArrayList();
}
public boolean push(Object ele) // Stack push function
{
Al.add(ele);
System.out.println("The added element is "+ele);
return true;
}
public boolean isEmpty() // Stack is empty function
{
if(size()==-1)
return true;
else
return false;
}
public Object pop() // Stack pop function
{
String s="Sorry Stack is empty";
if(size()==-1)
return s;
else
return Al.remove(Al.size()-1);
}
public int size() // Stack size function
{
int n=Al.size();
if(n==0)
return -1;
else
return n;
}
public void display() // Stack display function
{
int n=Al.size();
if(Al.size()!=-1)
{
for(int i=0;i
{
System.out.println(Al.get(n-1));
n=n-1;
}
}
else
System.out.println("Stack is empty");
}
}
class PS_Stack extends LIST // class Main
{
public static void main(String[] args)
{
LIST l=new LIST();
int n;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("\t*************************************************\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \tPlease choose your Option: \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t1 to push element in to Stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t2 to check Stack is empty \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t3 to Pop element from stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t4 to get size of the stack \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t5 to dipslay stack elements \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t6 to exit\t\t\t\t*\n\t*\t\t\t\t\t\t*");
System.out.println("\t*************************************************");
n=sc.nextInt();
switch (n)
{
case 1:
Object ole;
System.out.println("Enter an element to push in to stack :");
ole=sc.nextInt();
l.push(ole);
break;
case 2:
System.out.println(l.isEmpty());
break;
case 3:
System.out.println(l.pop());
break;
case 4:
System.out.println(l.size());
break;
case 5:
l.display();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Choose correct option");
}
}
}// End of main
}
import java.util.Scanner;
class LIST // class List
{
ArrayList Al;
LIST()
{
Al=new ArrayList();
}
public boolean push(Object ele) // Stack push function
{
Al.add(ele);
System.out.println("The added element is "+ele);
return true;
}
public boolean isEmpty() // Stack is empty function
{
if(size()==-1)
return true;
else
return false;
}
public Object pop() // Stack pop function
{
String s="Sorry Stack is empty";
if(size()==-1)
return s;
else
return Al.remove(Al.size()-1);
}
public int size() // Stack size function
{
int n=Al.size();
if(n==0)
return -1;
else
return n;
}
public void display() // Stack display function
{
int n=Al.size();
if(Al.size()!=-1)
{
for(int i=0;i
System.out.println(Al.get(n-1));
n=n-1;
}
}
else
System.out.println("Stack is empty");
}
}
class PS_Stack extends LIST // class Main
{
public static void main(String[] args)
{
LIST l=new LIST();
int n;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("\t*************************************************\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \tPlease choose your Option: \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t1 to push element in to Stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t2 to check Stack is empty \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t3 to Pop element from stack\t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t4 to get size of the stack \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t5 to dipslay stack elements \t\t*\n\t* \t\t\t\t\t\t*");
System.out.println("\t* \t6 to exit\t\t\t\t*\n\t*\t\t\t\t\t\t*");
System.out.println("\t*************************************************");
n=sc.nextInt();
switch (n)
{
case 1:
Object ole;
System.out.println("Enter an element to push in to stack :");
ole=sc.nextInt();
l.push(ole);
break;
case 2:
System.out.println(l.isEmpty());
break;
case 3:
System.out.println(l.pop());
break;
case 4:
System.out.println(l.size());
break;
case 5:
l.display();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Choose correct option");
}
}
}// End of main
}
Saturday, 4 February 2012
1. Write a C++ program to implement a complex class that contains two data members to store the real and imaginary parts of a complex number. Include member functions to initialize the data members to display the complex objects in the form ‘a+ib’. Use constructor and destructor for data members. Create a menu for the operations. Perform the following operations 1. Addition (use operator overloading) 2. Subtraction(use operator overloading) 3. Multiplication (use operator overloading) 4. Division Store the result in third object in the form of complex number. 2.
// Jayachandra
// addition,substraction,division and multiplication of two complex numbers
#include
using namespace std;
class Complex
{
public:
double getVolume()
{
cout<real + b.real;
com.imag = this->imag + b.imag;
return com;
}
// substraction
Complex operator-(const Complex& b)
{
Complex com;
com.real = this->real - b.real;
com.imag = this->imag - b.imag;
return com;
}
// multiplication
Complex operator*(const Complex& b)
{
Complex com;
com.real = this->real * b.real;
com.imag = this->imag * b.imag;
return com;
}
// division
Complex operator/(const Complex& b)
{
Complex com;
com.real = this->real / b.real;
com.imag = this->imag / b.imag;
return com;
}
private:
double real;
double imag;
};
// Main function for the program
int main( )
{
int n;
double i,r;
Complex Complex1;
Complex Complex2;
Complex Complex3;
cout<<"Plase enter real number for Complex1"<>r;
cout<<"Plase enter imaginary number Complex1"<>i;
Complex1.real_image(r,i);
cout<<"Plase enter real number for Complex2"<>r;
cout<<"Plase enter imaginary number Complex2"<>i;
Complex2.real_image(r,i);
Complex3 = Complex1 + Complex2;
for(;;)
{
cout<<"*****************************************\n*\t\t\t\t\t*"<>n;
switch(n)
{
case 1:
Complex3 = Complex1 + Complex2;
Complex3.getVolume();
break;
case 2:
Complex3 = Complex1 - Complex2;
Complex3.getVolume();
break;
case 3:
Complex3 = Complex1 * Complex2;
Complex3.getVolume();
break;
case 4:
Complex3 = Complex1 / Complex2;
Complex3.getVolume();
break;
case 5:
exit(0);
default:
cout<<"please choose correct option"<
// addition,substraction,division and multiplication of two complex numbers
#include
using namespace std;
class Complex
{
public:
double getVolume()
{
cout<
com.imag = this->imag + b.imag;
return com;
}
// substraction
Complex operator-(const Complex& b)
{
Complex com;
com.real = this->real - b.real;
com.imag = this->imag - b.imag;
return com;
}
// multiplication
Complex operator*(const Complex& b)
{
Complex com;
com.real = this->real * b.real;
com.imag = this->imag * b.imag;
return com;
}
// division
Complex operator/(const Complex& b)
{
Complex com;
com.real = this->real / b.real;
com.imag = this->imag / b.imag;
return com;
}
private:
double real;
double imag;
};
// Main function for the program
int main( )
{
int n;
double i,r;
Complex Complex1;
Complex Complex2;
Complex Complex3;
cout<<"Plase enter real number for Complex1"<
cout<<"Plase enter imaginary number Complex1"<
Complex1.real_image(r,i);
cout<<"Plase enter real number for Complex2"<
cout<<"Plase enter imaginary number Complex2"<
Complex2.real_image(r,i);
Complex3 = Complex1 + Complex2;
for(;;)
{
cout<<"*****************************************\n*\t\t\t\t\t*"<
switch(n)
{
case 1:
Complex3 = Complex1 + Complex2;
Complex3.getVolume();
break;
case 2:
Complex3 = Complex1 - Complex2;
Complex3.getVolume();
break;
case 3:
Complex3 = Complex1 * Complex2;
Complex3.getVolume();
break;
case 4:
Complex3 = Complex1 / Complex2;
Complex3.getVolume();
break;
case 5:
exit(0);
default:
cout<<"please choose correct option"<
Subscribe to:
Posts (Atom)