0% found this document useful (0 votes)
12 views

Java - Fast Learner Assignment

Uploaded by

mahiw70565
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java - Fast Learner Assignment

Uploaded by

mahiw70565
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

FAST LEARNER ASSIGNMENT

Student Name: Sidharth Shivam Singh UID: 21BCS1317


Section/Group: 21BCS_CC-613/A Branch: B.E. CSE
Date of performance: 06/04/2024 Semester: 6th
Subject Name: PBLJ Subject Code: 21CSH-319

Question 1: Next Greater

ElementJava :
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class NextGreaterElement {


public int[] nextGreaterElement(int[] nums1, int[] nums2)
{Map<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for (int num : nums2) {
while (!stack.isEmpty() && stack.peek() < num)
{map.put(stack.pop(), num);
}
stack.push(num);
}
int[] result = new int[nums1.length];
for (int i = 0; i < nums1.length; i++)
{
result[i] = map.getOrDefault(nums1[i], -1);
}
return result;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Question 2: Inheritance Example

Java
public class Animal {
String species;

public Animal(String species) {


this.species = species;
}

public void makeSound() {


System.out.println("Animal makes a sound");
}
}

public class Dog extends Animal {


public Dog() {
super("Canine");
}

@Override
public void makeSound() {
System.out.println("Woof! Woof!");
}
}

Question 3: Encoding Three Strings

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;
}
}

Question 4: Method Overloading for Mathematical Operations

java
public class MathOperations {
public int add(int a, int b) {
return a + b;
}

public double add(double a, double b) {


return a + b;
}

public int multiply(int a, int b) {


return a * b;
}

public double multiply(double a, double b) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
return a * b;
}
}

Question 5: String Appending Rule

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();
}

private static int getNumericValue(char ch) {


return Character.toLowerCase(ch) - 'a' + 1;
}
}

Question 6: Interface and Abstraction

java
public interface Shape {
double area();
double perimeter();
}

public class Circle implements Shape {


private double radius;

public Circle(double radius) {


this.radius = radius;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

@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);
}
}
```

Question 8: Custom Exception

java
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}

public class Example {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public void checkNumber(int number) throws CustomException {
if (number < 0) {
throw new CustomException("Number cannot be negative");
}
}
}

Question 9: Matching Words

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`

`throw`: It is used to explicitly throw an exception within a method or a block of


code.
Example:
java
public void checkNumber(int number) {
if (number < 0) {
throw new IllegalArgumentException("Number cannot be negative");
}
}

-`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
}

You might also like