博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java语言程序设计与数据结构》编程练习答案(第二十章)(二)
阅读量:4167 次
发布时间:2019-05-26

本文共 5672 字,大约阅读时间需要 18 分钟。

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

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

20.9

public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); Comparator
circleComparator = new Comparator
() {
@Override public int compare(Circle o1, Circle o2) {
return (int)(o2.getRadius()-o1.getRadius()); } }; PriorityQueue
test = new PriorityQueue<>(1, circleComparator); for(int i=0;i<10;i++){
test.add(new Circle(20*Math.random())); } for(int i=0;i<10;i++){
System.out.println(test.remove().getRadius()); } }}class Circle{
private double radius; private double square; public Circle(double r){
this.radius = r; this.square = Math.PI*r*r; } public double getRadius(){
return this.radius; } public double getSquare(){
return this.square; }}

20.10

public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); PriorityQueue
set1 = new PriorityQueue<>(); PriorityQueue
set2 = new PriorityQueue<>(); set1.offer("George"); set1.offer("Jim"); set1.offer("John"); set1.offer("Blake"); set1.offer("Kevin"); set1.offer("Michael"); set2.offer("George"); set2.offer("Katie"); set2.offer("Kevin"); set2.offer("Michelle"); set2.offer("Ryan"); PriorityQueue
union = new PriorityQueue<>(set1); for( String str: set2){
if(!union.contains(str)){
union.offer(str); } } PriorityQueue
intersect = new PriorityQueue<>(); for(String str : set1){
if(set2.contains(str)){
intersect.offer(str); } } PriorityQueue
diff = new PriorityQueue<>(); for(String str: set1){
if(!set2.contains(str)){
diff.offer(str); } } System.out.println(Arrays.toString(union.toArray())); System.out.println(Arrays.toString(intersect.toArray())); System.out.println(Arrays.toString(diff.toArray())); }}

20.11

public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.println("Please enter the code: "); String code = input.nextLine(); boolean isPair = true; Stack
test = new Stack<>(); for(int i=0;i

20.12

public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); MyPriorityQueue
test = new MyPriorityQueue<>(); test.offer("114"); test.offer("514"); MyPriorityQueue
test1 = (MyPriorityQueue
) test.clone(); test1.offer("1919810"); System.out.println(Arrays.toString(test.toArray())); System.out.println(Arrays.toString(test1.toArray())); }}class MyPriorityQueue
extends PriorityQueue
implements Cloneable{
@Override protected Object clone(){
MyPriorityQueue
myPriorityQueue = null; try{ myPriorityQueue = (MyPriorityQueue
) super.clone(); }catch (CloneNotSupportedException e){ e.printStackTrace(); } return myPriorityQueue; }}

20.13

🐎

20.14

public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); Stack
test = new Stack<>(); System.out.println("Please enter the RPN"); String rpn = input.nextLine(); rpn = rpn.replace(" ",""); for(int i=0;i
= '0' && tmp <='9'){
test.push((char)(tmp-'0')); } else{
int op1 = (int)test.pop(); int op2 = (int)test.pop(); int res; switch (tmp){
case '+': res = op1 + op2; test.push((char)res); break; case '-': res = op1 - op2; test.push((char)res); break; case '*': res = op1 * op2; test.push((char)res); break; case '/': res = op1 / op2; test.push((char)res); break; default: break; } } } System.out.println("The result is "+(int)(test.pop())); }}

20.15

🐔

20.16

参见

20.17

👨🐰🌶

20.18

20.21

public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); String[] list2 = {
"red","blue","green","yellow","orange","pink"}; Comparator
comparator = new Comparator
() {
@Override public int compare(String o1, String o2) {
return o1.charAt(o1.length()-1)-o2.charAt(o2.length()-1); } }; selectionSort(list2, comparator); System.out.println(Arrays.toString(list2)); } public static
void selectionSort(T[] list, Comparator< ? super T> comparator){
for(int i=0;i

第二十章 完

转载地址:http://hczai.baihongyu.com/

你可能感兴趣的文章
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
java多线程中的join方法详解
查看>>
idea添加gradle模块报错The project is already registered
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 拖放
查看>>
HTML5学习之——HTML 5 Canvas vs. SVG
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>
HTML5学习之——HTML 5 Web Workers
查看>>
HTML5学习之——HTML 5 Canvas
查看>>
HTML5学习之——HTML5 内联 SVG
查看>>
HTML5学习之——HTML 5 服务器发送事件
查看>>
SVG学习之——HTML 页面中的 SVG
查看>>
SVG 形状学习之——SVG圆形
查看>>
SVG 滤镜学习之——SVG 滤镜
查看>>