×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java Interface Programs Java Applet Basic
Java Mixed Programs - Java
8464    Arnab De    05/08/2017

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().

softetechnologies

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

softetechnologies

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

softetechnologies

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

softetechnologies

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

Java Interface Programs Java Applet Basic
softetechnologies
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.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
Truth can be stated in a thousand different ways, yet each one can be true.
Swami Vivekananda
385
50.69
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     41364
01/01/2018     35770
28/06/2017     33774
25/06/2018     32831
02/08/2017     32142
06/07/2017     26594
01/08/2017     26559
15/05/2017     26214
14/07/2017     21527
21/04/2018     20512
softetechnologies