Java语言程序设计(第3版)沈泽刚主编第4章课后习题答案

Java语言程序设计(第3版)沈泽刚主编第4章课后习题答案

第四章 类和对象
4.1 定义一个名为Person的类,编写程序,实现定义的Person类,实现数据的访问与修改。

public class Person {
    String name ;
    int age ;
    public void setName(String name){
        this.name = name ;
    }
    public String getName(){
        return name ;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public void speak(){
        System.out.println("姓名:" + name) ;
        System.out.println("年龄:" + age) ;
    }
    public static void main(String[] args){
        Person person = new Person() ;
        person.setName("王国栋") ;
        person.setAge(24) ;
        person.speak() ;
    }
}

4.2 定义一个名为Circle的类,编写程序测试圆类的所有方法。

public class Circle {
    double centerX ;
    double centerY ;
    double radius ;
    public Circle(double radius){
        this.radius = radius ;
    }
    public void setRadius(double radius){
        this.radius = radius ;
    }
    public double getRadius(){
        return radius ;
    }
    public double getArea(){
        return Math.PI * radius * radius ;
    }
    public double getPerimeter(){
        return 2 * Math.PI * radius ;
    }

    public static void main(String[] args){
        Circle circle = new Circle(1.0) ;
        System.out.println("圆形的面积为:" + circle.getArea()) ;
        System.out.println("圆形的周长为:" + circle.getPerimeter()) ;
        circle.setRadius(2.0) ;
        System.out.println("圆形的半径为:" + circle.getRadius()) ;
        System.out.println("圆形的周长为:" + circle.getPerimeter()) ;
        System.out.println("圆形的面积为:" + circle.getArea()) ;
    }
}

4.3 定义一个名为Rectangle的类表示矩形,编写程序测试这个类的所有方法

public class Rectangle {
    double length, width ;
    public Rectangle(double length, double width){
        this.length = length ;
        this.width = width ;
    }
    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
    public double getArea(){
        return length * width ;
    }
    public double getPerimeter(){
        return (length + width) * 2 ;
    }

    public static void main(String[] args){
        Rectangle rectangle = new Rectangle(1.0, 1.0) ;
        System.out.println("矩形的面积为:" + rectangle.getArea()) ;
        System.out.println("矩形的周长为:" + rectangle.getPerimeter()) ;
        rectangle.setLength(2.0) ;
        rectangle.setWidth(2.0) ;
        System.out.println("矩形的长为:" + rectangle.getLength() + "矩形的宽为:" + rectangle.getWidth()) ;
        System.out.println("矩形的面积为:" + rectangle.getArea())  ;
        System.out.println("矩形的周长为:" + rectangle.getPerimeter()) ;
    }
}

4.4 定义一个名为Triangle的三角形类,编写程序测试三角形类的所有方法。

public class Triangle {
    double a, b, c ;
    public Triangle(){
        this.a = 0 ;
        this.b = 0 ;
        this.c = 0 ;
    }
    public Triangle(double a, double b, double c){
        this.a = a ;
        this.b = b ;
        this.c = c ;
    }
    public double getArea(){
        double s = (a + b + c) / 2 ;
        double area = Math.sqrt(s*(s-a)*(s-b)*(s-c)) ;
        return area ;
    }

    public static void main(String[] args){
        Triangle triangle = new Triangle(3, 4, 5) ;
        System.out.println("三角形的面积为:" + triangle.getArea()) ;
    }
}

4.6编写程序,打印输出斐波那契数列的前20项。

public class Fibonacci {
    public static long fib(long n){
        if(n==1 || n==2){
            return 1 ;
        }
        return fib(n-1) + fib(n-2) ;
    }
    public static void main(String[] args){
        for(int i=1; i<=20; i++){
            System.out.println(fib(i)) ;
        }
    }
}

4.7 为一元二次方程设计一个名为QuadraticEquation的类,判断并打印方程组的根。

import java.util.Scanner;

/**
 * 4.7 为一元二次方程设计一个名为QuadraticEquation的类,判断并打印方程组的根。
 */
public class QuadraticEquation {
    private double a, b, c ;
    public QuadraticEquation(){}
    public QuadraticEquation(double a, double b, double c){
        this.a = a ;
        this.b = b ;
        this.c = c ;
    }

