Java Programs | Java Programming Examples

1. Write a program in Java to accept array elements and calculate the sum of elements.

public class Test {
static int[] arr={12,3,4,15};
static int sum()
{
    int sum=0;
    int i;
    for(i=0;i<arr.length;i++)
    {
        sum += arr[i];
    }
    return sum;
}
public static void main(String[] args)
{
    System.out.println(“Sum of given array is:” + sum());
}
}

Output:

Sum of given array is:34

Or we can do the above program by taking input from user

import java.util.Scanner;
public class ArraySum{
    public static void main(String[] args)
    {
        Scanner scanner=new Scanner(System.in);
        System.out.print(“Enter the size of the array:”);
        int size=scanner.nextInt();
        int[] array=new int[size];
        System.out.println(“Enter the elements of the array:”);
        for(int i=0;i<size;i++)
        {
            System.out.print(“Element” +(i+1) + “:”);
            array[i]=scanner.nextInt();
        }
        int sum=0;
        for(int i=0;i<size;i++){
            sum+=array[i];
        }
        System.out.println(“Sum of array elements:” +sum);
    }

}

Output:

Enter the size of the array:3

Enter the elements of the array:

Element1:4

Element2:6

Element3:5

Sum of array elements:15

2. Write a Java program to demonstrate multilevel inheritance.

class GrandParent {
    void displayGrandParent()
    {
        System.out.println(“I am the GrandParent”);
    }
}
class Parent extends GrandParent{
    void displayParent()
    {
        System.out.println(“I am the Parent”);
    }
}

class Child extends Parent{
    void displayChild()
    {
        System.out.println(“I am the Child”);
    }
}
public class MultilevelInheritance
{
    public static void main(String[] args)
    {
       Child child=new Child();
        child.displayGrandParent();
        child.displayParent();
        child.displayChild();
    }

}

Output:

I am the GrandParent

I am the Parent

I am the Child

3. Write a Java program using copy constructor to print the area of rectangle.

public class Rectangle {
    int length;
    int breadth;

 Rectangle(int l, int b)
{
  length=l;
  breadth=b;
}
Rectangle(Rectangle obj)
{
    length=obj.length;
    breadth=obj.breadth;
}
int area()
{
    return length*breadth;
}
}
class CopyConstructor{
    public static void main(String[] args)
    {
        Rectangle OriginalRectangle=new Rectangle(5,6);
        Rectangle CopiedRectangle=new Rectangle(OriginalRectangle);
        System.out.println(“Area of original rectangle:” +OriginalRectangle.area());
        System.out.println(“Area of copied rectangle:” +CopiedRectangle.area());
    }
}

Output:

Area of original rectangle:30

Area of copied rectangle:30

4. Write a Java program to the concept of method overloading

public class MethodOverloading {
    public static int add(int a,int b)
    {
        return a+b;
    }
    public static int add(int a,int b,int c)
    {
        return a+b+c;
    }
    public static double add(double a,double b)
    {
        return a+b;
    }
    public static void main(String[] args)
    {
        System.out.println(“Sum two integers: “+add(5,10));
        System.out.println(“Sum three integers: “+add(5,10,15));
        System.out.println(“Sum two doubles: “+add(3.5,7.2));
    }
}

Output:

Sum two integers: 15

Sum three integers: 30

Sum two doubles: 10.7

5. Write a Java program using interface.

interface Greeting{
    void greet();
}
class SimpleGreeting implements Greeting
{
    public void greet()
    {
        System.out.println(“Hello, welcome”);
    }
}
public class Test {
    public static void main(String[] args)
    {
        SimpleGreeting simpleGreet=new SimpleGreeting();
        simpleGreet.greet();
    }

Output:

Hello, welcome

2 thoughts on “Java Programs | Java Programming Examples”

  1. Greate pieces. Keep writing such kind of information on your site.

    Im really impressed by your site.
    Hey there, You’ve performed an incredible job.
    I will definitely digg it and personally
    suggest to my friends. I am confident they’ll be benefited from this
    web site.

    Reply

Leave a Comment