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

2014

The document describes 3 coding questions and solutions: 1. A class template 'Calculator' with methods to accept input values, find the max, min, and difference of the values and display the results. 2. A program to input values into a matrix, output the original matrix, find any saddle point values, and sort the main diagonal using insertion sort. 3. A program to read a sentence, arrange the words by length in ascending order while maintaining original order for words of same length, and output the sentence with first letter capitalized.

Uploaded by

vanikapoor2385
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)
51 views11 pages

2014

The document describes 3 coding questions and solutions: 1. A class template 'Calculator' with methods to accept input values, find the max, min, and difference of the values and display the results. 2. A program to input values into a matrix, output the original matrix, find any saddle point values, and sort the main diagonal using insertion sort. 3. A program to read a sentence, arrange the words by length in ascending order while maintaining original order for words of same length, and output the sentence with first letter capitalized.

Uploaded by

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

2014 :-

Question 1 :-
Write a class template 'Calculator' with the
following specifications:
Class : Display
Data members/Instance variables
int a,b,c;
Member Methods:
Name Purpose

void to accept the values of a and


Accept() b.

to find greater of the two


void number 'a' and 'b' and store
Max() the result in c. Display the
result.

to find smaller of the two


void number 'a' and 'b' and store
Min() the result in c. Display the
result.

to store the difference


void between 'a' and 'b' and store
Diff() the result in c. Display the
result.

CODING :-
import java.util.Scanner;
class Display {
int a;
int b;
int c;

void Accept() {
Scanner in = new
Scanner(System.in);
System.out.print("Enter value of
a: ");
a = in.nextInt();
System.out.print("Enter value of
b: ");
b = in.nextInt();
}

void Max() {
c = a > b ? a : b;
System.out.println("Greater
Number = " + c);
}

void Min() {
c = a < b ? a : b;
System.out.println("Smaller
Number = " + c);
}

void Diff() {
c = a - b;
System.out.println("Difference =
" + c);
}
public static void main(String
args[]) {
Display obj = new Display();
obj.Accept();
obj.Max();
obj.Min();
obj.Diff();
}}

QUESTION 2
1. Write a program to declare a square matrix A [ ] ] of order N (N<20).
2. Allow the user to input positive integers into this matrix. Perform the
following tasks on the matrix.
3. Output the original matrix.
4. Find the SADDLE POINT for the matrix. A saddle point is an element of
the matrix such that it is the minimum element for the row and the
maximum element for the column to which it belongs. Saddle point for a
given matrix is always unique. If the matrix has no saddle point, output
the message "NO SADDLE POINT".
5. If the matrix has a saddle point element then sort the elements of the
left diagonal in ascending order using insertion sort technique. All other
elements should remain unchanged.

Test your program for the following data and some random data:
Sample data:

Input:
n=4

Matrix A[ ][ ]

2 5 6 9

8 4 12 3

6 7 3 1

12 24 2 11

Output:

Matrix A[ ][ ]

2 5 6 9

8 4 12 3

6 7 3 1

12 24 2 11

No Saddle Point

Matrix after sorting the Principal diagonal:

2 5 6 9

8 3 12 3

6 7 4 1

12 24 2 11

Input:
n=3
Matrix A[ ][ ]

4 6 12

2 8 14

1 3 6

Output:

Matrix A[ ][ ]

4 6 12

2 8 14

1 3 6

Saddle Point = 4

Matrix after sorting the Principal diagonal:

4 6 12

2 6 14

1 3 8

Coding:-
import java.util.Scanner;

public class welcome


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of the matrix: ");
int n = in.nextInt();

if (n >= 20) {
System.out.print("Size is out of range");
return;
}

int a[][] = new int[n][n];

System.out.println("Enter elements of the matrix: ");


for (int i = 0; i < n; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt()

if (a[i][j] < 0) {
System.out.print("Invalid Input");
return;
}
}
}

System.out.println("Matrix A[ ][ ]");
printMatrix(a, n, n);

//Find the Saddle Point


boolean found = false;
for (int i = 0; i < n; i++) {
int rMin = a[i][0];
int cIdx = 0;

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


if (rMin > a[i][j]) {
rMin = a[i][j];
cIdx = j;
}
}

int k = 0;
for (k = 0; k < n; k++) {
if (rMin < a[k][cIdx]) {
break;
}
}

if (k == n) {
found = true;
System.out.println("Saddle Point = " + rMin);
break;
}
}

if (!found) {
System.out.println("No Saddle Point");
}

//Sort Left Diagonal with Insertion Sort


for (int i = 1; i < n; i++) {
int key = a[i][i];
int j = i - 1;

while (j >= 0 && a[j][j] > key) {


a[j + 1][j + 1] = a[j][j];
j--;
}

a[j + 1][j + 1] = key;


}

System.out.println("Matrix after sorting the Principal


diagonal:");
printMatrix(a, n, n);
}

public static void printMatrix(int arr[][], int m, int n) {


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Output :-

QUESTION 3-
Read a single sentence which terminates with a full stop (.). The words
are to be separated with a single blank space and are in lower case.
Arrange the words contained in the sentence according to the length
of the words in ascending order. If two words are of the same length
then the word occurring first in the input sentence should come first.
For both, input and output the sentence must begin in upper case.

Test your program for given data and also some random data:

Input:
The lines are printed in reverse order.
Output:
In the are lines order printed reverse.

Input:
Print the sentence in ascending order.
Output:
In the print order sentence ascending.
Input:
I love my Country.
Output:
I my love Country.

Coding :-

import java.util.*;

public class KboatWordLenSort


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
int len = str.length();

if (str.charAt(len - 1) != '.') {
System.out.println("Invalid Input!");
System.out.println("Sentence should end with full
stop.");
return;
}

if (Character.isLowerCase(str.charAt(0))) {
System.out.println("Invalid Input!");
System.out.println("Sentence should start with upper
case letter.");
return;
}

String ipStr = Character.toLowerCase(str.charAt(0)) +


str.substring(1, len - 1);
StringTokenizer st = new StringTokenizer(ipStr);
int wordCount = st.countTokens();
String strArr[] = new String[wordCount];

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


strArr[i] = st.nextToken();
}

for (int i = 0; i < wordCount - 1; i++) {


for (int j = 0; j < wordCount - i - 1; j++) {
if (strArr[j].length() > strArr[j + 1].length()) {
String t = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = t;
}
}
}

strArr[0] = Character.toUpperCase(strArr[0].charAt(0))
+ strArr[0].substring(1);

System.out.println("Sorted String:");
for (int i = 0; i < wordCount; i++) {
System.out.print(strArr[i]);
if (i == wordCount - 1) {
System.out.print(".");
}
else {
System.out.print(" ");
}
}
}
}

Output:-

You might also like