⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycomputer.java

📁 Java程序设计实验与实训源代码经典的JAVA学习教材
💻 JAVA
字号:
// coputer.java 
//这个程序是描述计算机的属性和状态的类
import java.util.*;

 class computer {
   private String pc_name;      //计算机品牌
   private String pc_color;     //计算机颜色
   private String pc_type;      //CPU型号
   private String pc_ram;       //内存容量
   private String pc_harddisk;  //硬盘容量
   private int pc_price;        //价格
   private String pc_state;     //工作状态

  
   public computer()  //默认构造函数,不带参数
 {
   pc_name="";                        //计算机品牌
   pc_color="";                       //计算机颜色
   pc_type="";                        //CPU型号
   pc_ram="";                          //内存容量
   pc_harddisk="";                     //硬盘容量
   pc_price=0;                        //价格
   pc_state="";                        //计算机状态
 }
 public computer(String aname,String acolor,String atype,String aram,String aharddisk,int aprice,String astate)  //带参数的构造函数
 {
   pc_name=aname;                         //计算机品牌初值
   pc_color=acolor;                       //计算机颜色初值
   pc_type=atype;                         //CPU型号初值
   pc_ram=aram;                           //内存容量初值
   pc_harddisk=aharddisk;                 //硬盘容量初值
   pc_price=aprice;                       //价格初值
   pc_state=astate;                       //计算机状态初值
 }
  void setcomputer(String aname,String acolor,String atype,String aram,String aharddisk,int aprice,String astate) //设置属性值的方法
   {
   pc_name=aname;                         //计算机品牌
   pc_color=acolor;                       //计算机颜色
   pc_type=atype;                         //CPU型号
   pc_ram=aram;                           //内存容量
   pc_harddisk=aharddisk;                 //硬盘容量
   pc_price=aprice;                       //价格
   pc_state=astate;                       //计算机状态
   }
    public String pc_open( String pc_state,int pc_price )//定义计算机打开的方法
   {
     
     System.out.println("工作状态:"+pc_state);
     if(pc_price<0){ System.out.println("计算机价格="+pc_price+"计算机价格不能是负数");};
     if(pc_price>50000){System.out.println("计算机价格="+pc_price+"计算机价格超过预算");};
     
     System.out.println("         *         ");
     System.out.println("    *    *    *    ");
     System.out.println("*   *    *    *   *");
     return pc_state; 
          
   }
   public String pc_close(String pc_state )//定义计算机关闭的方法
   {
   	//......
   	System.out.println("工作状态"+pc_state);
   	System.out.println("请稍后,正在关闭计算机");
   	return pc_state;
   }
   public String pc_hitch( String pc_state )//定义计算机挂起的方法
   {
   	//......
   	System.out.println("工作状态"+pc_state);
   	System.out.println("启动计算机请按Ctrl+Alt+Delete键");
   	return pc_state;
   }
   void printcomputer()                            //输出的方法
   {
   	 System.out.println("计算机品牌:"+pc_name+"计算机颜色:"+pc_color+"CPU型号:"+pc_type+"内存容量:" 
     +pc_ram+"硬盘容量:"+pc_harddisk+"价格:"+pc_price+"工作状态"+pc_state);
   }
  }   
   
   
   public class Mycomputer 
   {
   public static void main(String[] args)
   {
      computer stu1=new computer();                                         //创建计算机类的对象stu1;
      stu1.setcomputer("Dell","黑","PentiumⅣ","256k","2M",7096," 良好 ");  //调用方法setcomputer设置计算机信息
      stu1.printcomputer();                                                 //输出计算机的信息
      computer stu2=new computer();                                         //创建计算机类的对象stu2;
      stu2.setcomputer("联想","黑","PentiumⅣ","512k","2.4M",7296," 良好 ");//调用方法setcomputer设置计算机信息
      stu2.printcomputer();                                                 //输出计算机的信息 
      computer stu3=new computer();
      stu3.pc_open("pc_open",-1);   
      computer stu4=new computer();
      stu4.pc_close("pc_close"); 
      computer stu5=new computer();
      stu5.pc_hitch("pc_hitch"); 
      
  }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -