java 142 - 计算机类

构造计算机类,其中包含其配置信息:处理器、主板、内存、显示器、硬盘等设备,各个设备均有型号(字符串),特别的,处理器有主频(小数)和内核数(整数)、显示器有尺寸(整型)、内存和硬盘有容量数据(GB为单位)。请你尝试构造合适的类和类的关系来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。重写相关类的equals方法,使得两个配置完全相同的计算机为相同的计算机。重写相关类的toString函数,打印计算机的配置信息。
在main函数中
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        String model = scan.next();

        double fre = scan.nextDouble();

        String core = scan.next();

        CPU cpu = new CPU(model, fre, core);

        model = scan.next();

        Mainboard mainboard = new Mainboard(model);

        model = scan.next();

        int size = scan.nextInt();

        Memory memory = new Memory(model, size);

        model = scan.next();

        size = scan.nextInt();

        Screen screen = new Screen(model, size);

        model = scan.next();

        size = scan.nextInt();

        Harddisk harddisk = new Harddisk(model, size);

        Computer com1 = new Computer(cpu, mainboard, memory, screen, harddisk);

        

        model = scan.next();

        fre = scan.nextDouble();

        core = scan.next();

        cpu = new CPU(model, fre, core);

        model = scan.next();

        mainboard = new Mainboard(model);

        model = scan.next();

        size = scan.nextInt();

        memory = new Memory(model, size);

        model = scan.next();

        size = scan.nextInt();

        screen = new Screen(model, size);

        model = scan.next();

        size = scan.nextInt();

        harddisk = new Harddisk(model, size);

        Computer com2 = new Computer(cpu, mainboard, memory, screen, harddisk);

        

        System.out.println(com1.equals(com2));

        System.out.print(""+"Computer1:\n"+com1+"Computer2:
"+com2);

    }

}

interface Model{

    public static void model(String m){

    }

}

class CPU implements Model{

    String cpu_model;

    double cpu_fre;

    String cpu_core;

    public CPU(String m, double f, String c) {

        cpu_model = m;

        cpu_fre = f;

        cpu_core = c;

    }

    public static void model(String m){

    }

    public String toString() {

        return "CPU:
Model: "+cpu_model+"
Frequency: "+String.format("%.1f",cpu_fre)+"
Number of Cores: "+cpu_core+"
";

    }

    public boolean equals(CPU c) {

        if (cpu_model.equals(c.cpu_model) && cpu_fre == c.cpu_fre && cpu_core.equals(c.cpu_core)) {

            return true;

        }

        return false;

    }

}

class Mainboard implements Model{

    String main_model;

    public Mainboard(String m) {

        main_model = m;

    }

    public static void model(String m){

    }

    public String toString() {

        return "Mainboard:
Model: "+main_model+"
";

    }

    public boolean equals(Mainboard c) {

        if (main_model.equals(c.main_model)) {

            return true;

        }

        return false;

    }

}

class Memory implements Model{

    String Memory_model;

    int Memory_size;

    public Memory(String m, int s) {

        Memory_model = m;

        Memory_size = s;

    }

    public static void model(String m){

    }

    public String toString() {

        return "Memory:
Model: "+Memory_model+"
Size: "+Memory_size+"
";

    }

    public boolean equals(Memory c) {

        if (Memory_model.equals(c.Memory_model) && Memory_size == c.Memory_size) {

            return true;

        }

        return false;

    }

}

class Screen implements Model{

    private String scrmodel;

    private int scrsize;

    public Screen(String m, int s) {

        scrmodel = m;

        scrsize = s;

    }

    

    

    public String getScreen_model() {

        return scrmodel;

    }

    public void setScreen_model(String screen_model) {

        scrmodel = screen_model;

    }

    public int getScrsize() {

        return scrsize;

    }

    public void setScrsize(int scrsize) {

        this.scrsize = scrsize;

    }

    public static void model(String m){

    }

    public String toString() {

        return "Screen:
Model: "+scrmodel+"
Size: "+scrsize+"
";

    }

    public boolean equals(Screen c) {

        if (scrmodel.equals(c.scrmodel) && scrsize == c.scrsize) {

            return true;

        }

        return false;

    }

}

