Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created Circle #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions 5. Classes/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public class Circle {

// private instance variables, not accessible from outside the class
private String colour;
private double radius;

// default constructor with no arguments
public Circle() {
radius = 1;
colour = "blue";


}
// second constructor takes as argument the radius but has default colour

public Circle(double r) {
radius = r;
colour = "blue";

}

// public method to retrieve the radius
public double getRadius() {
return radius;
}

// public method to compute and return the area of circle
public double getArea() {
return (Math.round(radius * radius * Math.PI));
}

public Circle(double r, String c) {
this.radius = r;
this.colour = c;
}

public String getColour() {
return colour;
}

public void setRadius(double newRadius) {
radius = newRadius;
}

public void setColour(String newColour) {
colour = newColour;

}

@Override
public String toString() {
return "Circle{" +
"colour='" + colour + '\'' +
", radius=" + radius +
'}';
}
}
29 changes: 29 additions & 0 deletions 5. Classes/TestCircle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class TestCircle {

public static void main(String[] args) {
// Declare an instance of Circle class called circle1
// Invoke the default constructor
Circle circle1 = new Circle();
// invoke public methods
System.out.println("The circle has radius of" + circle1.getRadius() + "and area of" + circle1.getArea());
//declare an instance of Circle class called circle2
// invoke the second constructor
Circle circle2 = new Circle(8);
// invoke public methods
System.out.println("The circle has radius of " + circle2.getRadius() + " and area of " +
circle2.getArea());


Circle circle3 = new Circle(6, "PINK");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

System.out.println("Radus :" + circle3.getRadius() +" "+"Area :" + circle3.getArea() + "; color : " + circle3.getColour());

circle3.setRadius(34);
circle3.setColour("PURPLE");

System.out.println("Radus : " + circle3.getRadius() + " Area : "
+ circle3.getArea() + " color : " + circle3.getColour());

}
}