DEV Community

Ajay Sundar
Ajay Sundar

Posted on

Learning some basic concepts in Java

Today i got an oppurtunity to learn the basic concepts in java and it was interesting to learn and was a bit easily understandable.

CALCULATION OF THE AREA OF THE CIRCLE USING JAVA

Using the java programming language we know calculate the area of circle

public class Area{
    void AreaofCircle(){
        double pi = 3.14;
        double radius = 5;
        double r = radius * radius;
        System.out.println(pi * r);
    }

    public static void main(String[] args){
        Area obj = new Area();
        obj.AreaofCircle();
    }
}


OUTPUT
78.5

Enter fullscreen mode Exit fullscreen mode

Step by step Explanation

1. Class Declaration

public class Area {

Enter fullscreen mode Exit fullscreen mode

The class is named Area, which contains methods related to calculating the area.

2.Creating a Method

void AreaofCircle(){
    double pi = 3.14;
    double radius = 5;
    double r = radius * radius;
    System.out.println(pi * r);
}

Enter fullscreen mode Exit fullscreen mode
  • void AreaofCircle() is a method that performs the task of calculating the area of a circle.

  • double pi = 3.14; stores the value of π (pi).

  • double radius = 5; is the radius of the circle.

  • double r = radius * radius; calculates the square of the radius.

  • Finally, System.out.println(pi * r); prints the area of the circle using the formula π × r².

3.Main Method

public static void main(String[] args){
    Area obj = new Area();
    obj.AreaofCircle();
}

Enter fullscreen mode Exit fullscreen mode
  • Every Java program starts execution from the main method.

  • Area obj = new Area(); creates an object of the Area class.

  • obj.AreaofCircle(); calls the method to perform the calculation and display the result.

OUTPUT

when you run this program, the output will be:

78.5
Enter fullscreen mode Exit fullscreen mode

This is the area of a circle with a radius of 5 units.

How to calculate Simple Interest using Java

The formula for calculating simple interest is:

              SI = P*N*R/100
​
Enter fullscreen mode Exit fullscreen mode

Where:

  • P = Principal amount

  • N = Number of years

  • R = Rate of interest

class Simple{
    void Simpleinterest(){
        int P = 500;
        int N = 200;
        int R = 100;
        int SI = (P * N * R / 100);
        System.out.println(SI);
    }

    public static void main(String[] args){
        Simple obj = new Simple();
        obj.Simpleinterest();
    }
}

OUTPUT

100000

Enter fullscreen mode Exit fullscreen mode

Step by Step Explanation

1.Class Declaration

class Simple {

Enter fullscreen mode Exit fullscreen mode
  • The class is named Simple, which contains all the logic for calculating simple interest.

2.Method for Calculation

void Simpleinterest(){
    int P = 500;
    int N = 200;
    int R = 100;
    int SI = (P * N * R / 100);
    System.out.println(SI);
Enter fullscreen mode Exit fullscreen mode
  • void Simpleinterest(): This method calculates the simple interest.

  • int P = 500; → Principal amount is set to 500.

  • int N = 200; → Time period (years) is set to 200.

  • int R = 100; → Rate of interest is set to 100.

  • int SI = (P * N * R / 100); → Formula applied for calculation.

  • System.out.println(SI); → Displays the result on the console.

3.Main Method

public static void main(String[] args){
    Simple obj = new Simple();
    obj.Simpleinterest();
}

Enter fullscreen mode Exit fullscreen mode
  • Entry point of the Java program.

  • Simple obj = new Simple(); → Creates an object of class Simple.

  • obj.Simpleinterest(); → Calls the method to calculate and print the simple interest.

OUTPUT

When you run this program, the output will be:

100000


Enter fullscreen mode Exit fullscreen mode

Top comments (0)