ImageVerifierCode 换一换
格式:DOCX , 页数:51 ,大小:23.19KB ,
资源ID:17859987      下载积分:5 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-17859987.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java语言程序设计基础篇第十版第十三章练习答案讲课讲稿.docx)为本站会员(b****0)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

java语言程序设计基础篇第十版第十三章练习答案讲课讲稿.docx

1、java语言程序设计基础篇第十版第十三章练习答案讲课讲稿java语言程序设计基础篇第十版第十三章练习答案01public class Exercise13_01 public static void main(String args) TriangleNew triangle = new TriangleNew(1, 1.5, 1); triangle.setColor(yellow); triangle.setFilled(true); System.out.println(triangle); System.out.println(The area is + triangle.getAre

2、a(); System.out.println(The perimeter is + triangle.getPerimeter(); System.out.println(triangle); class TriangleNew extends GeometricObject private double side1 = 1.0, side2 = 1.0, side3 = 1.0; /* Constructor */ public TriangleNew() /* Constructor */ public TriangleNew(double side1, double side2, do

3、uble side3) this.side1 = side1; this.side2 = side2; this.side3 = side3; /* Implement the abstract method findArea in GeometricObject */ public double getArea() double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3); /* Implement the abstract method findC

4、ircumference in * GeometricObject */ public double getPerimeter() return side1 + side2 + side3; Override public String toString() / Implement it to return the three sides return TriangleNew: side1 = + side1 + side2 = + side2 + side3 = + side3; 02import java.util.ArrayList;public class Exercise13_02

5、public static void main(String args) ArrayList list = new ArrayList(); list.add(14); list.add(24); list.add(4); list.add(42); list.add(5); shuffle(list); for (int i = 0; i list.size(); i+) System.out.print(list.get(i) + ); public static void shuffle(ArrayList list) for (int i = 0; i list.size() - 1;

6、 i+) int index = (int)(Math.random() * list.size(); Number temp = list.get(i); list.set(i, list.get(index); list.set(index, temp); 03import java.util.ArrayList;public class Exercise13_03 public static void main(String args) ArrayList list = new ArrayList(); list.add(14); list.add(24); list.add(4); l

7、ist.add(42); list.add(5); sort(list); for (int i = 0; i list.size(); i+) System.out.print(list.get(i) + ); public static void sort(ArrayList list) for (int i = 0; i list.size() - 1; i+) / Find the minimum in the listi.list.length-1 Number currentMin = list.get(i); int currentMinIndex = i; for (int j

8、 = i + 1; j list.get(j).doubleValue() currentMin = list.get(j); currentMinIndex = j; / Swap list.get(i) with list.get(currentMinIndex) if necessary; if (currentMinIndex != i) list.set(currentMinIndex, list.get(i); list.set(i, currentMin); 04import java.util.*;public class Exercise13_04 static MyCale

9、ndar calendar = new MyCalendar(); public static void main(String args) int month = calendar.get(MyCalendar.MONTH) + 1; int year = calendar.get(MyCalendar.YEAR); if (args.length 2) System.out.println(Usage java Exercise13_04 month year); else if (args.length = 2) /use user-defined month and year year

10、 = Integer.parseInt(args1); month = Integer.parseInt(args0); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); else if (args.length = 1) /use user-defined month for the current year month = Integer.parseInt(args0); calendar.set(Calendar.MONTH, month-1); /set date to the fir

11、st day in a month calendar.set(Calendar.DATE, 1); /print calendar for the month printMonth(year, month); static void printMonth(int year, int month) /get start day of the week for the first date in the month int startDay = getStartDay(); /get number of days in the month int numOfDaysInMonth = calend

12、ar.daysInMonth(); /print headings printMonthTitle(year, month); /print body printMonthBody(startDay, numOfDaysInMonth); static int getStartDay() return calendar.get(Calendar.DAY_OF_WEEK); static void printMonthBody(int startDay, int numOfDaysInMonth) /print padding space before the first day of the

13、month int i = 0; for (i = 0; i startDay-1; i+) System.out.print( ); for (i = 1; i = numOfDaysInMonth; i+) if (i 10) System.out.print( +i); else System.out.print( +i); if (i + startDay - 1) % 7 = 0) System.out.println(); System.out.println(); static void printMonthTitle(int year, int month) System.ou

14、t.println( +calendar.getMonthName()+, +year); System.out.println(-); System.out.println( Sun Mon Tue Wed Thu Fri Sat); 05public class Exercise13_05 / Main method public static void main(String args) / Create two comparable circles Circle1 circle1 = new Circle1(5); Circle1 circle2 = new Circle1(4); /

15、 Display the max circle Circle1 circle = (Circle1) GeometricObject1.max(circle1, circle2); System.out.println(The max circles radius is + circle.getRadius(); System.out.println(circle); abstract class GeometricObject1 implements Comparable protected String color; protected double weight; / Default c

16、onstruct protected GeometricObject1() color = white; weight = 1.0; / Construct a geometric object protected GeometricObject1(String color, double weight) this.color = color; this.weight = weight; / Getter method for color public String getColor() return color; / Setter method for color public void s

17、etColor(String color) this.color = color; / Getter method for weight public double getWeight() return weight; / Setter method for weight public void setWeight(double weight) this.weight = weight; / Abstract method public abstract double getArea(); / Abstract method public abstract double getPerimete

18、r(); public int compareTo(GeometricObject1 o) if (getArea() 0) return o1; else return o2; / Circle.java: The circle class that extends GeometricObjectclass Circle1 extends GeometricObject1 protected double radius; / Default constructor public Circle1() this(1.0, white, 1.0); / Construct circle with

19、specified radius public Circle1(double radius) super(white, 1.0); this.radius = radius; / Construct a circle with specified radius, weight, and color public Circle1(double radius, String color, double weight) super(color, weight); this.radius = radius; / Getter method for radius public double getRad

20、ius() return radius; / Setter method for radius public void setRadius(double radius) this.radius = radius; / Implement the findArea method defined in GeometricObject public double getArea() return radius * radius * Math.PI; / Implement the findPerimeter method defined in GeometricObject public doubl

21、e getPerimeter() return 2 * radius * Math.PI; / Override the equals() method defined in the Object class public boolean equals(Circle1 circle) return this.radius = circle.getRadius(); Override public String toString() return Circle radius = + radius; Override public int compareTo(GeometricObject1 o)

22、 if (getRadius() (Circle1) o).getRadius() return 1; else if (getRadius() (Circle1) o).getRadius() return -1; else return 0; 06public class Exercise13_06 / Main method public static void main(String args) / Create two comarable rectangles ComparableCircle circle1 = new ComparableCircle(5); Comparable

23、Circle circle2 = new ComparableCircle(15); / Display the max rect ComparableCircle circle3 = (ComparableCircle)Max.max(circle1, circle2); System.out.println(The max circles radius is + circle3.getRadius(); System.out.println(circle3); class ComparableCircle extends Circle implements Comparable /* Co

24、nstruct a ComparableRectangle with specified properties */ public ComparableCircle(double radius) super(radius); Override public int compareTo(ComparableCircle o) if (getRadius() o.getRadius() return 1; else if (getRadius() 0) return o1; else return o2; 07public class Exercise13_07 public static voi

25、d main(String args) GeometricObject objects = new Square(2), new Circle(5), new Square(5), new Rectangle(3, 4), new Square(4.5); for (int i = 0; i objects.length; i+) System.out.println(Area is + objectsi.getArea(); if (objectsi instanceof Colorable) (Colorable)objectsi).howToColor(); interface Colo

26、rable void howToColor();class Square extends GeometricObject implements Colorable private double side; public Square(double side) this.side = side; Override public void howToColor() System.out.println(Color all four sides); Override public double getArea() return side * side; Override public double

27、getPerimeter() return 4 * side; 08import java.util.ArrayList;public class Exercise13_08 public static void main(String args) MyStack1 stack = new MyStack1(); stack.push(S1); stack.push(S2); stack.push(S); MyStack1 stack2 = (MyStack1) (stack.clone(); stack2.push(S1); stack2.push(S2); stack2.push(S); System.out.println(stack.getSize(); System.out.println(stack2.getSize(); class MyStack1 implements Cloneable private ArrayListO

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2