《Java语言程序设计与数据结构》编程练习答案(第九章)

《Java语言程序设计与数据结构》编程练习答案(第九章)

英文名:Introduction to Java Programming and Data Structure, Comprehensive Version, 11th Edition

9.1

public class book {
    public static void main(String[] args)
    {
        Rectangle r1 = new Rectangle(4,40);
        Rectangle r2 = new Rectangle(3.5,35.9);
        System.out.println("R1 "+r1.getWidth()+" "+r1.getHeight()+" "+r1.getArea()+" "+r1.getPerimeter());
        System.out.println("R2 "+r2.getWidth()+" "+r2.getHeight()+" "+r2.getArea()+" "+r2.getPerimeter());
    }
}

class Rectangle{
    private double width;
    private double height;
    public Rectangle(double w, double h)
    {
        this.width=w;
        this.height=h;
    }
    public Rectangle()
    {
        this(1,1);
    }
    public double getArea()
    {
        return width*height;
    }
    public double getPerimeter()
    {
        return 2*(width+height);
    }
    public double getWidth()
    {
        return width;
    }
    public double getHeight()
    {
        return height;
    }
}

9.2

public class book {
    public static void main(String[] args)
    {
        Stock s = new Stock("ORCL","Oracle Corporation");
        s.setPreviousClosingPrice(34.5);
        s.setCurrentPrice(34.35);
        System.out.println("The change percent is "+s.getChangePercent()+"%");
    }
}

class Stock
{
    private String symbol;
    private String name;
    private double previousClosingPrice;
    private double currentPrice;
    Stock(String symbol,String name)
    {
        this.symbol=symbol;
        this.name=name;
    }

    public void setPreviousClosingPrice(double previousClosingPrice) {
        this.previousClosingPrice = previousClosingPrice;
    }
    public void setCurrentPrice(double p)
    {
        this.currentPrice=p;
    }
    public double getChangePercent()
    {
        return (currentPrice-previousClosingPrice)/previousClosingPrice*100;
    }
}

9.3

import java.util.Date;
public class book {
    public static void main(String[] args)
    {
        long second = 10000;
        for(int i=0;i<8;i++)
        {
            Date curr = new Date(second);
            System.out.println(curr.toString());
            second*=10;
        }
    }
}

9.4

import java.util.Random;
public class book {
    public static void main(String[] args)
    {
        Random r = new Random(1000);
        for(int i=0;i<50;i++)
            System.out.print(r.nextInt(100)+" ");
    }
}

9.5

import java.util.GregorianCalendar;
public class book {
    public static void main(String[] args)
    {
        GregorianCalendar g = new GregorianCalendar();
        System.out.println(g.get(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH));
        g.setTimeInMillis(123456789876L);
        System.out.println(g.get(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH));
    }
}

9.6

public class book {
    public static void main(String[] args)
    {
        StopWatch timer = new StopWatch();
        //selection sort
        timer.stop();
        System.out.println(timer.getElapsedTime());
    }
}

class StopWatch
{
    private long startTime;
    private long endTime;
    StopWatch()
    {
        this.startTime=System.currentTimeMillis();
    }
    public void start()
    {
        this.startTime=System.currentTimeMillis();
    }
    public void stop()
    {
        this.endTime=System.currentTimeMillis();
    }
    public long getElapsedTime()
    {
        return this.endTime-this.startTime;
    }
}

9.7

import java.util.Date;
public class book {
    public static void main(String[] args)
    {
        Account swy = new Account(1122,2000);
        swy.setAnnualInterestRate(4.5/100);
        swy.withDraw(2500);
        swy.deposit(3000);
        System.out.println("Balance: "+swy.getBalance());
        System.out.println("Monthly Interest: "+swy.getMonthlyInterest());
        System.out.println("Register Date: "+swy.getDateCreated().toString());
    }
}

class Account
{
    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;
    public Account()
    {
        id=0;
        balance=0;
        annualInterestRate=0;
        dateCreated=new Date();
    }
    public Account(int di,double b)
    {
        id=di;
        balance=b;
        annualInterestRate=0;
        dateCreated=new Date();
    }
    public int getId(){
        return id;
    }
    public void setId(int j){
        id=j;
    }
    public double getBalance(){
        return balance;
    }
    public void setBalance(double j){
        balance=j;
    }
    public double getAnnualInterestRate(){
        return annualInterestRate;
    }
    public void setAnnualInterestRate(double j){
        annualInterestRate=j;
    }
    public Date getDateCreated(){
        return dateCreated;
    }
    public double getMonthlyInterestRate()
    {
        return annualInterestRate/12;
    }
    public double getMonthlyInterest(){
        return annualInterestRate/12*balance;
    }
    public void withDraw(double m){
        balance-=m;
    }
    public void deposit(double m){
        balance+=m;
    }
}

9.8

public class book {
    public static void main(String[] args)
    {
        Fan fan1 = new Fan();
        fan1.setSpeed(Fan.FAST);
        fan1.setRadius(10);
        fan1.setColor("yellow");
        fan1.setOn(true);
        Fan fan2 = new Fan();
        fan2.setSpeed(Fan.MEDIUM);
        System.out.println(fan1.toString());
        System.out.println(fan2.toString());
    }
}

class Fan
{
    final static int SLOW=1;
    final static int MEDIUM=2;
    final static int FAST=3;
    private int speed = SLOW;
    private boolean on = false;
    private double radius = 5;
    private String color="blue";
    public int getSpeed(){
        return speed;
    }
    public void setSpeed(int s){
        speed=s;
    }
    public boolean getOn(){
        return on;
    }
    public void setOn(boolean j){
        on=j;
    }
    public double getRadius(){
        return radius;
    }
    public void setRadius(double r){
        radius=r;
    }
    public String getColor(){
        return color;
    }
    public void setColor(String s){
        color=s;
    }
    public Fan(){
        speed=SLOW;
        on=false;
        radius=5;
        color="blue";
    }
    public String toString(){
        String re="";
        if(this.on)
            re=re+speed+" "+color+" "+radius;
        else
            re=re+"fan is off "+color+" "+radius;
        return re;
    }
}

