0% found this document useful (0 votes)
50 views

1.java Classes To Explain Inheritance: Package Class Float Class Extends Int Public Static Void New

1. The document contains 5 code snippets demonstrating different Java concepts: inheritance using classes Employee and Programmer, finding the largest number in an array, calculating factorials using recursion, differences between String and StringBuffer, and adding/removing elements from an ArrayList. 2. The first snippet shows a Programmer class inheriting from an Employee class, allowing access to the salary field while also including a bonus field. 3. The second snippet finds the largest number in an integer array by iterating through the array and comparing each element to the current largest number stored in a variable.

Uploaded by

satya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

1.java Classes To Explain Inheritance: Package Class Float Class Extends Int Public Static Void New

1. The document contains 5 code snippets demonstrating different Java concepts: inheritance using classes Employee and Programmer, finding the largest number in an array, calculating factorials using recursion, differences between String and StringBuffer, and adding/removing elements from an ArrayList. 2. The first snippet shows a Programmer class inheriting from an Employee class, allowing access to the salary field while also including a bonus field. 3. The second snippet finds the largest number in an integer array by iterating through the array and comparing each element to the current largest number stored in a variable.

Uploaded by

satya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Java Classes to Explain Inheritance

package End_Sem_Exam;
class Employee{
float salary=40000;
}
class programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
programmer p=new programmer();
System.out.println("programmer salary
is:"+p.salary);
System.out.println("Bonus of programmer
is:"+p.bonus);
}
}
//this explains inheritance
//programmer inherits employee
2.Finding Largest Number in an Array

package End_Sem_Exam;

public class Largest {


public void main(String args[]){
int number[] = {23,12,13,24,24,56,75,46};
int largest_number = number[0];
for(int i =0; i<=8; i++){
if(largest_number<number[i]){
largest_number=number[i];
}
else{
largest_number=largest_number+0;
}
}
}
}
3.Calculating Factorial using Recursion

package end_Sem_Exam2;

class Factorial{
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,factorial=1;
int number=5;//It is the number of which
we want to find factorial
factorial = factorial(number);
System.out.println("Factorial of
"+number+" is: "+factorial);
}
}
4.Usage of string and StringBuffer

package end_Sem_Exam2;

class Stbuffer{
public static void main(String args[]){
System.out.println("Hashcode of
String:");
String str="java";
System.out.println(str.hashCode());
str=str+"satya";
System.out.println(str.hashCode());

System.out.println("Hashcode of
StringBuffer:");
StringBuffer sb=new
StringBuffer("java");
System.out.println(sb.hashCode());
sb.append("satya");
System.out.println(sb.hashCode());
}
}
5.Adding and Removing From a Array List

package end_Sem_Exam2;
import java.util.ArrayList;
class book
{
public static void main(String[] args)
{

ArrayList<String> book = new


ArrayList<String>();//Creating an //arraylist of
strings
book.add("Digital signal
processing");
book.add("Digital communication");
book.add("Computer networks");
book.add("Semicoducter physics");
book.add("Fiber optics");
book.add("digital image
processing");
System.out.println("The elements of
the arraylist are"+book);

book.remove("digital image
processing");//Removing one book(digital image
processing) from the //arraylist
System.out.println("The elements of
the arraylist are"+book);
}
}

You might also like