Skip to content

Commit 4a68a9e

Browse files
Add files via upload
1 parent ce1b638 commit 4a68a9e

File tree

11 files changed

+233
-0
lines changed

11 files changed

+233
-0
lines changed

Exp5/A/A.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Exp5.A;
2+
3+
public interface A //interface A
4+
{
5+
public void meth1(); //meth1() declaration
6+
7+
public void meth2(); //meth2() declaration
8+
}

Exp5/A/MyClass.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Exp5.A;
2+
3+
public class MyClass implements A
4+
{
5+
@Override //annotation to direct compiler that the method is been overridden
6+
public void meth1()
7+
{
8+
System.out.println("Inside meth1() and MyClass implements meth1() of interface A");
9+
}
10+
11+
@Override //annotation to direct compiler that the method is been overridden
12+
public void meth2()
13+
{
14+
System.out.println("Inside meth2() and MyClass implements meth2() of interface A");
15+
}
16+
17+
public static void main(String[] args)
18+
{
19+
MyClass obj = new MyClass(); //creating a reference variable of class MyClass
20+
obj.meth1(); //calling the method meth1()
21+
obj.meth2(); //calling the method meth2()
22+
}
23+
}

Exp5/InterfaceUse/Sample.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package Exp5.InterfaceUse;
2+
3+
public interface Sample
4+
{
5+
//a field
6+
int value = 20; //in an interface the fields are public, static and final
7+
//two methods declared in the interface Sample
8+
public void printVal();
9+
public void changeVal(int n);
10+
}

Exp5/InterfaceUse/test.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Exp5.InterfaceUse;
2+
3+
public class test implements Sample
4+
{
5+
@Override //annotation to direct compiler that the method is been overridden
6+
public void changeVal(int n)
7+
{
8+
//previously value was initialised value 20
9+
//value = n;
10+
}
11+
@Override //annotation to direct compiler that the method is been overridden
12+
public void printVal()
13+
{
14+
System.out.println("Value : "+value);
15+
}
16+
17+
public static void main(String args[])
18+
{
19+
test obj = new test();
20+
obj.changeVal(30); //changing the value of field value
21+
obj.printVal(); //calling the printVal() func
22+
}
23+
}

Exp5/Operation/Operation.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Exp5.Operation;
2+
3+
public interface Operation
4+
{
5+
public float division(int n1,int n2); //method division()
6+
public int modules(int n1,int n2); //method modules()
7+
}
8+

Exp5/Operation/test.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Exp5.Operation;
2+
3+
import java.util.Scanner;
4+
public class test implements Operation
5+
{
6+
@Override //annotation to direct compiler that the method is been overridden
7+
public float division(int n1,int n2)
8+
{
9+
return (float)n1/n2; //returns the quotient of the two numbers s
10+
}
11+
@Override //annotation to direct compiler that the method is been overridden
12+
public int modules(int n1,int n2)
13+
{
14+
return n1%n2; //returns the remainder of two number
15+
}
16+
17+
public static void main(String args[])
18+
{
19+
Scanner in = new Scanner(System.in);
20+
test obj = new test(); //creating a reference variable of the class test
21+
//getting the input of two numbers
22+
System.out.println("Enter the two numbers : ");
23+
System.out.print("n1 : ");
24+
int n1 = in.nextInt();
25+
System.out.print("n2 : ");
26+
int n2 = in.nextInt();
27+
//calling the two methods of the interface implemented by the class test
28+
System.out.println("Division : " + obj.division(n1, n2));
29+
System.out.println("Modules : "+obj.modules(n1, n2));
30+
in.close();
31+
}
32+
}

Exp5/Stack/StackClass.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package Exp5.Stack;
2+
3+
import java.util.Scanner;
4+
public class StackClass implements StackInterface
5+
{
6+
int[] Stack = new int[10]; //integer array for stack
7+
int top=-1;
8+
@Override //overriding the method of interface
9+
public void push(int num) //push operation of the stack
10+
{
11+
if(this.top==Stack.length-1) //checking the overflow condition
12+
{
13+
System.out.println("Overflow...!");
14+
}
15+
else
16+
{
17+
top++;
18+
Stack[top]=num; //pushing into the stack
19+
}
20+
}
21+
@Override //overriding the method of interface stackInterface
22+
public void pop() //pop operation of the stack
23+
{
24+
if(top==-1) //checking the underflow condition
25+
{
26+
System.out.println("\nUnderflow...!");
27+
}
28+
else
29+
{
30+
System.out.println(Stack[top]+" is popped from the stack..!");
31+
top--; //popping out of the stack
32+
}
33+
}
34+
@Override
35+
public void displayStack() //displaying the stack
36+
{
37+
if(top==-1) //checking if the stack is empty or not
38+
{
39+
System.out.println("\nThe Stack is empty...!");
40+
}
41+
else
42+
{
43+
int i=top;
44+
System.out.println("\nStack elements are : ");
45+
//displaying the elements
46+
for(;i>=0;i--)
47+
{
48+
System.out.println(Stack[i]);
49+
}
50+
}
51+
}
52+
53+
public static void main(String[] args)
54+
{
55+
Scanner in = new Scanner(System.in);
56+
boolean check = true;
57+
StackClass obj = new StackClass(); //creating an instance of class StackClass
58+
59+
while(check==true)
60+
{
61+
System.out.println("Following operations can be performed : ");
62+
System.out.println("1. Push");
63+
System.out.println("2. Pop");
64+
System.out.println("3. Display");
65+
System.out.print("Enter your choice : ");
66+
int temp = in.nextInt();
67+
68+
switch(temp)
69+
{
70+
case 1 :
71+
{
72+
System.out.print("Enter the value you want to push : ");
73+
int num = in.nextInt();
74+
obj.push(num);
75+
break;
76+
}
77+
case 2 :
78+
obj.pop();
79+
break;
80+
case 3 :
81+
obj.displayStack();
82+
break;
83+
default : System.out.println("Invalid input...!");
84+
}
85+
System.out.print("Do you want to perform more ? (true/false) : ");
86+
check = in.nextBoolean();
87+
}
88+
in.close();
89+
}
90+
}

Exp5/Stack/StackInterface.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package Exp5.Stack;
2+
3+
public interface StackInterface
4+
{
5+
//methods of the StackInterface
6+
void push(int num); //push() operation
7+
void pop(); //pop() operation
8+
void displayStack(); //display() operation
9+
}

Exp5/Test/ToTestInt.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Exp5.Test;
2+
3+
import java.util.Scanner;
4+
public class ToTestInt
5+
{
6+
public static void main(String[] args)
7+
{
8+
Scanner in = new Scanner(System.in);
9+
System.out.print("Enter the number you want to find the sqaure (int) : ");
10+
int n = in.nextInt();
11+
arithmetic obj = new arithmetic(); //creating a refernce variable to the class airthmetic
12+
System.out.println("Square of number is : "+obj.square(n)); //calling the square() function to get the square of the int
13+
in.close();
14+
}
15+
}

Exp5/Test/arithmetic.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package Exp5.Test;
2+
3+
public class arithmetic implements test //arithmetic class implements the test interface
4+
{
5+
public int square(int n) //implementing the inly method of the test class
6+
{
7+
return n*n;
8+
}
9+
}

0 commit comments

Comments
 (0)