[Java] 计算机类 →→→→→进入此内容的聊天室

来自 , 2019-10-26, 写在 Java, 查看 112 次.
URL http://www.code666.cn/view/404dcc91
  1. import java.util.*;
  2. public class Main{
  3.     public static void main(String[] args) {
  4.          Scanner scan = new Scanner(System.in);
  5.          String model = scan.next();
  6.          double fre = scan.nextDouble();
  7.          int cores = scan.nextInt();
  8.          CPU cpu1 = new CPU(model, fre, cores);
  9.          
  10.          model = scan.next();
  11.          Mainboard mb1 = new Mainboard(model);
  12.          
  13.          model = scan.next();
  14.          int size = scan.nextInt();
  15.          Memory me1 = new Memory(model, size);
  16.          
  17.          model = scan.next();
  18.          size = scan.nextInt();
  19.          Screen sc1 = new Screen(model, size);
  20.          
  21.          model = scan.next();
  22.          String cap = scan.next();
  23.          Harddisk h1 = new Harddisk(model, cap);
  24.          
  25.          Computer com1 = new Computer(cpu1, mb1, me1, sc1, h1);
  26.          
  27.                          
  28.          model = scan.next();
  29.          fre = scan.nextDouble();
  30.          cores = scan.nextInt();
  31.          CPU cpu2 = new CPU(model, fre, cores);
  32.          
  33.          model = scan.next();
  34.          Mainboard mb2 = new Mainboard(model);
  35.          
  36.          model = scan.next();
  37.          size = scan.nextInt();
  38.          Memory me2 = new Memory(model, size);
  39.          
  40.          model = scan.next();
  41.          size = scan.nextInt();
  42.          Screen sc2 = new Screen(model, size);
  43.          
  44.          model = scan.next();
  45.          cap = scan.next();
  46.          Harddisk h2 = new Harddisk(model, cap);       
  47.          
  48.          Computer com2 = new Computer(cpu2, mb2, me2, sc2, h2);
  49.          System.out.println(com1.equals(com2));
  50.          System.out.println("Computer1:\n"+com1.toString());
  51.          System.out.println("Computer2:\n"+com2.toString());
  52.          
  53.          
  54.          scan.close();  
  55.     }
  56. }  
  57.  
  58. class Computer{
  59.         CPU cpu;
  60.         Mainboard mb;
  61.         Memory me;
  62.         Screen sc;
  63.         Harddisk h;
  64.         public Computer(CPU c, Mainboard _mb, Memory _me, Screen _sc, Harddisk _h) {
  65.                 cpu = c;
  66.                 mb = _mb;
  67.                 me = _me;
  68.                 sc = _sc;
  69.                 h = _h;
  70.         }
  71.         public boolean equals(Computer com) {
  72.                 if( cpu.equals(com.cpu) && mb.equals(com.mb) && me.equals(com.me) && sc.equals(com.sc) && h.equals(com.h) )
  73.                         return true;
  74.                 else
  75.                         return false;
  76.         }
  77.         public String toString() {
  78.                 return cpu.toString()+mb.toString()+me.toString()+sc.toString()+h.toString();
  79.         }
  80. }
  81.  
  82. class CPU{
  83.         String model;
  84.         double frequency;
  85.         int cores;
  86.         public CPU(String m, double f, int c) {
  87.                 model = m;
  88.                 frequency = f;
  89.                 cores = c;
  90.         }
  91.         public boolean equals(CPU cpu){
  92.                 if(this.model.equals(cpu.model)&&(this.frequency == cpu.frequency)&&(this.cores==cpu.cores)) {
  93.                         return true;
  94.                 }
  95.                 else
  96.                         return false;
  97.         }
  98.         public String toString() {
  99.                 return "CPU:\n" +"Model: " +model +"\n" +"Frequency: " +String.format("%.1f",frequency) +"\n" +"Number of Cores: " +cores +"\n";
  100.         }
  101. }
  102.  
  103. class Mainboard{
  104.         String model;
  105.         public Mainboard(String m) {
  106.                 model = m;
  107.         }
  108.         public boolean equals(Mainboard mb) {
  109.                 if(this.model.equals(mb.model))
  110.                         return true;
  111.                 else
  112.                         return false;
  113.         }
  114.         public String toString() {
  115.                 return "Mainboard:\n" +"Model: " +model +"\n";
  116.         }
  117. }
  118.  
  119. class Memory{
  120.         String model;
  121.         int size;
  122.         public Memory(String m, int s) {
  123.                 model = m;
  124.                 size = s;
  125.         }
  126.         public boolean equals(Memory me) {
  127.                 if(this.model.equals(me.model) && (this.size==me.size))
  128.                         return true;
  129.                 else
  130.                         return false;
  131.         }
  132.         public String toString() {
  133.                 return "Memory:\n" +"Model: " +model +"\n" +"Size: " +size +"\n";
  134.         }
  135. }
  136.  
  137. class Screen{
  138.         String model;
  139.         int size;
  140.         public Screen(String m, int s) {
  141.                 model = m;
  142.                 size = s;
  143.         }
  144.         public boolean equals(Screen sc) {
  145.                 if(this.model.equals(sc.model) && (this.size==sc.size))
  146.                         return true;
  147.                 else
  148.                         return false;
  149.         }
  150.         public String toString() {
  151.                 return "Screen:\nModel: " +model +"\n" +"Size: " +size +"\n";
  152.         }
  153. }
  154.  
  155. class Harddisk{
  156.         String model;
  157.         String size;
  158.         public Harddisk(String m, String s) {
  159.                 model = m;
  160.                 size = s;
  161.         }
  162.         public boolean equals(Harddisk h) {
  163.                 if(this.model.equals(h.model) && this.size.equals(h.size))
  164.                         return true;
  165.                 else
  166.                         return false;
  167.         }
  168.         public String toString() {
  169.                 return "Harddisk:\nModel: " +model +"\n" +"Size: " +size;
  170.         }
  171. }

回复 "计算机类"

这儿你可以回复上面这条便签

captcha