Java Mixed Programs
05-08-2017    719 times
Mixed java Programs

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.

Create a class called Time, which has three private instance variables – hour, min and sec. It contains a method called add() which takes one Time object as parameter and print the added value of the calling Time object and passes Time object. In the main method, declare two Time objects and assign values using constructor and call the add().

Tutorial at home

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 : ");
}
}

Output:

Time 1 : 5:23:12
Time 2 : 6:38:50
Time after addition : 12:2:2

Tutorial at home
Tutorial at home

Create a class called Complex, It contains a method called add() which takes one Complex object as parameter and return a Complex object to the main and print two complex no and added complex no

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();
}
}

Output:

10+ i20
15+ i25
25+ i45

Tutorial at home

Create a class Stack that declares a stack and the methods to perform push ( ), pop ( ) and checkEmpty ( ) operations on the stack. Create two stacks and write a menu-driven program to perform operations on the two stacks. Whenever the number of elements in the two stacks becomes equal, a message should automatically be generated displaying the number of elements in each stack.

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;
}
}

Tutorial at home
Tutorial at home

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:
}
}
}
}

freenotes
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post