DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
DAY- 5
Student Name: Himanshu UID: 21BCS5642
Branch: CSE Section/Group: 904-B
Subject Name: JAVA Date: 03/06/24
Ques 1) Java Strings Introduction
import [Link].*;
import [Link].*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String A=[Link]();
String B=[Link]();
/* Enter your code here. Print output to STDOUT. */
[Link]([Link]() + [Link]());
if([Link](B)>0){
[Link]("Yes");
}
else{
[Link]("No");
}
[Link]([Link]([Link](0)) +[Link](1)+"
"+[Link]([Link](0)) +[Link](1));
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
Ques 2) Add Strings
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder str = new StringBuilder();
int i = [Link]() - 1, j = [Link]() - 1, carry = 0, sum;
while (i >= 0 || j >= 0 || carry > 0) {
int digit1 = (i >= 0) ? [Link](i--) - '0' : 0;
int digit2 = (j >= 0) ? [Link](j--) - '0' : 0;
sum = digit1 + digit2 + carry;
[Link](sum % 10);
carry = sum / 10;
}
return [Link]().toString();
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
Ques 3) Largest Number
class Solution {
public String largestNumber(int[] nums) {
String[] s = new String[[Link]];
for(int i=0; i<[Link]; i++) s[i] = [Link](nums[i]);
[Link](s, (a,b) -> (b + a).compareTo(a + b));
return s[0].equals("0") ? "0" : [Link]("",s);
}
}
Ques 4) Rotate Image
class Solution {
public void rotate(int[][] matrix) {
int l = [Link];
int i=0;
int j= l-1;
while(i<j) {
for(int count=0; i+count<j; count++){
rotate(matrix, i, j, count);
}
i++; j--;
}
}
public void rotate(int[][] matrix, int i, int j, int count ) {
int temp = matrix[i][i+count];
matrix[i][i+count] = matrix[j-count][i];
matrix[j-count][i] = matrix[j][j-count];
matrix[j][j-count] = matrix[i+count][j];
matrix[i+count][j] = temp;
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
Ques 5) Let's Review
import [Link].*;
import [Link].*;
public class Solution {
public static void main(String[] args) {
String evenChars="";
String oddChars ="";
Scanner input = new Scanner([Link]);
int testNumber = [Link]();
[Link](); //cosumption line for nextInt
for(int i=0; i<testNumber ;i++){
String s = [Link]();
for(int j = 0; j<[Link]();j++){
String currentChar = [Link]([Link](j));
if(j%2==0)
evenChars+=currentChar;
else
oddChars+=currentChar;
}
[Link](evenChars +" "+ oddChars);
evenChars ="";
oddChars ="";
}
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING