0% found this document useful (0 votes)
3 views11 pages

Median of Two Sorted Arrays Program

Uploaded by

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

Median of Two Sorted Arrays Program

Uploaded by

chinmayck2006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OOPJ (BCS306A) Assignment-01 2025-2026

Program-01

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two
sorted arrays.
Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Program
import [Link].*;
import [Link];
public class MedianTwoArrays
{
public static double findMedian(int[] nums1, int[] nums2)
{
int[] merged = new int[[Link] + [Link]];
int i = 0, j = 0, k = 0;
while (i < [Link] && j < [Link])
{
if (nums1[i] < nums2[j])
{
merged[k++] = nums1[i++];
}
else
{
merged[k++] = nums2[j++];
}
}
while (i < [Link]) merged[k++] = nums1[i++];
while (j < [Link]) merged[k++] = nums2[j++];
int n = [Link];
if (n % 2 == 0)
{
return (merged[n/2 - 1] + merged[n/2]) / 2.0;
}
else
{
return merged[n/2];
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Name :Brunda U M ");
[Link]("USN:4YG24CS026");

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 1


OOPJ (BCS306A) Assignment-01 2025-2026

[Link]("\n------ OUTPUT ------");


[Link]("Enter size of nums1: ");
int m = [Link]();
int[] nums1 = new int[m];
[Link]("Enter " + m + " sorted elements of nums1:");
for (int i = 0; i < m; i++)
{
nums1[i] = [Link]();
}
[Link]("Enter size of nums2: ");
int n = [Link]();
int[] nums2 = new int[n];
[Link]("Enter " + n + " sorted elements of nums2:");
for (int i = 0; i < n; i++)
{
nums2[i] = [Link]();
}
double median = findMedian(nums1, nums2);
[Link]("Median of the two sorted arrays: " + median);
}
}
Output

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 2


OOPJ (BCS306A) Assignment-01 2025-2026
Program-02 Given an integer, , print its first multiples. Each multiple (where ) should be printed on a
new line in the form: N x i = result.

Example:

Sample Input

Sample Output

2x1=2

2x2=4

2x3=6

2x4=8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

Program

import [Link].*;

public class MultiplesWithDetails

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

[Link]("Name:Aishwarya B ");

[Link]("USN: 4YG24CS005”);

[Link]("Enter an integer N: ");

int N = [Link]();

for (int i = 1; i <= 10; i++)

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 3


OOPJ (BCS306A) Assignment-01 2025-2026
{

[Link](N + " x " + i + " = " + (N * i));

Output:

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 4


OOPJ (BCS306A) Assignment-01 2025-2026
Program 03:

You are given a 6*6 2D array. An hourglass in an array is a portion shaped like this:

abc

efg

For example, if we create an hourglass using the number 1 within an array full of zeros, it may look like this:

111000

010000

111000

000000

000000

000000

Actually, there are many hourglasses in the array above. The three leftmost hourglasses are the following:

111 110 100

1 0 0

111 110 100

The sum of an hourglass is the sum of all the numbers within it. The sum for the hourglasses above are 7, 4,
and 2, respectively.

Program

import [Link].*;

public class HourglassSum

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

int[][] arr = new int[6][6];

[Link]("Name:Aishwarya B ");

[Link]("USN:4YG24CS005 ");

[Link]("Enter the 6x6 array elements:");

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 5


OOPJ (BCS306A) Assignment-01 2025-2026
for (int i = 0; i < 6; i++)

for (int j = 0; j < 6; j++)

arr[i][j] = [Link]();

int maxSum = Integer.MIN_VALUE;

for (int i = 0; i < 4; i++)

{ for (int j = 0; j < 4; j++)

int sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2]+ arr[i + 1][j +1]+ arr[i + 2][j] +arr[i + 2][j + 1] + arr[i + 2][j + 2];

if (sum > maxSum) { maxSum = sum;

[Link]("Largest Hourglass Sum: " + maxSum);

Output:

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 6


OOPJ (BCS306A) Assignment-01 2025-2026
Program 04:

A class named Arithmetic with a method named add that takes integers as parameters and returns an
integer denoting their sum.

A class named Adder that inherits from a superclass named Arithmetic.

Your classes should not be public .

Input Format

You are not responsible for reading any input from stdin; a locked code stub will test your submission by
calling the add method on an Adder object and passing it integer parameters.

Output Format

You are not responsible for printing anything to stdout. Your add method must return the sum of its
parameters.

Program:

import [Link].*;

class Arithmetic

int add(int a, int b)

return a + b;

class Adder extends Arithmetic { }

class Main

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

[Link]("Name:Aishwarya B ");

[Link]("USN:4YG24CS005 ");

[Link]("Enter first number: ");

int x = [Link]();

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 7


OOPJ (BCS306A) Assignment-01 2025-2026
[Link]("Enter second number: ");

int y = [Link]();

Adder adder = new Adder();

int result = [Link](x, y);

[Link]("Sum using Adder class: " + result);

Output:

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 8


OOPJ (BCS306A) Assignment-01 2025-2026
Program 05:

Consider the following Sports class:

class Sports

String getName()

return "Generic Sports";

void getNumberOfTeamMembers()

[Link]( "Each team has n players in " + getName() );

Next, we create a Soccer class that inherits from the Sports class. We can override the getName method and
return a different, subclass-specific string:

class Soccer extends Sports

@Override

String getName()

return "Soccer Class";

Complete the code in your editor by writing an overridden getNumberOfTeamMembers method that prints
the same statement as the superclass' getNumberOfTeamMembers method, except that it replaces with
(the number of players on a Soccer team).

Output Format

When executed, your completed code should print the following:

Generic Sports

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 9


OOPJ (BCS306A) Assignment-01 2025-2026
Each team has n players in Generic Sports

Soccer Class

Each team has 11 players in Soccer Class

Program:

import [Link].*;

class Sports

String getName()

return "Generic Sports";

void getNumberOfTeamMembers()

[Link]("Each team has n players in " + getName());

class Soccer extends Sports

@Override

String getName()

return "Soccer Class";

@Override

void getNumberOfTeamMembers()

[Link]("Each team has 11 players in " + getName());

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 10


OOPJ (BCS306A) Assignment-01 2025-2026
class Main

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

Sports s1 = new Sports();

Soccer s2 = new Soccer();

[Link]("Name:Aishwarya B ");

[Link]("USN:4YG24CS005 ");

[Link]("\n--- Sports Class Output ---");

[Link]();

[Link]("\n--- Soccer Class Output ---");

[Link]();

Dept. of CSE, NCEH Brunda U M (4YG24CS026) 11

You might also like