JAVA语言程序设计基础篇(Chapter 9)课后习题参考答案

1. (简答题)

Each TV is an object with states (current channel, current volume level, power on or off) and behaviors (change channels, adjust volume, turn on/off). You can use a class to model TV sets. The UML diagram for the class is shown in the figure.

You need to implement the TV class. Write a test program that creates two TV objects and invokes the methods on the objects to perform actions for setting channels and volume levels and for increasing channels and volumes. Display the channel and volume of each TV object.

 

class Tv {
     int channel = 1;
     int volumeLevel = 0;
     boolean on = false;

    public Tv() {
    }

    public int getChannel() {
        return channel;
    }

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

    public int getVolume() {
        return volumeLevel;
    }

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

    public boolean isOn() {
        return on;
    }

    public void setOn(boolean on) {
        this.on = on;
    }

    public void turnOn() {
        this.on = true;
    }

    public void turnOff() {
        this.on = false;
    }

    public void channelUp() {
        this.channel++;
    }

    public void channelDown() {
        this.channel--;
    }

    public void volumeUp() {
        this.volumeLevel++;
    }

    public void volumeDown() {
        this.volumeLevel--;
    }

}

public class Demo1 {
    public static void main(String[] args) {
        Tv tv1 = new Tv();
        Tv tv2 = new Tv();
        tv1.turnOn();
        tv2.turnOn();
        tv1.setChannel(18);
        tv1.setVolume(10);
        tv2.setChannel(6);
        tv2.setVolume(10);

        System.out.println("-------------------------");

        System.out.println("TV1's Channel is :" + tv1.getChannel());
        System.out.println("Tv1's Volume is : " + tv1.getVolume());
        System.out.println("TV2's Channel is :" + tv2.getChannel());
        System.out.println("Tv2's Volume is : " + tv2.getVolume());
        tv1.channelUp();
        tv1.volumeUp();
        tv2.channelDown();
        tv2.volumeDown();

        System.out.println("TV1's Channel is :" + tv1.getChannel());
        System.out.println("Tv1's Volume is : " + tv1.getVolume());
        System.out.println("TV2's Channel is :" + tv2.getChannel());
        System.out.println("Tv2's Volume is : " + tv2.getVolume());
    }

}

2. (简答题)

(Use the  regorianCalendar class) Java API has the GregorianCalendar class in the java.util package, which you can use to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the methods get(GregorianCalendar.YEAR), get(GregorianCalendar.MONTH), and get(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day.

Write a program to perform two tasks:

■ Display the current year, month, and day.

■ The GregorianCalendar class has the setTimeInMillis(long), which can be used to set a specified elapsed time since January 1, 1970. Set the value to 1234567898765L and display the year, month, and day.

import java.util.GregorianCalendar;

public class Demo2 {
    public static void main(String[] args) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        System.out.println(gregorianCalendar.get(GregorianCalendar.YEAR) + " year " + (gregorianCalendar.get(GregorianCalendar.MONTH) + 1) + " month " + gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH) + " day");
        gregorianCalendar.setTimeInMillis(1234567898765L);
        System.out.println(gregorianCalendar.get(GregorianCalendar.YEAR) + " year " + (gregorianCalendar.get(GregorianCalendar.MONTH) + 1) + " month " + gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH) + " day");
    }
}

 

3. (简答题)

Linear equations: 

Please submit the source code in the text form, attach one picture to show the output of your program,and attach another picture to show the UML diagram.

import java.util.Scanner;

class LinearEquation {
    private double a, b, c, d, e, f;

    public LinearEquation(double a, double b, double c, double d, double e, double f) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.f = f;
    }

    public double getA() {
        return a;
    }

    public double getB() {
        return b;
    }

    public double getC() {
        return c;
    }

    public double getD() {
        return d;
    }

    public double getE() {
        return e;
    }

    public double getF() {
        return f;
    }

    public boolean isSolvable() {
        return (a * d - b * c) == 0;
    }

    public double getX() {

        double result = (e * d - b * f) / (a * d - b * c);
        return result;
    }

    public double getY() {

        double result = (a * f - e * c) / (a * d - b * c);
        return result;
    }

    public void getResult() {
        if (isSolvable()) {
            System.out.println("The equation has no solution !");
            return;
        }
        System.out.println("x is " + getX() + " and y is " + getY());
    }
}

public class Demo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a , b , c , d , e , f :");
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        double c = sc.nextDouble();
        double d = sc.nextDouble();
        double e = sc.nextDouble();
        double f = sc.nextDouble();
        LinearEquation le = new LinearEquation(a, b, c, d, e, f);
        le.getResult();
    }
}

 

4. (简答题)

The Location class:

import java.util.Scanner;

class Location {
    public int row;
    public int column;
    public double maxValue;

    public Location(int row, int column, double maxValue) {
        this.row = row;
        this.column = column;
        this.maxValue = maxValue;
    }

    public static Location locateLargest(double[][] a) {
        double maxV = a[0][0];
        int maxRow = 0;
        int maxCol = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if (a[i][j] > maxV) {
                    maxV = a[i][j];
                    maxRow = i;
                    maxCol = j;
                }
            }
        }

        return new Location(maxRow, maxCol, maxV);
    }

    public void maxValue() {
        System.out.println("The location of the largest element is " + maxValue + " at (" + row + "," + column + ")");
    }
}

public class Demo04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows and columns in the array :");
        int row = sc.nextInt();
        int column = sc.nextInt();
        double[][] array = new double[row][column];
        System.out.println("Enter the array: ");
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = sc.nextDouble();
            }
        }
        Location location = Location.locateLargest(array);
        location.maxValue();
    }
}

 

5. (简答题)

Quadratic Equations:

 

import java.util.Scanner;

class QuadraticEquation {
    private double a, b, c;

    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 discriminant = b * b - 4 * a * c;
        return discriminant;
    }

    public double getRoot1() {
        double r1 = (-b + Math.sqrt(getDiscriminant())) / 2 * a;
        return r1;
    }

    public double getRoot2() {
        double r2 = (-b - Math.sqrt(getDiscriminant())) / 2 * a;
        return r2;
    }

    public void getResult() {
        if (getDiscriminant() > 0) {
            System.out.println("The root1 is " + getRoot1() + " and the root2 is " + getRoot2());
        } else if (getDiscriminant() == 0) {
            System.out.println("The root is " + getRoot1());
        } else {
            System.out.println("The equation has no roots!");
        }
    }
}

public class Demo5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the a , b , c: ");
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        double c = sc.nextDouble();
        QuadraticEquation qe = new QuadraticEquation(a, b, c);
        qe.getResult();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值