Search This Blog

Loading...

Sunday, November 20, 2016

BlueJ Program to check whether the values in a list are in order


In this program, user will enter number of values in an array and the program will check whether the values are in order
(ascending).

For example if the values in the array are 3 5 23 32 34 90, the the output will be " The values are in order." and if the numbers are 3 5 23 32 34 90 21, the output would be "The values are not in order.".


import java.io.*;

class Order
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[];
int i;

Order()
{
 arr=new int[10];
}

public void takeValues() throws Exception
{
for(i=0;i<=9;i++)
{
System.out.print("\nEnter Value :");
arr[i]=Integer.parseInt(br.readLine());
}
}

public void show()
{
System.out.println("\nValues are :");
for(i=0;i<=9;i++)
{
System.out.print(" "+arr[i]);
}
}

boolean isOrder()
{
for(i=0;i<=8;i++)
{
 if(arr[i]>arr[i+1])
break;
}
if (i>8)
return true;
else
return false;
}

public static void main(String args[])throws Exception
{
 Order ob=new Order();
ob.takeValues();
ob.show();
if(ob.isOrder())
System.out.print("\nValues are in order.");
else
System.out.print("\nValues are not in order.");
}
}

Sunday, November 13, 2016

BlueJ Program to remove first and last character of each word from a sentence





In this program, user will enter a sentence and first and last characters from each words of the sentence will be removed.

If any word in the sentence contains only 1 or 2 characters then the word will be removed.

Example: Input sentence : This is a test for removing characters from each words
         Output sentence: hi es o emovin haracter ro ac ord


import java.io.*;
import java.util.*;
class Pat
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stk;
String s,s1,s2="";
int len;
public void takeSentence() throws Exception
{

System.out.print("Enter the sentence :");
s1=br.readLine();
}
public void modify()
{
stk=new StringTokenizer(s1);
while(stk.hasMoreTokens())
{
 s=stk.nextToken();
 len=s.length();
if(len >2)
 s2=s2+ " " + s.substring(1,len-1);

s2=s2.trim();
s1=s2;
}

public void show()
{
System.out.println(s1);
}
public static void main(String args[])throws Exception
{
 Pat ob=new Pat();
ob.takeSentence();
System.out.print("\nInput Sentence:");
ob.show();
ob.modify();
System.out.print("Output Sentence:");
ob.show();
}
}

Thursday, November 10, 2016

Mystic Number program using BlueJ



What is mystic number?


A mystic number is a number whose sum of the digits is 3. Point to be noted that the adding of the digits continue till it becomes 3.

For example, 552 is a mystic number. 5 + 5 + 2= 12 and again 1+2 = 3.

import java.io.*;
class Pat
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int n,sum;

public void show() throws Exception
{

System.out.print("Enter the number :");
n=Integer.parseInt(br.readLine());

while(n >9)
{
 sum=0;
while(n >0)
{
 sum+=n%10;
n=n/10;
}
 n=sum;
}
if(n==3)
System.out.println("\nThe number is a mystic number.");
else
System.out.print("The number is not a mystic number.");
}
public static void main(String args[])throws Exception
{
 Pat ob=new Pat();
ob.show();
}
}

Friday, November 4, 2016

ISC 2016 Computer Practical paper and solution



 Program 1:

Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is repeated until the original number is reached again.
A number is said to be prime if it has only two factors I and itself.
Example:
131
311
113
Hence, 131 is a circular prime.
Test your program with the sample data and some random data:
Example 1
INPUT :N = 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME
Example 2
INPUT :N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME
Example 3
INPUT :N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME
Question:
Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is repeated until the original number is reached again.
A number is said to be prime if it has only two factors I and itself.
Example:
131
311
113
Hence, 131 is a circular prime.
Test your program with the sample data and some random data:
Example 1
INPUT :N = 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME
Example 2
INPUT :N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME
Example 3
INPUT :N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME
Programming Code:
import java.io.*;
class CPrime
{
 int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void takeNumber()throws Exception
{
 while(true)
{
 System.out.println("Enter the Number (0 to exit):");
 n=Integer.parseInt(br.readLine());
  if(n==0)
break;
 if(isCircularPrime(n))
  System.out.println(n+ " is a Circular Prime Number.");
else
  System.out.println(n+ " is not a Circular Prime Number.");
}
}
boolean isCircularPrime(int n)
{
 int a,flag=1;
String s;
a=n;
System.out.println(a);
do
{
 if(isPrime(a))
{
 s=String.valueOf(a);
s=s.substring(1)+s.charAt(0);
a=Integer.parseInt(s);
System.out.println(a);
}
else
{
flag=0;
break;
}
}while(a!=n);
if(flag==1)
return true;
else
return false;
}
boolean isPrime(int n)
{
 int i;
for(i=2;i<=n-1;i++)
{
 if(n%i==0)
break;
}
if(i==n)
return true;
else
return false;
}
public static void main(String args[]) throws Exception
{
 CPrime ob=new CPrime();
ob.takeNumber();
}
}



 Program 2:

Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:
(a) Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange them in the matrix.
(b) Calculate the sum of both the diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix with their sum.
Test your program with the sample data and some random data:
Example 1
INPUT :M = 4
9   2   1   5  
8   13  8   4  
15  6   3   11 
7   12  23  8  
OUTPUT:
ORIGINAL MATRIX
9   2   1   5  
8   13  8   4  
15  6   3   11 
7   12  23  8  
REARRANGED MATRIX
9   2   1   5  
8   3   6   4  
15  8   13  11 
7   12  23  8  
DIAGONAL ELEMENTS
9           5  
    3   6      
    8   13     
