面向对象-第六次作业

题目1:

编写一个类Computer,类中含有一个求n的阶乘的方法。将该类打包,并在另一包中的Java文件App.java中引入包,在主类中定义Computer类的对象,调用求n的阶乘的方法(n值由参数决定),并将结果输出。

代码:

computer.java:

package factorial;
public class Computer {
    int count = 1;
    public  int inputcount(int n) {
        for (int i = 1; i<=n; i++) {
            count = count * i;
        }
        return count;
    }

}

app.java:

import factorial.Computer;
import java.util.*;
public class App {
    public static void main(String[] args) {
        System.out.print("请输入n的值:");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        Computer count = new Computer();
        n = count.inputcount(n);
        
        System.out.println("阶乘为:"+n);
        
        
        
    }
}

 

运行结果:

 


题目2:

设计一个MyPoint类,表示一个具有x坐标和y坐标的点,该类包括:

  • 两个私有成员变量x和y表示坐标值;
  • 成员变量x和y的访问器和修改器
  • 无参构造方法创建点(0,0);
  • 一个有参构造方法,根据参数指定坐标创建一个点;
  • distance方法(static修饰)返回参数为MyPoint类型的两个点对象之间的距离。

        编写主类Test,在主类中输入两点坐标,创建两个点对象,利用distance()方法计算这两个点之间的距离。

代码:

my

Mypoint.java:

package factorial;

public class MyPoint {
        double x;
        double y;
        public double getX() {     //访问器x
            return x;
        }
        public void setX(double x){   //修改器x
            this.x = x;
        }
        public double getY() {      //访问器y
            return y;
        }
        public void setY(double y){  //访问器y
            this.y = y;
        }
         MyPoint() {                          //无参构造方法
            x = 0;
            y = 0;
        }
         MyPoint(double x,double y){   //用户录入的位置
            this.x = x;
            this.y = y;
        }
        public static double distance(MyPoint p1,MyPoint p2) {    //返回两点间的距离
            double x1 =  p1.getX();
            double y1=  p1.getY();
            double x2 = p2.getX();
            double y2 = p2.getY();
            return(Math.sqrt((x1 - x2) * (x1 - x2) * (y1 - y2) * (y1 - y2)));
            
            
        }
}

Test.java:

package factorial;
import factorial.MyPoint;
import java.util.*;

public class Test {

    public static void main(String[] args) {
        System.out.println("请输入第一个坐标:");
        Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();    //坐标x1
        double y1 = input.nextDouble();    //坐标y1
        System.out.println("请输入第二个坐标:");
        double x2 = input.nextDouble();   //坐标x2
        double y2 = input.nextDouble();    //坐标y2
        MyPoint p1 = new MyPoint(x1,y1);  //录入p1的坐标
        MyPoint p2 = new MyPoint(x2,y2);  //录入p2的坐标
        double z =  MyPoint.distance(p1, p2);    //p1和p2之间的距离
        System.out.println("两个坐标的距离为:"+z);
        

    }

 

运行结果:

转载于:https://www.cnblogs.com/DandelionRain/p/11565821.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值