jhy第六次作业
题目1:在作业5的基础上,再创建一个柱体类,包含矩形对象、高和体积等三个成员变量,一个构造方法进行成员变量初始化,和计算体积、换底两个功能方法,在主类中输入长、宽、高,计算柱体体积,输入新的长、宽、高,创建新的矩形对象,并利用换底方法换底,再次计算柱体体积。
阶乘包
package app.java;
public class Computer {
int jc(int n) {
int ji=1;
int i;
for(i=1;i<n;i++) {
ji*=i;
}
return ji;
}
}
int jc(int n) {
int ji=1;
int i;
for(i=1;i<n;i++) {
ji*=i;
}
return ji;
}
}
主类包
package app.java;
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("输入操作数n:");
Computer computer=new Computer();
int zongji=computer.jc(6);
System.out.println(zongji);
}
// TODO Auto-generated method stub
System.out.println("输入操作数n:");
Computer computer=new Computer();
int zongji=computer.jc(6);
System.out.println(zongji);
}
}
![](https://img2018.cnblogs.com/blog/1791084/201909/1791084-20190922135946549-1166811200.png)
题目2:设计名为MyInteger的类,它包括: int型数据域value 一个构造方法,当指定int值时,创建MyInteger对象 数据域value的访问器和修改器 isEven( )和isOdd( )方法,如果当前对象是偶数或奇数,返回true 类方法isPrime(MyInteger i),判断指定的值是否为素数,返回true 在主类中创建MyInteger对象,验证MyInteger类中各方法。
MyPoint类
ackage app.java;
public class Mypoint {
double x;
double y;
public double getX(){// 建立访问器
return x;
}
public void setX(double x){// 建立修改器
this.x=x;
}
public double getY(){
return y;
}
public void setY(double y){
this.y=y;
}
Mypoint(){// 无参构造方法
x=0;
y=0;
}
public Mypoint(double x,double y){// 有参构造方法
this.x=x;
this.y=y;
}
public static double distance(Mypoint a1,Mypoint b1){
double x1=a1.getX();
double x2=b1.getX();
double y1=a1.getY();
double y2=b1.getY();
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
}
double x;
double y;
public double getX(){// 建立访问器
return x;
}
public void setX(double x){// 建立修改器
this.x=x;
}
public double getY(){
return y;
}
public void setY(double y){
this.y=y;
}
Mypoint(){// 无参构造方法
x=0;
y=0;
}
public Mypoint(double x,double y){// 有参构造方法
this.x=x;
this.y=y;
}
public static double distance(Mypoint a1,Mypoint b1){
double x1=a1.getX();
double x2=b1.getX();
double y1=a1.getY();
double y2=b1.getY();
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
}
Test主类
package app.java;
import java.util.Scanner;
public class Text1 {
private static Scanner reader;
public static void main(String[] args) {
// TODO Auto-generated method stub
double x1,y1,x2,y2;
reader = new Scanner(System.in);
System.out.println("输入第一个横坐标的值为");
x1=reader.nextDouble();
System.out.println("输入第一个纵坐标的值为");
y1=reader.nextDouble();
System.out.println("输入第二个横坐标的值为");
x2=reader.nextDouble();
System.out.println("输入第二个纵坐标的值为");
y2=reader.nextDouble();
Mypoint p1=new Mypoint(x1,y1);
Mypoint p2=new Mypoint(x2,y2);
System.out.println("两点之间的距离是:"+Mypoint.distance(p1, p2));//输出
}
// TODO Auto-generated method stub
double x1,y1,x2,y2;
reader = new Scanner(System.in);
System.out.println("输入第一个横坐标的值为");
x1=reader.nextDouble();
System.out.println("输入第一个纵坐标的值为");
y1=reader.nextDouble();
System.out.println("输入第二个横坐标的值为");
x2=reader.nextDouble();
System.out.println("输入第二个纵坐标的值为");
y2=reader.nextDouble();
Mypoint p1=new Mypoint(x1,y1);
Mypoint p2=new Mypoint(x2,y2);
System.out.println("两点之间的距离是:"+Mypoint.distance(p1, p2));//输出
}
![](https://img2018.cnblogs.com/blog/1791084/201909/1791084-20190922140324559-312660480.png)