Skip to content

Commit c3ac509

Browse files
committed
Java Interface
1 parent 223ef85 commit c3ac509

File tree

11 files changed

+282
-0
lines changed

11 files changed

+282
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7

InterfaceLabwork/_classpath.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

InterfaceLabwork/_project.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>InterfaceLabwork</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
212 Bytes
Binary file not shown.

InterfaceLabwork/bin/Queue.class

2.9 KB
Binary file not shown.

InterfaceLabwork/bin/Stack.class

2.32 KB
Binary file not shown.

InterfaceLabwork/bin/Summary.class

149 Bytes
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
public interface Collection {
3+
public int len();
4+
public void addItem(int a);
5+
public void removeItem();
6+
public void sort();
7+
public int search(int item);
8+
9+
}

InterfaceLabwork/src/Queue.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import java.util.NoSuchElementException;
2+
3+
public class Queue implements Summary, Collection {
4+
private int queue[];
5+
private int front, rear, size, len;
6+
7+
public Queue(int n) {
8+
size = n;
9+
len = 0;
10+
queue = new int[size];
11+
front = -1;
12+
rear = -1;
13+
}
14+
15+
public int len() {
16+
return len;
17+
}
18+
19+
public boolean isEmpty() {
20+
return front == -1;
21+
}
22+
23+
public void addItem(int a) {
24+
if (rear == -1) {
25+
front = 0;
26+
rear = 0;
27+
queue[rear] = a;
28+
} else if (rear + 1 >= size) {
29+
throw new IndexOutOfBoundsException("Overflow Exception");
30+
} else if (rear + 1 < size) {
31+
queue[++rear] = a;
32+
}
33+
len++;
34+
}
35+
36+
public void removeItem() {
37+
if (isEmpty()) {
38+
throw new NoSuchElementException("Underflow Exception");
39+
} else {
40+
len--;
41+
int elem = queue[front];
42+
if (front == rear) {
43+
front = -1;
44+
rear = -1;
45+
} else {
46+
front++;
47+
}
48+
//return elem;
49+
}
50+
}
51+
52+
public void sort() {
53+
//int n = top; //stackArray.length;
54+
int temp = 0;
55+
for (int i = front; i < rear; i++) {
56+
for (int j = 1; j < (rear - i); j++) {
57+
if (queue[j - 1] > queue[j]) {
58+
temp = queue[j - 1];
59+
queue[j - 1] = queue[j];
60+
queue[j] = temp;
61+
}
62+
}
63+
}
64+
}
65+
66+
public int search(int item){
67+
int n = queue.length;
68+
for(int i=0;i<n;i++){
69+
if(queue[i] == item){
70+
return i;
71+
}
72+
}
73+
return 0;
74+
}
75+
76+
public int mean(){
77+
int sm = 0;
78+
for(int i=front;i<rear;i++){
79+
sm += queue[i];
80+
}
81+
return sm/Math.abs(front - rear);
82+
}
83+
public int median(){
84+
sort();
85+
//int l = len();
86+
return queue[Math.abs(front - rear)/2];
87+
88+
}
89+
public int range(){
90+
sort();
91+
int highest = queue[rear - 1];
92+
int lowest = queue[0];
93+
94+
return (highest - lowest);
95+
}
96+
97+
public void display() {
98+
System.out.println("Queue = ");
99+
if (len == 0) {
100+
System.out.println("Empty");
101+
return;
102+
}
103+
for (int i = front; i <= rear; i++) {
104+
System.out.println(queue[i] + " ");
105+
}
106+
System.out.println();
107+
}
108+
109+
public static void main(String[] args) {
110+
// TODO Auto-generated method stub
111+
Queue q = new Queue(6);
112+
113+
q.addItem(5);
114+
q.addItem(6);
115+
q.addItem(7);
116+
117+
q.removeItem();
118+
119+
q.display();
120+
121+
System.out.println(q.mean());
122+
123+
}
124+
125+
126+
}

InterfaceLabwork/src/Stack.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
public class Stack implements Summary, Collection {
2+
private int maxSize;
3+
private int[] stackArray;
4+
private int top;
5+
6+
public Stack(int s) {
7+
maxSize = s;
8+
stackArray = new int[maxSize];
9+
top = -1;
10+
}
11+
12+
public int len() {
13+
return maxSize;
14+
}
15+
16+
public void addItem(int a) {
17+
stackArray[++top] = a;
18+
}
19+
20+
public void removeItem() {
21+
//int t = top - 1;
22+
top = top - 1;
23+
}
24+
25+
public void sort() {
26+
int n = top; //stackArray.length;
27+
int temp = 0;
28+
for (int i = 0; i < n; i++) {
29+
for (int j = 1; j < (n - i); j++) {
30+
if (stackArray[j - 1] > stackArray[j]) {
31+
temp = stackArray[j - 1];
32+
stackArray[j - 1] = stackArray[j];
33+
stackArray[j] = temp;
34+
}
35+
}
36+
}
37+
}
38+
39+
public int search(int item){
40+
int n = stackArray.length;
41+
for(int i=0;i<n;i++){
42+
if(stackArray[i] == item){
43+
return i;
44+
}
45+
}
46+
return 0;
47+
}
48+
49+
public int mean(){
50+
int sm = 0;
51+
for(int i=0;i<top;i++){
52+
sm += stackArray[i];
53+
}
54+
return sm/top;
55+
}
56+
public int median(){
57+
sort();
58+
//int l = len();
59+
return stackArray[top/2];
60+
61+
}
62+
public int range(){
63+
sort();
64+
int highest = stackArray[top - 1];
65+
int lowest = stackArray[0];
66+
67+
return (highest - lowest);
68+
}
69+
70+
public void displaySort(){
71+
int n = top;//stackArray.length;
72+
for(int i=0;i<n;i++){
73+
System.out.println(stackArray[i]);
74+
}
75+
}
76+
77+
public static void main(String[] args) {
78+
// TODO Auto-generated method stub
79+
Stack st = new Stack(6);
80+
st.addItem(2);
81+
st.addItem(6);
82+
st.addItem(4);
83+
st.addItem(5);
84+
st.addItem(1);
85+
//st.addItem(7);
86+
//st.addItem(1);
87+
//st.addItem(11);
88+
st.removeItem();
89+
//st.removeItem();
90+
//st.removeItem();
91+
92+
//Range
93+
System.out.println("Range: " + st.range());
94+
95+
st.sort();
96+
97+
st.displaySort();
98+
//Mean
99+
System.out.println("Mean: " + st.mean());
100+
//Median
101+
System.out.println("Median : " + st.median());
102+
103+
104+
}
105+
106+
}

0 commit comments

Comments
 (0)