构造手机类,包含其配置信息:型号(字符串)、内存大小(整数)、存储空间(整数,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]
时间: 2024-03-25 07:41:57 浏览: 20
```
import java.util.Objects;
public 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;
}
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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CellPhone cellPhone = (CellPhone) o;
return memory == cellPhone.memory &&
storage == cellPhone.storage &&
Objects.equals(model, cellPhone.model);
}
@Override
public int hashCode() {
return Objects.hash(model, memory, storage);
}
@Override
public String toString() {
return "CellPhone [model:" + model + ", memory:" + memory + ", storage:" + storage + ", price:" + price + "]";
}
}
```
测试结果:
输入:
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]