版权声明:欢迎转载,但转载时请注明原文地址 https://blog.csdn.net/weixin_42110638/article/details/84954546
241 - 手机类
Time Limit: 1000 Memory Limit: 65535
Submit: 234 Solved: 133
Description
构造手机类,包含其配置信息:型号(字符串)、内存大小(整数)、存储空间(整数,GB为单位)、价格(整数)。提供带参数的构造函数,重写其equals方法,使得两个相同配置(型号、内存、存储相同即可,价格可不同)的手机为相等的手机。重写其toString函数,打印手机的配置信息,形式为CellPhone [model:xxx, memory:xxx, storage:xxx, price:xxx] main函数中从键盘读入两个手机对象,比较他们是否相等,输出他们的配置信息。
Input
两个计算机对象,包含型号、内存、存储空间、价格
Output
两个对象是否相等,两个对象的配置信息
Sample Input
P20 8 64 4999 P20 8 64 4999
Sample Output
true CellPhone [model:P20, memory:8, storage:64, price:4999] CellPhone [model:P20, memory:8, storage:64, price:4999]
HINT
Pre Append Code
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);
}
}
Post Append Code
答案:
class CellPhone {
private String model;//型号
private int memory;//内存大小
private int storage;//存储空间
private int price;
public CellPhone(String model, int memory, int storage, int price) {
this.model = model;
this.memory = memory;
this.storage = storage;
this.price = price;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CellPhone other = (CellPhone) obj;
if (memory != other.memory)
return false;
if (model == null) {
if (other.model != null)
return false;
} else if (!model.equals(other.model))
return false;
if (storage != other.storage)
return false;
return true;
}
@Override
public String toString() {
return "CellPhone [model:" + model + ", memory:" + memory + ", storage:" + storage + ", price:" + price + "]";
}
}