    public double getA() {
        return a;
    }

    public double getB() {
        return b;
    }

    public double getC() {
        return c;
    }

    public double getDiscriminant(){
        double a = getA() ;
        double b = getB() ;
        double c = getC() ;
       return b * b - 4 * a * c ;
    }
    public static void main(String[] args){
        System.out.println("请输入一元二次方程的三个系数a,b,c") ;
        Scanner input = new Scanner(System.in) ;
        Double a = input.nextDouble() ;
        Double b = input.nextDouble() ;
        Double c = input.nextDouble() ;
        QuadraticEquation qe = new QuadraticEquation(a,b,c) ;
        if(qe.getDiscriminant() > 0){
            System.out.println("一元二次方程有两个根") ;
            double x1 = ((-b) + Math.sqrt(Math.pow(b,2) - 4 * a * c)) / (2 * a) ;
            double x2 = ((-b) - Math.sqrt(b * b - 4 * a * c)) / (2 * a) ;
            System.out.println("第1个实数根为:" + x1) ;
            System.out.println("第2个实数根为:" + x2) ;
        }else if(qe.getDiscriminant() == 0){
            System.out.println("一元二次方程仅有一个实数根") ;
            double x = (-b) / (2 * a) ;
            System.out.println("方程的实数根为:" + x) ;
        }else{
            System.out.println("方程无实数根") ;
        }
    }
}

4.8 定义一个名为TV的类表示电视机,编写程序实现该类所有的方法。

public class TV {
    int channel ;
    int volumeLevel ;
    boolean on ;
    public TV(){}
    public void turnOn(){
        on = true ;
        System.out.println("打开电视机") ;
    }
    public void turnOff(){
        System.out.println("关闭电视机") ;
        on = false ;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }

    public void setVolumeLevel(int volumeLevel) {
        this.volumeLevel = volumeLevel;
    }