9.9

public class book {
    public static void main(String[] args)
    {
        RegularPolygon p = new RegularPolygon();
        RegularPolygon pp = new RegularPolygon(6,4);
        RegularPolygon ppp = new RegularPolygon(10,4,5.6,7.8);
        System.out.println(p.getPerimeter()+" "+p.getArea());
        System.out.println(pp.getPerimeter()+" "+pp.getArea());
        System.out.println(ppp.getPerimeter()+" "+ppp.getArea());
    }
}

class RegularPolygon
{
    private int n=3;
    private double side=1;
    private double x=0;
    private double y=0;
    public RegularPolygon(){
        n=3;
        side=1;
    }
    public RegularPolygon(int num,double len){
        n=num;
        side=len;
    }
    public RegularPolygon(int hum,double len,double xx,double yy){
        n=hum;
        side=len;
        x=xx;
        y=yy;
    }
    public void setN(int nn){
        n=nn;
    }
    public int getN(){
        return n;
    }
    public void setSide(double ss){
        side=ss;
    }
    public double getSide(){
        return side;
    }
    public void setX(double xx){
        x=xx;
    }
    public double getX(){
        return x;
    }
    public void setY(double yy){
        y=yy;
    }
    public double getY(){
        return y;
    }
    public double getPerimeter(){
        return n*side;
    }
    public double getArea(){
        return n*side*side/(4*Math.tan(Math.PI/n));
    }
}

9.10

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a b c: ");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        QuadraticEquation ass = new QuadraticEquation(a,b,c);
        if(ass.getDiscriminant()<0)
            System.out.println("The equation has no roots.");
        else if(ass.getDiscriminant()==0)
            System.out.println("One root "+ass.getRoot1());
        else
            System.out.println("Two roots "+ass.getRoot1()+" "+ass.getRoot2());
    }
}

class QuadraticEquation
{
    private double a;
    private double b;
    private double c;
    public QuadraticEquation(double aa,double bb,double cc){
        a=aa;
        b=bb;
        c=cc;
    }
    public double getA(){
        return a;
    }
    public double getB(){
        return b;
    }
    public double getC(){
        return c;
    }
    public double getDiscriminant(){
        return b*b-4*a*c;
    }
    public double getRoot1(){
        return (-1*b+Math.sqrt(b*b-4*a*c))/(2*a);
    }
    public double getRoot2(){
        return (-1*b-Math.sqrt(b*b-4*a*c))/(2*a);
    }
}

9.11

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter a b c d e f: ");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double d = input.nextDouble();
        double e = input.nextDouble();
        double f = input.nextDouble();
        LinearEquation ass = new LinearEquation(a,b,c,d,e,f);
        if(ass.isSolvable())
            System.out.println(ass.getX()+" "+ass.getY());
        else
            System.out.println("The equation has no solutions.");
    }
}

class LinearEquation
{
    private double a,b,c,d,e,f;
    public LinearEquation(double aa,double bb,double cc,double dd,double ee,double ff){
        a=aa;
        b=bb;
        c=cc;
        d=dd;
        e=ee;
        f=ff;
    }
    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(){
        return (e*d-b*f)/(a*d-b*c);
    }
    public double getY(){
        return (a*f-e*c)/(a*d-b*c);
    }
}

9.12

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the four points x1 y1 x2 y2 x3 y3 x4 y4: ");
        double x1=input.nextDouble();
        double y1=input.nextDouble();
        double x2=input.nextDouble();
        double y2=input.nextDouble();
        double x3=input.nextDouble();
        double y3=input.nextDouble();
        double x4=input.nextDouble();
        double y4=input.nextDouble();
        LinearEquation ass = new LinearEquation(y1-y2,x2-x1,y3-y4,x4-x3,(y1-y2)*x1-(x1-x2)*y1,(y3-y4)*x3-(x3-x4)*y3);
        if(ass.isSolvable())
            System.out.println(ass.getX()+" "+ass.getY());
        else
            System.out.println("No joining.");
    }
}

class LinearEquation
{
    private double a,b,c,d,e,f;
    public LinearEquation(double aa,double bb,double cc,double dd,double ee,double ff){
        a=aa;
        b=bb;
        c=cc;
        d=dd;
        e=ee;
        f=ff;
    }
    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(){
        return (e*d-b*f)/(a*d-b*c);
    }
    public double getY(){
        return (a*f-e*c)/(a*d-b*c);
    }
}

9.13

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of rows and columns in the array: ");
        int rows = input.nextInt();
        int columns = input.nextInt();
        double[][] dick = new double[rows][columns];
        System.out.print("Enter the array: ");
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<columns;j++)
                dick[i][j]=input.nextDouble();
        }
        Location holy = locateLargest(dick);
        System.out.println("The location of the largest element is "+holy.maxValue+" at ("+holy.row+", "+holy.column+")");
    }
    public static Location locateLargest(double[][] a){
        Location ass = new Location();
        ass.maxValue=a[0][0];
        ass.column=0;
        ass.row=0;
        int rowCount = a.length;
        int columnCount = a[0].length;
        for(int i=0;i<rowCount;i++)
        {
            for(int j=0;j<columnCount;j++)
            {
                if(a[i][j]>ass.maxValue)
                {
                    ass.row=i;
                    ass.maxValue=a[i][j];
                    ass.column=j;
                }
            }
        }
        return ass;
    }
}

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

第九章 完

发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104576671