/*This code snippet is an example of operator overloading, constructor, destructor,mutator, faclitator methods Also it is divided in two parts, one a defintion part and another a declaration part */ #includeusing namespace std; #include "complex1.h" //Constructor Complex1 :: Complex1() { this->m_real = 0; this->m_imaginary = 0; } Complex1 :: Complex1(int m_real, int m_imaginary) { this->m_real = m_real; this->m_imaginary = m_imaginary; } //inspector Methods int Complex1 :: get_real() const { return this->m_real; } int Complex1 :: get_imaginary() const { return this->m_imaginary; } //Mutator Methods void Complex1 :: set_real(int m_real) { this->m_real = m_real; } void Complex1 :: set_imaginary(int m_imaginary) { this->m_imaginary = m_imaginary; } //Facilitator Methods void Complex1 :: accept() { cout << "Enter m_real and m_imaginary parts of a complex number ::"; cin >> this->m_real >> this->m_imaginary; } void Complex1 :: display() const { if(this->m_imaginary >= 0) cout << this->m_real << "+" << this->m_imaginary << "i" << endl; else if(this->m_imaginary < 0) cout << this->m_real << this->m_imaginary << "i" << endl; } Complex1 Complex1 :: complex_add(const Complex1& c1) const { Complex1 temp; temp.m_real = this->m_real + c1.m_real; temp.m_imaginary = this->m_imaginary + c1.m_imaginary; return temp; } Complex1 Complex1 :: complex_subtract(const Complex1& c1) const { Complex1 temp; temp.m_real = this->m_real - c1.m_real; temp.m_imaginary = this->m_imaginary - c1.m_imaginary; return temp; } Complex1 Complex1 :: operator+(const Complex1& c) const { return complex_add(c); } Complex1 Complex1 :: operator-(const Complex1& c) const { return complex_subtract(c); } Complex1 Complex1 :: operator++() { this->m_real++; return *this; } Complex1 Complex1 :: operator++(int) { Complex1 temp = *this; this->m_real++; return temp; } Complex1 Complex1 :: operator--() { this->m_real--; return *this; } Complex1 Complex1 :: operator--(int) { Complex1 temp = *this; this->m_real--; return temp; } bool Complex1 :: operator==(const Complex1& c)const { if(this->m_real == c.m_real && this->m_imaginary == c.m_imaginary) return true; else return false; } bool Complex1 :: operator!=(const Complex1& c) const { if(this->m_real != c.m_real || this->m_imaginary != c.m_imaginary) return true; else return false; } Complex1 :: ~Complex1() { cout << "Destroying... " << endl; } ostream& operator<<(ostream& out,const Complex1& c) { if(c.m_imaginary >= 0) out << c.m_real << "+" << c.m_imaginary << "i" << endl; else if(c.m_imaginary < 0) out << c.m_real << c.m_imaginary << "i" << endl; return out; } istream& operator>>(istream& in,Complex1& c) { in >> c.m_real >> c.m_imaginary; return in; } // Class Declaration class Complex1 { private: int m_real,m_imaginary; public: //constructors Complex1(); Complex1(int, int); //inspector Methods int get_real() const; int get_imaginary() const; //Mutator Methods void set_real(int ); void set_imaginary(int ); //Facilitator Methods void accept(); void display() const; Complex1 complex_add(const Complex1& c) const; Complex1 complex_subtract(const Complex1& c) const; Complex1 operator+(const Complex1& c) const; Complex1 operator-(const Complex1& c) const; Complex1 operator++(); Complex1 operator++(int); Complex1 operator--(); Complex1 operator--(int); bool operator==(const Complex1&) const; bool operator!=(const Complex1&) const; friend ostream& operator<<(ostream& ,const Complex1&); friend istream& operator>>(istream& ,Complex1&); //destructor ~Complex1(); };
Tuesday, October 20, 2009
Operator Overloading, Constructor, Destructor, Mutator, Facilitator methods in C++
Monday, October 19, 2009
Code Snippet to implement stack in Java
class MyStack
{
private:
int arr[5];
int top;
public:
MyStack()
{
int i;
for(i=0;i<5;i++)
{
arr[i]=0;
}
top=0;
}
void push(int val)
{
if(full())
System.out.println("Stack is Full.........\n");
this.arr[top]=val;
this.top=this.top+1;
}
int pop()
{
if(empty())
System.out.println("Stack is Empty.........\n");
this.top=this.top-1;
return arr.[top];
}
boolean empty()
{
if(this.top==0)
return true;
else
return false;
}
boolean full()
{
if(this.top==5)
return true;
else
return false;
}
}
class DemoStack
{
public static void main(String[] args)
{
}
}
A program to print all possible combination of three digits(Java)
/*For example if three digits are 1,2 and 3 then all the possible combinations are 123,132,231,213,321 and 312)*/
class Combination
{
public static int reverseNumber(int no)
{
int ono=0,rev=0,rem;
ono=no;
while(ono!=0)
{
rem=ono%10;
ono/=10;
rev= (rev*10)+rem;
}
return rev;
}
public static void main(String[] args)
{
int no,rem;
String num=javax.swing.JOptionPane.showInputDialog("Enter Three Digit Number");
no=Integer.parseInt(num);
for(int j=0;j<2;j++)
{
for(int i=0;i<3;i++)
{
rem=no%10;
no=no/10;
no=(rem*100)+no;
System.out.println("numbers are:"+no);
}
no=reverseNumber(no);
}
}
A class to validate, increment, decrement and compare dates(Java)
class ValidateDate
{
int day,month,year;
ValidateDate()
{
this.day=1;
this.month=1;
this.year=1;
}
ValidateDate(int d,int m,int y)
{
this.day=d;
this.month=m;
this.year=y;
}
void incrementByOneDay()
{
this.day=this.day+1;
if(this.day > 30 )
{
this.day=this.day-30;
this.month=this.month+1;
if(this.month > 12)
{
this.month=this.month-12;
this.year=this.year+1;
}
}
}
void decrementByOneDay()
{
this.day=this.day-1;
if(this.day == 0 )
{
this.day=30;
this.month=this.month-1;
if(this.month == 0)
{
this.month=12;
this.year=this.year-1;
}
}
}
boolean compareDates(ValidateDate d)
{
if(this.day==d.day && this.month==d.month && this.year==d.year)
return true;
else
return false;
}
void displayDate()
{
System.out.print("Date=>"+this.day);
System.out.print(":"+this.month);
System.out.print(":"+this.year);
}
public static void main(String[] args)
{
ValidateDate d1=new ValidateDate(30,12,2001);
d1.displayDate();
d1.incrementByOneDay();
System.out.println("After incrementing:");
d1.displayDate();
d1.decrementByOneDay();
System.out.println("After decrementing:");
d1.displayDate();
ValidateDate d2=new ValidateDate(30,12,2001);
if(d1.compareDates(d2))
System.out.println("EQUAL");
else
System.out.println("NOT EQUAL");
}
}
A program find the factorial of a number using recursive functions (Java)
class Factorial
{
public static int factorialRec(int no)
{
int ans;
if(no==1)
return 1;
ans=no*(factorialRec(no-1));
return ans;
}
public static void main(String[] args)
{
int fact,num;
String number=javax.swing.JOptionPane.showInputDialog("Enter Number");
num=Integer.parseInt(number);
fact=factorialRec(num);
System.out.println("Factorial of number::"+fact);
}
}
Friday, October 16, 2009
Code snippet to reverse the contents of a file (Java)
package reverse;
/* If contents of the file abc.txt are "Cricket is a religion in India", the below code will create a temp.txt file containing the output "aidnI ni noigiler a si tekcirC" */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
public class Reverse {
public static void main(String[] args) throws IOException
{
FileReader fr=new FileReader("D:\\java\\workspace\\Reversefile\\src\\reverse\\abc.txt");//Enter full path of the file whose contents are to be reversed.
BufferedReader br =new BufferedReader(fr);
FileWriter fw=new FileWriter("temp.txt");
String str="";
while((str=br.readLine())!=null)
{
System.out.println(str);
fw.write(new StringBuffer(str+"\n").reverse().toString());
}
fw.close();
fr.close();
br.close();
}
}
Code snippet to read and display a file from a folder (Java)
package files;
import java.io.BufferedReader;
import java.io.IOException;
public class FileReader
{
public static void main(String[] args) throws IOException
{
BufferedReader fr = new BufferedReader(new java.io.FileReader
("D:\\Amit\\JavaAssignments\\Lab3\\MyBook.java"));//Give the complete path of the file you want to read. Above is an example.
String str;
while((str = fr.readLine())!= null)
{
System.out.println(str);
}
}
}
Subscribe to:
Comments (Atom)