矩形类
public class Rectangle {
private int width;
private int height;
private String shape;
Rectangle(){
width = 10;
height = 10;
shape = "#";
}
Rectangle(int in_width, int in_height, String in_shape){
width = in_width;
height = in_height;
shape = in_shape;
}
int getArea(){
int result = width * height;
return result;
}
int getRoundLen(){
int result = (width + height) * 2;
return result;
}
void scaleWidth(double factor){
if(factor <= 10){
width *= factor;
}
else{
width *= 10;
}
}
void printSelf(){
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
System.out.print(shape);
}
System.out.println();
}
}
void changeShape(String _shape)
{
shape = _shape;
}
}
测试矩形类
import java.util.Random;
public class Main3 {
public static void main(String[] args) {
// int a = 33;
// int a = 90;
Rectangle rect = new Rectangle(5,3,"@");
int area = rect.getArea();
int len = rect.getRoundLen();
rect.printSelf();
// rect.width *= 100;
rect.scaleWidth(2);
rect.changeShape("#");
rect.printSelf();
System.out.println(area);
System.out.println(len);
Rectangle rect2 = new Rectangle(5,8,"#");
area = rect2.getArea();
len = rect2.getRoundLen();
System.out.println(area);
System.out.println(len);
rect2.scaleWidth(2);
area = rect2.getArea();
len = rect2.getRoundLen();
System.out.println(area);
System.out.println(len);
}
}
Point类
public class Point {
private double x;
private double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point(double _x, double _y) {
x = _x;
y = _y;
}
}
Circle类
public class Circle {
private double radius;
private Point center;
Circle(){
radius = 1;
center = new Point(0,0);
}
Circle(double _radius, double _center_x, double _center_y){
radius = _radius;
center = new Point(_center_x, _center_y);
}
boolean isPointInCircle(Point point){
double distance = Math.sqrt((center.getX() - point.getX())*(center.getX() - point.getX())
+ (center.getY() - point.getY())*(center.getY() - point.getY()));
if(distance < radius){
return true;
}
else{
return false;
}
}
}
测试Circle类
import java.util.Random;
public class TestCircle {
public static void main(String[] args) {
Circle circle = new Circle(2,2,2);
Point p1 = new Point(0,0);
System.out.println(circle.isPointInCircle(p1));
Point p2 = new Point(3,3);
System.out.println(circle.isPointInCircle(p2));
Random random = new Random();
int in_sum = 0;
int sum = 0;
for(int i = 0; i < 1000000; i++){
double x = random.nextDouble()*4;
double y = random.nextDouble()*4;
Point p = new Point(x,y);
if(circle.isPointInCircle(p))
{
in_sum++;
}
sum++;
}
double result = in_sum*1.0/sum;
System.out.println(result*4);
}
}