2014
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
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
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
4 6 12
2 6 14
1 3 8
Coding:-
import java.util.Scanner;
if (n >= 20) {
System.out.print("Size is out of range");
return;
}
if (a[i][j] < 0) {
System.out.print("Invalid Input");
return;
}
}
}
System.out.println("Matrix A[ ][ ]");
printMatrix(a, n, n);
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");
}
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.*;
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;
}
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:-