文档库 最新最全的文档下载
当前位置:文档库 › Java基础第4章编程题答案

Java基础第4章编程题答案

Java基础第4章编程题答案
Java基础第4章编程题答案

第四章编程题

1. 按要求完成以下题目

(1)

Circle( ) // 将半径设为0

Circle(double r ) // 创建 Circle 对象时将半径初始化为r

③三个成员方法

double getArea( ) //获取圆的面积

double getPerimeter( ) // 获取圆的周长

void show( ) //将圆的半径、周长、面积输出到屏幕

(2)编写一个圆柱体类Cylinder ,它继承于上面的Circle 类。还拥有:

①一个成员变量double hight(私有,浮点型);// 圆柱体的高;

②构造方法

Cylinder (double r, double h )//创建Circle对象时将半径初始化为r

③成员方法

double getVolume( ) // 获取圆柱体的体积

void showVolume( ) // 将圆柱体的体积输出到屏幕

编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。

【参考答案】

import java.math.*;

class Circle{

private double radius;

public Circle() {

radius=0;

}

public Circle(double r) {

radius=r;

}

void setRadius(double r) {

this.radius=r;

}

double getRadius()

{

return this.radius;

}

double getArea() {

return Math.PI*radius*radius;

}

double getPerimeter() {

return Math.PI*2*radius;

}

void show() {

System.out.println("圆的半径为"+radius+"\n圆的周长为

"+this.getPerimeter()+"\n圆的面积为"+this.getArea());

}

}

class Cylinder extends Circle{

double hight;

public Cylinder(double r,double h) {

super.setRadius(r);

this.hight=h;

}

double getVolume()

{

double volume=super.getArea()*hight;

return volume;

}

void showVolume() {

System.out.println("圆柱的面积为"+this.getVolume());

}

}

public class Example01 {

public static void main(String[] args) {

Circle circle=new Circle(5);

circle.show();

Cylinder cylinder=new Cylinder(2,5);

cylinder.showVolume();

}

}

2.按要求编写一个Java应用程序:

(l)定义一个接口CanCry,描述会吼叫的方法public void cry()

(2)分别定义狗类(Dog)和猫类(Cat),实现CanCry接口。实现方法的功能分别为:打印输出“我是狗,我的叫声是汪汪汪”、“我是猫,我的叫声是喵喵喵”。(3)定义一个主类G,

①定义一个void makeCry(CanCry c)方法,其中让会吼叫的事物吼叫。

②在main方法中创建狗类对象(dog)、猫类对象(cat)、G类对象(g),用g 调用makeCry方法,让狗和猫吼叫。

【参考答案】

interface CanCry{

public void cry();

}

class Dog implements CanCry{

public void cry()

{

System.out.println("我是狗,我的叫声是汪汪汪");

}

}

class Cat implements CanCry{

public void cry()

{

System.out.println("我是猫,我的叫声是喵喵喵");

}

}

public class G{

void makeCry(CanCry c) {

c.cry();

}

public static void main(String[] args) {

Dog dog=new Dog();

Cat cat=new Cat();

G g1=new G();

g1.makeCry(dog);

g1.makeCry(cat);

}

}

3.编写一个Java程序,并满足如下要求:

(1)编写一个Car类,具有:

属性:品牌(mark)一一String类型

功能:驾驶(void drive())

(2)定义Car类的子类SubCar,具有:

属性:价格(price)、速度(speed)一一int型

功能:变速(void speedChange(int newSpeed),把新速度赋给speed

(3)定义主类E,在其main方法中创建SubCar类的两个对象:aodi和benchi 的对象并测试其对象的特性:

(4)输出效果如下:

【参考答案】

class Car{

String make;

void drive() {

System.out.println("本车正在驾驶");

}

}

class SubCar extends Car{

int price;

int speed;

void speedChange(int newSpeed) {

this.speed=newSpeed;

}

}

public class Chap403 {

public static void main(String[] args) {

SubCar aodi=new SubCar();

SubCar benchi=new SubCar();

aodi.make="奥迪";

aodi.price=50;

aodi.speed=80;

System.out.println("本车的品牌是"+aodi.make);

System.out.println("奥迪车的价格是"+aodi.price);

System.out.println("奥迪车原来的速度是"+aodi.speed);

aodi.drive();

aodi.speedChange(120);

System.out.println("奥迪车现在的速度是"+aodi.speed);

benchi.make="奔驰";

benchi.price=100;

benchi.speed=90;

System.out.println("本车的品牌是"+benchi.make);

System.out.println("奔驰车的价格是"+benchi.price);

System.out.println("奔驰车原来的速度是"+benchi.speed);

benchi.drive();

benchi.speedChange(120);

System.out.println("奔驰车现在的速度是"+benchi.speed);

}

}

4. 编写程序,提供实现各种数学计算的方法,并进行测试。包括如下几项。(1)两个数的加、减、乘、除。

(2)求某数的相反数、倒数、绝对值。

(3)取两数中较大的和较小的。

(4)对浮点数(double型)的计算功能。如:给定浮点数d,取大于或等于d 的最小整数,取小于或等于d的最大整数,计算最接近d的整数值,计算d的平方根、自然对数log(d)等。

(5)计算以double型数a为底数,b为指数的幂。

【参考答案】

import java.math.*;

import java.util.Scanner;

import https://www.wendangku.net/doc/6d11935663.html,ng.Exception;

class DivideByException extends Exception{

public DivideByException() {

super();

}

public DivideByException(String message) {

super(message);

}

}

class MyMath{

int add(int x,int y) {

return x+y;

}

int sub(int x,int y) {

return x-y;

}

int mul(int x,int y) {

return x*y;

}

int divide(int x,int y){

try {

return x/y;

}catch(Exception e) {

System.out.println("除数不能为零");

return -1;

}

}

int opposite(int x) {

return -x;

}

double reciprocal(int x)throws DivideByException { if(x==0)

throw new DivideByException("0的倒数为无穷大");

// throw new DivideByException();

return (double)1/x;

}

int myabs(int x) {

return Math.abs(x);

}

int max(int x,int y) {

if(x>=y)

return x;

else

return y;

}

int min(int x,int y) {

if(x<=y)

return x;

else

return y;

}

double mySqrt(double x) {

double d,minx,maxx,sqrtd,logd;

minx=(Math.ceil(x));

maxx=(Math.floor(x));

if(Math.abs(minx-x)>=Math.abs(maxx-x))

d=maxx;

else

d=minx;

System.out.println(d);

sqrtd=Math.sqrt(d);

logd=Math.log(d);

System.out.println("平方根为"+sqrtd);

return logd;

}

double myPower(double a,double b) {

return Math.pow(a, b);

}

}

public class Chap404 {

public static void main(String[] args) {

int num,x,y;

double a,b;

MyMath mymath=new MyMath();

Scanner scanner=new Scanner(System.in);

System.out.println("1--两个整数加、减、乘、除");

System.out.println("2--整数相反数,倒数和绝对值");

System.out.println("3--两个整数较大值,较小值");

System.out.println("4--浮点数取整的平方根何自然对数");

System.out.println("5--指数幂");

System.out.println("请输入您要进行的操作");

num=scanner.nextInt();

switch(num) {

case 1:System.out.println("请输入用于计算的两个数");

x=scanner.nextInt();

y=scanner.nextInt();

System.out.println(x+"+"+y+"="+mymath.add(x, y));

System.out.println(mymath.sub(x, y));

System.out.println(mymath.mul(x, y));

System.out.println(mymath.divide(x, y));

break;

case 2:System.out.println("请输入数");

x=scanner.nextInt();

System.out.println("相反数是"+mymath.opposite(x));

try{

System.out.println("倒数是"+mymath.reciprocal(x));

}catch(DivideByException e) {

System.out.println(e.getMessage());

}

System.out.println("绝对值是"+mymath.myabs(x));

break;

case 3:System.out.println("请输入用于计算的两个数");

x=scanner.nextInt();

y=scanner.nextInt();

System.out.println("较大值是"+mymath.max(x, y));

System.out.println("较小值是"+mymath.min(x, y));

break;

case 4:System.out.println("请输入浮点数");

a=scanner.nextDouble();

System.out.println("自然对数为"+mymath.mySqrt(a));

break;

case 5:System.out.println("请输入底数和幂");

a=scanner.nextDouble();

b=scanner.nextDouble();

System.out.println("结果="+mymath.myPower(a, b));

break;

}

}

}

5. 编写一个抽象类Shape,声明计算图形面积的抽象方法。再分别定义Shape 的子类Circle(圆)和Rectangle(矩形),在两个子类中按照不同图形的面积计算公式,实现Shape类中计算面积的方法。

【参考答案】

import java.math.*;

abstract class Shape{

public abstract void getArea();

}

class Circle extends Shape{

private double radius;

public Circle(double radius) {

this.radius=radius;

}

public void getArea() {

double myArea=Math.PI*radius*radius;

System.out.println("圆的面积为"+myArea);

}

}

class Rectangle extends Shape{

private double length,width;

public Rectangle(double length,double width) {

this.length=length;

this.width=width;

}

public void getArea() {

double myArea;

myArea=length*width;

System.out.println("矩形的面积为"+myArea);

}

}

public class Chap405 {

public static void main(String[] args) {

Circle circle=new Circle(5);

circle.getArea();

Rectangle rectangle=new Rectangle(3,5);// TODO

Auto-generated method stub

rectangle.getArea();

}

}

6. 定义一个接口,接口中有3个抽象方法如下。

(1)“long fact(int m);”方法的功能为求参数的阶乘。

(2)“long intPower(int m,int n);”方法的功能为求参数m的n次方。(3)“Boolean findFactor(int m,int n);”方法的功能为判断参数m加上参数n的和是否大于100。

定义类实现该接口,编写应用程序,调用接口中的3个方法,并将调用方法所得的结果输出。

【参考答案】

import java.util.Scanner;

interface Function{

long fact(int m);

long intPower(int m,int n);

Boolean findFactor(int m,int n);

}

class MyFunction implements Function{

public long fact(int m) {

int i=1,factorial=1;//阶乘

while(i<=m)

{factorial*=i;

i++;

}

return factorial;

}

public long intPower(int m,int n) {

int i=1,ipower=1;

for(i=1;i<=n;i++) {

ipower*=m;

}

return ipower;

}

public Boolean findFactor(int m,int n) {

if((m+n)>100)

return true;

else

return false;

}

}

public class Chap406 {

public static void main(String[] args) {

MyFunction myfunction=new MyFunction();

int m,n;

Scanner scanner=new Scanner(System.in);

System.out.println("请输入参数m的值");

m=scanner.nextInt();

System.out.println("请输入参数n的值");

n=scanner.nextInt();

System.out.println(m+"的阶乘="+myfunction.fact(m));// TODO Auto-generated method stub

System.out.println(m+"的"+n+"次方

="+myfunction.intPower(m, n));

if(myfunction.findFactor(m,n))

System.out.println(m+"+"+n+"的和大于100");

else

System.out.println(m+"+"+n+"的和不大于100");

scanner.close();

}

}

7.定义一个人类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉,工作。

(1)根据人类,派生一个学生类,增加属性:学校、学号;重写工作方法(学生的工作是学习)。

(2)根据人类,派生一个工人类,增加属性:单位、工龄;重写工作方法(工人的工作是建设美丽家园)。

(3)根据学生类,派生一个学生干部类,增加属性:职务;增加方法:开会。(4)编写主函数分别对上述3类具体人物进行测试。

【参考答案】

class Person{

private String name;

private String sex;

private int age;

private String country;

void eat()

{

System.out.println("吃饭");

}

void sleep()

{

System.out.println("睡觉");

}

void work()

{

System.out.println("工作");

}

void setName(String name) {

https://www.wendangku.net/doc/6d11935663.html,=name;

}

String getName() {

return name;

}

void setSex(String sex)

{

this.sex=sex;

}

String getSex(){

return sex;

}

void setAge(int age) {

this.age=age;

}

int getAge() {

return age;

}

void setCountry(String country) {

this.country=country;

}

String getCountry(){

return country;

}

}

class Student extends Person{

private String school;

private String num;

void work() {

System.out.println("学生的工作是学习");

}

void setSchool(String school) {

this.school=school;

}

String getSchool() {

return school;

}

void setNum(String num) {

this.num=num;

}

String getNum(){

return num;

}

}

class Worker extends Person{

private String unit;

private int workingAge;

void work() {

System.out.println("工人的工作是建设美丽家园");

}

void setUnit(String unit) {

this.unit=unit;

}

String getUnit() {

return unit;

}

void setWorkingAge(int workingAge) {

this.workingAge=workingAge;

}

int getWorkingAge(){

return workingAge;

}

}

class Cadre extends Student{

private String position;

void setPosition(String position) {

this.position=position;

}

String getPosition()

{

return position;

}

void meeting()

{

System.out.println("学生干部正在开会");

}

}

public class Chap407 {

public static void main(String[] args) {

Student stu1=new Student();

stu1.setName("张三");

stu1.setAge(20);

stu1.setSex("女");

stu1.setCountry("中国");

stu1.setSchool("青岛农业大学海都学院");

stu1.setNum("2019001");

System.out.println("姓名:"+stu1.getName());

System.out.println("性别:"+stu1.getSex());

stu1.work();

stu1.sleep();

// 工人类、学生干部类测试略

}

}

8.编写程序,要求输入若干整数,输入的同时计算前面输入各数的乘积,若乘积超过100000,则认为是异常,捕获并处理这个异常,输出信息。

【参考答案】

import java.util.Scanner;

import https://www.wendangku.net/doc/6d11935663.html,ng.Exception;

public class Chap408 {

public static void main(String[] args) {

long product=1;

int i;

Scanner scanner=new Scanner(System.in);

while(product<=100000) {

System.out.println("请输入要继续相乘的数");

i=scanner.nextInt();

product*=i;

System.out.println("目前的乘积为"+product);

if(product>100000) {

scanner.close();

try {

throw new Exception("乘积已超上限");

}catch(Exception e){

System.out.println(e.getMessage());

}

}

}

}

}

8.

也可以按照第10题答案样式,先自定义异常,然后处理。

9.编写一个登录界面,要求用户名只能由1至10位数字组成,密码只能有6

位,任何不符合用户名和密码要求的情况都视为异常,必须捕获并处理异常。(提示: String regx1="\\d{1,10}"; //\\d表示数字,{1,10}表示1-10位数

if(!name.matches(regx1))//表示判定姓名的输入值跟regx规定的是否一致)

【参考答案】

import java.util.Scanner;

public class Chap409 {

public static void main(String[] args) throws RuntimeException {

String name;

String password;

Scanner scanner=new Scanner(System.in);

System.out.println("请输入用户名(1-10位数字组成)");

name=scanner.next();

System.out.println("请输入密码(6位数字组成)");

password=scanner.next();

String regx1="\\d{1,10}";

String regx2="\\d{6}";

scanner.close();

if(!name.matches(regx1))

try {

throw new RuntimeException("用户名只能是1-10位数字组成");

}catch(RuntimeException e) {

System.out.println(e.getMessage());

}

else

System.out.println("用户名正确");

if(!password.matches(regx2))

try{

throw new RuntimeException("密码只能是6位");

}catch(RuntimeException e) {

System.out.println(e.getMessage());

}

else

{

System.out.println("密码正确");

System.out.println("恭喜"+name+"登录成功");

}

}

}

10.设计自己的异常类表示对负数求平方根的错误;在类Test的主方法中,从键盘输入一个数,若输入的数不小于O,则输出它的平方根;若小于O,则抛出自

定义异常;在程序中处理异常并输出提示信息、“输入错误,不能对负数求平方根”。

【参考答案】

import java.util.Scanner;

import java.math.*;

class SqrtByException extends Exception{

public SqrtByException(String message) {

super(message);

System.out.println(message);

}

}

public class Chap410 {

public static double TestException() throws SqrtByException{ double s;

Scanner scanner=new Scanner(System.in);

s=scanner.nextDouble();

scanner.close();

if(s<0)// TODO Auto-generated method stub

throw new SqrtByException("负数不能求平方根");

else

return Math.sqrt(s);

}

public static void main(String[] args){

try {

System.out.println(TestException());

}catch(SqrtByException e) {

}

}

}

相关文档