PROGRAM-10
Program to convert the case of inputted sentence and arrange the words in alphabetical order.
ALGORITHM
Step1-Start.
Step2-We have made parameterized constructor and the parameter is
String str1and we have assigned str to str1.
Step3-Input the String in the function getdata.
Step4-We have convert the case of the String and find out the length of
the String in the function proceed.
Step5-We have done type casting and stored the character in c we have
found the character of the string in ch as well as the next character
of the string in ch1.
Step6-We have checked the condition i.e. (ch== ‘’||ch1== ‘c’).If this
condition found true then go to Step7 .
Step7-We have taken out the character of the string and check the condition i.e.(ch!= ‘’).If this condition found true then go to step-8.
Step8-We have taken the character of the String in String s
Step9-Apply break
Step10-We have print the arranged string in the function disp.
Step11-Use inheritance and we have made main function in class alph
Step12-We have called the function made above and we have arrange
the string in alphabetical order
Step13-End
PROGRAM
import java.io.*;
class stream
{
String str;
stream(String str1)
{
str=str1;
}
void getdata()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter a String");
str=d.readLine();
}
void proceed()
{
int i,l,j,k;
char ch,c,ch1;
String s= new String(" ");
str=" "+str+" ";
str=str.toUpperCase();
l=str.length();
for(i=65;i<90;i++)
{
c=(char)i;
for(j=0;j<l-1;j++)
{
ch=str.charAt(j);
ch1=str.charAt(j+1);
if(ch==' ' && ch1==c)
{
for(j++;j<l;j++)
{
ch=str.charAt(j);
if(ch!=' ')
{
s=s+ch;
}
else if(ch==' ')
{
s=s+" ";
break;
}
}
}
}
}
str=" ";
str=s;
}
void disp()
{
System.out.println(str);
}
}
class alph
{
public static void main(String args[])throws Exception
{
stream ob=new stream(" ");
ob.getdata();
ob.proceed();
ob.disp();
}
}
INPUT/OUTPUT-
VARIABLE DESCRIPTION-
VARIABLE | TYPE OF VARIABLE | FUNCTION |
I ,j | Integer | For Loop |
l | Integer | Length of the string |
ch, ch1 | Character | For storing the character of the string |
str, str1,s | String | For storing the String |
********************************************************************************************
PROGRAM-11
PROGRAM TO ARRANGE GIVEN ARRAY IN
ODDS AND EVEN ORDER.
ALGORITHM
Step1- Start.
Step2-Initialise the array i.e. int a [ ] =new int[10].
Step3-Now declare the constructor arr ( ) and within it
initialize 0 to all the values of array.
Step4-Now make another function input ( ).
Step5-Initialise a loop from 0 to 9 i.e. for(x=0; x<10; x++).
Step6-Within this loop accept the number by the user and store number in a[ ].
Step7-Now make another function arrange ( ).
Step8-Within the function initialize a loop from 0 to 9 i.e. for(x=0; x<10; x++) and initialise temp=0.
Step9-Run another loop from 0 to 8 i.e. for(y=0; y<9; y++) and check if (a[y] %2==0 && a[y+1%2! =0).If true then goto step 10.
Step10-Initialise temp=a[y], a[y] =a[y+1] and a[y+1] =temp and then close all the loops.
Step11-Initialise another loop for(x=0; x<10; x++) and print the values of a[x].
Step12-End
PROGRAM
import java.io.*;
class arr
{
int x;
int a[]=new int[10];
arr()
{
for(x=0;x<10;x++)
{
a[x]=0;
}
}
void input()throws IOException
{
int x;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(x=0;x<10;x++)
{
System.out.println("Enter any number");
a[x]=Integer.parseInt(br.readLine());
}
}
void arrange()
{
int x,y;
for(x=0;x<01;x++)
{
int temp=0;
for(y=0;y<9;y++)
{
if(a[y]%2==0&&a[y+1]%2!=0)
{
temp=a[y];
a[y]=a[y+1];
a[y+1]=temp;
}
}
}
for(x=0;x<10;x++)
{ System.out.println(a[x]);
}
}
}
INPUT/OUTPUT-
VARIABLE DESCRIPTION
VARIABLE | TYPE OF VARIABLE | FUNCTION |
a[ ] | Integer | Input10 numbers. |
x,y | Integer | Loop variables |
Temp | Integer | Counter variable |
********************************************************************************************
PROGRAM-12
TO PRINT FOLLOWING PATTERN
AAAA1
BBB12
CC123
D1234
12345
ALGORITHM-
Step-1 Start
Step-2 Take the limit value from the user.
Step-3 Initialization of variables.
Step-4 Take the length of the string.
Step-5 Run the loop for printing the no. of lines.
Step-6 Run the loop for printing the alphabets.
Step-6 Print the alphabets
Step-7 Run the loop to print the numbers.
Step-8 Print the number/s.
Step-9 Change the line.
Step-10 Exit
PROGRAM
class pattern9
{
public static void main(int n)
{
int a,b,c;
char s='A';
for (a=1;a<=n;a++)
{
for (b=1;b<=n-a;b++)
{
System.out.print(s);
}
for (c=1;c<=a;c++)
{
System.out.print (c);
}
System.out.println();
s++;
}
}
}
VARIABLE DESCRIPTION-
VARIABLE USED | DATATYPE | DESCRIPTION |
n | Integer type | To store the limit value. |
a | Integer type | For running the loop to print the no. of lines. |
b | Integer type | For running the loop to print the alphabets. |
c | Integer type | For running the loop to print the numbers. |
s | Character type | Counter variable for printing alphabets. |
INPUT/OUTPUT-
INPUT | OUTPUT |
3 | AA1 B12 123 |
6 | AAAAA1 BBBB12 CCC123 DD1234 E12345 123456 |
No comments:
Post a Comment