Object-Oriented Programming

Object-oriented programming (OOP) is at the core of Java. In fact, all Java programs are to at least some extent object-oriented. OOP is so integral to Java that it is best to understand its basic principles before you begin writing even simple Java programs. Therefore, this topic begins with a discussion of the theoretical aspects … Read more

Java Program to print Odd numbers from 1 to n or 1 to 100

public class JavaEx { public static void main(String args[]) { int n = 100; System.out.print(“Odd Numbers from 1 to “+n+” are: “); for (int i = 1; i <= n; i++) { if (i % 2 != 0) { System.out.print(i + ” “); } } } }

Java Program to Add Two Complex Numbers

  public class ComplexNumber{ double real, img; ComplexNumber(double r, double i){ this.real = r; this.img = i; } public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) { ComplexNumber temp = new ComplexNumber(0, 0); temp.real = c1.real + c2.real; temp.img = c1.img + c2.img; return temp; } public static void main(String args[]) { ComplexNumber c1 = new … Read more

Java Program to Calculate Compound Interest

public class JavaExm { public void calculate(int p, int t, double r, int n) { double amount = p * Math.pow(1 + (r / n), n * t); double cinterest = amount – p; System.out.println(“Compound Interest after ” + t + ” years: “+cinterest); System.out.println(“Amount after ” + t + ” years: “+amount); } public … Read more