class Harddisk implements Model{

    private String harmodel;

    private int harsize;

    public Harddisk(String m, int s) {

        harmodel = m;

        harsize = s;

    }

    

    

    public String getHarmodel() {

        return harmodel;

    }

    public void setHarmodel(String harmodel) {

        this.harmodel = harmodel;

    }

    public int getHarsize() {

        return harsize;

    }

    public void setHarsize(int harsize) {

        this.harsize = harsize;

    }

    public static void model(String m){

    }

    public String toString() {

        return "Harddisk:
Model: "+harmodel+"
Size: "+harsize+"
";

    }

    public boolean equals(Harddisk c) {

        if (harmodel.equals(c.harmodel) && harsize == c.harsize) {

            return true;

        }

        return false;

    }

}

class Computer{

    private CPU cpu;

    private Mainboard mainboard;

    private Memory memory;

    private Screen screen;

    private Harddisk harddisk;

    

    public CPU getCpu() {

        return cpu;

    }

    public void setCpu(CPU cpu) {

        this.cpu = cpu;

    }

    public Mainboard getMainboard() {

        return mainboard;

    }

    public void setMainboard(Mainboard mainboard) {

        this.mainboard = mainboard;

    }

    public Memory getMemory() {

        return memory;

    }

    public void setMemory(Memory memory) {

        this.memory = memory;

    }

    public Screen getScreen() {

        return screen;

    }

    public void setScreen(Screen screen) {

        this.screen = screen;

    }

    public Harddisk getHarddisk() {

        return harddisk;

    }

    public void setHarddisk(Harddisk harddisk) {

        this.harddisk = harddisk;

    }

    public Computer(CPU c, Mainboard ma, Memory me, Screen s, Harddisk h) {

        cpu = new CPU(c.cpu_model, c.cpu_fre, c.cpu_core);

        mainboard = new Mainboard(ma.main_model);

        memory = new Memory(me.Memory_model, me.Memory_size);

        screen = new Screen(s.getScreen_model(), s.getScrsize());

        harddisk = new Harddisk(h.getHarmodel(), h.getHarsize());

    }

    public boolean equals(Computer c) {

        if (cpu.equals(c.cpu) 

                && mainboard.equals(c.mainboard) 

                && memory.equals(c.memory) 

                && screen.equals(c.screen) 

                && harddisk.equals(c.harddisk)) {

            return true;

        }

        return false;

    }

    public String toString() {

        return cpu.toString()+mainboard.toString()+memory.toString()+screen.toString()+harddisk.toString();

    }

}

 

面向对象是一种程序设计的思想,它将程序中的数据和对数据的操作封装在一起,形成对象。对象是的一个实例,定义了对象的属性和行为。在Java中,面向对象的概念包括与对象的关系、封装、构造函数、this关键字、static关键字以及设计模式等方面。 设计模式是在软件设计中常用的解决问题的经验总结,它提供了一套可重用的解决方案。在Java中,单例设计模式是一种常见的设计模式之一,它保证一个只有一个实例,并提供一个全局访问点。通过使用单例设计模式,可以确保在程序中只有一个对象实例被创建,从而节省了系统资源并提高了性能。 通过使用单例设计模式,可以实现以下效果: - 限制一个只能有一个实例。 - 提供一个全局访问点,使其他对象可以方便地访问该实例。 - 保证对象的唯一性,避免多个对象的状态不一致。 在Java中,实现单例设计模式有多种方式,包括饿汉式、懒汉式、双重检测锁等。每种方式都有各自的特点和适用场景,开发者可以根据具体的需求选择合适的实现方式。设计模式是一种通用的解决问题的方法,它可以在面向对象的程序设计中提供灵活、可复用的解决方案。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [计算机后端-Java-Java核心基础-第15章 面向对象07 14. 设计模式与单例设计模式.avi](https://download.csdn.net/download/programxh/85435560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [完整版Java全套入门培训课件 Java基础 03-面向对象(共18页).pptx](https://download.csdn.net/download/qq_27595745/21440470)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值