作业1:

例题9.1 制作圆类,根据圆的半径求出周长及面积

package com.swift;
//抽象的方法构成类,把属性和方法进行封装
public class Circle {
// 两个方面一个是字段也称属性,另一个是方法,也称实现的功能
private double radius; // 构造方法,有参构造
public Circle(double radius) {
this.radius = radius;
}
//方法的重载,参数不同
// 构造方法,无参构造
public Circle() {
this.radius = 1;
} // 求圆面积的方法
public double getArea() {
return radius * radius * Math.PI; } // 求圆周长的方法
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public void setRadius(double newRadius) {
this.radius=newRadius;
}
}

圆类的展示类

package com.swift;

public class DemoCircle {
public static void main(String[] args) {
Circle circle1=new Circle();
double area=circle1.getArea();
System.out.println(area);
Circle circle2=new Circle(10);
System.out.println(circle2.getArea());
System.out.println(circle1.getPerimeter());
System.out.println(circle2.getPerimeter());
double ridius=10;
double areaCircle=Math.PI*ridius*ridius;
System.out.println(areaCircle);
circle2.setRadius(5);
System.out.println(circle2.getArea());
}
}

作业2:

把上边的两个圆的类合并成一个类,并实现同样功能

package com.swift;

public class SimpleCircle {

    private double radius;
public SimpleCircle() {
this.radius=1;
}
public SimpleCircle(double radius){
this.radius=radius;
}
public double getArea() {
// TODO Auto-generated method stub
return Math.PI*radius*radius;
}
public double getPerimeter() {
return 2*Math.PI*radius;
} public static void main(String[] args) {
SimpleCircle cir1=new SimpleCircle();
System.out.println("The area of the circle of radius "+cir1.radius+" is "+cir1.getArea());
SimpleCircle cir2=new SimpleCircle(10);
System.out.println("The area of the circle of radius "+cir2.radius+" is "+cir2.getArea());
}
}

作业3:

例题9.3 造一台电视机,并且实现调频道和调声音大小功能

package com.swift;

public class TV {
public int channel=1;
public int volumeLevel=1;
public boolean on=false; public TV() { }
public void turnOn() {
on =true;
System.out.println("电视机已经打开了。");
}
public void turnOff() {
on=false;
System.out.println("电视机已经关闭了。");
}
public int getChannel() {
return channel;
}
public void setChannel(int channel) {
if(on) {
System.out.println("电视机已开,可以开始调台了。");
if(channel>=1&&channel<=120) {
this.channel = channel;
System.out.println("频道已经调到 "+channel+" 台");
}else {
System.out.println("你要调的频道不存在。");
}
}else {
System.out.println("电视机关着的时候是不能调台的");
}
}
public int getVolumeLevel() {
return volumeLevel;
}
public void setVolumeLevel(int volumeLevel) {
if(on) {
System.out.println("电视机已开,声音大小可调了");
if(volumeLevel>=1&&volumeLevel<=7) {
this.volumeLevel = volumeLevel;
System.out.println("声音的大小设置成了 "+volumeLevel+" 大小");
}
}else {
System.out.println("电视机关着的时候是不能调声音的");
} }
public void channelUp() {
if(on&&channel<120) {
channel++;
}
}
public void channelDown() {
if(on&&channel>1) {
channel--;
}
}
public void volumeUp() {
if(on&&volumeLevel<7) {
volumeLevel++;
}
}
public void volumeDown() {
if(on&&volumeLevel>1) {
volumeLevel--;
}
} }

测试电视机的类

package com.swift;

public class TestTV {

    public static void main(String[] args) {
// TV tv1=new TV();
// tv1.turnOff();
// tv1.setChannel(30);
// tv1.setVolumeLevel(3); TV tv2=new TV();
tv2.turnOn();
System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
tv2.channelUp();
System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
tv2.channelUp();
System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
tv2.channelUp();
System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
tv2.volumeUp();
System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
tv2.volumeUp();
System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
tv2.volumeUp();
System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel); } }

