Some Java Programs to practice.(Part 1)

Codynn
8 Min Read

+

I’ve included a list of some of the Java programs below. You may also view all of the Java programs on our mobile app. I strongly advise you to use our mobile app for a better experience.

Please click the link to get our app–> https://play.google.com/store/apps/details?id=com.allbachelor.javaapp&hl=en&gl=US

1. Write a program to display volume of sphere.

import java.util.Scanner;

class VolumeSphere{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the radius of sphere");
        double radius = sc.nextDouble();
        double volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
        System.out.println("Volume of sphere is " + volume);
    }

}

2.Write a program to display area of 4 walls.

import java.util.*;

class AreaOfWall {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of wall");
       double length = sc.nextDouble();
       System.out.println("Enter the breadth of wall");
       double breadth = sc.nextDouble();
       double area = 2 * (length + breadth);
       System.out.println("Area of wall is " + area);
   }

}

3.Write a program to display total surface area and volume of cuboid.

import java.util.*;

class VolumeOfCuboid {
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of cuboid");
       double length = sc.nextDouble();
       System.out.println("Enter the breadth of cuboid");
       double breadth = sc.nextDouble();
       System.out.println("Enter the height of cuboid");
       double height = sc.nextDouble();
       double area = 2 * (length * breadth + length * height + breadth * height);
       double volume = length * breadth * height;
       System.out.println("Area of cuboid is " + area);
       System.out.println("Volume of cuboid is " + volume);
   }

}

4.Write a program to display total surface area of cube.

import java.util.*;

class AreaOfCube{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the side of cube");
       double side = sc.nextDouble();
       double area = 6 * (side * side);
       System.out.println("Area of cube is " + area);
   }


}

5.Write a program to display area of rectangle.

import java.util.*;

class AreaOfRectangle{
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of rectangle");
       double length = sc.nextDouble();
       System.out.println("Enter the breadth of rectangle");
       double breadth = sc.nextDouble();
       double area = length * breadth;
       System.out.println("Area of rectangle is " + area);
   }


}

6.Write a program to display circumference of circle.

import java.util.*;

class CircumferenceOfCircle{
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the radius of circle");
       double radius = sc.nextDouble();
       double circumference = 2 * Math.PI * radius;
       System.out.println("Circumference of circle is " + circumference);
   }



}

7.Write a program to display area of triangle when three sides are given.

import java.util.*;

class AreaOfTriangle{
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of triangle");
       double length = sc.nextDouble();
       System.out.println("Enter the breadth of triangle");
       double breadth = sc.nextDouble();
       System.out.println("Enter the height of triangle");
       double height = sc.nextDouble();
       double area = (length * breadth * height) / 2;
       System.out.println("Area of triangle is " + area);
   }




}

8.Write a program to display area and circumference of circle.

import java.util.*;

class CircumferenceOfCircle{
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the radius of circle");
       double radius = sc.nextDouble();
       double area = Math.PI * (radius * radius);
       double circumference = 2 * Math.PI * radius;
       System.out.println("Area of circle is " + area);
       System.out.println("Circumference of circle is " + circumference);
   }




}

9.Write a program to ask distance in kilometer and convert into miles.

import java.util.*;

class DistanceInMiles{
           public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the distance in kilometer");
       double distance = sc.nextDouble();
       double miles = distance * 0.621371;
       System.out.println("Distance in miles is " + miles);
   }





}

10.Write a program to input distance in meter and convert into kilometer and meter.

import java.util.*;

class DistanceInMeter{
           public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the distance in meter");
       double distance = sc.nextDouble();
       double kilometer = distance / 1000;
       double meter = distance % 1000;
       System.out.println("Distance in kilometer is " + kilometer);
       System.out.println("Distance in meter is " + meter);
   }





}

11.Write a program to calculate potential energy of body. [PE=MGH where G=9.8]

import java.util.*;

class PotentialEnergyOfBody{
           public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the mass of body");
       double mass = sc.nextDouble();
       System.out.println("Enter the height of body");
       double height = sc.nextDouble();
       double potentialEnergy = mass * 9.8 * height;
       System.out.println("Potential energy of body is " + potentialEnergy);
   }





}

12.Write a program to display perimeter of rectangle.

import java.util.*;

class PerimeterOfRectangle{
           public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of rectangle");
       double length = sc.nextDouble();
       System.out.println("Enter the breadth of rectangle");
       double breadth = sc.nextDouble();
       double perimeter = 2 * (length + breadth);
       System.out.println("Perimeter of rectangle is " + perimeter);
   }
}

13.Write a program to input principal, rate, time and display simple interest and total amount.

import java.util.*;

class SimpleInterest{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the principal");
       double principal = sc.nextDouble();
       System.out.println("Enter the rate");
       double rate = sc.nextDouble();
       System.out.println("Enter the time");
       double time = sc.nextDouble();
       double simpleInterest = (principal * rate * time) / 100;
       double totalAmount = principal + simpleInterest;
       System.out.println("Simple interest is " + simpleInterest);
       System.out.println("Total amount is " + totalAmount);
   }





}

14.Write a program to ask in kilogram and convert into grams.

import java.util.*;

class ConvertIntoGrams{
            public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the weight in kilogram");
        double weight = sc.nextDouble();
        double gram = weight * 1000;
        System.out.println("Weight in grams is " + gram);
    }


}

15.Write a program to display total surface area and volume of sphere.

import java.util.Scanner;

class SurfaceAreaAndVolumeOfSphere{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the radius of sphere");
       double radius = sc.nextDouble();
       double surfaceArea = 4 * Math.PI * (radius * radius);
       double volume = 4 * Math.PI * (radius * radius * radius) / 3;
       System.out.println("Surface area of sphere is " + surfaceArea);
       System.out.println("Volume of sphere is " + volume);
   }

   }
Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *