Java Programming Examples: Array, Polymorphism, and Encapsulation
Java Programming Examples
1. Calculate Sum and Average of an Array
This program demonstrates how to calculate the sum and average of elements in an array.
package mypack;public class Array_demo { public static void main(String[] args) { int[] a = {1, 2, 3, 4, 5, 6, 7, 8}; int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; } System.out.println("Sum: " + sum); System.out.println("Average: " + sum / a.length); }}
2. Compile Time Polymorphism
This program demonstrates compile-time polymorphism (method overloading) in Java.
package mypack;public class Compile { void add(int a, int b) { System.out.println(a + b); } void add(int a, double b) { System.out.println(a + b); } void add(double a, double b) { System.out.println(a + b); } void add(double a, double b, double c) { System.out.println(a + b + c); } public static void main(String[] args) { Compile c = new Compile(); c.add(5, 7); c.add(5, 2.5); c.add(7.2, 3.8); c.add(5.4, 1.9, 7.2); }}
3. Arithmetic Operations using Encapsulation
This program demonstrates encapsulation by performing simple arithmetic operations using classes with private attributes and public methods.
package mypack;import java.util.Scanner;class Addition { private int num1; private int num2; public void setAdd(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public void getAdd() { int result = (num1 + num2); System.out.println(result); }}class Subtraction { private int num1; private int num2; public void setSub(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public void getSub() { int result = (num1 - num2); System.out.println(result); }}class Multiplication { private int num1; private int num2; public void setMul(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public void getMul() { int result = (num1 * num2); System.out.println(result); }}class Division { private int num1; private int num2; public void setDiv(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public void getDiv() { int result = (num1 / num2); System.out.println(result); }}public class Calculator { public static void main(String[] args) { while (true) { try (Scanner s = new Scanner(System.in)) { Addition a = new Addition(); Subtraction su = new Subtraction(); Multiplication m = new Multiplication(); Division d = new Division(); System.out.println("Select from the following choices-"); System.out.println("1 for Addition"); System.out.println("2 for Subtraction"); System.out.println("3 for Multiplication"); System.out.println("4 for Division"); System.out.println("Enter your choice:"); int choice = s.nextInt(); if (choice >= 1 && choice <= 4) { System.out.println("Enter the first number:"); int num1 = s.nextInt(); System.out.println("Enter the second number:"); int num2 = s.nextInt(); System.out.println("------------------"); switch (choice) { case 1: a.setAdd(num1, num2); a.getAdd(); break; case 2: su.setSub(num1, num2); su.getSub(); break; case 3: m.setMul(num1, num2); m.getMul(); break; default: d.setDiv(num1, num2); d.getDiv(); } } } } }}