欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java期末考试

程序员文章站 2022-07-05 21:55:56
...

FAN类

import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc= new Scanner(System.in);
        while(sc.hasNextLine()) {
            String line =sc.nextLine();
            String[] arr=line.split(",");
//            if(arr[3]="0")
//                System.out.println();
            Fan f=new Fan();
            f.setSpeed(Integer.parseInt(arr[0]));
            f.setColor(arr[1]);
            f.setRadius(Double.parseDouble(arr[2]));
            f.setOn(arr[3].equals("1")?true:false);//当字符串比较值时,不能直接用==,而是用String.equals()来比较
            System.out.println(f);
        }
    }


}
class Fan {
    private static final int SLOW=1;
    private static final int MEDIUM=2;
    private static final int FAST=3;
    private int speed=Fan.SLOW;
    private boolean on =true;
    private double radius =5;
    private String color ="blue";
    
    
    public Fan() {
        super();
        // TODO Auto-generated constructor stub
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public boolean isOn() {
        return on;
    }
    public void setOn(boolean on) {
        this.on = on;
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    //@Override
    public String toString() {
        String info="";
        if(this.on) {
            return info="speed:"+this.speed+",color:"+this.color+",radius:"+this.radius;
        }
        else
            return info="fan is off,color:"+this.color+",radius:"+this.radius;
    }
}

图形类

import java.util.Scanner;
import java.text.DecimalFormat;




public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        Shape[] shapes=new Shape[3];
        while(input.hasNextLine()) {
            for(int i=0;i<shapes.length;i++) {
                String s=input.nextLine();
                String[] arr=s.split(",");
                if(arr[0].equals("C")||arr[0].equals("c")) {
                    shapes[i]=new Circle(Double.parseDouble(arr[1]));
                }
                if(arr[0].equals("R")||arr[0].equals("r")) {
                    shapes[i]=new Rectangle(Double.parseDouble(arr[1]),Double.parseDouble(arr[2]));
                }
            }
            for(int i=0;i<shapes.length;i++) {
                System.out.println(shapes[i]);
            }
        }
    }
}
abstract class Shape  {
    
    public abstract double getArea();
    
    
    public abstract double getPerimeter();
    
}
class Circle extends Shape {


    private double radius;


    public Circle() {
        super();
    }


    public Circle(double radius) {
        super();
        this.radius = radius;
    }


    public double getRadius() {
        return radius;
    }


    public void setRadius(double radius) {
        this.radius = radius;
    }


    @Override
    public double getArea() {        
        return Math.PI * this.radius * this.radius;
    }


    @Override
    public double getPerimeter() {
        return Math.PI * this.radius * 2;
    }


    @Override
    public String toString() {
        DecimalFormat df=new DecimalFormat("0.00");
        return "Circle:area=" + df.format(getArea()) + ",perimeter=" + df.format(getPerimeter());
    }
    
    


}
class Rectangle extends Shape {


    private double width;
    private double height;


    public Rectangle() {
        super();
    }


    public Rectangle(double width, double height) {
        super();
        this.width = width;
        this.height = height;
    }


    public double getWidth() {
        return width;
    }


    public void setWidth(double width) {
        this.width = width;
    }


    public double getHeight() {
        return height;
    }


    public void setHeight(double height) {
        this.height = height;
    }


    @Override
    public double getArea() {
        return this.width * this.height;
    }


    @Override
    public double getPerimeter() {
        return 2 * (this.height + this.width);
    }


    @Override
    public String toString() {
        DecimalFormat df=new DecimalFormat("0.00");
        return "Rectangle:area=" + df.format(getArea()) + ",perimeter=" + df.format(getPerimeter());
    }


    
    
}

各种各样的门

import java.util.Scanner;






public class Main {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc =new Scanner(System.in);
        Door[] doors=new Door[3];
        while(sc.hasNextLine()) {
            for(int i=0;i<doors.length;i++) {
                String line = sc.nextLine();
                String[] arr = line.split(",");
                
                if(arr[0].equals("F")||arr[0].equals("f")) {
                    doors[i]=new FangDaoMen(arr[1]);
                }
                else {
                    doors[i]=new PuTongMen(arr[1]);
                }                
                        
            }
            for(int i=0;i<doors.length;i++) {
                System.out.println(doors[i]);
            }
            
        }
    }


}
abstract class Door {
    private  String color;
    public Door(String color) {
        super();
        this.color = color;
    }


    public Door() {
        super();
        // TODO Auto-generated constructor stub
    }


    public String getColor() {
        return color;
    }


    public void setColor(String color) {
        this.color = color;
    }


    public abstract String open();
    
    public abstract String close();    
}
interface Alarm {


    String alarm();
    
}
class FangDaoMen extends Door implements Alarm{


    


    public FangDaoMen() {
        super();
        // TODO Auto-generated constructor stub
    }
    public FangDaoMen(String color) {
        super(color);
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "FangDaoMen [open()=" + open() + ", close()=" + close() + ", alarm()=" + alarm() + ", getColor()="
                + getColor() + "]";
    }


    


    @Override
    public String alarm() {
        // TODO Auto-generated method stub
        return "FangDaoMen alarming";
    }


    @Override
    public String open() {
        // TODO Auto-generated method stub
        return "FangDaoMen open";
    }


    @Override
    public String close() {
        // TODO Auto-generated method stub
        return "FangDaoMen close";
    }
    
}
class PuTongMen extends Door{


    


    
    public PuTongMen() {
        super();
        // TODO Auto-generated constructor stub
    }


    public PuTongMen(String color) {
        super(color);
        // TODO Auto-generated constructor stub
    }


    @Override
    public String toString() {
        return "PuTongMen [open()=" + open() + ", close()=" + close() + ", getColor()=" + getColor() + "]";
    }


    @Override
    public String open() {
        // TODO Auto-generated method stub
        return "PuTongMen open";
    }


    @Override
    public String close() {
        // TODO Auto-generated method stub
        return "PuTongMen close";
    }
    
}
相关标签: java