Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Solutions to Java Lab 8 
 
import java.awt.Color; 
 
public class Racecar { 
    private final static double TOP_SPEED = 300.0; 
    private String name; 
    private Color color; 
 
    public Racecar(String name, Color color) { 
        this.name = name; 
        this.color = color; 
    } 
 
    public String getName() { 
        return name; 
    } 
 
    public Color getColor() { 
        return color; 
    } 
 
    public static Racecar race(Racecar r1, Racecar r2) { 
        double r1speed = Math.random() * TOP_SPEED; 
        double r2speed = Math.random() * TOP_SPEED; 
 
        if (r1speed > r2speed) { 
            return r1; 
        } else if (r1speed < r2speed) { 
            return r2; 
        } else { 
            return null; 
        } 
    } 
 
    public static void main(String args[]) { 
        Racecar a = new Racecar("Corvette", Color.BLUE); 
        Racecar b = new Racecar("Porsche",  Color.RED); 
        Racecar winner = race(a, b); 
 
        if (winner == null) { 
            System.out.println("The race is a tie"); 
        } else { 
            System.out.println(winner.getName() + " won!"); 
        } 
    } 
}