DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
Assignment 1
Student Name: Himanshu UID: 21BCS5642
Branch: CSE Section/Group: SC-904-B
Subject Name: Advance Java Date: 24/05/24
Ques 1) Intro to Conditional Statements:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
int n = [Link]();
[Link]();
String ans = "";
if(n % 2 == 1) {
ans = "Weird";
}
else {
if (n >= 2 && n <= 5)
ans = "Not Weird";
else if (n >= 6 && n <= 20)
ans = "Weird";
else if (n > 20)
ans = "Not Weird";
}
[Link](ans);
}
}
Ques 2):Class vs Instance:
import [Link].*;
import [Link].*;
public class Person {
private int age;
public Person(int initialAge) {
int ageToSet = initialAge;
if (ageToSet < 0) {
[Link]("Age is not valid, setting age to
0.");
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
ageToSet = 0;
}
age = ageToSet;
}
public void amIOld() {
String output = "";
if (age < 13) {
output = "You are young.";
} else if (age < 18) {
output = "You are a teenager.";
} else {
output = "You are old.";
}
[Link](output);
}
public void yearPasses() {
age++;
}
public static void main(String[] args) {
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
Ques 3):Median of two sorted arrays:
import [Link];
class Solution {
public double findMedianSortedArrays(int[]
nums1, int[] nums2) {
int n = [Link];
int m = [Link];
int[] merged = new int[n+m];
int k =0;
for (int i =0;i<n;i++){
merged[k++] = nums1[i];
}
for(int i =0;i<m;i++){
merged[k++] = nums2[i];
}
[Link](merged);
int total = [Link];
if(total % 2 == 1){
return (double) merged[total/2];
}else{
int middle1 = merged[total/2-1];
int middle2 = merged[total / 2];
return ((double) middle1 +(double) middle2) / 2.0;
}
}
}
Ques 4) Wildcard Matching:
class Solution:
def isMatch(self, s: str, p: str) -> bool:
dp = [[-1 for _ in range(len(p))]for _ in range(len(s))]
def helper(index1,index2):
if index1>=len(s) and index2>=len(p):
return True
if index1>=len(s) and index2<len(p):
while index2<len(p):
if p[index2]!="*":
return False
index2+=1
return True
if index2>=len(p):
return False
if dp[index1][index2]!=-1:
return dp[index1][index2]
if s[index1]==p[index2] or p[index2]=='?':
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
dp[index1][index2]= helper(index1+1,index2+1)
elif s[index1]!=p[index2] and p[index2]!="*":
dp[index1][index2]=False
elif p[index2]=='*':
dp[index1][index2]= helper(index1+1,index2+1) or helper(index1,index2+1) or
helper(index1+1,index2)
return dp[index1][index2]
return helper(0,0)