0% found this document useful (0 votes)
14 views7 pages

Output: 16

The document outlines several Java programming tasks, including generating random numbers, performing matrix operations, checking Fibonacci numbers, calculating harmonic numbers, and checking for palindromes. Each task is accompanied by code snippets demonstrating the implementation of the required functionality. Additionally, an encoding mechanism is described that involves character swapping and case conversion.

Uploaded by

darsi3048
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)
14 views7 pages

Output: 16

The document outlines several Java programming tasks, including generating random numbers, performing matrix operations, checking Fibonacci numbers, calculating harmonic numbers, and checking for palindromes. Each task is accompanied by code snippets demonstrating the implementation of the required functionality. Additionally, an encoding mechanism is described that involves character swapping and case conversion.

Uploaded by

darsi3048
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
You are on page 1/ 7

1.

Define a class which will generate a random number in between the range
low and high. Here, low and high are the two fields of the class and
randGen(low, high) is the method in the class to generate random number.

public class program {

static int randGen(int low, int high) {


int r = (int)(Math.random() * (high - low + 1) +
low);
return r;
}

public static void main(String args[]) {


int low = 10;
int high = 18;

int random = randGen(low, high);

System.out.print(random);
}
}

Output:

16
2. Define a class Matrix2D which will include row, column and a 2D array of
integer numbers of size row×column as data. Define the methods for
matrix addition, subtraction and multiplication, where each method take
an argument as another matrix to do the operation with the current matrix
and return the resultant matrix. Also, define another method print() to
print the matrix it stores. Write a main class, where you create three
matrix object a, b and c of type Matrix2D and obtain result c = a.add(b)
for addition of two matrix, that is, c = a+b, etc.

public class Matrix2D {


static int addition(int row, int col, int a[][], int
b[][]) {
int c[][] = new int[row][col];
//adding and printing addition of 2 matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = a[i][j] + b[i][j];
System.out.print(c[i][j] + " ");
}
System.out.println(); //new line
}
}

static int subtraction(int row, int col, int a[][],


int b[][]) {
int c[][] = new int[row][col];
//adding and printing addition of 2 matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = a[i][j] - b[i][j];
System.out.print(c[i][j] + " ");
}
System.out.println(); //new line
}
}

int row, col;

int a[][] = new int[row][col];


int b[][] = new int[row][col];
addition(row, col, a[][], b[][]);
subtraction(row, col, a[][], b[][]);

}
}

public class main {


public static void main(String args[]) {

Matrix2D a = new Matrix2D();


Matrix2D b = new Matrix2D();
Matrix2D c = new Matrix2D();

}
}

3. The n?th (n ? 2) Fibonacci number is defined as follow: Fn=Fn-1+Fn-2 with F1 = F0 = 1 Define a


class which will solve the following. Read any integer number x from the keyboard and write a
method int checkFibonacci(int x), if it is an n?th Fibonacci number, and if so, then the value of
n. Call the method from the main() method.

import java.util.Scanner;

public class FibonacciSeries {

static void checkFibonacci(int x) {


int firstTerm = 0;
int secondTerm = 1;
int thirdTerm = 0;
int count = 0;

while (thirdTerm < x) {


count++;
thirdTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = thirdTerm;
}

if (thirdTerm == x) {
System.out.println(count + 1);
} else {
System.out.println("Number doesn't belongs to
Fibonacci series");
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter positive number :");
int inputNumber = sc.nextInt();
checkFibonacci(inputNumber);
}
}

Output:

Enter positive number : 21 8 Enter positive number : 22 Number doesn't belongs to


Fibonacci series

4. The n?th Harmonic number is defined as Hn=1+1/2+1/3+…+1/n. Write a recursive function to


calculate Hn, for any integer n > 0. [You should verify your calculation using the calculation of
the sum of the series iteratively and writing another function for that.]

//Java Program to find the nCr


import java.util.*;
public class program {

//Method to calculate harmonic sum

static double harmonic(double n) {


// Base condition
if (n < 2)
return 1;

else
return 1 / n + (harmonic(n - 1));
}

// Java Program to find sum of harmonic series


// Function to return sum of
// harmonic series
static double check(double n) {
double i, s = 0.0;
for (i = 1; i <= n; i++)
s = s + 1 / i;
return s;
}

public static void main(String[] args) {


//Take input from the variables
//Create instance of the Scanner Class
Scanner sc = new Scanner(System.in);
double n;
System.out.println("Enter the value of n :");
n = sc.nextDouble(); //Initialize the variables

double out = harmonic(n);

double out_verify = check(n);

if (out == out_verify) {
System.out.println("Output = " + out + " is
matching!");
} else {
System.out.println("Not matching!");
}

}
}
Enter the value of n : 6 Output = 2.4499999999999997 is matching!
5. Read a string of any characters from the keyboard. Write a recursive
function to check if it is a palindrome or not. For example, MALAYALAM
is palindrome and PUPPY is not. Now, extend the same thing to check if a
sentence (i.e., collection of words) is a palindrome or not. For example,
MADAM TOP POT MADAM is palindrome sequence.

import java.util.Scanner;
public class program {
//Recursive function that checks
//whether the string is palindrome or not
static boolean checkPalindrome(String str, int s, int
e) {
if (s == e) // If there is only one character
return true;
// If first and last characters do not match
if ((str.charAt(s)) != (str.charAt(e)))
return false;
// If there are multiple characters, check if
// middle substring is also palindrome or not.
if (s < e + 1)
return checkPalindrome(str, s + 1, e - 1);
return true;
}
static boolean isPalindrome(String str) {
int n = str.length();
// If string is empty,then it is palindrome
if (n == 0)
return true;
return checkPalindrome(str, 0, n - 1);
}
// Driver Code
public static void main(String args[]) {
//Take input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String :");
String str = sc.nextLine(); //Input the string
//Check whether palindrome or not
if (isPalindrome(str))
System.out.println(str + " is palindrome");
else
System.out.println(str + " is not a
palindrome");
}
}
Enter the String : MALAYALAM MALAYALAM is palindrome Enter the
String : PUPPY PUPPY is not a palindrome Enter the String : MADAM TOP
POT MADAM MADAM TOP POT MADAM is palindrome

Exercise Question (Should verify by TRA)


An encoding mechanism is decided as follows. It is a two-step method to convert
a word into another word of the same length. The first and the last letters are
swapped, then the second letter and the last but one letters and so on.

The word obtained after swapping of letters are then undergo the following
conversions.

i. Convert lower case to upper case and vice-versa.

ii.Change a character to another with the following rule. A ?? D or a ?? d B ?? E


or b ?? e C ?? F or c ?? f …. …. …. …. …. W ?? Z or w ?? z X ?? a or x ?? a Y ??
B or y ?? b Z ?? C or z ?? c Write a method void encode() to encode a text read
from the keyboard. The function should print the displayed character before its
termination. Write another function void decode()to get back the original text,
which has been converted by void encode().

You might also like