Skip to content

Commit f653556

Browse files
committed
Queue
1 parent b8d8d85 commit f653556

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed

Queue/Main.java

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
import java.util.Random;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
7+
Random rnd = new Random();
8+
Queue first_dna = new Queue(1000); // first queue for dna
9+
Queue comp_dna = new Queue(1000); // second queue for complement dna
10+
Queue amino = new Queue(1000); // third queue for triple dna code
11+
Queue aminoAcids_name = new Queue(1000); // fourth queue for the amino acids
12+
13+
for (int i = 0; i < 21; i++) {
14+
char symbol; // for the base
15+
int number = rnd.nextInt(4); // generate base randomly for first dna
16+
if(number == 0) {
17+
symbol ='A';
18+
first_dna.enqueue(symbol);
19+
}
20+
else if(number == 1) {
21+
symbol ='T';
22+
first_dna.enqueue(symbol);
23+
}
24+
else if(number == 2) {
25+
symbol ='G';
26+
first_dna.enqueue(symbol);
27+
}
28+
else if(number == 3) {
29+
symbol ='C';
30+
first_dna.enqueue(symbol);
31+
}
32+
}
33+
Queue saved_dna1 = new Queue(1000);
34+
System.out.println("-First DNA-"); // print first dna
35+
for (int i = 0; i <21; i++) {
36+
System.out.print(first_dna.peek() + " ");
37+
first_dna.enqueue(first_dna.dequeue());
38+
}
39+
System.out.println();
40+
// construct a new queue which contains the complement of the DNA sequence --- complement dna
41+
for(int i =0; i < 21; i++) {
42+
char new_symbol; // complement base
43+
char symbol; // base
44+
symbol = (char) first_dna.peek();
45+
if(symbol == 'A') { // A-T
46+
new_symbol = 'T';
47+
comp_dna.enqueue(new_symbol);
48+
first_dna.enqueue(first_dna.dequeue());
49+
}
50+
else if(symbol == 'T') { // T-A
51+
new_symbol = 'A';
52+
comp_dna.enqueue(new_symbol);
53+
first_dna.enqueue(first_dna.dequeue());
54+
}
55+
else if(symbol == 'G') { // G-C
56+
new_symbol = 'C';
57+
comp_dna.enqueue(new_symbol);
58+
first_dna.enqueue(first_dna.dequeue());
59+
}
60+
else if(symbol == 'C') { // C-G
61+
new_symbol = 'G';
62+
comp_dna.enqueue(new_symbol);
63+
first_dna.enqueue(first_dna.dequeue());
64+
}
65+
}
66+
System.out.println("\n-Complement of first DNA-"); // print the complement dna
67+
for (int i = 0; i < 21; i++) {
68+
System.out.print(comp_dna.peek() + " ");
69+
first_dna.enqueue(comp_dna.dequeue());
70+
}
71+
72+
for(int i =0; i<21;i++) { // This is necessary because it will used in crossover part
73+
saved_dna1.enqueue(first_dna.dequeue());
74+
}
75+
Queue saved_dna2 = new Queue(1000); // this is necessary too. It will used in amino acids part
76+
77+
for(int i =0; i < 21; i++) { // this is the most important part. Without it, amino acids printed wrong. There was a problem in here, when I fix it with adding this code.
78+
char new_symbol;
79+
char symbol;
80+
symbol = (char) first_dna.peek();
81+
if(symbol == 'A') { // A-T
82+
new_symbol = 'T';
83+
saved_dna2.enqueue(new_symbol);
84+
first_dna.enqueue(first_dna.dequeue());
85+
}
86+
else if(symbol == 'T') { // T-A
87+
new_symbol = 'A';
88+
saved_dna2.enqueue(new_symbol);
89+
first_dna.enqueue(first_dna.dequeue());
90+
}
91+
else if(symbol == 'G') { // G-C
92+
new_symbol = 'C';
93+
saved_dna2.enqueue(new_symbol);
94+
first_dna.enqueue(first_dna.dequeue());
95+
}
96+
else if(symbol == 'C') { // C-G
97+
new_symbol = 'G';
98+
saved_dna2.enqueue(new_symbol);
99+
first_dna.enqueue(first_dna.dequeue());
100+
}
101+
}
102+
System.out.println();
103+
// for the second part ( amino acids )
104+
for (int i = 0; i < 7; i++) {
105+
String amino_acid ="";
106+
for (int j = 0; j < 3; j++) {
107+
amino_acid += saved_dna2.dequeue(); // I stored the dna codes 3-3
108+
}
109+
amino.enqueue(amino_acid);
110+
}
111+
System.out.println("\n**Each 3 letters of DNA code for one amino acid**");
112+
113+
for (int i = 0; i < 7; i++) {
114+
System.out.print(amino.peek() + " ");
115+
amino.enqueue(amino.dequeue());
116+
}
117+
System.out.println();
118+
for (int i = 0; i < 7; i++) { // A DNA sequence codes for a sequence of amino acids that make up a protein. Each 3 letters of DNA code for one amino acid
119+
String piece = (String) amino.dequeue();
120+
// starts with A
121+
if (piece.startsWith("AA")) {
122+
if (piece.endsWith("A") || piece.endsWith("G")) aminoAcids_name.enqueue("Lys");
123+
else aminoAcids_name.enqueue("Asn");
124+
}
125+
else if ( piece.startsWith("AC")) aminoAcids_name.enqueue("Thr");
126+
else if (piece.startsWith("AG")) {
127+
if (piece.endsWith("A") || piece.endsWith("G")) aminoAcids_name.enqueue("Arg");
128+
else aminoAcids_name.enqueue("Ser");
129+
}
130+
else if (piece.startsWith("AT")) {
131+
if (piece.endsWith("G")) aminoAcids_name.enqueue("Stop");
132+
else aminoAcids_name.enqueue("Ile");
133+
}
134+
// starts with C
135+
else if (piece.startsWith("CA")) {
136+
if (piece.endsWith("A") || piece.endsWith("G")) aminoAcids_name.enqueue("Gln");
137+
else aminoAcids_name.enqueue("His");
138+
}
139+
else if ( piece.startsWith("CC")) aminoAcids_name.enqueue("Pro");
140+
else if ( piece.startsWith("CT")) aminoAcids_name.enqueue("Leu");
141+
else if ( piece.startsWith("CG")) aminoAcids_name.enqueue("Arg");
142+
// starts with T
143+
else if (piece.startsWith("TA")) {
144+
if (piece.endsWith("A") || piece.endsWith("G")) aminoAcids_name.enqueue("Stop");
145+
else aminoAcids_name.enqueue("Tyr");
146+
}
147+
else if ( piece.startsWith("TC")) aminoAcids_name.enqueue("Ser");
148+
else if (piece.startsWith("TT")) {
149+
if (piece.endsWith("A") || piece.endsWith("G")) aminoAcids_name.enqueue("Leu");
150+
else aminoAcids_name.enqueue("Phe");
151+
}
152+
else if (piece.startsWith("TG")) {
153+
if (piece.endsWith("A")) aminoAcids_name.enqueue("Stop");
154+
else if (piece.endsWith("C") || piece.endsWith("T")) aminoAcids_name.enqueue("Cys");
155+
else aminoAcids_name.enqueue("Trp");
156+
}
157+
// starts with G
158+
else if (piece.startsWith("GA")) {
159+
if (piece.endsWith("A") || piece.endsWith("G")) aminoAcids_name.enqueue("Glu");
160+
else aminoAcids_name.enqueue("Asp");
161+
}
162+
else if ( piece.startsWith("GC")) aminoAcids_name.enqueue("Ala");
163+
else if ( piece.startsWith("GT")) aminoAcids_name.enqueue("Val");
164+
else if ( piece.startsWith("GG")) aminoAcids_name.enqueue("Gly");
165+
}
166+
System.out.println("\n-Amino Acids-"); // printed amino acids
167+
168+
for (int i = 0; i < 7; i++) {
169+
System.out.print(aminoAcids_name.peek() + " ");
170+
aminoAcids_name.enqueue(aminoAcids_name.dequeue());
171+
}
172+
System.out.println();
173+
174+
Queue second_dna = new Queue(1000); // second dna that act in crossover
175+
for (int i = 0; i < 21; i++) {
176+
char symbol;
177+
int number = rnd.nextInt(4); // generate symbols randomly for second dna
178+
if(number == 0) {
179+
symbol ='A';
180+
second_dna.enqueue(symbol);
181+
}
182+
else if(number == 1) {
183+
symbol ='T';
184+
second_dna.enqueue(symbol);
185+
}
186+
else if(number == 2) {
187+
symbol ='G';
188+
second_dna.enqueue(symbol);
189+
}
190+
else if(number == 3) {
191+
symbol ='C';
192+
second_dna.enqueue(symbol);
193+
}
194+
}
195+
System.out.println("\n-Second DNA before crossover-"); // print second dna. It will used for crossover part
196+
for (int i = 0; i < 21; i++) {
197+
System.out.print(second_dna.peek() + " ");
198+
second_dna.enqueue(second_dna.dequeue());
199+
}
200+
System.out.println();
201+
// Crossover part
202+
int crossover = 1 + rnd.nextInt(20); // random selected crossover point
203+
System.out.println("\nRandomly generated crossover point:" + crossover);
204+
for (int i = 0; i < 21; i++) {
205+
if(i < crossover) {
206+
saved_dna1.enqueue(saved_dna1.dequeue());
207+
second_dna.enqueue(second_dna.dequeue());
208+
}
209+
else // crossover starting here. it happenes when i>= crossoverpoint
210+
{
211+
saved_dna1.enqueue(second_dna.dequeue());
212+
second_dna.enqueue(saved_dna1.dequeue());
213+
}
214+
}
215+
//print first and second dna after crossover
216+
System.out.println("\n\t**After Crossover**");
217+
System.out.println("-First DNA-");
218+
for (int i = 0; i < 21 ; i++) {
219+
System.out.print(saved_dna1.peek() + " ");
220+
saved_dna1.enqueue(saved_dna1.dequeue());
221+
}
222+
System.out.println(); // print second one
223+
System.out.println("-Second DNA-");
224+
for (int i = 0; i < 21; i++) {
225+
System.out.print(second_dna.peek() + " ");
226+
second_dna.enqueue(second_dna.dequeue());
227+
}
228+
}
229+
}

