0% found this document useful (0 votes)
100 views16 pages

Java Pro

1. The document contains 7 code snippets showing examples of Java programs demonstrating different concepts: - Character case conversion methods - Pass by value vs pass by reference - Method overloading - Recursion - Factorial programs - Class member visibility (private vs public) 2. The code snippets include classes with methods to calculate vowels, consonants, spaces in a string, and programs showing pass by value, recursion, factorial calculation, method overloading, and use of private and public class members and access modifiers. 3. Various concepts demonstrated include character manipulation, loops, if-else conditions, recursion, method overloading, and visibility of class members using private and public access modifiers.

Uploaded by

dinesh9866119219
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views16 pages

Java Pro

1. The document contains 7 code snippets showing examples of Java programs demonstrating different concepts: - Character case conversion methods - Pass by value vs pass by reference - Method overloading - Recursion - Factorial programs - Class member visibility (private vs public) 2. The code snippets include classes with methods to calculate vowels, consonants, spaces in a string, and programs showing pass by value, recursion, factorial calculation, method overloading, and use of private and public class members and access modifiers. 3. Various concepts demonstrated include character manipulation, loops, if-else conditions, recursion, method overloading, and visibility of class members using private and public access modifiers.

Uploaded by

dinesh9866119219
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1. Character.

toLowercase( ) method p - 271


