JAVA语言程序设计课后习题----第七单元解析(仅供参考)

1  本题水题,就是想让你理解继承的含义

1 public class Animaal {
2     public double weight;
3     public void eat(){
4     }
5 }
1 public class Bird extends Animaal {
2     public int numberOfFins;
3     public void fly(){}
4 
5 
6 }
1 public class Dog extends Animaal {
2     public int numberOflegs;
3     public void walk(){}
4 
5 
6 }
1 public class Fish extends Animaal{
2     public int numberOfFins;
3     public void swim(){}
4 
5 
6 }

 2  本题主要考类的继承和方法的重写,方法的重写关键字@Override

 

1 public class Circle {
2     public int radius;
3     public double getArea(){
4         return Math.PI*radius*radius;
5     }
6 }
 1 public class Cylinder extends Circle {
 2     public double height;
 3 
 4     public Cylinder() {
 5     }
 6 
 7     public Cylinder(double height) {
 8         this.height = height;
 9     }
10     public Cylinder(int radius,double height){
11         this.radius=radius;
12         this.height=height;
13     }
14     //    体积
15     public double getVolume(){
16         return Math.PI*radius*radius*height;
17     }
18     //    覆盖Circle 里面的getArea函数求圆柱的体积
19     @Override
20     public double getArea(){
21         return Math.PI*radius*radius*2+2*Math.PI*radius*height;
22     }
23 }
 1 import java.util.Scanner;
 2 
 3 public class ch08 {
 4     public static void main(String[] args) {
 5 
 6         Scanner input = new Scanner(System.in);
 7         System.out.println("请输入圆柱的半径、高");
 8         Cylinder cylinder = new Cylinder(input.nextInt(),input.nextDouble());
 9         System.out.println("圆柱的体积为:"+cylinder.getVolume());
10         System.out.println("圆柱的面积为:"+cylinder.getArea());
11     }
12 }

 3  本题水题,考的方法和上面两题类似,注意题目要求即可

 1 public class Auto {
 2     public double speed;
 3     //    启动的方法
 4     public void start(){
 5         System.out.println("插上钥匙、系上安全带、一脚把离合踩到底、挂上一档、打左转向、缓慢抬脚、汽车平稳启动");
 6     }
 7     //    加速的方法
 8     public void speedUp(){
 9         System.out.println("脚慢踩油门、松开油门、踩离合到底、换挡、加速完成");
10     }
11     //    停止的方法
12     public void stop(){
13         System.out.println("慢踩刹车、达到额定速度、松开刹车、踩离合、换挡、持续到速度为0");
14     }
15 }
1 public class Bus extends Auto {
2     public int passenger;
3     public Bus(int passenger){this.passenger=passenger;}
4     //    上车
5     public int gotOn(){ return ++passenger;
6     }
7     //    下车
8     public int gotOff(){return --passenger;}
9 }
 1 import java.util.Scanner;
 2 
 3 public class ch09 {
 4     public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         System.out.println("请输入开始车上的人数");
 7         Bus bus = new Bus(input.nextInt());
 8         System.out.println("旅客上车");
 9         System.out.println(bus.gotOn());
10         bus.start();
11         bus.speedUp();
12         System.out.println("旅客上下车");
13         System.out.println(bus.gotOff());
14         bus.stop();
15     }
16 }

 

4  本题主要考继承和抽象方法,关键字@Override

 

 

1 public class Shape {
2     public int radius;
3     public int l;
4     public double getPrimeter(){
5         return 0;
6     }
7     public double getArea(){return 0;}
8 }
 1 public class Square extends Shape {
 2     public Square() {
 3     }
 4 
 5     public Square(int radius){this.radius=radius;}
 6     @Override
 7     public double getPrimeter(){
 8         return 4*radius;
 9     }
10     @Override
11     public double getArea(){
12         return radius*radius;
13     }
14 
15 }
1 public class ch10 {
2     public static void main(String[] args) {
3         Square square = new Square(10);
4         System.out.println("正方形的周长为:"+square.getPrimeter());
5         System.out.println("正方形的面积为:"+square.getArea());
6     }
7 }

