0% found this document useful (0 votes)
10 views13 pages

Java Programming Laboratory Exercises

Uploaded by

VK LAKSHMANDEV
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)
10 views13 pages

Java Programming Laboratory Exercises

Uploaded by

VK LAKSHMANDEV
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

VK.

LAKSHMANDEV 24I433

DATE : 23/10/2024

23I311 - JAVA PROGRAMMING LABORATORY

CA 1

SET: 1:

MISSING POSITIVE NUMBER

PROGRAM:

import [Link].*;

class s {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

int n = [Link]();

int[] nums = new int[n];

for (int i = 0; i < n; i++) {

nums[i] = [Link]();

boolean[] found = new boolean[n + 1];

for (int num : nums) {

if (num > 0 && num <= n) {

found[num] = true;

int missingPositive = 1;

for (int i = 1; i <= n; i++) {

if (!found[i]) {

missingPositive = i;

break;

}}

if (missingPositive == n) {

missingPositive = n + 1;

[Link](missingPositive);}}
[Link] 24I433

OUTPUT:

SET : 2

ATM:

PROGRAM:

import [Link].*;

public class ATM {

public static final int[] denominations = {20, 50, 100, 200, 500};

public static int[] banknotes = new int[5];

static {

Scanner scanner = new Scanner([Link]);

for (int i = 0; i < 5; i++) {

banknotes[i] = 0;

for (int i = 0; i < 5; i++) {

banknotes[i] += [Link]();

int amount = [Link]();

int[] result = new int[5];

int[] tempBanknotes = [Link]();

for (int i = 4; i >= 0; i--) {

int maxNotes = amount / denominations[i];

int notesToUse = [Link](maxNotes, tempBanknotes[i]);

result[i] = notesToUse;

amount -= notesToUse * denominations[i];


[Link] 24I433

if (amount == 0) {

for (int i = 0; i < 5; i++) {

banknotes[i] -= result[i];

[Link]();

public static void main(String[] args) {

OUTPUT:

SET : 3

DIAGONAL AND ADJACENT SUM

PROGRAM:

import [Link].*;

import [Link].*;

class m {

public static void main(String[] args) {

Scanner s = new Scanner([Link]);


[Link] 24I433

int rs = [Link]();

int cs = [Link]();

int[][] a = new int[rs][cs];

for (int i = 0; i < rs; i++) {

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

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

for (int i = 0; i < rs; i++) {

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

[Link](a[i][j] + " ");

[Link]();

int target = [Link]();

boolean found = false;

for (int i = 0; i < rs; i++) {

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

if (a[i][j] == target) {

found = true;

if (j - 1 >= 0 && j + 1 < cs && i + 1 < rs && i - 1 >= 0 && j - 1 >= 0 && j + 1 < cs && i + 1 < rs
&& i - 1 >= 0 ) {

[Link](a[i][j - 1] + a[i][j + 1] + a[i + 1][j] + a[i - 1][j]);

[Link](a[i - 1][j - 1] + a[i - 1][j + 1] + a[i + 1][j - 1] + a[i + 1][j + 1]);}

else [Link]("0");

if (!found) {

[Link]("0");

}
[Link] 24I433

OUTPUT:

SET: 4:

STRING OPERATIONS:

import [Link].*;

class str{

public static void main(String[] args){

Scanner s = new Scanner([Link]);

String e = [Link]();

char[] arr = [Link]();

int count = [Link];

[Link](count);

String[] w = [Link]("\\s+");

int wl =[Link];

[Link](wl);

int count1 = 0;

for (char c:arr){

if (c == '.' || c == '!' || c == '?') {

count1+=1;

}
[Link] 24I433

[Link](count1);

OUTPUT:

SET : 5

VIRUS INFECTION:

PROGRAM:

import [Link].*;

class v {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

String strengths = [Link]();

int iterations = 0;

int n = [Link]();

String current = strengths;

while (true) {

char[] next = [Link]();

boolean changed = false;

for (int i = 0; i < n; i++) {

int strongest = [Link](i) - '0';

for (int j = i - 1; j >= 0; j--) {

if (strongest - ([Link](j) - '0') >= i - j) {

strongest = [Link](strongest, [Link](j) - '0');

} else {
[Link] 24I433

break;

for (int j = i + 1; j < n; j++) {

if (strongest - ([Link](j) - '0') >= j - i) {

strongest = [Link](strongest, [Link](j) - '0');

} else {

break;

next[i] = (char) (strongest + '0');

if (next[i] != [Link](i)) {

changed = true;

current = new String(next);

iterations++;

if (!changed) {

break;

[Link](iterations);

OUTPUT:
[Link] 24I433

SET :6:

QUADRUPLETS

PROGRAM

import [Link].*;

class a1{

public static void main(String[] args){

Scanner s =new Scanner([Link]);

int size =[Link]();

int[] a = new int[size];

for (int i=0;i<size;i++){

a[i] = [Link]();

int ta = [Link]();

List<int[]> result = new ArrayList<>();

for (int i=0;i<size-3;i++){

for (int j=i+1;j<size-2;j++){

for (int k= j+1;k<size-1;k++){

for (int z = k+1;z<size;z++){

if (a[i]+a[j]+a[k]+a[z] == ta){

[Link](new int[]{a[i],a[j],a[k],a[z]});

for (int[] com:result){

[Link]([Link](com));

}
[Link] 24I433

OUTPUT:

SET: 7

BALANCED PARANTHESIS:

PROGRAM:

import [Link].*;

class a{

public static void main(String[] args){

Scanner s = new Scanner([Link]);

String size = [Link]();

int rc=0,cc=0,sc=0;

for(int j=0;j<[Link]();j++){

char i = [Link](j);

if (i == '{'){

cc +=1;

else if(i == '}'){

cc -= 1;

if (cc < 0){

[Link]("false");

else if (i=='['){

sc +=1;

}
[Link] 24I433

else if (i==']'){

sc-=1;

if (sc <0){

[Link]("false");

else if (i=='('){

rc +=1;

else if (i==')'){

rc -=1;

if (rc<0){

[Link]("false");

if (rc == 0 && cc == 0 && sc ==0){

[Link]("true");

else{

[Link]("false");

}}

OUTPUT:
[Link] 24I433

SET : 8:

HTML TAGS:

PROGRAM:

import [Link].*;

public class op {

private static String input;

static {

Scanner scanner = new Scanner([Link]);

input = [Link]();

StringBuilder result = new StringBuilder();

StringBuilder tagName = new StringBuilder();

StringBuilder tempContent = new StringBuilder();

boolean isStartTag = false;

boolean isEndTag = false;

boolean invalidContent = false;

String openTag = "";

for (int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if (ch == '<') {

if ([Link]() > 0 && [Link]()) {

[Link](tempContent);

[Link](0);

[Link](0);

isStartTag = true;

isEndTag = (i + 1 < [Link]() && [Link](i + 1) == '/');

if (isEndTag) i++;

} else if (ch == '>') {

isStartTag = false;

String currentTag = [Link]();


[Link] 24I433

if (isEndTag) {

if ([Link](currentTag)) {

openTag = "";

[Link](tempContent);

[Link](0);

} else {

invalidContent = true;

break;

} else {

if ([Link]()) {

openTag = currentTag;

} else {

[Link](0);

} else if (isStartTag) {

[Link](ch);

} else if ([Link]()) {

[Link](ch);

} else {

[Link](ch);

if (invalidContent || ![Link]()) {

[Link]("Output: None");

} else {

[Link]("Output: " + ([Link]() > 0 ? [Link]().trim() : "None"));

}
[Link] 24I433

public static void main(String[] args) {

OUTPUT:

Common questions

Powered by AI

The ATM program employs a greedy algorithm to dispense cash by preferring larger denominations first. This approach helps minimize the total number of banknotes required for a given withdrawal amount, following the predetermined denomination sequence from largest to smallest. While efficient in optimal scenarios, this strategy assumes sufficient availability of larger denominations and may not adapt well when significant large denomination depletion occurs, potentially failing to dispense an amount that smaller denominations could cover .

Handling of newlines isn't explicitly managed in the balanced parenthesis program; the input is treated as a continuous string. If present, newlines would be considered as part of the sequence when parsing, potentially leading to incorrect balance checks if not appropriately sanitized beforehand. Capturing and stripping newline characters before evaluation ensures accuracy across long sequences by maintaining logical continuity, a consideration potentially neglected without preprocessing .

The matrix diagonal and adjacent sum program effectively computes sums around a target in dense matrices; however, its design doesn't inherently optimize for sparse matrices. As it iterates over every cell to identify the target and evaluate potential sums, it may perform redundantly in sparsely filled matrices where many zeroes or irrelevant values prevail. Efficient handling of sparse matrices typically involves specialized data structures or algorithms that this program doesn't incorporate .

The program attempts to parse HTML tags by recognizing sequences between '<' and '>' as tags, distinguishing between opening and closing tags. For nested tags or malformed tags, the current design lacks a stack-based mechanism to truly match pairs across different levels of nesting, potentially misinterpreting elements as valid. It marks incomplete or mismatched sequences as invalid, but incorporating stack structures could improve correctness and reliability with complex HTML by precisely managing tag hierarchies .

The balanced parenthesis program employs counters for each type of bracket. It immediately checks balance status when an unmatched closing bracket is encountered, printing 'false' and potentially terminating early. This adaptive design prevents runtime errors due to bracket mismatch on-the-fly, ensuring incorrect sequences are promptly flagged without processing the entirety of an invalid input, thus enhancing runtime performance with fail-fast mechanisms .

The Java program targets efficiently determining the smallest missing positive integer from a given array of integers. Initially, it populates a boolean array to keep track of numbers from 1 to n, the size of the array. It marks an entry in the boolean array as true if the corresponding number exists in the input. Finally, it scans through the boolean markers to find the first unset value, identifying it as the smallest missing positive number. This design works within the constraint that the expected input consists of n positive integers where n is constrained by the array's limit .

The quadruplets program calculates all unique quadruples summing to a target value using four nested loops over an array. This brute-force approach guarantees finding all valid combinations but inherently scales poorly, as its time complexity is O(n^4), making it inefficient for larger inputs. For increased efficiency, leveraging hashing or sorting could improve performance by reducing unnecessary iterations and eliminating duplicates more effectively .

The program simulates state changes in a string representing strengths by iteratively computing a new string each cycle, where each character adjusts based on conditions involving its neighbors. A char at position i changes if a stronger character within reachable bounds influences it. The simulation continues until no more changes occur, noted by the 'changed' flag. This approach effectively captures iterative processes but may be computationally intensive, depending on input size and initial configurations .

Indexed calculations determine relative "infection" spread by comparing the strength values at each position, using index offsets to decide if and how a stronger value can modify a neighboring position. This impact spreads according to the indexed difference and strength disparity, with position i adjusting to retain the optimal local maximum strength achievable from its immediate neighbors. The program replicates differential-like calculation mechanisms to simulate realistic propagation patterns across numeric strings .

The program processes a string to count punctuation marks by iterating through each character and incrementing a counter when encountering '.', '!', or '?'. While this method accurately counts specified punctuation types, it lacks extensibility for other punctuation marks without manually amending the character checks. Incorporating regular expressions or leveraging Java's built-in libraries for more comprehensive punctuation handling could enhance flexibility and maintainability .

You might also like