一、用程序描述下面这幅图。鸟类、昆虫类各生成一个对象即可
interface Flyanimal{
void fly();
}
class Insect {
int legnum=6;
}
class Flybird {
int legnum=2;
void eggs(){} ;
}
class Ant extends Insect implements Flyanimal {
public void fly (){
System. out. println("Ant can fly");
}
}
class Pigeon extends Flybird implements Flyanimal {
public void fly (){
System. out. println("pigeon can fly !") ;
}
public void egg(){
System. out. println("pigeon can lay eggs!");
}
}
public class Test3{
public static void main(String args[]){
Ant a=new Ant();
a.fly();
System. out. println("Ant's legs are"+a. legnum);
Pigeon p= new Pigeon();
p. fly();
p. egg();
}
}二、智能机、老年机都是移动电话,它们都可以开机、关机、接电话、发短信。
其中智能机还可以上网、拍照、听音乐、办公。苹果牌红色手机开机,苹果牌红色的手机上网,
三星牌白色手机发送短信,三星牌白色手机听音乐,
HTC牌黑色手机通话中,HTC牌黑色手机拍照。用程序描述这一过程
package seventeen;
abstract class Mobile{
String brand;
String color;
public void poweron(){
System.out.println(this.brand+this.color+"手机开机");
}
public void poweroff() {
System.out.println(this.brand+"牌手机关机");
}
public void sendmessage() {
System.out.println(this.brand+this.color+"发送短信");
}
public void phone() {
System.out.println(this.brand+this.color+"通话中。。。");
}
}
interface AI{
public void internet();
public void office();
public void music();
public void camera();
}
class Samsung extends Mobile implements AI{
Samsung(){
}
Samsung(String brand,String color){
this.brand=brand;
this.color=color;
}
@Override
public void internet() {
System.out.println(this.brand+this.color+"牌手机上网。。。");
}
@Override
public void office() {
System.out.println(this.brand+"牌手机办公");
}
@Override
public void music() {
System.out.println(this.brand+this.color+"手机播放音乐。。。");
}
@Override
public void camera() {
System.out.println(this.brand+"牌手机拍照。。。");
}
}
class Apple extends Mobile implements AI{
Apple(){
}
Apple(String brand,String color){
this.brand=brand;
this.color=color;
}
@Override
public void internet() {
System.out.println(this.brand+this.color+"牌手机上网。。。");
}
@Override
public void office() {
System.out.println(this.brand+"牌手机办公");
}
@Override
public void music() {
System.out.println(this.brand+this.color+"手机播放音乐。。。");
}
@Override
public void camera() {
System.out.println(this.brand+"牌手机拍照。。。");
}
}
class HTC extends Mobile implements AI{
HTC(){
}
HTC(String brand,String color){
this.brand=brand;
this.color=color;
}
@Override
public void internet() {
System.out.println(this.brand+this.color+"牌手机上网。。。");
}
@Override
public void office() {
System.out.println(this.brand+"牌手机办公");
}
@Override
public void music() {
System.out.println(this.brand+this.color+"手机播放音乐。。。");
}
@Override
public void camera() {
System.out.println(this.brand+"牌手机拍照。。。");
}
}
public class Test1 { public static void main(String[] args) {
// TODO Auto-generated method stub
Samsung s = new Samsung("三星","白色");
Apple a = new Apple("苹果","红色");
HTC h = new HTC("HTC","黑色");
s.sendmessage();
s.music();
a.poweron();
a.internet();
h.phone();
h.camera();
}}三、求三角形面积周长
package third; abstract class Shape {
public abstract double getcircle(); public abstract double getArea();
}
public class Trigle extends Shape{
int a,b,c;
double p=0.0; //p=(a+b+c)/2p*(p-a)*(p-b)*(p-c)的平方根----海伦公式
Trigle(int a,int b, int c,double p)
{
this.a=a;
this.b=b;
this.c=c;
this.p=(a+b+c)/2.0;//注意:分母必须是2.0,否则精度会丢失
}
@Override
public double getcircle(){
return a+b+c;
}
@Override
public double getArea(){
return Math. sqrt(p*(p-a)*(p-b)*(p-c));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape s = new Trigle(3,4,5);
System.out.println("三条边长度分别为3、4、5厘米的三角形周长是:"+s.getcircle()+"厘米");
System.out.println("三条边长度分别为3、4、5厘米的三角形面积是:"+s.getArea()+"平方厘米");
}
}
四、防盗门,可以开门、关门。当小偷来的时候,可以报警。用程序描述这一过程。
解析:开门、关门反应本质——抽象类,报警反应功能——接口//创建一个接口
interface Alarm {
public abstract void alarm();
}
//创建一个抽象类,反应门的本质开门、关门
abstract class Door {
public abstract void open();//抽象方法为开门
public abstract void close();//抽象方法为关门
}
//继承抽象类实现接口
public class alarmdoor extends Door implements Alarm{
@Override
public void alarm() {
System.out.println("----小偷来了报警----");
}
@Override
public void open() {
System.out.println("----开门----");
}
@Override
public void close() {
System.out.println("----关门----");
}
public static void main(String[] args) {
alarmdoor d = new alarmdoor();
d.open();
d.close();
d.alarm(); }
}
interface Flyanimal{
void fly();
}
class Insect {
int legnum=6;
}
class Flybird {
int legnum=2;
void eggs(){} ;
}
class Ant extends Insect implements Flyanimal {
public void fly (){
System. out. println("Ant can fly");
}
}
class Pigeon extends Flybird implements Flyanimal {
public void fly (){
System. out. println("pigeon can fly !") ;
}
public void egg(){
System. out. println("pigeon can lay eggs!");
}
}
public class Test3{
public static void main(String args[]){
Ant a=new Ant();
a.fly();
System. out. println("Ant's legs are"+a. legnum);
Pigeon p= new Pigeon();
p. fly();
p. egg();
}
}二、智能机、老年机都是移动电话,它们都可以开机、关机、接电话、发短信。
其中智能机还可以上网、拍照、听音乐、办公。苹果牌红色手机开机,苹果牌红色的手机上网,
三星牌白色手机发送短信,三星牌白色手机听音乐,
HTC牌黑色手机通话中,HTC牌黑色手机拍照。用程序描述这一过程
package seventeen;
abstract class Mobile{
String brand;
String color;
public void poweron(){
System.out.println(this.brand+this.color+"手机开机");
}
public void poweroff() {
System.out.println(this.brand+"牌手机关机");
}
public void sendmessage() {
System.out.println(this.brand+this.color+"发送短信");
}
public void phone() {
System.out.println(this.brand+this.color+"通话中。。。");
}
}
interface AI{
public void internet();
public void office();
public void music();
public void camera();
}
class Samsung extends Mobile implements AI{
Samsung(){
}
Samsung(String brand,String color){
this.brand=brand;
this.color=color;
}
@Override
public void internet() {
System.out.println(this.brand+this.color+"牌手机上网。。。");
}
@Override
public void office() {
System.out.println(this.brand+"牌手机办公");
}
@Override
public void music() {
System.out.println(this.brand+this.color+"手机播放音乐。。。");
}
@Override
public void camera() {
System.out.println(this.brand+"牌手机拍照。。。");
}
}
class Apple extends Mobile implements AI{
Apple(){
}
Apple(String brand,String color){
this.brand=brand;
this.color=color;
}
@Override
public void internet() {
System.out.println(this.brand+this.color+"牌手机上网。。。");
}
@Override
public void office() {
System.out.println(this.brand+"牌手机办公");
}
@Override
public void music() {
System.out.println(this.brand+this.color+"手机播放音乐。。。");
}
@Override
public void camera() {
System.out.println(this.brand+"牌手机拍照。。。");
}
}
class HTC extends Mobile implements AI{
HTC(){
}
HTC(String brand,String color){
this.brand=brand;
this.color=color;
}
@Override
public void internet() {
System.out.println(this.brand+this.color+"牌手机上网。。。");
}
@Override
public void office() {
System.out.println(this.brand+"牌手机办公");
}
@Override
public void music() {
System.out.println(this.brand+this.color+"手机播放音乐。。。");
}
@Override
public void camera() {
System.out.println(this.brand+"牌手机拍照。。。");
}
}
public class Test1 { public static void main(String[] args) {
// TODO Auto-generated method stub
Samsung s = new Samsung("三星","白色");
Apple a = new Apple("苹果","红色");
HTC h = new HTC("HTC","黑色");
s.sendmessage();
s.music();
a.poweron();
a.internet();
h.phone();
h.camera();
}}三、求三角形面积周长
package third; abstract class Shape {
public abstract double getcircle(); public abstract double getArea();
}
public class Trigle extends Shape{
int a,b,c;
double p=0.0; //p=(a+b+c)/2p*(p-a)*(p-b)*(p-c)的平方根----海伦公式
Trigle(int a,int b, int c,double p)
{
this.a=a;
this.b=b;
this.c=c;
this.p=(a+b+c)/2.0;//注意:分母必须是2.0,否则精度会丢失
}
@Override
public double getcircle(){
return a+b+c;
}
@Override
public double getArea(){
return Math. sqrt(p*(p-a)*(p-b)*(p-c));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape s = new Trigle(3,4,5);
System.out.println("三条边长度分别为3、4、5厘米的三角形周长是:"+s.getcircle()+"厘米");
System.out.println("三条边长度分别为3、4、5厘米的三角形面积是:"+s.getArea()+"平方厘米");
}
}
四、防盗门,可以开门、关门。当小偷来的时候,可以报警。用程序描述这一过程。
解析:开门、关门反应本质——抽象类,报警反应功能——接口//创建一个接口
interface Alarm {
public abstract void alarm();
}
//创建一个抽象类,反应门的本质开门、关门
abstract class Door {
public abstract void open();//抽象方法为开门
public abstract void close();//抽象方法为关门
}
//继承抽象类实现接口
public class alarmdoor extends Door implements Alarm{
@Override
public void alarm() {
System.out.println("----小偷来了报警----");
}
@Override
public void open() {
System.out.println("----开门----");
}
@Override
public void close() {
System.out.println("----关门----");
}
public static void main(String[] args) {
alarmdoor d = new alarmdoor();
d.open();
d.close();
d.alarm(); }
}