0% found this document useful (0 votes)
18 views

Practical Exam

Uploaded by

sethianehal213
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)
18 views

Practical Exam

Uploaded by

sethianehal213
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/ 6

Program 1

Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N
> 2 and N < 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
1. Sort the elements of the single-dimensional array in ascending order using any standard
sorting technique and display the sorted elements.
2. Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
1258
1251
1212
1125
3. Display the filled matrix in the above format.
Test your program for the following data and some random data:
Example 1
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
137
131
113

Code
import java.util.Scanner;

public class Array


{
public static void sortArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("ENTER VALUE OF N: ");
int n = in.nextInt();

if (n <= 2 || n >= 10) {


System.out.println("MATRIX SIZE OUT OF RANGE");
return;
}

int a[] = new int[n];


int b[][] = new int[n][n];

System.out.println("ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:");


for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}

sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}

for (int i = n - 1, r = 0; i >= 0; i--, r++) {


for (int j = 0; j <= i; j++) {
b[r][j] = a[j];
}

for (int k = n - 1; k > i; k--) {


b[r][k] = a[k - i - 1];
}
}

System.out.println();
System.out.println("FILLED MATRIX:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}

Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The words
are to be separated by a single blank space and are in uppercase.
Perform the following tasks:
(a) Check for the validity of the accepted sentence.
(b) Convert the non-palindrome words of the sentence into palindrome words by concatenating the
word by its reverse (excluding the last character).
Example:
The reverse of the word HELP would be LEH (omitting the last alphabet) and by concatenating both,
the new palindrome word is HELPLEH. Thus, the word HELP becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would become ABBA and not
ABBBA and XAZZZ becomes XAZZZAX.
[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]
(c) Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
INPUT:
THE BIRD IS FLYING.
OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF

CODE
import java.util.*;

public class Palindrome


{

public static boolean isPalindrome(String word) {


boolean palin = true;

int len = word.length();


for (int i = 0; i <= len / 2; i++) {
if (word.charAt(i) != word.charAt(len - 1 - i)) {
palin = false;
break;
}
}

return palin;
}

public static String makePalindrome(String word) {


int len = word.length();
char lastChar = word.charAt(len - 1);
int i = len - 1;
while (word.charAt(i) == lastChar) {
i--;
}

StringBuffer sb = new StringBuffer(word);


for (int j = i; j >= 0; j--) {
sb.append(word.charAt(j));
}

return sb.toString();
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.println("ENTER THE SENTENCE:");
String ipStr = in.nextLine().trim().toUpperCase();
int len = ipStr.length();

char lastChar = ipStr.charAt(len - 1);


if (lastChar != '.'
&& lastChar != '?'
&& lastChar != '!') {
System.out.println("INVALID INPUT");
return;
}
String str = ipStr.substring(0, len - 1);

StringTokenizer st = new StringTokenizer(str);


StringBuffer sb = new StringBuffer();

while (st.hasMoreTokens()) {
String word = st.nextToken();
boolean isPalinWord = isPalindrome(word);
if (isPalinWord) {
sb.append(word);
}
else {
String palinWord = makePalindrome(word);
sb.append(palinWord);
}
sb.append(" ");
}

String convertedStr = sb.toString().trim();

System.out.println();
System.out.println(ipStr);
System.out.println(convertedStr);
}
}

Program 3
An Evil number is a positive whole number which has even number of 1's in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of 1's. A few evil numbers are
3, 5, 6, 9…. Design a program to accept a positive whole number and find the binary equivalent of
the number and count the number of 1's in it and display whether it is a Evil number or not with an
appropriate message. Output the result in format given below:
Example 1
Input: 15
Binary Equivalent: 1111
No. of 1's: 4
Output: Evil Number

CODE
import java.util.Scanner;

public class KboatEvilNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = in.nextInt();
if (n < 0) {
System.out.println("Invalid Input");
return;
}
int count = 0;
int p = 0;
int binNum = 0;

while (n > 0) {
int d = n % 2;
if (d == 1)
count++;
binNum += (int)(d * Math.pow(10, p));
p++;
n /= 2;
}

System.out.println("Binary Equivalent: " + binNum);


System.out.println("No. of 1's: " + count);

if (count % 2 == 0)
System.out.println("Output: Evil Number");
else
System.out.println("Output: Not an Evil Number");
}
}

PROGRAM 4
Snowball string
CODE
import java.io.*;
class snowball
{
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string terminated by a full stop(.)");
String s=br.readLine();
int l = s.length();
int iofs = s.indexOf(' ');
String sw = s.substring(0,iofs);
int lsw = sw.length();

int i, p;
boolean flag = true;
p = iofs+1;

for(i=iofs+1; i < l;i++){

char ch = s.charAt(i);
if(ch == ' ' || ch == '.'){
String w = s.substring(p,i);

if(lsw > w.length()){


flag = false;
break;
}
p = i+1;
}
}

if(flag)
System.out.println("Snowball string");
else
System.out.println("Not a snowball string");

}//eom
}

You might also like