    public void channelUp(){
        if(on)
        if(channel >=1 && channel <= 19)
        channel ++ ;
        else{
            System.out.println("已加载所有频道") ;
        }
    }
    public void channelDown(){
        if(on)
        if(channel >=2 && channel <= 20){
            channel -- ;
        }else{
            System.out.println("已加载所有频道") ;
        }
    }
    public void volumeUp(){
        if(on)
        if(volumeLevel >= 1 && volumeLevel <= 19){
            volumeLevel ++ ;
        }else{
            System.out.println("当前为系统最大音量") ;
        }
    }
    public void volumeDown(){
        if(on)
        if(volumeLevel >=2 && volumeLevel <= 20){
            volumeLevel -- ;
        }else{
            System.out.println("当前是系统最小音量") ;
        }
    }
    public static void main(String[] args){
        TV tv = new TV() ;
        tv.turnOn();
        tv.setChannel(5);
        tv.setVolumeLevel(10);
        System.out.println("当前频道为:" + tv.channel) ;
        System.out.println("当前音量为:" + tv.volumeLevel) ;
        tv.channelUp() ;
        tv.volumeUp() ;
        System.out.println("当前频道为:" + tv.channel) ;
        System.out.println("当前音量为:" + tv.volumeLevel) ;
        tv.channelDown();
        tv.volumeDown();
        System.out.println("当前频道为:" + tv.channel) ;
        System.out.println("当前音量为:" + tv.volumeLevel) ;
        tv.turnOff();
    }
}

4.9 编写一个名MyInteger的类,编写应用测试程序测试该类的所有方法。

public class MyInteger {
    int value ;
    public  MyInteger(int value){
        this.value = value ;
    }
    public int getValue(){
        return value ;
    }
    public boolean isEven(){
        return value % 2 == 0 ? true : false ;
    }
    public boolean isOdd(){
        return value % 2 == 1 ? true : false ;
    }
    public boolean isPrime(){
        for(int i=2; i<value; i++){
            if(value % i == 0){
                return false ;
            }
        }
        return true ;
    }
    public boolean isEven(int value){
        return value % 2 == 0 ? true : false ;
    }
    public boolean isOdd(int value){
        return value % 2 == 1 ? true : false ;
    }
    public boolean isPrime(int value){
        for(int i=2; i<value; i++){
            if(value % i == 0){
                return false ;
            }
        }
        return true ;
    }
    public boolean isEven(MyInteger myInteger){
       int value =  myInteger.getValue() ;
       return value % 2 == 0 ? true : false ;
    }
    public boolean isOdd(MyInteger myInteger){
        int value = myInteger.getValue() ;
        return value % 2 == 1 ? true : false ;
    }
    public boolean isPrime(MyInteger myInteger){
        int value = myInteger.getValue() ;
        return isPrime(value) ;
    }
    public boolean equals(int value){
        return this.value == value ;
    }
    public boolean equals(MyInteger myInteger){
        int value = myInteger.getValue() ;
        return value == this.value ;
    }
    public int parseInt(char [] arr){
        int result = 0 ;
        int scale = 1 ;
        for(int i=arr.length-1; i>=0; i--){
            int d = arr[i] - '0' ;
            result += d * scale ;
            scale *= 10 ;
        }
        return result ;
    }
    public int parseInt(String s){
        return Integer.parseInt(s) ;
    }
    public static void main(String[] args){
        MyInteger myInteger = new MyInteger(7) ;
        System.out.println(myInteger.isPrime(myInteger)) ;
        System.out.println(myInteger.isEven(myInteger)) ;
        System.out.println(myInteger.isOdd(myInteger)) ;
        char [] c = {'1','2','3','4','5'} ;
        System.out.println(myInteger.parseInt(c)) ;
        System.out.println(myInteger.parseInt("12345")) ;
    }
}

4.10 输出前20个回文素数。

public class PrimePalindrome {
    public static boolean isPrime(int n){
        for(int i=2; i<n; i++){
            if(n % i == 0){
                return false ;
            }
        }
        return true ;
    }
    public static boolean isPalindrome(int n){
        String s = String.valueOf(n) ;
        int low = 0 , high = s.length() - 1 ;
        while(low < high){
            if(s.charAt(low) != s.charAt(high)){
                return false ;
            }
            low ++ ;
            high -- ;
        }
        return true ;
    }
    public static void  main(String[] args){
        int n = 2, count = 0, count1 = 0 ;
        while(count1< 20){
            if(isPrime(n) && isPalindrome(n)){
                count ++ ;
                count1 ++ ;
                System.out.print(n + " ") ;
                if(count == 10){
                    System.out.println() ;
                    count = 0 ;
                }
            }
            n ++ ;
        }
    }
}

4.11 定义一个名为Account的类实现账户管理。

import java.time.LocalDate;

//4.11 定义一个名为Account的类实现账户管理。
public class Account {
    int id ;
    double balance ;
    double annualRate ;
    LocalDate dateCreated ;
    public Account(){}
    public Account(int id, double balance){
        this.id = id ;
        this.balance = balance ;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualRate() {
        return annualRate;
    }

    public void setAnnualRate(double annualRate) {
        this.annualRate = annualRate;
    }

    public LocalDate getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(LocalDate dateCreated) {
        this.dateCreated = dateCreated;
    }

   public void withdraw(double amount){
        balance = balance - amount ;
   }
   public void deposit(double amount){
        balance = balance + amount ;
   }
    public static void main(String[] args){
        Account account = new Account(101, 1000.00) ;
        account.withdraw(100);
        System.out.println("账户余额:" + account.getBalance()) ;
        account.deposit(200);
        System.out.println("账户余额:" + account.getBalance()) ;
    }
}

  • 14
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值