0% found this document useful (0 votes)
11 views19 pages

Java Programs for Student Practical Tasks

The document contains a series of Java programming practicals completed by Aditya Kalura, including programs for taking command line inputs, calculating maturity amounts for bank deposits, checking for friendly pairs, replacing digits in integers, sorting arrays in zigzag fashion, rearranging positive and negative numbers, and finding saddle points in matrices. Each practical includes source code and outputs demonstrating the functionality of the programs. The practicals are structured sequentially, showcasing various programming concepts and techniques.

Uploaded by

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

Java Programs for Student Practical Tasks

The document contains a series of Java programming practicals completed by Aditya Kalura, including programs for taking command line inputs, calculating maturity amounts for bank deposits, checking for friendly pairs, replacing digits in integers, sorting arrays in zigzag fashion, rearranging positive and negative numbers, and finding saddle points in matrices. Each practical includes source code and outputs demonstrating the functionality of the programs. The practicals are structured sequentially, showcasing various programming concepts and techniques.

Uploaded by

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

1

Practical No. 1:

Write a java program to take input as a command line argument. Your name, course,
universityrollno and semester. Display the information.

Source Code:

public class studentDetails {


public static void main(String args[]) {

if ([Link] != 4) {

[Link]("Enter all the fields!");

String Name = args[0];

String UniversityRollNo = args[1];

String Course = args[2];


String Semester = args[3];

[Link]("Name: " + Name);

[Link]("UniversityRollNo: " + UniversityRollNo);

[Link]("Course: " + Course);

[Link]("Semester: " + Semester);

Aditya Kalura Section:B2 Roll no.: 6


2

Output:

Aditya Kalura Section:B2 Roll no.: 6


3

Practical No. 2:

Using the switch statement, write a menu-driven program to calculate the maturity amount of
a bank deposit.

The user is (i) Term Deposit (ii) Recurring Deposit For option (i) accept Principal (p), rate of
interest (r) and time period in years (n). Calculate and output the maturity amount (a)
receivable using the formula a = p[1 + r / 100]n.

For option (ii) accept monthly installment (p), rate of interest (r) and time period in months
(n). Calculate and output the maturity amount (a) receivable using the formula a = p * n + p *
n(n + 1) / 2 * r / 100 * 1 / 12. For an incorrect option, an appropriate error message should be
displayed.

Source Code:

import [Link].*;
public class maturityAmount {
public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

boolean exit = false;

while (!exit) {

[Link]("(1) Term Deposit");

[Link]("(2) Recurring Deposit");


[Link]("(3) Exit");
int choice = [Link]();

switch (choice) {

case 1: {

[Link]("Principle rate :=> ");

int p = [Link]();

[Link]("rate of intrest :=> ");


int r = [Link]();

[Link]("Time period in year :=> ");

int n = [Link]();

int ammount = (int) (p * [Link](1 + r / 100.0, n));


[Link]("maturity amount" + ammount);

Aditya Kalura Section:B2 Roll no.: 6


4

break;

case 2: {

[Link]("Principle rate :=> ");


int p = [Link]();

[Link]("rate of intrest :=> ");

int r = [Link]();

[Link]("Time period in months :=> ");

int n = [Link]();

int ammount = (p * n) + (p * n * ((n + 1) / 2) * (r / 100) * (1 / 12));

[Link]("maturity amount" + ammount);


break;
}

case 3: {

exit = true;

break;

default: {

[Link]("Error :=> Incorrect choice Entered\nexiting...");


exit = true;

break;

[Link]();

}
}

Aditya Kalura Section:B2 Roll no.: 6


5

Output:

Practical:3

Aditya Kalura Section:B2 Roll no.: 6


6

Program to find if the given numbers are Friendly pair or not (Amicable or not). Friendly Pair
are two or more numbers with a common abundance.

Source Code:

import [Link];

class friendlyPair {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);


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

int num1 = [Link]();

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

int num2 = [Link]();

int sum1 = 0;

int sum2 = 0;

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


if (num1 % i == 0) {

sum1 += i;

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

if (num2 % i == 0) {

sum2 += i;
}

double ratio1 = (double) sum1 / num1;

double ratio2 = (double) sum2 / num2;

if (ratio1 == ratio2) {

[Link]("Friendly pair");

} else {
[Link]("Not a friendly pair");

Aditya Kalura Section:B2 Roll no.: 6


7

[Link]();

Aditya Kalura Section:B2 Roll no.: 6


8

Output:

Aditya Kalura Section:B2 Roll no.: 6


9

Q4. Program to replace all 0's with 1 in a given integer. Given an integer as an input, all the
0's in the number has to be replaced with 1

SOURCE CODE:

import [Link];

public class Replace {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


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

int num = [Link]();

int result = 0;

int placeValue = 1;

while (num > 0) {

int digit = num % 10;

if (digit == 0) {
digit = 1;

result = result + (digit * placeValue);

placeValue *= 10;

num /= 10;

[Link]("Modified number: " + result);


[Link]();

Aditya Kalura Section:B2 Roll no.: 6


10

Output:

Practical 5:

Aditya Kalura Section:B2 Roll no.: 6


11

Printing an array into Zigzag fashion. Suppose you were given an array of integers, and you
are told to sort the integers in a zigzag pattern. In general, in a zigzag pattern, the first integer
is less than the second integer, which is greater than the third integer, which is less than the
fourth integer, and so on. Hence, the converted array should be in the form of e1 < e2 > e3 <
e4 > e5 < e6.

SOURCE CODE:

import [Link];

public class zigzag {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the size of the array: ");

int n = [Link]();

int[] arr = new int[n];


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

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

arr[i] = [Link]();

for (int i = 1; i < [Link] - 1; i = i + 2) {

if (!(arr[i] > arr[i - 1])) {

int temp = arr[i];


arr[i] = arr[i - 1];

arr[i - 1] = temp;

if (!(arr[i] > arr[i + 1])) {

int temp = arr[i];

arr[i] = arr[i + 1];

arr[i + 1] = temp;

Aditya Kalura Section:B2 Roll no.: 6


12

[Link]("Modified array:");

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

[Link](arr[i] + " ");


}

[Link]();

Aditya Kalura Section:B2 Roll no.: 6


13

Output:

Aditya Kalura Section:B2 Roll no.: 6


14

Practical 6:

The problem to rearrange positive and negative numbers in an array . Method: This approach
moves all negative numbers to the beginning and positive numbers to the end but changes the
order of appearance of the elements of the array.

SOURCE CODE:

import [Link];

public class rearrange {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the size of the array:");

int num = [Link]();

int[] arr = new int[num];

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

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

arr[i] = [Link]();

}
[Link]("The original array is: "); // printing the original array

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

[Link](arr[i] + " ");

int i = 0;

int j = 0;

while (i < num) {

while (i < num && arr[i] < 0) {


i++;

j = i;

while (j < num && arr[j] >= 0) {

j++;
}

Aditya Kalura Section:B2 Roll no.: 6


15

if (j < num) {

int temp = arr[i]; // swapping the numbers

arr[i] = arr[j];

arr[j] = temp;
}

i++;

[Link]("\nThe rearranged array is: "); // printing rearranged array

for (int k = 0; k < num; k++) {

[Link](arr[k] + " ");

}
[Link]();
}

Aditya Kalura Section:B2 Roll no.: 6


16

Output:

Aditya Kalura Section:B2 Roll no.: 6


17

Q7. Program to find the saddle point coordinates in a given matrix. A saddle point is an
element of the matrix, which is the minimum element in its row and the maximum in its
column.

SOURCE CODE:

import [Link];

public class Saddle {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows and columns: ");

int rows = [Link]();

int columns = [Link]();

int arr[][] = new int[rows][columns];

[Link]("Enter array elements: ");

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


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

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

[Link]();

boolean found = false;

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

int minRow = arr[i][0];


int colIdx = 0;

for (int j = 1; j < columns; j++) {

if (arr[i][j] < minRow) {

minRow = arr[i][j];

colIdx = j;
}

Aditya Kalura Section:B2 Roll no.: 6


18

boolean isSaddle = true;

for (int k = 0; k < rows; k++) {

if (arr[k][colIdx] > minRow) {


isSaddle = false;

break;

if (isSaddle) {

[Link]("Saddle point found: " + minRow + " at (" + i + ", " + colIdx +
")");

found = true;

}
if (!found) {

[Link]("Saddle point not found.");

Aditya Kalura Section:B2 Roll no.: 6


19

Output:

Aditya Kalura Section:B2 Roll no.: 6

Common questions

Powered by AI

The Java program calculates the maturity amount for Term and Recurring Deposits using specific formulas. For Term Deposit, it uses the formula a = p[1 + r / 100]^n, assuming annual compounding of interest and integer inputs for principal, rate, and time period in years. For Recurring Deposit, it uses a = p * n + p * n(n + 1) / 2 * r / 100 * 1 / 12, assuming monthly inputs for installment amounts. These formulas imply assumptions about the user providing valid integer inputs and do not account for decimal places or potential errors in input type .

The 'Friendly Pair' program determines if two numbers are friendly pairs by calculating the sum of proper divisors for each number and then comparing the ratio of the sum of divisors to the original number for both numbers. Friendly pairs have the condition that these ratios are equal: sum1/num1 = sum2/num2. It iterates through each number's potential divisors to find the sum of divisors, computes the ratios, and checks if they are equal to determine if they are friendly pairs .

User errors, such as entering an incorrect choice for deposit type, are managed using a default case in the switch statement which prints an error message and exits the program, implying that although the program anticipates incorrect inputs, its robustness could be improved by providing users with an option to re-enter their choice or correcting inputs dynamically rather than terminating immediately .

The Java program takes user input as command line arguments for displaying student details such as name, university roll number, course, and semester. If the number of arguments provided is not equal to four, the program prompts the user with the message 'Enter all the fields!' indicating that all necessary fields were not supplied, thus preventing incorrect or incomplete data from being processed .

Amicable numbers are two numbers related in that the sum of the proper divisors of each is equal to the other number. The program examining friendly pairs searches for pairs based on similar abundance, a ratio of sum of divisors to the number itself, which extends the idea of amicable pairs by comparing these ratios. 'Friendly pairs' are related because they generalize the condition to a set of numbers sharing the same divisor sum-to-number ratio, highlighting a broader class of relationships beyond the pair-specific property of traditional amicable numbers .

Replacing zeros with ones in an integer transforms it by eliminating any occurrence of the digit zero, potentially altering its value significantly if zero was predominant. Challenges in applying this logic to different numeral systems include dealing with different zero representations or digit lengths. For larger datasets, efficiency becomes critical, and the method will need optimization to handle massive numbers or long integers without leading to incorrect outputs or performance bottlenecks, especially when zero-filling or leading zeroes need to be handled differently .

The saddle point detection code identifies points by iterating over each row in the matrix and finding the minimum value in that row, then checking if this value is the maximum in its column. A saddle point exists if, for any element, it is the smallest in its respective row and largest in its column. The program uses nested loops to check every row's minimum against its column counterpart’s maximum values, marking it as a saddle point if the conditions are satisfied .

The program rearranges an array into a zigzag pattern by iterating through the array, ensuring that every second element is greater than the elements immediately before and after it. This is achieved by swapping adjacent elements where necessary to satisfy the condition arr[i] < arr[i+1] > arr[i+2], leading to a pattern matching e1 < e2 > e3 < e4 > e5, and so on. The algorithm uses local swaps to achieve the desired pattern without fully sorting the array, which allows it to maintain a zigzag order without entirely reordering all elements .

The rearrangement method involves iterating over the array while maintaining two indices: one for traversing the array to find positive numbers and another to place these positive numbers in the correct position after moving all negatives to the start. The algorithm swaps positive numbers with negatives to segregate names without preserving their original order. The trade-off is that while this method effectively separates negative and positive numbers, it does not maintain the original sequence and may not be optimal for datasets where order is significant .

The purpose of the program that replaces all 0's with 1 in a given integer is to transform the integer so that no digits in the transformed number are zero. This is achieved by iterating through each digit of the number, checking if it is zero, and replacing it with one while reconstructing the integer. Potential issues include handling leading zeros which might occur due to type limitations, and misinterpretations where the original intent of zero might be significant (e.g., representing null or empty data) not captured in the result .

You might also like