Multi purpose java programs are describe follow for the all students who are study java in any classes. This programs are use easiest syntax and logic for the students. All the students are requested to understand the programs and slove different kinds of problems. If any problems please comments in the comment box.
public class Time
{
private int hour, min, sec;
Time(int h,int m,int s)
{
hour=h;
min=m;
sec=s;
}
Time()
{
hour=0;
min=0;
sec=0;
}
public Time add(Time tx)
{
Time t=new Time();
t.sec= this.sec + tx.sec;
if(t.sec>60)
{
t.sec -=60;
t.min++;
}
t.min += this.min + tx.min;
if(t.min>60)
{
t.min -=60;
t.hour++;
}
t.hour += this.hour + tx.hour;
if(t.hour>24)
{
t.hour -=24;
}
return t;
}
void display(String str)
{
System.out.println(str + " " + hour + ":" + min + ":" + sec);
}
public static void main(String []args)
{
Time t1=new Time(5,23,12);
Time t2=new Time(6,38,50);
Time t3=t1.add(t2);
t1.display("Time 1 : ");
t2.display("Time 2 : ");
t3.display("Time after addition : ");
}
}
Time 1 : 5:23:12
Time 2 : 6:38:50
Time after addition : 12:2:2
public class Complex
{
private int a,b;
public Complex(int a, int b)
{
this.a=a;
this.b=b;
}
public Complex()
{
a=0;
b=0;
}
public Complex add(Complex x)
{
Complex cx=new Complex();
cx.a=this.a+x.a;
cx.b=this.b+x.b;
return cx;
}
public void display()
{
System.out.println(a + "+ i" + b);
}
}
public class ComplexTest
{
public static void main(String []args)
{
Complex c1=new Complex(10,20);
Complex c2=new Complex(15,25);
c1.display();
c2.display();
Complex c3=c1.add(c2);
c3.display();
}
}
10+ i20
15+ i25
25+ i45
public class Stack
{
private int []s;
private int top,max;
Stack(int i)
{
max=i;
s=new int[max];
top=-1;
}
void push(int data)
{
if(top==max)
{
System.out.print("Stack is Overflow");
}
else
{
s[++top]=data;
}
}
int pop()
{
if(top==-1)
{
System.out.print("Stack is Underflow");
return 0;
}
else
{
return s[top--];
}
}
boolean checkEmpty()
{
if(max==-1)
{
return true;
}
else
{
return false;
}
}
public int count()
{
if(top>-1)
return top+1;
else
return 0;
}
}
import java.util.*;
public class StackImp
{
public void display(Stack st1,Stack st2)
{
if(st1.count()==st2.count())
{
System.out.println("Total Numbers of Elements in both stack is : " + st2.count());
}
}
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
Stack s1=new Stack(20);
Stack s2=new Stack(20);
int ch,data;
StackImp si=new StackImp();
while(true)
{
System.out.println("\n1. Push in Stack 1");
System.out.println("2. Push in Stack 2");
System.out.println("3. Pop in Stack 1");
System.out.println("4. Pop in Stack 1");
System.out.println("Enter Your Choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter The Data");
data=sc.nextInt();
s1.push(data);
si.display(s1,s2);
break;
case 2:
System.out.println("Enter The Data");
data=sc.nextInt();
s2.push(data);
si.display(s1,s2);
break;
case 3:
System.out.println(s1.pop());
si.display(s1,s2);
break;
case 4:
System.out.println(s2.pop());
si.display(s1,s2);
break;
default:
}
}
}
}