Java - Fast Learner Assignment
Java - Fast Learner Assignment
ElementJava :
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
Java
public class Animal {
String species;
@Override
public void makeSound() {
System.out.println("Woof! Woof!");
}
}
java
public class StringEncoding {
public static String[] encodeStrings(String[] inputs) {
String[] outputs = new String[3];
for (int i = 0; i < 3; i++) {
String[] parts = new String[3];
int length = inputs[i].length();
if (length % 3 == 0) {
int partLength = length / 3;
parts[0] = inputs[i].substring(0, partLength);
parts[1] = inputs[i].substring(partLength, 2 * partLength);
parts[2] = inputs[i].substring(2 * partLength);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} else if (length % 3 == 1) {
int partLength = length / 3;
parts[0] = inputs[i].substring(0, partLength);
parts[1] = inputs[i].substring(partLength, 2 * partLength + 1);
parts[2] = inputs[i].substring(2 * partLength + 1);
} else {
int partLength = length / 3;
parts[0] = inputs[i].substring(0, partLength + 1);
parts[1] = inputs[i].substring(partLength + 1, 2 * partLength + 1);
parts[2] = inputs[i].substring(2 * partLength + 1);
}
if (i == 0) {
outputs[0] = parts[0] + parts[1] + parts[2];
} else if (i == 1) {
outputs[1] = parts[1] + parts[2] + parts[0];
} else {
outputs[2] = parts[2] + parts[0] + parts[1];
outputs[2] = outputs[2].toUpperCase();
}
}
return outputs;
}
}
java
public class MathOperations {
public int add(int a, int b) {
return a + b;
}
java
public class StringAppendingRule {
public static String appendString(String input) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < input.length() - 1; i++) {
char ch1 = input.charAt(i);
char ch2 = input.charAt(i + 1);
int sum = getNumericValue(ch1) + getNumericValue(ch2);
int remainder = sum % 26;
char newChar = remainder == 0 ? '0' : (char) ('a' + remainder - 1);
result.append(ch1).append(newChar);
}
result.append(input.charAt(input.length() - 1));
return result.toString();
}
java
public interface Shape {
double area();
double perimeter();
}
@Override
public double area() {
return Math.PI * radius
* radius;
}
@Override
public double perimeter() {
return 2 * Math.PI * radius;
}
}
Question 7: Random Letter Addition
java
public class RandomLetterAddition {
public static char findAddedLetter(String s, String t) {
int sumS = 0;
int sumT = 0;
for (int i = 0; i < s.length(); i++) {
sumS += s.charAt(i);
}
for (int i = 0; i < t.length(); i++) {
sumT += t.charAt(i);
}
return (char) (sumT - sumS);
}
}
```
java
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
java
public class WordMatcher {
public static String matchFound(String input1, String input2) {
String[] words = input2.split(":");
StringBuilder output1 = new StringBuilder();
for (String word : words) {
if (word.contains(input1.replace('_', ' '))) {
output1.append(word.toUpperCase()).append(":");
}
}
return output1.toString();
}
}
Question 10: Difference between `throw` and `throws`
-`throws`: It is used in method signatures to declare that a method may throw one
or more types of exceptions.
Example:
java
public void readFile(String fileName) throws FileNotFoundException {
// Code to read the file
}