程序片段编程题
1. 定义接口CircleInterface,类Circle实现接口,实现计算圆的周长和面积,测试其特性。
【问题描述】
定义接口CircleInterface,类Circle实现接口,实现计算圆的周长和面积,测试其特性。
【输入形式】
请输入圆的半径
【输出形式】
圆的面积是:
圆的周长是:
【样例输入】
Please input r: 2
【样例输出】
The area is : 12.56636
The circumference is : 12.56636
【样例说明】
根据样例输出,完成程序填空。
//计算圆的周长和面积
import java.util.Scanner;
interface CircleInterface
{
final float PI=3.14159f;
float getArea(float r);
float getCircumference(float r);
}
class Circle implements CircleInterface
{
public float getArea(float r)
{
return PI*r*r;
}
public float getCircumference(float r)
{
return 2*PI*r;
}
}
public class Test {
public static void main(String[] args) {
Circle c = new Circle();
Scanner s = new Scanner(System.in);
System.out.println("Please input r: ");
float r = s.nextFloat();
System.out.println("The area is : "+ c.getArea(r));
System.out.println("The circumference is : "+c.getCircumference(r));
}
}
2. 定义抽象类Vehicles具有抽象方法wheel, 接口MoneyFare具有方法charge,类Bus和Taxi实现抽象类和接口,Car实现接口,测试其特性。
【问题描述】
定义抽象类Vehicles具有抽象方法wheel, 接口MoneyFare具有方法charge,类Bus和Taxi实现抽象类和接口,Car实现接口,测试其特性。
【输入形式】
请输入公交车线次:347
请输入出租车名字:yellow
请输入你的车品牌:BMW
【输出形式】
公交车347有6个轮子
公交车347票价2元每人
出租车yellow有4个轮子
出租车yellow为2元每公里
如果这辆BMW车是你的就免费乘坐
【样例输入】
347
Yellow
BMW
【样例输出】
The bus 347 has 6 wheels.
The bus 347 2 yuan per people
The taxi Yellow has 4 wheels.
The taxi Yellow 2 yuan per km
The car BMW is free if it is yours
【样例说明】
根据样例输出,完成程序填空。
import java.util.Scanner;
abstract class Vehicles
{
abstract void wheel(String name);
}
interface MoneyFare
{
void charge(String name);
}
class Bus extends Vehicles implements MoneyFare
{
void wheel(String name)
{
System.out.println("The bus "+ name +" has 6 wheels.");
}
public void charge(String name)
{
System.out.println("The bus "+ name +" 2 yuan per people");
}
}
class Taxi extends Vehicles implements MoneyFare
{
void wheel(String name)
{
System.out.println("The taxi "+ name +" has 4 wheels.");
}
public void charge(String name)
{
System.out.println("The taxi "+ name +" 2 yuan per km");
}
}
class Car implements MoneyFare
{
public void charge(String name)
{
System.out.println("The car "+ name +" is free if it is yours");
}
}
public class Test
{
public static void main(String args[])
{
Bus bus = new Bus();
Taxi taxi = new Taxi();
Car car = new Car();
String busname,taxiname,carname;
Scanner s = new Scanner(System.in);
System.out.println("Please input the bus line: ");
busname = s.nextLine();
System.out.println("Please input the taxi name: ");
taxiname = s.nextLine();
System.out.println("Please input the car brand: ");
carname = s.nextLine();
bus.wheel(busname);
bus.charge(busname);
taxi.wheel(taxiname);
taxi.charge(taxiname);
car.charge(carname);
}
}