定义交通工具类 Vehicle ,具有重量 weight 、类型 category 属性,具有输出车辆信息 printInfo 方法;定义 Contest 接口,具有参加竞赛 join 方法;
定义汽车类 Car ,维承 Vehicle 类,并增加成员变量 String speed [:存储汽车三次测试的速度值;增加构造方法
Car ( int weight , String category , intLspeed );重写 printInfo 方法。
实现 Contest 接口,实现其抽象方法 join ,如果最大速度值超过130则输出“可以参加竞赛”,否则输出“功率不够不适合参赛”。然后在 main 方法中测试。
样例 l :
输入:2跑车105150128
们出:
重量:2吨
类型:跑车
速度:105150128可以参加竞赛
样例2:
输入;3 suv 90125110
输出:
重量:3吨
类型: sUV
速度:90125110
功率不够不适合参赛
速度之间没空格的?构造方法的速度类型也不对啊。题目截图看下。
import java.util.Scanner;
public class Car extends Vehicle implements Contest{
String speed;
Car ( int weight , String category , String speed){
this.weight=weight;
this.category=category;
this.speed=speed;
}
@Override
public void printInfo() {
System.out.println("重量:"+weight+"吨");
System.out.println("类型:"+category);
System.out.println("速度:"+speed);
}
@Override
public void join() {
boolean flag=false;
String[] s = speed.split(" ");
for(int i=0;i<s.length;i++){
if (Integer.valueOf(s[i])>130){
flag=true;
break;
}
}
if (flag){
System.out.println("可以参加竞赛");
}else{
System.out.println("功率不够不适合参赛");
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Car car=new Car(in.nextInt(),in.next(),in.nextInt()+" "+in.nextInt()+" "+in.nextInt());
car.printInfo();
car.join();
}
}
class Vehicle {
int weight;
String category ;
public void printInfo(){
}
}
public interface Contest {
public void join();
}
成员变量 String speed根据输入的int来生成,join函数中分解出最低速度、最高速度和一般速度,并根据最高速度输出结果。
运行结果:
Vehicle 类:
public class Vehicle {
protected int weight;
protected String category;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public void printInfo(){
System.out.println("重量:"+weight+"吨");
System.out.println("类型:"+category);
}
}
Contest接口:
public interface Contest {
public abstract void join();
}
Car类:
public class Car extends Vehicle implements Contest{
private String speed;
public Car(int weight , String category , int speed){
setWeight(weight);
setCategory(category);
this.speed = Integer.toString(speed);
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
@Override
public void join() {
// TODO Auto-generated method stub
/*
最大速度值超过130则输出“可以参加竞赛”,
否则输出“功率不够不适合参赛”*/
int maxspeed;
if(speed.length() == 9){
maxspeed = Integer.parseInt(speed.substring(3,6));
}else if(speed.length() == 8 || speed.length() == 7)
maxspeed = Integer.parseInt(speed.substring(2,5));
else
maxspeed = Integer.parseInt(speed.substring(2,4));
//System.out.println("maxspeed="+maxspeed);
if(maxspeed > 130)
System.out.println("可以参加竞赛");
else
System.out.println("功率不够不适合参赛");
}
@Override
public void printInfo(){
System.out.println("重量:"+weight+"吨");
System.out.println("类型:"+category);
System.out.print("速度:"+speed+" ");
join();
}
}
测试类:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int w = sc.nextInt();
String cat = sc.next();
int sp = sc.nextInt();
Car c = new Car(w,cat,sp);
c.printInfo();
}
}
翻译题
把汉语描述翻译成java即可
答题不易 有用请采纳
import java.util.Scanner;
interface Contest {
public abstract String join();
}
class Vehicle {
private int weight;
private String category;
protected Vehicle(int weight, String category) {
super();
this.weight = weight;
this.category = category;
}
public String printInfo() {
return "重量:" + weight + "吨\n类型:" + category;
}
}
class Car extends Vehicle implements Contest {
private String[] speed=new String[3];
protected Car(int weight, String category, int [] speed) {
super(weight, category);
for (int i = 0; i < speed.length; i++) {
this.speed[i]=String.valueOf(speed[i]);
}
}
@Override
public String join() {
boolean flag=false;
for (int i = 0; i < speed.length; i++) {
if(Integer.parseInt(speed[i])> 130) {
flag=true;
break;
}
}
return flag?"可以参加竞赛":"功率不够不适合参赛";
}
@Override
public String printInfo() {
// TODO Auto-generated method stub
return super.printInfo() + "\n速度:" + speed[0]+" "+speed[1]+" "+speed[2]+"\n"+ this.join();
}
}
public class Answer7732632 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入:");
int weight=input.nextInt();
String category=input.next();
int speed[]=new int[3];
for (int i = 0; i < speed.length; i++)
speed[i]=input.nextInt();
Car car = new Car(weight, category, speed);
System.out.println(car.printInfo());
}
}