Code for Drawing a Circle in Java
Coffee program to calculate or to printarea of a circle in a simple method. If y'all were new to java or at the offset stage and then, Bank check – 500+ simple Java programs for beginners.
- X Star Pattern Java Program – Patterns
- Square Star Pattern Programme In Java – Patterns
The post-obit Java program to print the expanse of a circle has been written in five simple unlike ways, static method, using constructor, Interface, inheritance with sample outputs for each program.
Table of contents: 5 Simple ways to print AOC
- Using Static Method
- Using Interface
- Inheritance
- Using Constructor
- Using Method
As well Cheque : Perimeter of Circumvolve Java
# Below is the online execution tool, for the following programme 🙂
Impress Area Of Circumvolve v Different Ways With Examples
Beneath is the program to calculate the AOC in java, if you were new to coffee coding then we can likewise share the complete step by steps with detailed explanation on how this java program works, just below this code check information technology out.
1. Static Method
import java . util . Scanner ; class AreaOfCircle { public static void primary ( Cord args [ ] ) { Scanner s = new Scanner ( System . in ) ; Arrangement . out . println ( "Enter the radius:" ) ; double r = southward . nextDouble ( ) ; double area = ( 22 * r * r ) / seven ; System . out . println ( "Area of Circle is: " + area ) ; } } |
Output:
Enter the radius : 7 Area of circle : 154.0 |
As we know that formula in math is :
But, if you were written like that, and then the system won't empathise, until and unless you assign the value of "PIE" is 22/vii. As usual , y'all e'er stand for the values in binary numbers.
The organisation tin can't able to sympathise whether the given number is a number, or given alphabetic character is a letter. All it understood as " Graphic symbol " or just a " Symbol ". Here goes the complete stride by pace explanation of the to a higher place code and how information technology works.
Stride – 1:
import java . util . Scanner ; |
( hither 'import' is a keyword in coffee used to get features from inbuilt packages. hither nosotros using a package called until it consists of many classes and we using one of the form Scanner to get command over console which is the interface betwixt user and program. )
Step – 2:
public static void main ( String args [ ] ) |
( The main function, where the execution of program startfrom here onwards )
Step – iii:
Scanner s = new Scanner ( System . in ) ; |
( 1. The scanner is a grade used to scan the input data which was given past the user through a console.
so to get access on a console we desire to create an object Syntax:new Scanner(); subsequently creating an object that reference volition store in variable 'southward' )
Step – 4:
Organisation . out . println ( "Enter the radius:" ) ; |
( above code is giving instructions for the user to give the input for radius)
Footstep – 5:
double r = s . nextDouble ( ) ; |
( here above education is get input in the required format. first nosotros want to know near nextDouble() method it takes a token(collection of symbols split up by white space example:"ABC", "DEF" , "GHI","10″,"20″)
which is given past user.when user give 10 as input really in user perspective information technology is number but any number or string which entered on console by default those are strings, 1o as string merely we want x as number format for that we have method to catechumen cord to number(Int, Double, Float, Long…..) some of those method are:
1.nextDouble() ,
2 nextFloat(),
3.nextInt(),
iv.nextLong()
v.nextShort()
6.next() or nextString() ).
Step – half dozen:
double area = ( 22 * r * r ) / 7 ; |
Stride – seven: System.out.println("Area of Circle is: " + area); ( In one case, you entered the radius , the value stored in a particular part ( nextDouble(); ) and read those values with the assistance of a scanner and brandish the output for a given value.
A : The major departure is where double tin correspond the output even afterwards the decimal indicate , whereas in " Int " only the numbers before decimal point will take into consideration.
The second method for the above program #sample method – 2, with online compile using control line arguments method :
Here is the sample line for an integer :
allow arg[0]="x″;
int r=Integer.parseInt(args[0]);//r=10;
The purpose of using the "Double " is , whenever y'all enter the radius of a particular number like "vii" , the answer is 153.86 , numbers after decimal points volition also be displayed on the screen. Whereas if you lot apply the "int" , the digits after the decimal signal will be loss.
If you have any doubts related to this programme , practise annotate here. We are glad to help you out.
2. Using Interface
There you get another plan using the interface with sample outputs.
i 2 3 four v 6 vii viii 9 ten 11 12 13 14 15 16 17 18 nineteen 20 21 22 23 | import java . util . * ; interface AreaCal { void circle ( ) ; } class AreaOfCircle implements AreaCal { double area ; public void circle ( double r ) { surface area = ( 22 * r * r ) / 7 ; } public static void primary ( String args [ ] ) { AreaOfCircle 10 ; Scanner south = new Scanner ( Arrangement . in ) ; Organisation . out . println ( "Enter the radius:" ) ; double rad = s . nextDouble ( ) ; x = new AreaOfCircle ( ) ; ten . circle ( rad ) ; Arrangement . out . println ( "Surface area of Circle is: " + x . expanse ) ; } } |
3. Java Program Using Inheritance
- Another program to print AOC using inheritance with sample example output.
ane 2 three 4 5 vi 7 8 9 ten eleven 12 thirteen 14 15 16 17 xviii 19 twenty 21 | import java . util . Scanner ; class AreaCalculation { double area ; void circumvolve ( double r ) { expanse = ( 22 * r * r ) / vii ; } } grade AreaOfCircle extends AreaCalculation { public static void master ( String args [ ] ) { Scanner s = new Scanner ( Arrangement . in ) ; System . out . println ( "Enter the radius:" ) ; double rad = s . nextDouble ( ) ; AreaOfCircle a = new AreaOfCircle ( ) ; a . circle ( rad ) ; System . out . println ( "Area of Circle is: " + a . area ) ; } } |
Output:
Enter the radius : 63 Area of Circumvolve is : 12474.0 |
four. Using Constructor
Using constructor to print AOC with outputs.
1 ii 3 4 5 six 7 eight nine x 11 12 xiii fourteen 15 16 17 xviii xix 20 21 | import coffee . util . Scanner ; class Area { double area ; Area ( double r ) { expanse = ( 22 * r * r ) / seven ; } } class AreaOfCircle { public static void main ( String args [ ] ) { Scanner south = new Scanner ( System . in ) ; System . out . println ( "Enter the radius:" ) ; double rad = due south . nextDouble ( ) ; Expanse a = new Area ( rad ) ; Organization . out . println ( "Area of Circle is: " + a . surface area ) ; } } |
Output:
Enter the radius : fifteen Area of Circle is : 707.1428571428571 |
5. Using Method
import java . util . Scanner ; form AreaOfCircle { public static void main ( Cord args [ ] ) { Scanner s = new Scanner ( System . in ) ; Arrangement . out . println ( "Enter the radius:" ) ; double rad = s . nextDouble ( ) ; double a = area ( rad ) ; Arrangement . out . println ( "Expanse of Circle is: " + a ) ; } static double area ( double r ) { render ( 22 * r * r ) / 7 ; } } |
More Programs:
- Perimeter Of Circle
- Perimeter of rectangle
- Perimeter of square
- Area Of Rectangle
- Area Of Triangle
Source: https://javatutoring.com/calculate-area-of-circle-java-program/
Belum ada Komentar untuk "Code for Drawing a Circle in Java"
Posting Komentar