# Java程序设计实验5---sdust

Java程序设计笔记 专栏收录该内容
8 篇文章 2 订阅

## 6-42 可定制排序的矩形 (20 分)

import java.util.Comparator;
import java.util.Arrays;
import java.util.Scanner;

/*你的代码被嵌在这里*/

public class Main {
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
//输入矩形个数
int num_rectangle = scan.nextInt();
Rectangle[]  recs = new Rectangle[num_rectangle];
//输入每个矩形的长和宽
for(int i=0;i<num_rectangle;i++){
int length = scan.nextInt();
int width = scan.nextInt();
Rectangle rec = new Rectangle(length,width);
recs[i] = rec;
}
//按照面积由大到小排序
Arrays.sort(recs);
//打印前n-1个矩形的面积
for(int i=0;i<recs.length-1;i++){
System.out.print(recs[i].getArea()+",");
}
//打印最后一个矩形的面积
System.out.print(recs[recs.length-1].getArea());
scan.close();
}
}

3 1 2 3 4 2 3

12,6,2

### Accepted Code

class Rectangle implements Comparable<Rectangle> {

int length;
int width;

Rectangle(int l, int w) {
length = l;
width = w;
}

int getArea() {
return length * width;
}

@Override
public int compareTo(Rectangle o) {
if (o.getArea() < this.getArea()) {
return -1;
} else if (o.getArea() == this.getArea()) {
return 0;
} else {
return 1;
}

}
}

## 6-50 图书类 (20 分)

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Book b1 = new Book(s.next(),
s.nextInt(),
s.next(),
s.nextInt());
Book b2 = new Book(s.next(),s.nextInt(),s.next(),s.nextInt());

System.out.println(b1);
System.out.println(b2);
System.out.println(b1.equals(b2));

}

}

/* 你的代码被嵌在这里 */

ThinkingInJava
86
BruceEckel
4
CoreJava
95
CayS.Horstmann
10

name: ThinkingInJava, price: 86, author: BruceEckel, edition: 4
name: CoreJava, price: 95, author: CayS.Horstmann, edition: 10
false

### Accepted Code

class Book {
String name;
int price;
String author;
int edition;

Book(String name, int price, String author, int edition) {
this.author = author;
this.edition = edition;
this.name = name;
this.price = price;
}

@Override
public String toString() {
return "name: " + name + ", price: " + price + ", author: " + author + ", edition: " + edition;
}

@Override
public boolean equals(Object o) {

if (o == null || this == null) return false;

if (o instanceof Book) {
Book b = (Book)o;
if (name.toLowerCase().equals(b.name.toLowerCase()) && author.toLowerCase().equals(b.author.toLowerCase())
&& edition == b.edition) {
return true;
}
}

return false;
}

}

## 7-43 jmu-Java-04面向对象进阶–02-接口-Comparator (20 分)

Arrays.sort可以对所有实现Comparable的对象进行排序。但如果有多种排序需求，如有时候需对name进行降序排序，有时候只需要对年龄进行排序。使用Comparable无法满足这样的需求。可以编写不同的Comparator来满足多样的排序需求。

#1.编写PersonSortable2类 属性：private name(String)、private age(int)

toString方法：返回格式name-age

#2 编写Comparator类

#3.main方法中

System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));

5
zhang 15
zhang 12
wang 14
Wang 17
li 17

NameComparator:sort
Wang-17
li-17
wang-14
zhang-15
zhang-12
AgeComparator:sort
zhang-12
wang-14
zhang-15
Wang-17
li-17
//最后两行是标识信息

### Accepted Code

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

int n = scan.nextInt();
PersonSortable2 [] P = new PersonSortable2[n];

for(int i = 0; i< n; i ++){
P[i] = new PersonSortable2(scan.next(), scan.nextInt());
}

Arrays.sort(P, (new NameComparator()));
System.out.println("NameComparator:sort");
for(int i = 0; i < n; i ++){
System.out.println(P[i]);
}

Arrays.sort(P, (new AgeComparator()));
System.out.println("AgeComparator:sort");
for(int i = 0; i < n; i ++){
System.out.println(P[i]);
}

System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));

scan.close();
}

}

class PersonSortable2 {
private String name;
private int age;

public PersonSortable2(String n, int a) {
name = n;
age = a;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

@Override
public String toString() {
return new String(name.concat("-") + age);
}
}

class NameComparator implements Comparator<PersonSortable2> {

public int compare(PersonSortable2 o1, PersonSortable2 o2) {
return o1.getName().compareTo(o2.getName());
}

}

class AgeComparator implements Comparator<PersonSortable2>{

@Override
public int compare(PersonSortable2 o1, PersonSortable2 o2) {
return o1.getAge() - o2.getAge();
}

}
• 0
点赞
• 3
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

12-30 2817
06-14 276
07-11 9万+
11-15 678
10-17 881
01-12 128
08-15 1203
09-26 2581
©️2021 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客

LiuLin_Hao

¥2 ¥4 ¥6 ¥10 ¥20

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