7           8      
SUM OF THE DIAGONAL ELEMENTS = 59
Programming Code:


import java.io.*;
class CSort
{
 int m,arr[][],dummy[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void takeNumber()throws Exception
{
 while(true)
{
 System.out.println("Enter the Number of rows(3 to 10):");
 m=Integer.parseInt(br.readLine());
  if(m>=3 && m<=10)
break;
}
arr=new int[m][m];
dummy=new int[(m-2)*(m-2)];
 for(int i=0;i<=m-1;i++)
{
 for(int j=0;j<=m-1;j++)
{
  System.out.println("Enter Value:");
arr[i][j]=Integer.parseInt(br.readLine());
}
}

}
void show()
{
 for(int i=0;i<=m-1;i++)
{
 for(int j=0;j<=m-1;j++)
{
  System.out.print(" "+arr[i][j]);

}
System.out.println();
}
}
void takeToDummy()
{
 int x=0;
for(int i=0;i<=m-1;i++)
{
for(int j=0;j<=m-1;j++)
{
if(i!=0 && i!=m-1 && j!=0 && j!=m-1)
dummy[x++]=arr[i][j];
}
}

}

void takeToArray()
{
 int x=0;
for(int i=0;i<=m-1;i++)
{
for(int j=0;j<=m-1;j++)
{
if(i!=0 && i!=m-1 && j!=0 && j!=m-1)
arr[i][j]=dummy[x++];
}
}
}
void showDiagonals()
{
 int left,right;
left=right=0;
System.out.println("Diagonal values are as follows.\n");
for(int i=0;i<=m-1;i++)
{
 for(int j=0;j<=m-1;j++)
{
if (i==j ||i+j==m-1)
 System.out.print(" "+arr[i][j]);
else
 System.out.print(" ");
if(i==j)
left+=arr[i][j];
else if(i+j==m-1)
right+=arr[i][j];
}
System.out.println();
}
System.out.println("Sum of Left Diagonal values="+left);
System.out.println("Sum of Right Diagonal values="+right);
}
void sort()
{
 int i,j,t;
for(i=0;i<(m-2)*(m-2)-1;i++)
{
 for(j=i+1;j<(m-2)*(m-2);j++)
{
 if(dummy[i]>dummy[j])
{
t=dummy[i];
dummy[i]=dummy[j];
dummy[j]=t;
}
}
}
}
public static void main(String args[]) throws Exception
{
 CSort ob=new CSort();
ob.takeNumber();
System.out.println("Original Array\n");
ob.show();
ob.takeToDummy();
ob.sort();
ob.takeToArray();
System.out.println("Arranged Array\n");
ob.show();
ob.showDiagonals();
}
}


 Program 3:

Write a program to accept a sentence which may be terminated by '.', '?' or '!' only. The words may be separated by
more than one blank space and are in upper case.

Perform the following tasks:
a) Find the number of words beginning and ending with a vowel.
b) Place the words which begin and end with a vowel at the beginning, followed by the words as they occur in the sentence.

Test your program with the sample date:

Example 1:

INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL: 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

Example 2: 

INPUT:  YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL: 2
A ARE YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.


Example 3: 

INPUT:  LOOK BEFORE YOU LEAP.

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL: 0
LOOK BEFORE YOU LEAP.

Example 4: 

INPUT:  HOW ARE YOU@

OUTPUT: INPUT INPUT

import java.io.*;
class CPrime
{
 int x=0,y=0,i;
char ch;
String str,s;
String vowel[],other[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void takeString()throws Exception
{
System.out.println("Enter the String (in upper case):");
 str=br.readLine().toUpperCase();
 ch=str.charAt(str.length()-1);
if((ch>=65 && ch<=90) || ch=='.' ||ch=='?' ||ch=='!')
{}
else
{
System.out.println("Wrong Input:");
Return;
}
System.out.println(" Original String:"+str);
if( ch=='.' ||ch=='?' ||ch=='!')
str=str.substring(0,str.length()-1);
vowel=new String[str.length()];
other=new String[str.length()];
}
void breakString()
{
 while(true)
{
 i=str.indexOf(' ');
if(i==-1)
break;
s=str.substring(0,i).trim();
str=str.substring(i+1).trim();
if (sVowel(s))
vowel[x++]=s;
else
other[y++]=s;
}
if (sVowel(str))
vowel[x++]=str;
else
other[y++]=str;
}
boolean sVowel(String s)
{
 int flag=0;
switch(s.charAt(0))
{
 case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
flag+=1;
}
switch(s.charAt(s.length()-1))
{
 case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
flag+=1;
}
if(flag==2)
return true;
else
return false;
}

void sort()
{
 int i,j;
String t;
for(i=0;i<=x-2;i++)
{
 for(j=i+1;j<=x-1;j++)
{
 if(vowel[i].compareTo(vowel[j])>0)
{
t=vowel[i];
vowel[i]=vowel[j];
vowel[j]=t;
}
}
}
}
void show()
{
System.out.println("\n Number of words beginning and ending with vowel:"+x);
 str="";
for(i=0;i<=x-1;i++)
str=str+ " " + vowel[i];
for(i=0;i<=y-1;i++)
str=str+ " " + other[i];
if (ch=='.' ||ch=='?' ||ch=='!')
str=str.trim()+ch;
System.out.print(" Modified String:"+str);
}
public static void main(String args[]) throws Exception
{
 CPrime ob=new CPrime();
ob.takeString();

ob.breakString();

ob.sort();
ob.show();

}

}

CHECK HERE FOR PREVIOUS POSTS

Google+ Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner