Uploaded by Mark Ahrens

Circle

advertisement
import java.lang.Math;
import java.util.Scanner;
public static class Circle {
double radius;
public Circle(double r){
radius = r;
}
public void area(){
double area = Math.PI * radius * radius;
System.out.println("Area is " + area);
}
public void circumference(){
double c = 2 * Math.PI * radius;
System.out.println("Circumference is " + c);
}
// main class
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Circle cir1 = new Circle(5.1);
Circle cir2 = new Circle(20.1);
cir1.area();
cir2.area();
cir1.circumference();
cir2.circumference();
System.out.println("Enter a value for circle 1: ");
double v1 = input.nextInt();
cir1.setValue(v1);
cir1.getValue();
}
public void setValue(double v){
radius = v;
}
public void getValue(){
System.out.println("value is now: " + radius);
}
} // end main class
// class for Rectangle object
public static class Rect{
double w;
double l;
Rect(){
l = 1; // default length for Rectangle object
w = 1; // default width for Rectangle object
}
// method for user defined Rectangle shape
Rect(double len, double width ){
this.l = len;
this.w = width;
}
public double Perimeter(){
return ((l * 2) + (w*2));
}
public double Area(){
return (l*w);
}
}
}
Download