6-1 图书类 (20分)
构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition)
,提供该类的toString()
和equals()
方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。 Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。
输入描述:
两本书信息
输出描述:
两本书的打印信息 两本书是否相等
裁判测试程序样例:
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
重写equals与toString方法
class Book {
String name;
int price;
String author;
int edition;
public Book(String name, int price, String author, int edition) {
this.name = name;
this.price = price;
this.author = author;
this.edition = edition;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getEdition() {
return edition;
}
public void setEdition(int edition) {
this.edition = edition;
}
@Override
public String toString() {
return "name: " + this.name +
", price: " + this.price +
", author: " + this.author +
", edition: " + this.edition;
}
@Override
public boolean equals(Object o) {
//if (this == o) return true;
if (o == null) return false;
else {
boolean result = false;
if (o instanceof Book) {
Book book = (Book) o;
//当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。注意此处无关价格
if (book.name.equalsIgnoreCase(this.name)&&book.author.equalsIgnoreCase(this.author)&&book.edition==this.edition)
result = true;
}
return result;
}
}
}
6-2 手机类 (20分)
构造手机类,包含其配置信息:型号(字符串)、内存大小(整数)、存储空间(整数,GB为单位)、价格(整数)。提供带参数的构造函数,重写其equals方法,使得两个相同配置(型号、内存、存储相同即可,价格可不同)的手机为相等的手机。重写其toString函数,打印手机的配置信息,形式为CellPhone [model:xxx, memory:xxx, storage:xxx, price:xxx] main函数中从键盘读入两个手机对象,比较他们是否相等,输出他们的配置信息。
输入描述:
两个计算机对象,包含型号、内存、存储空间、价格
输出描述:
两个对象是否相等,两个对象的配置信息
裁判测试程序样例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CellPhone c1 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
CellPhone c2 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
System.out.println(c1.equals(c2));
System.out.println(c1);
System.out.println(c2);
}
}
/* 你的代码将被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
P20 8 64 4999
P20 8 64 4999
输出样例:
在这里给出相应的输出。例如:
true
CellPhone [model:P20, memory:8, storage:64, price:4999]
CellPhone [model:P20, memory:8, storage:64, price:4999]
class CellPhone {
String model;
int memory;
int storage;
int price;
public CellPhone(String model, int memory, int storage, int price) {
this.model = model;
this.memory = memory;
this.storage = storage;
this.price = price;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
public int getStorage() {
return storage;
}
public void setStorage(int storage) {
this.storage = storage;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String toString()
{
return "CellPhone [model:"+model+", memory:"+memory+", storage:"+storage+", price:"+price+"]";
}
public boolean equals(Object o)
{
boolean result=false;
if(o==null) return false;
else
{
CellPhone cell=(CellPhone) o;
if(cell.model.equals(this.model)&&cell.memory==this.memory&&cell.storage==this.storage)
result=true;
}
return result;
}
}
6-3 可定制排序的矩形 (20分)
从键盘录入表示矩形个数的数字n,然后录入n个矩形的长和宽,然后对这n个矩形按照面积从大到小排序,并输出排序后的每个矩形的面积要求:根据题目中的附加代码,提供RectangleComparator的实现
输入描述:
矩形个数,每个矩形的长和宽
输出描述:
由大到小排序的每个矩形的面积
裁判测试程序样例:
import java.util.Comparator;
import java.util.Arrays;
import java.util.Scanner;
class Rectangle {
private int length;
private int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getArea(){
return this.length*this.width;
}
@Override
public String toString() {
return length + ", " + width;
}
}
/*你的代码被嵌在这里*/
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, new RectangleComparator());
//打印前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
class RectangleComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Rectangle r1=(Rectangle)o1;
Rectangle r2=(Rectangle)o2;
if (r1.getArea() < r2.getArea()) return 1;
else if (r1.getArea() > r2.getArea()) return -1;
else return 0;
}
}
7-1 教师类 (20分)
设计一个教师类Teacher,要求: 属性有编号(int no)、姓名(String name)、年龄(int age)、所属学院(String seminary),为这些属性设置相应的get和set方法。 为Teacher类重写equals方法,要求:当两个教师对象的no相同时返回true。 重写Teacher类的toString方法,通过该方法可以返回“no: , name:, age: **, seminary: **”形式的字符串。
输入格式:
两个教师对象的编号,姓名,年龄,学院
输出格式:
教师的信息 两个教师是否相等
输入样例:
在这里给出一组输入。例如:
1 Linda 38 SoftwareEngineering
2 Mindy 27 ComputerScience
输出样例:
在这里给出相应的输出。例如:
no: 1, name:Linda, age: 38, seminary: SoftwareEngineering
no: 2, name:Mindy, age: 27, seminary: ComputerScience
false
ans:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
Teacher t1=new Teacher(cin.nextInt(),
cin.next(),
cin.nextInt(),
cin.next());
Teacher t2=new Teacher(cin.nextInt(),
cin.next(),
cin.nextInt(),
cin.next());
System.out.println(t1);
System.out.println(t2);
System.out.println(t1.equals(t2));
}
}
class Teacher
{
int no;
String name;
int age;
String seminary;
public Teacher(int no, String name, int age, String seminary) {
this.no = no;
this.name = name;
this.age = age;
this.seminary = seminary;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSeminary() {
return seminary;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Teacher teacher = (Teacher) o;
boolean result=false;
if(this.no==teacher.no&&this.age==teacher.age&&this.name.equals(teacher.name)&&this.seminary.equals(teacher.seminary))
result=true;
return result;
}
@Override
public String toString() {
return "no: "+no+", name:"+name+", age: "+age+", seminary: "+seminary;
}
}
7-2 教师类-2 (20分)
修改题目143
修改教师类,使得由多个Teacher对象所形成的数组可以排序(编号由低到高排序),并在main函数中使用Arrays.sort(Object[] a)方法排序
定义一个类TeacherManagement,包含教师数组,提供方法add(Teacher[]),使其可以添加教师,提供重载方法search,方法可以在一组给定的教师中,根据姓名或年龄返回等于指定姓名或年龄的教师的字符串信息,信息格式为:“no: , name:, age: **, seminary: **”。如果没有满足条件的教师,则返回“no such teacher”。
输入格式:
教师个数 教师信息 待查找教师的姓名 待查找教师的年龄
输出格式:
排序后的信息 按姓名查找的老师信息 按年龄查找的老师信息
输入样例:
在这里给出一组输入。例如:
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27
输出样例:
在这里给出相应的输出。例如:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
no: 3, name: Linda, age: 38, seminary: SoftwareEngineering
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by name:
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by age:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
ans:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
Teacher []teacher=new Teacher[n];
for (int i = 0; i < n; i++) {
teacher[i]=new Teacher(cin.nextInt(),
cin.next(),
cin.nextInt(),
cin.next());
}
Arrays.sort(teacher);
for (int i = 0; i < n ; i++) {
System.out.println(teacher[i]);
}
TeacherManagement teacherManagement=new TeacherManagement(n,teacher);
String name=cin.next();
teacherManagement.search(name);
int age=cin.nextInt();
teacherManagement.search(age);
cin.close();
}
}
class Teacher implements Comparable
{
int no;
String name;
int age;
String seminary;
public Teacher(int no, String name, int age, String seminary) {
this.no = no;
this.name = name;
this.age = age;
this.seminary = seminary;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSeminary() {
return seminary;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Teacher teacher = (Teacher) o;
boolean result=false;
if(this.no==teacher.no&&this.age==teacher.age&&this.name.equals(teacher.name)&&this.seminary.equals(teacher.seminary))
result=true;
return result;
}
@Override
public String toString() {
return "no: "+no+", name: "+name+", age: "+age+", seminary: "+seminary;
}
@Override
public int compareTo(Object o) {
Teacher t = (Teacher) o;
if (this.getNo() > t.getNo()) return 1;
else if (this.getNo() < t.getNo()) return -1;
else return 0;
}
}
class TeacherManagement
{
int num;
Teacher []teacher;
public TeacherManagement(int num, Teacher[] teacher) {
this.num = num;
this.teacher = teacher;
}
public void add(Teacher t)
{
teacher[num]=t;
this.num++;
}
public void search(String na)
{
System.out.println("search by name:");
boolean flag=false;
for (int i = 0; i < num; i++) {
if(teacher[i].getName().equals(na))
{
System.out.println(teacher[i]);
flag=true;
}
}
if(flag==false)
{
System.out.println("no such teacher");
}
}
public void search(int a)
{
System.out.println("search by age:");
boolean flag=false;
for (int i = 0; i < num; i++) {
if(teacher[i].getAge()==a)
{
System.out.println(teacher[i]);
flag=true;
}
}
if(flag==false)
{
System.out.println("no such teacher");
}
}
}
7-3 家电类 (20分)
某大型家电企业拥有一批送货卡车,运送电视机、洗衣机、空调等家电。编程计算每个卡车所装载货物的总重量。要求有一个Appliance(家电)接口和有三个实现类TV、WashMachine和AirConditioner,这些类能够提供自重。有一个Truck类,包含了该货车上的所有家电,用一个集合(数组或集合类)表示。 Main函数中程序能够输出Truck类所装载货物的总重量。
输入格式:
家电数量 家电种类编号 家电重量
注意:各个家电的编号为:TV:1 WashMachine:2 AirConditioner:3
输出格式:
总重量
输入样例:
在这里给出一组输入。例如:
5
1 20
2 30
3 25
3 30
2 40
输出样例:
在这里给出相应的输出。例如:
145
ans:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Truck truck=new Truck();
truck.get();
int sum=truck.getSum();
System.out.println(sum);
}
}
interface Appliance
{
int getWeight();
}
//该货车上的所有家电,用一个集合(数组或集合类)表示
//数组的话考虑用二维数组,同时储存家电的种类和数量
//采用集合类的方法,创建一个App类
class App implements Appliance
{
int weight;
public App(int weight)
{
this.weight=weight;
}
@Override
public int getWeight()
{
return 0;
}
}
class TV extends App implements Appliance
{
public TV(int w)
{
super(w);
}
@Override
public int getWeight(){
return weight;
}
}
class WashMachine extends App implements Appliance
{
public WashMachine(int w)
{
super(w);
}
@Override
public int getWeight(){
return weight;
}
}
class AirConditioner extends App implements Appliance
{
public AirConditioner(int w)
{
super(w);
}
@Override
public int getWeight(){
return weight;
}
}
class Truck
{
Scanner sc=new Scanner(System.in);
int sum;
int num=sc.nextInt();
App a[]=new App[num];
//获取所有家电
public void get()
{
for (int i = 0; i < num ; i++) {
int n=sc.nextInt();
int w=sc.nextInt();
switch (n){
case 1:{
a[i]=new TV(w);break;
}
case 2: {
a[i]=new WashMachine(w);break;
}
case 3:{
a[i]=new AirConditioner(w);break;
}
default:break;
}
}
}
public int getSum()
{
sum=0;
for (int i = 0; i < num; i++) {
sum += a[i].getWeight();
}
return sum;
}
}
Add函数记得数组扩容!!!