java第九次作业:第九章例题3个
作业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个的更多相关文章
- JAVA第九次作业
JAVA第九次作业 (一)学习总结 1.用思维导图对javaIO操作的学习内容进行总结. 参考资料: XMind. 2.下面的程序实现了文件的拷贝,但采用的是一个字节一个字节的读写方式,效率很低.使用 ...
- Week09《java程序设计》第九次作业总结
Week09<java程序设计>第九次作业总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 答: 2. 书面作业 本次作业题集集合 1. Li ...
- 2017-2018-2 1723《程序设计与数据结构》第九周作业 & 第二周结对编程 总结
作业地址 第九次作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1878 (作业界面已评分,可随时查看,如果对自己的评分有意 ...
- 2017-2018-1 20179205《Linux内核原理与设计》第九周作业
<Linux内核原理与设计>第九周作业 视频学习及代码分析 一.进程调度时机与进程的切换 不同类型的进程有不同的调度需求,第一种分类:I/O-bound 会频繁的进程I/O,通常会花费很多 ...
- 【西北师大-2108Java】第九次作业成绩汇总
[西北师大-2108Java]第九次作业成绩汇总 作业题目 面向对象程序设计(JAVA) 第11周学习指导及要求 实验目的与要求 (1)理解泛型概念: (2)掌握泛型类的定义与使用: (3)掌握泛型方 ...
- JAVA实验报告及第九周总结
Java第九周作业 实验报告七 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负 ...
- 2018-2019-1 20189221 《Linux内核原理与分析》第九周作业
2018-2019-1 20189221 <Linux内核原理与分析>第九周作业 实验八 理理解进程调度时机跟踪分析进程调度与进程切换的过程 进程调度 进度调度时机: 1.中断处理过程(包 ...
- C#基础第九天-作业答案-储蓄账户(SavingAccount)和信用账户(CreditAccount)
class Bank { //Dictionary<long,Account> dictionary=new Dictionary<long,Account>(); DataT ...
- C#基础第九天-作业-储蓄账户(SavingAccount)和信用账户(CreditAccount)
要求1:完成以下两种账户类型的编码.银行的客户分为两大类:储蓄账户(SavingAccount)和信用账户(CreditAccount),两种的账户类型的区别在于:储蓄账户不允许透支,而信用账户可以透 ...
随机推荐
- [Xcode 实际操作]七、文件与数据-(23)UI Testing系统界面测试功能的使用
目录:[Swift]Xcode实际操作 本文将演示UI Testing系统界面测试功能的使用. 如果项目中尚未引入界面测试功能,请点击项目属性面板->[General]面板左下角的[+]图标 - ...
- Label-Free Proteomic Analysis of Exosomes Secreted from THP-1- Derived Macrophages Treated with IFN‑α Identifies Antiviral Proteins Enriched in Exosomes (文献分享一组-张霞)
文献名:Label-Free Proteomic Analysis of Exosomes Secreted from THP-1- Derived Macrophages Treated with ...
- Luogu P1074靶形数独【搜索/剪枝】By cellur925
题目传送门 显然是一个搜索.但是开始没有任何的剪枝,暴力从\((1,1)\)点开始搜索,很自然地T了6个点. #include<cstdio> #include<algorithm& ...
- 搭建hustoj现场环境
所需:就用了台普通电脑作为web以及数据库端,两台数据库实验室提供的服务器拿来做评测机. 根据提示将三台都装上hustoj 当然我是用之前比赛遗留下来的judge { 可以用以下来代替 wget ht ...
- Java | 基础归纳 | 静态方法与实例方法的区别
静态方法和实例方法的区别主要体现在两个方面: 在外部调用静态方法时,可以使用"类名.方法名"的方式,也可以使用"对象名.方法名"的方式.而实例方法只有后面这种方 ...
- mongodb数据库分片实现链接
http://www.lanceyan.com/tech/arch/mongodb_shard1.html
- [题解](最短路)luogu_P5122 Fine Dining
首先理解这里的美味值相当于给你更多时间让你经过这个草垛的, 也就是在经过草垛时可以给你的时间减少w[i],这样能否比最短路不慢 然而我们并不容易知道怎么走才是最好的,所以要想办法避免找这个方案 我们新 ...
- 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchronized关键字修饰)
不多说,直接上干货! 扩展博客 牛客网Java刷题知识点之多线程同步的实现方法有哪些 为何要使用同步? java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查 ...
- 开机启动+Linux发送邮件
需求:检测Linux上Tomcat是否允许,挂了的话给运维发送邮件通知 实现:编写脚本一直检测Tomcat进程是否存活,否则给运维发送邮件,脚本设置开机时自动启动 1.Linux发送邮件 vim /e ...
- SpringBoot实现登陆拦截
一.创建interceptor包,在interceptor中创建一个拦截器并实现HandlerInterceptor 代码: @Componentpublic class LoginHandlerIn ...