Public class charTolowerCase {
Void count ( ) {
String text = “I am studying in ICSE 10 th class”,
Int spaces =0, vowels =0, letters =0;
Int textLength( ); //get string length
For(int i=0; i< textLength; i++) {
Char ch=character.toLowerCase(text.charAt(i));
If(ch== ‘a’|| ch==’e’ || ch= =’o’ ||ch==’u’) {
Vowel++;
}
If(Character.isLetter(ch)) {
Letters++;
}
If (Character.isWhitespace(ch)) {
Spaces++;
}

Int consonants=letters-vowels;

System.out.println(“The text contains vowels:+”\n”+”consonants:”{+consonants+”\n”+”spaces:”+space);

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++;
}

Int consonants = letters –vowels;

System.out.println(“The text contains vowels:


“+vowels+”\n”+”consonants:”+consonants+”\n”+”spaces:”+spaces);

3. A simple program on pass by value.

Class PassByValue {

Public void change( ) {

Int a=12;

System.out.println(“Original Value of a =” +a);

System.out.println(“Changed Value of a =” +value(a));

System.out.println(“Again the Value of a =” +a);

Public statec int value(int x) {

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);
}
}

1. Showing class members visibility. P - 316

Import java.io. *;

Class Emp {

Private string empname;

Private string pfno;

Public long salary;

Void inputdata( ) throws IOException{

Inputstreamreader reader = new inputstreamreader(system.in);


Bufferedreader input = new Bufferedreader(reader);

System.out.print(“Enter Name of employee::”);

Empname = input.readline( );

System.out.print(“Enter PF No. Of the Employee::”)

Pfno=input.readline( );

System.out.print(“Enter Basic Pay of the Employee::”);

String a =input.readling( );

Salary = Long.paraseLong(a);

Void outputdata( ) {

System.out.println(“ =============================================”);

System.out.println(“Name of Employee :::” +empname);

System.out.println (“PF No :::” +pfno);

System.out.println(“Basic Pay :::” +salary);

System.out.println(“D.A. :::” +(calculated( )));

System.out.println(“HRA :::” +(calculatehra( )));

System.out.println(“Gross pay :::” +(grosspay( )));

Public static void main (string args[]) throws IOException {

Emp employee=new emp( );

Employee.inputdata( );

Employee.outputdata( );

} Private double calculated( ) {

Return((0.15*salary));
}

Private double calculatehra( ) {

Return ((0.01* salary));

Private double grosspay( ) {

Return(sarary +(0.15 *salary) +(0.01 *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(“========================================”);

System.out.println(“Name of Employee :::” +empname);

System.out.println(“Basic Pay :::” +salary);


System.out.println(“D.A. :::” +(calculated( )));
System.out.println(“HRA :::” +(calculatehra( )));
}
Private double calculated( ) {

Return((0.15*salary));

Private double calculatehra( ) {

Return ((0.01* 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(“========================================”);

System.out.println(“Name of Employee :::” +empname);

System.out.println(“Address :::” +address);


System.out.println(“City :::” +city);

System.out.println(“Basic Pay :::” +salary);


}
Public void calculated( ) {
Da=1.5 * salary;
System.out.pritln(“DA=” +da);
}
Pubic void calculatehra( ) {
Hra = 0.1 *salary;
System.out.println(“HRA =” +hra);
}
Public void pfduction( ) {
Pf =0.12 * salary;
System.out.println(“PF Deduction=” +pf);
}
Public void calculategross( ) {
Grosspay =salary+hra+da;
System.out.println(“Gross salary of Employee=” +grosspay);
}
Public void calculatenetpay( ) {
Netpay =grosspay –pf;
System.out.println(“Net Pay of Employee=” +netpay);
}
}
Class privaepublicAccess{
Public void main( ) {
EmployeeExample employee=new employeeexample( );
Employee.inputdata( );
//now passing the value of employee object.
Employee.calculateda( );
Employee.calculatehra( );
Employee.calculategross( );
Employee.pfdeduction( );
Employee.calculatenetpay( );
}
}
4. Pass by reference by using private and public access.
//use of private and public access method using pass by reference
Import java.io.*;
Class RefPripub{
Private string empname;
Private stirng address;
Private string city;
Public double salary;
Double grosspay;
Double da, hra,
Values from main ( )
Void inputdata(string a pf, netpay;
//Receving, string b, string c,double value) {
Empname =a;
Address =b;
City =c;
Salary = value;
}

:::” +empname);

System.out.println(“Address :::” +address);

System.out.println(“City :::” +city);

System.out.println(“Basic Pay :::” +salary);


Da=1.5 * salary;

}
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(“========================================”);

System.out.println(“Name of Employee :::” +empname);

System.out.println(“Address :::” +address);


System.out.println(“City :::” +city);

System.out.println(“Basic Pay :::” +emo.salary);


}
Public void calculated( ) {
Emo.Da=1.5 *emo. salary;
System.out.pritln(“DA=” +emo.da);
}
Pubic void calculatehra( ) {
Emo.Hra = 0.1 *emo.salary;
System.out.println(“HRA =” +emo.hra);
}
Public void pfduction( ) {
emo.Pf =0.12 * emo. salary;
System.out.println(“PF Deduction=” + emo.pf);
}
Public void calculategross( ) {
emo.Grosspay = emo.salary+ emo.hra+ emo.da;
System.out.println(“Gross salary of Employee=” + emo.grosspay);
}
Public void calculatenetpay( ) {
emo.Netpay = emo.grosspay – emo.pf;
System.out.println(“Net Pay of Employee=” + emo.netpay);
}
}
Class NestedClasses {
Public void main( ) {
Nesting employee=new Nesting ( );
Employee.inputdata( );(“Vikas Mathotra”,”1435/1,Kalia Colony”,”Jalandhar”,18000);
Employee.outputdata( );
//now passing the value of employee object.
Employee.calculateda( );
Employee.calculatehra( );
Employee.calculategross( );
Employee.pfdeduction( );
Employee.calculatenetpay( );
}
}
6. Program
Public class AppendInsert{
Public void main( ){
String Buffer a =new stringBuffer( );
a.insert(0, “computer Applications”);
system.out.println(a);
stirngBuffer b = new stringBuffer(“Computer Applications”);
system.out.println(b);
stringBuffer c = new stringBuffer(20);
c.insert(0, “Computer Applications”);
}

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( )

public class ArrayChar {

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];

Byte b[ ] =new byte[20];

Str.getChars(beginning, end, a, 0);

Str,getBytes(5, 17, b, 0);

System.out.println(“The gerBytes characters are =” +b);

String str1 = “Malhotra& Malhotra”;

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’));
}

12. string substring(int startIndex, int endlndex)


public class Modistring {
public void main( ) {
string str = “Computer Applications”:
system.out.println(str.substring(2));
}

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( ));
}

15. changing case of string


public class stringCase {
public void main( ) {
string str1 = “Computer Applications”;
system.out.println(str1.toUpperCase( ));
}

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);
}

18. Comparing Two Strings


Class Comparestrings{
Public void main( ) {
String s1, s2;
S1 =”VIKAS”;
S2=s1;
System.out.println(“Same Thing ?” +(s1 == s2));
S2 =new string(s1);
System.out.println(“same Thing?” +(s1 ==s2));
System.out.println(“Same Result ?” +s1.equals(s2));
}

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]);

You might also like