Professional Documents
Culture Documents
In package
package FIRST;
Output-
2. Arrays
Exercise 1: Write a java program to illustrate how to declare,
instantiate, initialize, and traverse the Java array.
Exercise 2: Write a program to print the array elements
using for-each loop
Exercise 3: Write a programto demonstrate the way of
passing an array to method
Note*-All three exercises related to arrays have been done in a single program
Code-
import java.util.Scanner;
{ int sum=0;
for(int x:a)
{sum+=x;}
return sum;
for(int i=0;i<10;i++)
for(int i=0;i<10;i++)
{
System.out.print(arr[i]+" ");//Traversing the array
System.out.println();
for(int x:arr)
System.out.println();
int sum=sum_of_array(arr);
Output-
3. PolymorphismandInheritance
Exercise 1: Write a java program for method overloading
Code-
import java.util.Scanner;
class student
{
public void introduce(String name)
{
System.out.println("The name of the student is:"+name);
}
public void introduce(String name,int reg_no)
{
System.out.println("The name of the student is:"+name);
System.out.println("Registration number is:"+reg_no);
}
}
}
Output-
class employee
{
public void introduce(String name,int Id)
{
System.out.println("Employee name:"+name);
System.out.println("Employee Id:"+Id);
}
}
}
public class MAIN
{
public static void main(String[] args) {
System.out.println("Shashwat Shankar Singh");
System.out.println("209301500");
employee e1=new employee();
e1.introduce("Shashwat",69);
programmer p1=new programmer();
p1.introduce("Shashwat",616);
}
}
Output-
Code-
import java.util.Scanner;
class arithemtic
{ int add(int a,int b)
{return a+b;}
double add(double a,double b)
{
return a+b;
}
Output-
class animal
{
public void eat()
{
System.out.println("The animal is eating");
}
}
class Herbivore extends animal
{
}
}
Output-
class parent
{
void display_parent()
{System.out.println("This is parent class");}
}
class child extends parent
{
void display_child()
{
System.out.println("This is child class");
}
}
}
}
Output-
Code-
class member
{
public String Name;
public int age;
public int ph_no;
public String address;
public int salary;
public void print_salary()
{
System.out.println("The salary of the member is:"+salary);
}
}
class employee extends member
{
String specialization;
}
class manager extends member
{
String department;
}
}
}
Output-
}
}
Output-
Code-
import java.util.Scanner;
class rectangle
{ private int length;
private int breath;
rectangle(int length,int breath)
{
this.length=length;
this.breath=breath;
}
public void area()
{
System.out.println("The area is:"+length*breath);
}
public void perimeter()
{
System.out.println("The perimeter is:"+(2*(length+breath)));
}
}
class square extends rectangle
{
square(int side)
{
super(side, side);
}
}
}
Output-