5  本题主要考方法的覆盖,构造方法的重写

1 public class Rectangle {
2     public double length,width;
3 
4     public Rectangle(double length, double width) {
5         this.length=length;
6         this.width=width;
7     }
8 }
 1 public class Cuboid extends Rectangle{
 2     public double height;
 3 
 4     public Cuboid(double length, double width, double height) {
 5         super(length, width);
 6         this.height = height;
 7     }
 8 
 9     public double volume(){
10         return length*width*height;
11     }
12 }
1 public class ch11 {
2     public static void main(String[] args) {
3         System.out.println("长方体的长、宽、高分别为10、5、2!");
4         Cuboid cuboid = new Cuboid(10,5,2);
5         System.out.println("长方体的体积为:"+cuboid.volume());
6     }
7 }

 

转载于:https://www.cnblogs.com/PerZhu/p/10892240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java语言程序设计课后答案 作业参考答案 习题一 4、如何建立和运行Java程序, 首先启动文本编辑器,如记事本、UltraEdit等,编辑程序代码,并以.Java作为文件 扩展名保存程序源代码;然后进入dos环境利用javac编译源程序,生成扩展名为.class的 字节码文件;再利用命令java运行字节码文件,得到程序的运行结果。在集成开发环境J builder、Eclipse下,可以完成程序的编辑、编译、调试及运行等所有任务。 5、 public class LikeJava { public static void main(String [] args) { System.out.println("I Like Java Very much!"); } } 习题二 5、 (1) 45 (2) false (3) 14 (4) 14 (5),6 (6) true (7) 12 9、 public class Volume { public static void main(String [] args) { double r=0,v=0; r=double.parseDouble(args[0]); v=4*3.14159/3*r*r*r; System.out.println("球体积为:"+v); } } 习题三 8、 public class Factorials { public static void main(String args[]) { int i, j; long s=0, k; i=1; do //外循环开始 { k = 1; j=1; do{//内循环开始 k = k * j; //内循环体 j++; }while(j<=i);//内循环结束 System.out.println(i + "!=" + k); s = s + k; i++; }while(i<=20); //外循环结束 System.out.println("Total sum=" + s); } } 10、 public class Num { public static void main(String[]args) { int i,j,k,n; for (n=100;n<1000;n++) { i=n/100; j=(n-i*100)/10; k=n%10; if (i*i*i+j*j*j+k*k*k==n) System.out.print(n+" "); } } } 习题四 5、 import java.util.Scanner; class Factor{ long fac(int m) {if(m==0""m==1)return 1; else return m*fac(m-1); } public static void main(String [] args) {int i,n; long sum=0; String s=""; Scanner input=new Scanner(System.in); System.out.print("Please input n: "); n=input.nextInt(); Factor f=new Factor(); for(i=1;i<=n;i++) { System.out.println(f.fac(i)); sum=sum+f.fac(i); s=s+i+"!+"; } System.out.println(s.substring(0,s.length()-1)+"="+sum); } } 习题五 2、 import java.io.*; public class YangHuiOk { public static void main (String args[]) throws IOException { int max,a[][],i,j; char x; System.out.print("请输入杨辉三角要显示的行数: "); x=(char)System.in.read(); max = Integer.parseInt(String.valueOf(x)); a=new int[max][]; for (i=0;i<max;i++) { a[i]=new int[i+1]; } a[0][0]=1; for (i=1;i<max;i++) { a[i][0]=1; a[i][a[i].length-1]=1; for (j=1;j<a[i].length-1;j++) { a[i][j]=a[i-1][j-1]+a[i-1][j]; } } for(i=0;i<max;i++) { //for(j=0;j<=max-i;j++) System.out
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值