package cn.ALAN_CF.thinking_5;
import java.util.Scanner;
public class Shape {
public static void show (int x, Shaped[] shapes ){
for(int i = 0; i < x; i++){
System.out.printf("%.2f",shapes[i].getArea());
System.out.print(" ");
if(i != x-1) {
System.out.printf("%.2f\n",shapes[i].getPerimeter());
}
else{
System.out.printf("%.2f",shapes[i].getPerimeter());
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
Shaped [] shapes = new Shaped[x];
int t;
double r,l,w;
for(int i = 0; i < x; i++){
t = input.nextInt();
switch(t){
case 1:{
r = input.nextDouble();
shapes[i] = new Circles(r);
}
break;
case 2: {
l = input.nextDouble();
w = input.nextDouble();
shapes[i] = new Rectangles(l,w);
}
break;
default:
System.out.println("非法输入");
}
}
show(x,shapes);
}
}
interface Shapes{
float getArea();
float getPerimeter();
}
class Shaped implements Shapes{
protected double r;
protected double width;
protected double length;
public Shaped(double r){
this.r = r;
}
public Shaped(double length,double width){
this.length = length;
this.width = width;
}
@Override
public float getArea() {
return 0;
}
@Override
public float getPerimeter() {
return 0;
}
}
class Circles extends Shaped{
public Circles(double r){
super(r);
}
@Override
public float getArea(){
return (float) (Math.PI * this.r * this.r);
}
@Override
public float getPerimeter(){
return (float) (2.0 * Math.PI * this.r);
}
}
class Rectangles extends Shaped{
public Rectangles (double length,double width){
super(length,width);
}
@Override
public float getArea() {
return (float) (this.length * this.width);
}
@Override
public float getPerimeter() {
return (float) (this.length * 2.0 + this.width * 2.0);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a5508cc162cd6953608dc03c58501b1e.png)
package cn.Alan_DS;
public class Book {
private String name;
private int number;
private double price;
private static int auto = 0;
private static int num = 0;
public Book(String name,double price){
auto++;
this.name = name;
this.number = auto;
this.price = price;
num++;
}
public static void main(String[] args) {
Book [] books = {
new Book("Java程序设计",34.5),
new Book("数据结构",44.8),
new Book("C++程序设计",35.0)
};
for(int i = 0; i < books.length; i++) {
System.out.println("书名:" + books[i].name + ", 书号: " +
books[i].number + ", 书价:" + books[i].price);
}
System.out.print("图书总册数为: " + num);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c576513b090ccef617910d15ed693b4a.png)