java第九次作业:第九章例题3个的更多相关文章

  1. JAVA第九次作业

    JAVA第九次作业 (一)学习总结 1.用思维导图对javaIO操作的学习内容进行总结. 参考资料: XMind. 2.下面的程序实现了文件的拷贝,但采用的是一个字节一个字节的读写方式,效率很低.使用 ...

  2. Week09《java程序设计》第九次作业总结

    Week09<java程序设计>第九次作业总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 答: 2. 书面作业 本次作业题集集合 1. Li ...

  3. 2017-2018-2 1723《程序设计与数据结构》第九周作业 & 第二周结对编程 总结

    作业地址 第九次作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1878 (作业界面已评分,可随时查看,如果对自己的评分有意 ...

  4. 2017-2018-1 20179205《Linux内核原理与设计》第九周作业

    <Linux内核原理与设计>第九周作业 视频学习及代码分析 一.进程调度时机与进程的切换 不同类型的进程有不同的调度需求,第一种分类:I/O-bound 会频繁的进程I/O,通常会花费很多 ...

  5. 【西北师大-2108Java】第九次作业成绩汇总

    [西北师大-2108Java]第九次作业成绩汇总 作业题目 面向对象程序设计(JAVA) 第11周学习指导及要求 实验目的与要求 (1)理解泛型概念: (2)掌握泛型类的定义与使用: (3)掌握泛型方 ...

  6. JAVA实验报告及第九周总结

    Java第九周作业 实验报告七 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负 ...

  7. 2018-2019-1 20189221 《Linux内核原理与分析》第九周作业

    2018-2019-1 20189221 <Linux内核原理与分析>第九周作业 实验八 理理解进程调度时机跟踪分析进程调度与进程切换的过程 进程调度 进度调度时机: 1.中断处理过程(包 ...

  8. C#基础第九天-作业答案-储蓄账户(SavingAccount)和信用账户(CreditAccount)

    class Bank { //Dictionary<long,Account> dictionary=new Dictionary<long,Account>(); DataT ...

  9. C#基础第九天-作业-储蓄账户(SavingAccount)和信用账户(CreditAccount)

    要求1:完成以下两种账户类型的编码.银行的客户分为两大类:储蓄账户(SavingAccount)和信用账户(CreditAccount),两种的账户类型的区别在于:储蓄账户不允许透支,而信用账户可以透 ...

随机推荐

  1. 分布式通信-tcp/ip 单播

    服务端 public class SingleBroadCastSocketServer { public static void main(String[] args) { ServerSocket ...

  2. python错误之RuntimeError: dictionary changed size during iteration

    pythonn报错信息: C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Ad ...

  3. Codeforces 1143B(思维、技巧)

    自己水平太低,不丢人. 结论是最后选取的数后缀一定是若干个9,暴举即可.然而暴举也有暴举的艺术. ll n; ll dfs(ll n) { if (n == 0) return 1; if (n &l ...

  4. YII报错笔记:<pre>PHP Notice &#039;yii\base\ErrorException&#039; with message &#039;Uninitialized string offset: 0&#039; in /my/test/project/iot/vendor/yiisoft/yii2/base/Model.php:778

    YII常见报错笔记 报错返回的代码如下: <pre>PHP Notice 'yii\base\ErrorException' with message 'Uninitialized str ...

  5. rsync服务的安装与配置

    rsync 服务的安装配置与客户端的同步操作   1. 使用xinetd服务运行rsync服务: 服务器端: 1.关闭selinux,设置iptables开放xinetd的873端口 2. yum - ...

  6. 阿里云-域名免费申请ssl证书过程

    1.运行证书服务docker docker run --entrypoint="/bin/sh" -it --name certbotsh certbot/certbot:late ...

  7. Unix高级编程之文件权限

    1.访问权限表 st_mode 意义 S_IRUSR 用户-读 S_IWUSR 用户-写 S_IXUSR 用户-执行 S_IRGRP 组-读 S_IWGRP 组-写 S_IXGRP 组-执行 S_IR ...

  8. ecshop属性 {$goods.goods_attr|nl2br} 标签的赋值相关

    1.nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />). 2. 如果要向{$goods.goods_attr|nl2br}赋新值,这个值是保存 ...

  9. yii2 操作数据库

    1.查询 User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User::find()- ...

  10. (wp8.1开发)添加数据(SQLite)库到app

    wp8.1只支持SQLite. 如何添加SQLite支持请看这里 我这里要说的是如何添加自己的数据库 1.添加数据库到项目中 2.右击选择属性 3.将生成操作改成内容 4.直接就可以引用数据库文件了