Java实⽤程序设计课后习题西安电⼦科技⼤学
⽂章⽬录
第⼆章
3.尝试将下表中的浮点数相除运算填写完整
1. 说明:
这⾥的有穷正数和有穷负数,我分别使⽤的是100和-100来表⽰,还有就是Infinity和NaN,我使⽤的是diea,可以直接使⽤,⽽我同学使⽤的是eclipse,他们不能正确显⽰,可以使⽤第⼆⼗⾄⼆⼗⼀页⾥⾯的来替代
2. 代码
import static java.lang.Double.NaN;
import static jdk.nashorn.internal.objects.Global.Infinity;
import java.util.ArrayList;
import java.util.List;
public class Test23 {
public static void main(String[] args) {
List<Double> list = new ArrayList<>();
list.add(100.0);
list.add(-100.0);
list.add(+0.0);
list.add(-0.0);
list.add(Infinity);
list.add(-Infinity);
list.add(NaN);
for (int i = 0; i < list.size(); i++) {
double x = (i);
for (int i1 = 0; i1 < list.size(); i1++) {
double y = (i1);
System.out.println(x + "除" + y + "的结果是" + x / y);
} } }
}
19.吸⾎⿁数字是指位数为偶数的数字,可以由⼀对数字相乘得到,这对数字各包含乘积的⼀半位数的数字,数字选取后可任意排序。
1. 说明
观察可以得到吸⾎⿁数字的特征,可以发现吸⾎⿁数字最多只能有两个0,所以可以依此来对数字进⾏⼀定的筛选,先将四位数的每⼀位数字提取出来,然后限制⼗位和个位不同时为零,接着对四个数字
进⾏组合并与原四位数进⾏⽐较,从⽽出⽬标数字
2. 代码
import java.util.ArrayList;
import java.util.List;
public class Test219 {
public static void main(String[] args) {
int a,b,c,d;
List<Integer> arrays = new ArrayList<>();
for(int i = 1000;i<9999;i++){
a = i/1000;
b = i/100 - a*10;
c = i/10 -a*100 -b*10;
d = i - a*1000 - b*100 - c*10;
if((c+d)!=0) {
if((b*10+a)*(c*10+d)==i){
System.out.println(i + "=" + b + a + "*" + c +d);
}
if((c*10+a)*(b*10+d)==i){
System.out.println(i+ "=" + c + a + "*" + b +d);
}
if((a*10+d)*(c*10+b)==i){
System.out.println(i+ "=" + a + d + "*" + c +b);
} } } }
}
第三章
2.创建⼀个NewRectangle类
1. 说明
这个题⽐较简单,根据要求来即可
2. 代码
public class NewRectangle {
double width,height;
public NewRectangle() {
this.height = 0.0;
this.width = 0.0;
}
public NewRectangle(double width, double height) { this.width = width;
this.height = height;
}
public double getArea(){
return width*height;
}
public double getPerimeter(){
return width+height;
}
}
3.在第⼆题的基础上,完成以下程序1.定义Point类
public class Point {
double x,y;
public Point() {
x = 0.0;
y = 0.0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double distance(Point p){
double result = Math.pow(x-p.x, 2) + Math.pow(y-p.y,2);
return Math.sqrt(result);
}
}
2.修改第⼆题实现的NewRectangle类,加⼊⼀个Point类型的成员这⾥2,3,4,5写在⼀起
public class NewRectangle1 {
double width,height;
Point p;
public NewRectangle1() {
this.height = 0.0;
this.width = 0.0;
this.p.x = 0.0;
this.p.y = 0.0;
}
public NewRectangle1(double width, double height) {
this.width = width;
this.height = height;
}
public NewRectangle1(double width, double height,double x,double y) {
this.width = width;
this.height = height;
this.p.x = x;
this.p.y = y;
}
public double getArea(){
return width*height;
}
public double getPerimeter(){
return width+height;
}
boolean bPointIn(Point p){
return this.p.x < p.x
&& p.x < this.p.x + this.width
&& this.p.y < p.y
&& p.y < this.p.y + this.height;
}
public void includeOrOverlap(NewRectangle1 newRectangle1){
if (bPointIn(newRectangle1.p)){
if (newRectangle1.p.x + newRectangle1.width < this.p.x + this.width
&&newRectangle1.p.y + newRectangle1.height < this.p.y + this.height){
System.out.println("当前矩形包含另⼀个矩形");
}else {
System.out.println("两个矩形有重叠部分");
java程序设计基础视频}
}else {
System.out.println("两个矩形既不存在重叠也不存在包含关系");
}
}
}
9.创建⼀个⽗类cycle,再创建他的三个⼦类Unicycle Bicycle和Tricycle
1. 这⾥的三个⼩问我就写在⼀起
2. 代码
public class Cycle {
int wheels;
public String name;
public Cycle(String str){
name = str;
}
static Unicycle unicycle = new Unicycle("Unicycle"); public int wheel(){
return this.wheels;
}
public static void ride(Cycle cycle){
cycle.wheel();
if (cycle instanceof Unicycle){
((Unicycle) cycle).balance();
}
if (cycle instanceof Bicycle){
((Bicycle) cycle).balance();
}
}
public static void main(String[] args) {
Unicycle unicycle = new Unicycle("Unicycle");
Bicycle bicycle = new Bicycle("Bicycle");
Tricycle tricycle = new Tricycle("Tricycle");
ride(unicycle);
ride(bicycle);
ride(tricycle);
}
}
public class Tricycle extends Cycle {
int wheels = 3;
private String name;
public Tricycle(String str) {
super(str);
name = str;
}
@Override
public int wheel() {
System.out.println("Tricycle的车轮数为:"+ wheels); return this.wheels;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论