Professional Documents
Culture Documents
Shusanto Project
Shusanto Project
8)
Consequtive characters
9)
encode of string
10)
Bubble sort
Java
With
blue-j
environment
1) /**WAP to accept a single dimensional
import java.io.*;
class SaddlePoint
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the order of the matrix : ");
int n=Integer.parseInt(br.readLine());
int
if(f==0)
{
System.out.println("No saddle point");
}
}
}
import java.util.*;
public class subarray
{
public static void main (String args[])
{
Scanner sc= new Scanner (System.in);
int n, i, j, sm, k, s=0, num;
System.out.println ("Enter size of array:");
n=sc.nextInt ();
int a[]= new int [n];
System.out.println ("Enter "+n+" numbers in array:");
for (i=0; i<n; i++)
a[i]= sc.nextInt ();
System.out.println ("Enter the number for subarrays:");
num=sc.nextInt ();
for (i=0; i<n-1; i++)
{
sm= a[i];
for (j=i+1; j<n; j++)
{
sm+=a[j];
if (sm==num)
{
s++;
System.out.print ("{");
for (k=i; k<=j; k++)
System.out.print (a[k]+" ");
System.out.print ("}");
}//if
} //for loop (inner)
}//for loop (outer)
if (s>0)
{
System.out.println (" are the subarrays");
System.out.println ("No. of subarrays: "+s);
}//if
else
System.out.println ("No subarrays");
}//main
}//class
5)//If in a nxn matrix the four corners are Prime its a Symmetric
matrix else not Symmetric matrixa
import java.util.*;
class corners
{
int prime(int x)
{
int f=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
f++;
}
return f;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
corners ob=new corners();
System.out.println("Enter size of array");
int n=sc.nextInt();
int ar[][]=new int[n][n];
int i,j;
System.out.println("Enter array values");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
ar[i][j]=sc.nextInt();
}
}
System.out.println("The original array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(ar[i][j]+"\t");
}
System.out.println();
}
int a=ob.prime(ar[0][0]);
int b=ob.prime(ar[0][n-1]);
int c=ob.prime(ar[n-1][0]);
int d=ob.prime(ar[n-1][n-1]);
if(a==2&&b==2&&c==2&&d==2)
System.out.println("Symmetric matrix");
else
System.out.println("Not a symmetric matrix");
}
}
6)// Searching a particular city from a list using binary search method
import java.util.*;
public class binarystring
{
public static void main(String []args)
{
Scanner sc=new Scanner (System.in);
String
c[]={"AGRA","BHOPAL","DELHI","GANGTOK","JAMMU","KOLKATA","
MYSORE","PATNA","SHIMLA","UDAIPUR"};
int flg=0,fst=0,lst=9,md=0;
System.out.println("The cities from the array are :");
for(int i=0;i<10;i++)
{
System.out.print(c[i]+" ");
}
System.out.println();
System.out.println("Enter city name to Search:");
String str=sc.nextLine().toUpperCase();
while(fst<=lst)
{
md=(fst+lst)/2;
if(c[md].compareTo(str)<0)
fst=md+1;
if(c[md].compareTo(str)> 0)
lst=md-1;
if(c[md].compareTo(str)==0)
{
flg=1;
break;
}//if
}//while
if(flg==1)
System.out.println("City name Present in the Array at position
"+(md+1));
else
System.out.println("City name not Present in the Array ");
}}
7 )To display Pascal Triangle
import java.util.*;
public class Pascal_Triangle
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,n,k,p=5;
int m[]=new int[20];
System.out.println("Enter number of rows of the pascal
Triangle");
n=in.nextInt();
m[0]=1;
for(i=0;i<n;i++)
{
for(k=p;k>=1;k--)
System.out.print("");
for(j=0;j<=i;j++)
System.out.print(m[j]+"");
System.out.println();
p--;
for(j=i+1;j>0;j--)
m[j]=m[j]+m[j-1];
}
}
}
8//Consequtive characters
import java.util.*;
public class conscecutivestar
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word ");
String s=sc.next();
s=s+" ";s=s.toUpperCase();
for(int i=0;i<s.length()-1;i++)
{
int c=(int)s.charAt(i);
int c1=(int)s.charAt(i+1);
if(c1==(c+1))
System.out.print("*");
else
System.out.print(s.charAt(i));
}
}
}
9)//encode of string
public class encode
{
public static void main(String str)
{
str=str.toUpperCase();
String s="";
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
s=s+ch;
else
{
if (ch=='Y')
ch='A';
else
if(ch=='Z')
ch='B';
else
ch+=2;
s=s+ch;
}
}
System.out.println("The new String :"+s);}}
10//Bubble sort
import java.io.*;
public class sort
{
public static void main (String args[]) throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i,t,k;
int ar[]=new int[5];
System.out.println("Enter 5 no. in the array one by one ");
for(i=0;i<5;i++)
ar[i]=Integer.parseInt(in.readLine());
for(i=0;i<5-1;i++)
{
for(k=0;k<5-i-1;k++)
{
if(ar[k]< ar[k+1])
{
t=ar[k];ar[k]=ar[k+1];ar[k+1]=t; }// if
}//k
}//i
System.out.println(" Array in descending order ");
for(i=0;i<5;i++)
System.out.print(ar[i]+" ");
}
}
ACKNOWLEDGEMENT
https://www.GOOGLE.com
https://YAHOO.com/free-java-projects
BOSE NAGAR
MADHYAMGRAM
Test your program with the sample data and some random data:
Example 1
INPUT : N = 5
Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D
Key: B C D A A
OUTPUT : Scores :
Participant 1 D A B C C
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2
Example 2
INPUT : N = 4
Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B
Key: A C D B B
OUTPUT : Scores :
Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3
Highest score:
Participant 1
Participant 4
Example 3
INPUT : N = 12
import java.io.*;
class Quiz
{
public static void main(String args[])throws IOException
{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Number of participants: ");
int n = Integer.parseInt(br.readLine());
int highest = 0;
if(n < 4 || n > 10){
System.out.println("INPUT SIZE OUT OF RANGE.");
return;
}
char q[][] = new char[n][5];
char a[] = new char[5];
int score[] = new int[n];
System.out.println("Key to the questions:");
for(int i = 0; i < a.length; i++)
a[i] = br.readLine().charAt(0);
System.out.println("Answers by participants:");
for(int i = 0; i < n; i++){
System.out.println("Participant " + (i + 1));
for(int j = 0; j < 5; j++){
q[i][j] = br.readLine().charAt(0);
if(q[i][j] == a[j])
score[i]++;
}
if(highest < score[i])
highest = score[i];
}
for(int i = 0; i < n; i++)
System.out.println("Participant " + (i + 1) + " = " + score[i]);
System.out.println("Highest score(s):");
for(int i = 0; i < n; i++)
if(score[i] == highest)
System.out.println("Participant " + (i + 1));
}
}
INPUT : N = 726
OUTPUT :
48 x 15 = 720
6 x 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
Example 2
INPUT : N = 140
OUTPUT :
48 X 2 = 96
24 x 1 = 24
12 x 1 = 12
6 x 1 = 6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6
Example 3
INPUT : N = 4296
import java.util.*;
class BoxPacking_ISC2017
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Output:
Enter number of boxes to be packed : 815
OUTPUT :
48 x 16 = 768
24 x 1 = 24
12 x 1 = 12
6 x 1 = 6
Remaining Boxes 5 x 1 = 5
Question:3(ISE 2015)
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. Print an error message if the input does not terminate with ‘.’
or ‘?’. You can assume that no word in the sentence exceeds 15
characters, so that you get a proper formatted output.
OUTPUT:
Intelligence Plus Character Is Education
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
OUTPUT:
God is Great
God 1 2
Is 1 1
Great 2 3
Example 3
OUTPUT:
Invalid Input.
Programming Code:
/**
* The class Q3_ISC2015 inputs a
sentence, converts the first letter of each
word to
* uppercase and find the number of vowels
and consonants
* @author : www.guideforschool.com
* @Program Type : BlueJ Program – Java
* @Question Year : ISC Practical 2015
Question 3
*/
import java.util.*;
class Q3{
public static boolean isVowel( char ch
){
String vowel="aeiouAEIOU";
return vowel.indexOf(ch)>=0;
}
public static boolean isConsonant(
char ch ){
return Character.isLetter(ch) &&
!isVowel(ch);
}
public static void main( String args[] ){
Scanner sc = new Scanner(
System.in );
System.out.print( "INPUT: ");
String input = sc.nextLine();
char ch=input.charAt(input.length()-
1);
if( ch!='.' && ch!='?'){
System.out.println("OUTPUT:Inva
lid Input");
}else{
input=input+" ";
String word="";
int vowels=0, consonants=0;
String
ans="",out=String.format("%-
16s\t%s\t%s\n","Word","Vowels","Conson
ants");
for( int i=0; i<input.length()-1;i++
){
ch=input.charAt(i);
if(ch==' ' || ch==',' || ch=='?' ||
ch=='.' || ch==';' ){
out+=String.format("%-
16s\t%2d\t%2d\n",word,vowels,consonan
ts);
ans=ans+word+ " ";
word="";
vowels=consonants=0;
}else{
if(isVowel(ch)) vowels++;
if(isConsonant(ch))
consonants++;
if(word.equals(""))
ch=Character.toUpperCase(ch);
word = word+ch;
}
}
ans=ans.trim();
System.out.println("OUTPUT:\n"+
ans+"\n"+out);
}
}
}