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

这是《Java语言程序设计与数据结构》第十一版第九章的编程练习答案,涵盖9.1至9.13各节。通过这些练习,读者可以深入理解和应用Java编程及数据结构的知识。
摘要由CSDN通过智能技术生成

《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)+"/"
  • 25
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值