import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Test t=new Test();
Thread th=new Thread(t);
th.start();
}
}
class Test implements Runnable {
public void run() {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=n;i>=0;i–){
System.out.println(i);
try
{
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
7-9 设计圆和圆柱体 (10分)
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i = 0; i < n; i++) {
String str = input.next();
if(str.equals(“Circle”)) {
Circle c = new Circle(input.nextDouble());
System.out.println(“The area of " + c.toString() + " is " + String.format(”%.2f",c.getArea()));
System.out.println(“The perimeterof " + c.toString() + " is “+ String.format(”%.2f”,c.getPerimeter()));
} else if(str.equals(“Cylinder”)) {
Cylinder r = new Cylinder(input.nextDouble(), new Circle(input.nextDouble()));
System.out.println(“The area of " + r.toString() + " is " + String.format(”%.2f",r.getArea()));
System.out.println(“The volume of " + r.toString() + " is " + String.format(”%.2f",r.getVolume()));
}
}
}
}
class Circle{
private double radius;
public Circle(double radius){
this.radius = radius;
}
public Circle(){
radius = 0;
}
public void setRadius(double r){
radius = r;
}
public double getRadius(){
return this.radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
public double getPerimeter(){
return 2 * Math.PI * radius;
}
@Override
public String toString(){
return “Circle(r:” + radius + “)”;
}
}
class Cylinder{
private double height;
private Circle circle;
public Cylinder(double height, Circle circle){
this.height = height;
this.circle = circle;
}
public Cylinder(){
height = 0;
circle = new Circle(0);
}
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height;
}
public void setCircle(Circle circle){
this.circle = circle;
}
public Circle getCircle(){
return circle;
}
public double getArea(){
return 2 * Math.PI * circle.getRadius() * circle.getRadius() + 2 * Math.PI * circle.getRadius() * height;
}
public double getVolume(){
return Math.PI * circle.getRadius() * circle.getRadius() * height;
}
@Override
public String toString(){
return “Cylinder(h:” + height + “,” + circle.toString() + “)”;
}
}
7-10 设计一个Tiangle异常类 (20分)
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
try {
Triangle t = new Triangle(s1,s2,s3);
System.out.println(t);
}catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
}
}
class IllegalTriangleException extends Exception{
private double side1;
private double side2;
private double side3;
public IllegalTriangleException(double side1,double side2 ,double side3) {
super("Invalid: "+side1+","+side2+","+side3);//message是父类成员变量,使用super向上传参!
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
}
class Triangle{
private double side1;
private double side2;
private double side3;
public Triangle(double side1,double side2,double side3)
throws IllegalTriangleException{
if((side1+side2)>side3&(side1+side3)>side2&(side2+side3)>side1)
{this.side1=side1;this.side2=side2;this.side3=side3;}
else
throw new IllegalTriangleException(side1,side2,side3);
}
@Override
public String toString(){
return "Triangle ["+"side1="+this.side1+", side2="+this.side2+", side3="+this.side3+"]";
}
}