Computer Assignment
Computer Assignment
Program :- 2
Write a program of function overloading method for
changing number, string and char.
Solution:-
import java.util.*;
class funcoverload
{
void number(int a, int b)
{
if(a>b)
System.out.println(“GREATER NO IS:-“+a);
else
System.out.println(“SMALLER NO IS:-“+a);
}
void number(string a, string b)
{
if(a.length( )>=b.length( ))
System.out.println(“FIRST STRING IS:-“+a);
else
System.out.println(“SECOND STRING IS :-“+b);
}
void number(char a)
{
System.out.print(“CHARACTER IS:-“+a);
}
public static void main(String args[])
{
funcoverload f1=new funcoverload();
f1.number(2,5);
f1.number(“H1”,”HELLO”);
f1.number(‘A’);
}
}
Variable Table
Variable Data Type Description
Name
A String To store string
length Int To store length of string
B String To store string
A Int To store integer value
B Int To store integer value
A Char To store character value
Program:- 3
Write a program of menu driven to display a piglatin
word and display the given pattern by user’s choice by
scanner method.
Eg:- B
BL
BLU
BLUE
BLUEJ
Solution:-
import java.util.*;
class piglatin
{
public static void main (String args[])
{
int I,len,ch;
String s, strl, str3, chat1;
Scanner sc=new Scanner (System.in);
System.out.println(“Enter the string”);
S=sc.nextLine();
S=s.to uppercase();
System.out.println(“Enter your choice press 1. Piglatin2.
Pattern”);
ch=sc.nextInt();
len=s.length();
Switch(ch);
{
case 1:
System.out.println(“The piglatin word is :”);
for (i-0;i<len;i++)
{
Char c=s.charAt(i);
If(c==’A’||c==’E’||c==’I’||c==’O’||c==’U’|c==’a’||
c==’e’||c==’i’||c==’o’||c==’u’)
{
break;
}
}
Strl=s.substring(i)+s.substring(o,1)+”AY”;
System.out.println(strl);
break;
case2:
System.out.println (“The pattern is:”);
For (i=0; i<len; i++)
{
System.out.println(s.substring(o,i));
}
break;
default;
System.out.println(“wrong choice”);
}
}
}
Variable Table
Variable Name Date Type Description
I String Loop variable
len Int To store length of
string
ch Int To store choice
entered by user
C Char To store
character
C String To store string
Program 4.
Write a program to accept any string then encode every
alphabet forward by one position.
Solution:-
import java.util.*;
class string|
{
Public static void main (String args[])
{
Scanner sc=new Scanner (System.in);
System.out.println(“Enter any string”);
String s=sc.nextLine();
String out=””;
for(int i=0; i<s.length();i++)
{
char ch=s.charAt(i);
if(ch>65&&ch<=89)
ch=(char)(ch+1);
else if (ch>97 &&ch<=121)
ch=’A’;
else if (ch==122)
ch=’a’;
out=out+ch;
}
System.out.println(“Encoded string =”+out);
}
}
Variable Table
Variable Name Data Type Description
S String To store string
len String To store length of
string
I int Inner loop
variable
out String To store output
string
ch String To store
character value
Program 5:
Write a program to accept a string and convert the string
into upper case and output the double letter sequence in
the string .
Solution:
import java.util.*;
class stringmeth
{
String strl.str;
Int a,b, count=0;
Scanner sc=new scanner (System.in);
System.out.println(“Enter the string”);
Str=sc.nextline();
Strl=str.to uppercase();
System.out.println(str);
Int len=strl.length();
For(int j=0; j<=len;j++)
{
a=(int)strl.charAt(j);
if(len==j+1)
{
Break;
}
b=(int) str1.charAt(j+1);
If (a==b)
{
count++;
}
}
System.out.println(“Total double letter sequence
is:”+count);
}
}
Variable Table
Variable Name Data Type Description
str String To store string
len Int To store length of
string
I Int Inner loop
variable
strl String To store
uppercase string
Count Int To store
occurrence the
letter
a Int To store integer
value
b int To store integer
value