-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab8.java
More file actions
355 lines (268 loc) · 11.3 KB
/
Copy pathLab8.java
File metadata and controls
355 lines (268 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
class HexaToDecimal {
// Method to convert a single hexadecimal character to its decimal value
static int hexadecimal(char hexChar) {
if (hexChar >= '0' && hexChar <= '9') {
return hexChar - '0'; // Convert '0'-'9' to 0-9
} else if (hexChar >= 'A' && hexChar <= 'F') {
switch (hexChar) {
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
}
}
return -1;
}
static int convert(String input) {
input = input.toUpperCase(); // Convert the input string to uppercase
int decimal = 0;
int length = input.length();
for (int i = 0; i < length; i++) {
char hexChar = input.charAt(length - 1 - i); // Process characters from right to left
decimal += hexadecimal(hexChar) * Math.pow(16, i);
}
return decimal;
}
public static void main(String[] args) {
String hexString = "f"; // Example input
int decimalValue = convert(hexString);
System.out.println("The decimal value of hexadecimal " + hexString + " is " + decimalValue);
}
}
class Adjacentspaces {
static String removeAdjacentSpaces(String input) {
StringBuilder result = new StringBuilder();
boolean lastWasSpace = false;
for (int i = 0; i < input.length(); i++) {
char currentChar = input.charAt(i);
if (currentChar == ' ') {
if (!lastWasSpace) {
result.append(currentChar);
lastWasSpace = true;
}
} else {
result.append(currentChar); // Append non-space characters
lastWasSpace = false;
}
}
return result.toString();
}
public static void main(String[] args) {
// Test cases
String input = "This is an example";
System.out.println("Original: \"" + input + "\"");
System.out.println("Processed: \"" + removeAdjacentSpaces(input) + "\"");
String input2 = " Leading and trailing spaces ";
System.out.println("Original: \"" + input2 + "\"");
System.out.println("Processed: \"" + removeAdjacentSpaces(input2) + "\"");
}
}
class DuplicateRemover {
static String removeConsecutiveDuplicates(String input) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
if (i == 0 || input.charAt(i) != input.charAt(i - 1)) {
result.append(input.charAt(i));
}
}
return result.toString();
}
public static void main(String[] args) {
String input = "ABBCCCCCBBAB";
System.out.println("Original: " + input);
System.out.println("Processed: " + removeConsecutiveDuplicates(input));
String input2 = "AABBBAA";
System.out.println("Original: " + input2);
System.out.println("Processed: " + removeConsecutiveDuplicates(input2));
}
}
5) Write a function that takes as input a string and returns true if the string is a palindrome,
and false otherwise. A palindrome is a string that reads the same forwards or backwards.
public class PalindromeChecker {
// Function to check if a string is a palindrome
public static boolean isPalindrome(String input) {
// Sanitize the input manually
StringBuilder sanitizedBuilder = new StringBuilder();
for (char c : input.toCharArray()) {
if (Character.isLetterOrDigit(c)) { // Check if character is alphanumeric
sanitizedBuilder.append(Character.toLowerCase(c)); // Add lowercase version of the character
}
}
String sanitized = sanitizedBuilder.toString();
// Reverse the sanitized string
String reversed = new StringBuilder(sanitized).reverse().toString();
// Check if the original sanitized string matches the reversed string
return sanitized.equals(reversed);
}
public static void main(String[] args) {
// Test cases
System.out.println(isPalindrome("A man, a plan, a canal, Panama")); // true
System.out.println(isPalindrome("racecar")); // true
System.out.println(isPalindrome("hello")); // false
System.out.println(isPalindrome("No 'x' in Nixon")); // true
}
}
6)
public class WatsonCrickPalindromeChecker {
// Function to check if a DNA string is a Watson-Crick complemented palindrome
public static boolean isWatsonCrickPalindrome(String dna) {
// Validate the DNA string
for (char c : dna.toUpperCase().toCharArray()) {
if ("ATCG".indexOf(c) == -1) {
return false; // Invalid DNA character
}
}
// Compute the reverse complement
StringBuilder reverseComplement = new StringBuilder();
for (int i = dna.length() - 1; i >= 0; i--) {
char complement = getComplement(dna.charAt(i));
reverseComplement.append(complement);
}
// Check if the DNA string equals its reverse complement
return dna.equalsIgnoreCase(reverseComplement.toString());
}
// Helper function to get the complement of a DNA base
private static char getComplement(char base) {
switch (Character.toUpperCase(base)) {
case 'A': return 'T';
case 'T': return 'A';
case 'C': return 'G';
case 'G': return 'C';
default: return ' '; // Shouldn't happen if input is valid DNA
}
}
public static void main(String[] args) {
// Test cases
System.out.println(isWatsonCrickPalindrome("ATCGAT")); // true
System.out.println(isWatsonCrickPalindrome("ATGC")); // false
System.out.println(isWatsonCrickPalindrome("AATT")); // true
System.out.println(isWatsonCrickPalindrome("CCGG")); // true
}
}
7)
Write a function that takes as input a DNA string of A, C, G, and T characters and returns
the string in reverse order with all of characters replaced by their complements. For
example, if the input is ACGGAT, then return ATCCGT.
public class ReverseComplement {
// Function to compute the reverse complement of a DNA string
public static String reverseComplement(String dna) {
StringBuilder reverseComplement = new StringBuilder();
// Loop through the string in reverse order
for (int i = dna.length() - 1; i >= 0; i--) {
char complement = getComplement(dna.charAt(i));
reverseComplement.append(complement);
}
return reverseComplement.toString();
}
// Helper function to get the complement of a DNA base
private static char getComplement(char base) {
switch (Character.toUpperCase(base)) {
case 'A': return 'T';
case 'T': return 'A';
case 'C': return 'G';
case 'G': return 'C';
default: throw new IllegalArgumentException("Invalid DNA character: " + base);
}
}
public static void main(String[] args) {
// Test cases
System.out.println(reverseComplement("ACGGAT")); // ATCCGT
System.out.println(reverseComplement("ATGC")); // GCAT
System.out.println(reverseComplement("AATT")); // AATT
System.out.println(reverseComplement("CCGG")); // CCGG
}
}
10)
In DNA sequence analysis, a complemented palindrome is a string equal to its reverse
complement. Adenine (A) and Thymine (T) are complements, as are Cytosine (C) and
Guanine (G). For example, ACGGT is a complement palindrome. Such sequences act as
transcription-binding sites and are associated with gene amplification and genetic
instability. Given a text input of N characters, find the longest complemented palindrome
that is a substring of the text. For example, if the text is GACACGGTTTTA then the
longest complemented palindrome is ACGGT. Hint: consider each letter as the center of a
possible palindrome of odd length, then consider each pair of letters as the center of a
possible palindrome of even length.
public class LongestComplementedPalindrome {
// Function to find the longest complemented palindrome in a DNA string
public static String findLongestComplementedPalindrome(String dna) {
String longestPalindrome = "";
int n = dna.length();
for (int i = 0; i < n; i++) {
// Odd-length palindromes (centered around a single character)
String oddPalindrome = expandAroundCenter(dna, i, i);
if (oddPalindrome.length() > longestPalindrome.length()) {
longestPalindrome = oddPalindrome;
}
// Even-length palindromes (centered around a pair of characters)
if (i < n - 1) {
String evenPalindrome = expandAroundCenter(dna, i, i + 1);
if (evenPalindrome.length() > longestPalindrome.length()) {
longestPalindrome = evenPalindrome;
}
}
}
return longestPalindrome;
}
// Function to expand around the center and find the complemented palindrome
private static String expandAroundCenter(String dna, int left, int right) {
int n = dna.length();
while (left >= 0 && right < n && isComplement(dna.charAt(left), dna.charAt(right))) {
left--;
right++;
}
return dna.substring(left + 1, right);
}
// Function to check if two characters are Watson-Crick complements
private static boolean isComplement(char a, char b) {
return (a == 'A' && b == 'T') || (a == 'T' && b == 'A') ||
(a == 'C' && b == 'G') || (a == 'G' && b == 'C');
}
public static void main(String[] args) {
// Test cases
System.out.println(findLongestComplementedPalindrome("GACACGGTTTTA")); // ACGGT
System.out.println(findLongestComplementedPalindrome("ATGCGCTAGGCT")); // GCGC
System.out.println(findLongestComplementedPalindrome("AATTCCGG")); // AATTCCGG
System.out.println(findLongestComplementedPalindrome("GATTACA")); // TT
}
}
11)
Mypackage::
Mathoperation.java –
package mypackage;
public class MathOperations {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
}
Tringoperation –
package mypackage;
public class StringOperations {
public String concatenate(String a, String b) {
return a + b;
}
public int getLength(String str) {
return str.length();
}
}
Main –
import mypackage.MathOperations;
import mypackage.StringOperations;
public class Main {
public static void main(String[] args) {
MathOperations m1 = new MathOperations();
int sum = m1.add(5, 10);
int product = m1.multiply(4, 7);
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
StringOperations s1 = new StringOperations();
String concatenated = s1.concatenate("Hello, ", "World!");
int length = s1.getLength("Java Programming");
System.out.println("Concatenated String: " + concatenated);
System.out.println("Length of String: " + length);
}
}