21BCS7540 Sneha Gupta Day1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

JAVA – DAY 1

Student Name: Sneha Gupta UID: 21BCS7540


Branch: CSE Section/Group: 21BCS_CC-652-A
Subject Name: Java Date of Performance: 24/05/24

1. Aim:
To demonstrate the concept of programming algorithms.

2. Objective:
1) Class vs. Instance - In this challenge, we're going to learn about the difference betweena class
and an instance; because this is an Object Oriented concept, it's only enabled in certain
languages. (Level – Easy)
2) Intro to Conditional Statements- In this challenge, we learn about conditional statements.
(Level – Easy)
3) Java Int to String - You are given an integer n , you have to convert it into a string. If your code
successfully converts into a string the code will print "Good job". Otherwise it will print
"Wrong answer". n can range between -100 to 100 inclusive. (Level – Easy)
4) Median of Two Sorted Arrays - Given two sorted arrays nums1 and nums2 of size m and n
respectively, return the median of the two sorted arrays. The overall run time complexity
should be O(log (m+n)). (Level – Hard)
5) Loops - In this challenge, we will use loops to do some math. (Level – Easy)
6) Operators - Given the meal price (base cost of a meal), tip percent (the percentage of the meal
price being added as tip), and tax percent (the percentage of the meal price being added as tax)
for a meal, find and print the meal's total cost. Round the result to the nearest integer. (Level –
Easy)
7) Wildcard Matching - Given an input string (s) and a pattern (p), implement wildcard pattern
matching with support for '?' and '*' where: '?' Matches any single character. ‘*' Matches any
sequence of characters (including the empty sequence). The matching should cover the entire
input string (not partial). (Level – Hard)
8) Bitwise AND of Numbers Range - Given two integers left and right that represent the range
[left, right], return the bitwise AND of all numbers in this range, inclusive. (Level – Medium)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Code and Output:

1)
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int testNumber = input.nextInt();
for(int i =0; i<testNumber ;i++){
int age = input.nextInt();
Person age1 = new Person(age);
age1.amIOld();
for(int j=0; j<3; j++){
age1.yearPasses();
}
age1.amIOld();
System.out.println();
}
input.close();
}
}
class Person{
private int age;
public Person(int initialAge){
if(initialAge<0){
this.age=0;
System.out.println("Age is not valid, setting age to 0.");
}
else {
this.age=initialAge;
}
}

int yearPasses(){
this.age +=1;
return this.age;
}
void amIOld(){
if(this.age<13) {
System.out.println("You are young.");
}
else if(this.age>= 13 && this.age<18) {
System.out.println("You are a teenager.");
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

else {
System.out.println("You are old.");
}
}
}

2)
import java.util.Scanner;
public class Solution {
private static String checker(int num) {
if (num%2==1) {
return "Weird";
}
else {
if (num>=2 && num<=5) {
return "Not Weird";
}
else if (num>=6 && num<=20) {
return "Weird";
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

else {
return "Not Weird";
}
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
String answer = checker(num);
System.out.println(answer);
}
}

3)
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] arr = new int[nums1.length + nums2.length];
System.arraycopy(nums1, 0, arr, 0, nums1.length);
System.arraycopy(nums2, 0, arr, nums1.length, nums2.length);
Arrays.sort(arr);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if(arr.length % 2 !=0){
int mid = arr.length / 2;
return (double) arr[mid];
}
int mid = arr.length / 2;
return (double) (arr[mid -1 ] + arr[mid])/2;
}
}

4)
import java.util.*;
public class Solution {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
int n = in .nextInt();
in.close();
String s=Integer.toString(n);
if (n == Integer.parseInt(s)) {
System.out.println("Good job");

}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

else {
System.out.println("Wrong answer.");
}
}

catch (Exception e) {
System.out.println("Unsuccessful Termination!!");
}
}
}

5)
import java.util.Scanner;
class Result {
double meal_cost;
int tip_percent;
int tax_percent;
public static void solve(double meal_cost, int tip_percent, int tax_percent) {
double meal_tip = meal_cost/100*tip_percent;
double meal_tax = meal_cost/100*tax_percent;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
double total = meal_cost+meal_tip+meal_tax;
Math.round(total);
System.out.println(Math.round(total));
}
}
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double meal_cost = sc.nextDouble();
int tip_percent = sc.nextInt();
int tax_percent = sc.nextInt();
Result.solve(meal_cost, tip_percent, tax_percent);
}
}

6)
import java.util.Scanner;
public class Solution {
public static void table(int n) {
for (int i=1;i<=10;i++) {
System.out.println(n+" x "+i+" = "+n*i);
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
table(n);
}
}

7)
class Solution {
public boolean isMatch(String s, String p) {
return solve(s,p,0,0,new Boolean[s.length()][p.length()]);
}
private boolean solve(String s,String t,int i,int j,Boolean[][] dp){
if(i==s.length()){
return hasNoLCLeft(t,j);
}
if(j==t.length()){
return i==s.length();
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if(dp[i][j]!=null){
return dp[i][j];
}
if(Character.isLowerCase(t.charAt(j)) && s.charAt(i)!=t.charAt(j)){
return dp[i][j] = false;
}
if(t.charAt(j)=='*'){
if(solve(s,t,i+1,j,dp)){ return true; }
if(solve(s,t,i,j+1,dp)){ return true; }
}
return dp[i][j] = solve(s,t,i+1,j+1,dp)? true: false;
}
private boolean hasNoLCLeft(String t,int j){
for(int i=j;i<t.length();i++){
if(t.charAt(i)!='*'){
return false;
}
}
return true;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

8)
class Solution {
public int rangeBitwiseAnd(int left, int right) {
int rightShiftCnt = 0;
while(left != right){
left = left >> 1;
right = right >> 1;
rightShiftCnt++;
}
int commonPref = (right << rightShiftCnt);
return commonPref;
}

You might also like