Professional Documents
Culture Documents
Java Pro
Java Pro
Java Pro
Int consonants=letters-vowels;
2. Program
Public class charToupperCase {
Void count( ) {
String text =”I am a student of ICSE 10 th class”;
Int spaces=0, vowels=0, letters=0;
Int textLength =text.length( ); //Get string length
For(int i=0; i<textLength; i++) {
Char ch=character.toUpperCase(text.charAt(i));
If(ch ==’A’ ||ch ==’E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) {
Vowels++;
}
If(character.isLetter(ch)) {
Letters++;
}
If(character.isWhitespace(ch)) {
Spaces++;
}
Class PassByValue {
Int a=12;
X=10;
Return x;
4. Program
Import java.io.*;
Class Fact {
Void factorial(int n) {
Int I;
Int f =1;
For (i=1; i< =n; i++) {
F*=I;
}
System.out.println(“Factorial of “ +n+”is =” +f);
}
Public void main(int n) {
Fact nfactorial =new Fact( );
//now passing value into method
Nfactorial.factorial(n);
}
}
5. Program
Class Recurrsive {
Public static long factorial(int n) {
If(n<0) {
Return -1;
}
Else
If(n==0_ {
Return 1;
}
Else {
Return n * factorial (n -1);
}
}
}
6. Program
Class MethodOverloading {
//method to add two integer variables
Int addition(int x, int y) {
Return(x + y);
}
//method to add two float variables
Float addition(float x, float y) {
Return ( x+ y);
}
Public void main( ) {
Int n1 =10;
Int n2 =15;
Jint r1;
Float x1 =12;
Float x2 =9;
Float r2;
R1 = addition(n1,n2);
R2 =addition(x1, x2);
System.out.println(“r1=”+r1);
System.out.println(“r2=” +r2);
}
}
7. Program
Class Overload {
Void showdata(string name) {
System.out.println(“Name is “ +name);
}
Void showdata(int rollno) {
System.out.println(“Roll No.” +rollno);
}
Void showdata(float marks) {
System.out.println(“Marks” +marks);
}
Void showdata(double fee) {
System.out.println (“Fee “ +fee);
}
Public static void main( ) {
Overload test = new Overload( ) {
Test.showdata(“Ankur”);
Test.showdata(12335);
Test.showdata(498.5d);
Test.showdata(1200.50f);
}
}
Import java.io. *;
Class Emp {
Empname = input.readline( );
Pfno=input.readline( );
String a =input.readling( );
Salary = Long.paraseLong(a);
Void outputdata( ) {
System.out.println(“ =============================================”);
Employee.inputdata( );
Employee.outputdata( );
Return((0.15*salary));
}
Class Emp1{
2. Program
//class members visibility
Import java.io.*;
Class OfficeEmployee{
Private string empname;
Public long salary;
Void inputdata() throws IOException{
InputstreamReader reader =new InputstreamEReader (system.in);
BufferedReader reader=new BufferedReader(reader);’
System.out.prijnt(“Enter Name of Emploee ::”);
Empname =input.readline();
System.out.print(“Enter Basic Pay of the Employee ::”);
String a = input.readLine();
Salary =Long.parseLong(a);
}
Void outputdata() {
System.out.println(“========================================”);
Return((0.15*salary));
}
Class Emp2 {
Public static void main (string args[]) throws IOException {
Office Employee employee=new OfficeEmployee();
Employee.inputdata();
Employee.outputdata();
}
3. Program
Pass by value by using private and public access.
Import java.io *;
Class EmployeeExample {
Private string empname;
Private string address;
Private string city;
Public double salary;
Doubole grosspay;
Double da, hra, pf, netpay,
//Now Receiving Values from main( )
Void inputdata(string a, string b, string c. Double value) {
Empname = a;
Address =b;
City =c;
Salary=value;
}
Public void outputdata( ) {
System.out.println(“========================================”);
:::” +empname);
}
Public void calculated(RefPriPub x ) {
System.out.pritln(“DA=” +da);
} Public void outputdata( ) {
System.out.println(“========================================”);
System.out.println(“Name of Employee
Pubic void calculatehra(RefPriPub x) {
Hra = 0.1 *salary;
System.out.println(“HRA =” +hra);
}
Public void pfduction(RefPriPub x) {
Pf =0.12 * salary;
System.out.println(“PF Deduction=” +pf);
}
Public void calculategross(RefPriPub x) {
Grosspay =salary+hra+da;
System.out.println(“Gross salary of Employee=” +grosspay);
}
Public void calculatenetpay(RefPriPub x ) {
Netpay =grosspay –pf;
System.out.println(“Net Pay of Employee=” +netpay);
}
}
Class privaepublicAccess{
Public void main( ) {
RefPriPub employee =new RefPriPub ( );
Employee.inputdata(“Ram Shankar”, “1435/1”,”Lucknow”,8000);
Employee.outputdata( );
//now passing the reference of employee object.
Employee.calculateda(employee);
Employee.calculatehra(employee);
Employee.calculategross(employee);
Employee.pfdeduction(employee);
Employee.calculatenetpay(employee );
}
}
5. Program
//Nesting of classes with Methods
Import java.io.*;
Class nesting {
Private string empname;
Private stirng address;
Private string city;
Emoluments emo =new emoluments( ); //creating another nested class emoluments
Class Emoluments {
Public double salary;
Public Double grosspay;
Public doublie da, hra, pf, netpay;
} //End of Inner class
//Receving Values from main( )
Void inputdata(string a, string b, string c, doublic value) {
Empname =a;
Address =b;
City =c;
Emo.salary =value; //using class object
}
Public void outputdata( ) {
System.out.println(“========================================”);
7. String length
Public class stringLength{
Public void main( ) {
Char str[ ]={‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’};
String a =new string(str);
System.out.print(“Length of the string =”);
System.out.println(a.length( ));
}
8. String concatenation
Public class stringConcatenation{
Public void main( ) {
String str =”I am Poonam Mehta.” +
“I read in O P J Senior Model School, “+
“Fatehabad, Haryana,”;
System.out.println(str);
}
8. Getchars( )
Public class ExtractChar {
Public void main ( ) {
String str= “I am Poonam Mehta.” +
“I read in O P J Senior Model School, “ +
“Fatehabad, Haryana”;
Int beginning =5;
Int end =17;
Char a[ ] = new char[end _beginning];
Str.getChars(beginning, end, a, 0);
System.out.println(a);
}
}
9. getBytes( )
public class Bytechar {
public void main( ) {
string str= “I am Poonam Mahta.”+
“I read in O P J Senior Model School,” +
“Fatehabad, Haryana”;
Int beginning =5;
Int end =17;
Char a[ ] =new char[ end –beginning];
Byte b[ ] =new byte[20];
Str.getChars(beginning, end, a, 0);
Str,getBytes(5, 17, b, 0);
System.out.println(“The getBytes characters are =” +b);
}
10. tocharArray( )
“Fatehabad, Haryana”;
Int end=17;
System.out.println(str1.toCharArray( ));
}
11. lastindexof( )
public class LastIndexOfCh {
public static void main(string args[ ]) {
string s =”Honesty is the Best Policy”;
system.out.pritnln(s);
system.out.println(“lastIndexOf(b) ->” +s.lastIndexOf(98));
system.out.println(“lastIndexOf(b) ->” +s.lastIndexOf(‘b’));
}
13. concat( )
public class stringConc {
public void main( ) {
string str1 = “Computer Applications”;
string str 2= str1.concat(“For Class 10 th ICSE”);
Systm.out.println(str2);
}
14. trim( )
public class string trim {
public void main( ) {
string str1 = “ Computer Applications “;
system.out.println(str1. Trim( ));
}
16. insert
class Evar62 {
public void main( ) {
stringBuffer str = new StringBuffer(“I am solving problem.”);
str.insert(5, “learning Java and”);
system.out.println(str);
}
17. reverse( )
class Ever {
public void main ( ) {
stringBuffer str =new stringBuffer(“PRATIEK MALHOTRA”);
system.out.println(“Original String =” +str);
str.reverse( );
system.out.println(“Reversed String = “ +str);
}
19. Program
Class compareTo{
Static string s[] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, ‘Thursday”, “Friday”,
“Saturday”};
Public static void main(stiring args[]) {
For (int i=0; i<s.length; i++) {
For (int j=i+1; j<s.length; j++) {
If(s[j].compareTo(s[i]<0) {
String temp;
Temp =s[i];
S[i] =s[j];
S[j] = temp;
}
System.out.println(s[i]);