课后习题4-12
Posted Lyh3012648079
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课后习题4-12相关的知识,希望对你有一定的参考价值。
定义一个datatype类,能处理包含字符型,整形,浮点型3种类型的数据,给出其构造函数。
#include <iostream> using namespace std; class DataType private: char a; int n; float x; enum character, integer, floating, vartype; public: DataType(char a) vartype=character; this->a=a; DataType(int n) vartype=integer; this->n=n; DataType(float x) vartype=floating; this->x=x; void print() switch(vartype) case character: cout<<"zifu"<<a<<endl; break; case integer: cout<<"zhengshu"<<n<<endl; break; case floating: cout<<"fudiashu"<<x<<endl; break; ; int main() DataType a(\'c\'),b(1),c(1.44F); a.print(); b.print(); c.print();
课后习题1
1 package com.ju; 2 3 public class power { 4 5 //颜色 6 private String yanse; 7 8 //品牌 9 private String pinpai; 10 11 //电池容量 12 private double mah; 13 14 //当前容量 15 private double dqmah; 16 17 //充电 18 public void chongdian(double dianliang) 19 { 20 if (dianliang < 0) 21 { 22 System.out.println("请及时充电"); 23 } 24 else if (this.dqmah + dianliang > this.mah) 25 { 26 double bccd = this.mah-this.dqmah; 27 28 this.dqmah = this.mah; 29 30 System.out.println("已充满,请关闭充电电源" +bccd+"毫安"); 31 } 32 else 33 { 34 this.dqmah += dianliang; 35 System.out.println("这次充了"+dianliang+"毫安"); 36 } 37 } 38 private double xiaohao = 0; 39 40 private double xiaohaozl() 41 { 42 return this.xiaohaozl(); 43 } 44 45 //使用 消耗电量 46 public void xiaohaodl(double xiaohaozl,double xiaohao) 47 { 48 49 if(this.dqmah <=0) 50 { 51 System.out.println("电量低,请充电后使用"); 52 53 return; 54 } 55 if (xiaohaozl<0 || xiaohao<0) 56 { 57 System.out.println("无法使用"); 58 } 59 if (this.dqmah < 0 ) 60 { 61 this.dqmah = 0; 62 63 System.out.println("请充电!"); 64 } 65 } 66 67 public String getYanse() { 68 return yanse; 69 } 70 71 public void setYanse(String yanse) { 72 this.yanse = yanse; 73 } 74 75 public String getPinpai() { 76 return pinpai; 77 } 78 79 public void setPinpai(String pinpai) { 80 this.pinpai = pinpai; 81 } 82 83 public double getMah() { 84 return mah; 85 } 86 87 public void setMah(double mah) { 88 this.mah = mah; 89 } 90 91 public double getDqmah() { 92 return dqmah; 93 } 94 95 public void setDqmah(double dqmah) { 96 this.dqmah = dqmah; 97 } 98 99 public power(String pinpai, double mah, double dqmah) { 100 super(); 101 this.pinpai = pinpai; 102 this.mah = mah; 103 this.dqmah = dqmah; 104 } 105 public static void main(String[]args) 106 { 107 power myMi = new power("小米",20000,500); 108 109 System.out.println("品牌 = "+ myMi.getPinpai()); 110 111 System.out.println("电量 = "+ myMi.getMah()); 112 113 114 } 115 116 } 117 118 }
以上是关于课后习题4-12的主要内容,如果未能解决你的问题,请参考以下文章
Java编程思想第4版官方完整版及官方习题课后答案(code和pdf)