设计交通工具类Vehicle,其中的私有属性包括:类别kind、速度speed、颜色color;其中的公共方法包括:设置类别setKind( )、设置速度setSpeed( )、设置颜色setColor( )、获取类别getKind( )、获取速度getSpeed( )、获取颜色getColor( )。
定义测试类Main,在其main方法中创建两个Vehicle的对象v1和v2,通过键盘输入数据,为其设置不同的类别、速度和颜色,同时输出其所有信息。
输入格式:
前三行分别输入v1的类别、速度、颜色,
后三行分别输入v2的类别、速度、颜色。
输出格式:
第一行输出v1的类别、速度和颜色,
第二行输出v2的类别、速度和颜色。
输入样例:
在这里给出一组输入。例如:
摩托车
50
红色
小汽车
100
黑色
输出样例:
在这里给出相应的输出。例如:
第一辆是摩托车,时速为50.0km/h,颜色为红色
第二辆是小汽车,时速为100.0km/h,颜色为黑色
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String kind1 = in.next();
double speed1 = in.nextDouble();
String color1 = in.next();
Vehicle v1 = new Vehicle(kind1,speed1,color1);
v1.print1();
String kind2 = in.next();
double speed2 = in.nextDouble();
String color2 = in.next();
Vehicle v2 = new Vehicle(kind2,speed2,color2);
v2.print2();
}
}
class Vehicle{
String kind;
double speed;
String color;
Vehicle(String C_kind,double C_speed,String C_color){
kind = C_kind;
speed = C_speed;
color = C_color;
}
void print1(){
System.out.println("第一辆是" + kind +",时速为" + speed +"km/h,颜色为"+color);
}
void print2(){
System.out.println("第二辆是" + kind +",时速为" + speed +"km/h,颜色为"+color);
}
}