Queue/Queue.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
3+
public class Queue {
4+
private int rear,front;
5+
private Object[] elements;
6+
public Queue(int capacity) {
7+
elements = new Object[capacity];
8+
rear = -1;
9+
front = 0;
10+
}
11+
12+
public boolean isEmpty() {
13+
return rear < front; // true döner
14+
}
15+
16+
public boolean isFull() {
17+
return (rear + 1 == elements.length); // trueeeeeeeeeeee
18+
}
19+
20+
public void enqueue(Object data) {
21+
22+
if(isFull()) {
23+
System.out.println("Queue is full!");
24+
}
25+
else {
26+
rear++;
27+
elements[rear] = data;
28+
}
29+
}
30+
31+
public Object dequeue() {
32+
if(isEmpty()) {
33+
System.out.println("Queue is empty!");
34+
return null;
35+
}
36+
else {
37+
Object retData = elements[front];
38+
elements[front] = null;
39+
front++;
40+
return retData;
41+
}
42+
}
43+
44+
public Object peek() {
45+
if(isEmpty()) {
46+
System.out.println("Queue is empty!");
47+
return null;
48+
}
49+
else {
50+
Object retData = elements[front];
51+
return retData;
52+
}
53+
}
54+
55+
public int size() {
56+
return (rear - front + 1);
57+
}
58+
59+
60+
61+
}

0 commit comments

Comments
 (0)