zoukankan      html  css  js  c++  java
  • 2020.8.16(周报6)

    一、今日学习内容  

    构造函数和析构函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合的多重应用:

      1、设计一个用于人事管理的“人员”类。由于考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号、性别、出生日期、身份证号等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。

    复制代码
     1 //日期类:
     2 import java.util.Scanner;
     3 public class Date2 {
     4     protected int year,month,day;
     5     Date2(){}
     6     Date2(int y,int m,int d){
     7         year=y;
     8         month=m;
     9         day=d;
    10     }
    11     public void setDate() {
    12         Scanner con=new Scanner(System.in);
    13         year=con.nextInt();
    14         month=con.nextInt();
    15         day=con.nextInt();
    16     }
    17     void display() {
    18         System.out.println(year+"年"+month+"月"+day+"日");
    19     }
    20 }
    21 //人员类:
    22 import java.util.*;
    23 public class People2 {
    24     private int num;
    25     private char sex;
    26     private Date2 birthday=new Date2();
    27     private String ID;
    28     People2(){}
    29     People2(int n,char s,int y,int m,int d,String i){
    30         num=n;
    31         sex=s;
    32         birthday.year=y;
    33         birthday.month=m;
    34         birthday.day=d;
    35         ID=i;
    36     }
    37     public void input() {
    38         System.out.println("基本信息:\n编号:");
    39         Scanner con=new Scanner(System.in);
    40         num=con.nextInt();
    41         System.out.println("性别:");
    42         sex=con.next().charAt(0);
    43         System.out.println("出生日期:");
    44         birthday.setDate();
    45         System.out.println("身份证号:");
    46         ID=con.next();
    47     }
    48     public void output() {
    49         System.out.println("编号:"+num);
    50         System.out.println("性别:"+sex);
    51         System.out.print("出生日期:");
    52         birthday.display();
    53         System.out.print("身份证号:"+ID);
    54     }
    55     public static void main(String[] args) {
    56         People2 x=new People2();
    57         x.input();
    58         x.output();
    59     }
    60 }
    复制代码

       2、定义一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示Tree对象的ages的值。

    复制代码
     1 import java.util.Scanner;
     2 public class Tree {
     3     private int ages;
     4     Tree(){}
     5     Tree(int a){
     6         ages=a;
     7     }
     8     public int grow(int years) {
     9         return ages+years;
    10     }
    11     public void age() {
    12         System.out.println("树龄为:"+ages);
    13     }
    14 
    15     public static void main(String[] args) {
    16         Tree m=new Tree(10);
    17         int n;
    18         Scanner con=new Scanner(System.in);
    19         n=con.nextInt();
    20         m.age();
    21         System.out.println("年之后树龄为:"+m.grow(n));
    22     }
    23 }
     

      3、编写一个名为CPU的类,描述一个CPU的以下信息:时钟频率,最大不会超过3000MHZ;字长,可以是32位或64位;核数,可以是单核、双核或四核;是否支持超线程。各项信息要求使用位域来表示。

    复制代码
     1 public class CPU {
     2     private double clockrate;
     3     private int wordsize;
     4     private int heshu;
     5     private boolean m;
     6     CPU(double c,int w,int h,boolean n){
     7         clockrate=c;
     8         wordsize=w;
     9         heshu=h;
    10         m=n;
    11     }
    12     public void display() {
    13         System.out.println("时钟频率为:"+clockrate+"MHz");
    14         System.out.println("字长为:"+wordsize);
    15         System.out.println("核数为:"+heshu);
    16         if(m==true)
    17             System.out.println("支持线程");
    18         else
    19             System.out.println("不支持线程");
    20     }
    21 
    22     public static void main(String[] args) {
    23         CPU MyCPU=new CPU(2500,32,4,true);
    24         MyCPU.display();
    25     }
    26 }
    复制代码

     二、遇到的问题

        字符、字符数组的输入输出不会。

    三、明日计划

       继续完成相关例题的验证。

  • 相关阅读:
    紫书 例题8-6 UVa 1606(扫描法)
    紫书 例题8-5 UVa11054(等价转换)
    紫书 例题8-4 UVa 11134(问题分解 + 贪心)
    紫书 例题8-3 UVa 1152(中途相遇法)
    紫书 例题8-2 UVa 11605(构造法)
    Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0
    2016ACM/ICPC亚洲区沈阳站
    Tenka1 Programmer Contest D
    [Gym-101512C] 凸包+最远点对
    ZOJ
  • 原文地址:https://www.cnblogs.com/marr/p/13580198.html
Copyright © 2011-2022 走看看