zoukankan      html  css  js  c++  java
  • 第6次作业--static关键字、对象

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

    代码:

    /** 创建包Factorial 类Computer 定义一个整形变量k赋值为1,创建方法Factorial传入值n控制循环次数,利用for循环计算阶乘,返回K*/

    package
    Factorial; public class Computer { static int k=1; public static int Factorial (int n) { for(int i=1;i<=n;i++) { k*=i; } return k; } }
    /** 创建包Factorial1_2,导入包Factorial的Computer类,创建主方法,用户输入数字,声明对象传入值num*/
    package Factorial1_2;
    import Factorial.Computer;
    import java.util.*;
    
    public class Computer1_2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("输入数字");
            Scanner reader = new Scanner(System.in);
            int num = reader.nextInt();
            Computer s1 = new Computer( );
            System.out.println("阶乘为"+s1.Factorial(num));
    
        }
    
    }

    运行结果:

    题目2:

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

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

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

    代码:

    package Example;
    public class Coordinates {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyPoint num1 = new MyPoint(1,2);
            MyPoint num2 = new MyPoint(3,4); //在主方法中传入两个坐标
            
           
            
            System.out.println("两点的距离为:"+MyPoint.distance(num1, num2));//输出结果,将两个坐标传入distance方法
    
        }
    
    }
    class MyPoint{
        private double x;
        private double y;  //私有成员变量X和Y
        public MyPoint(){
            x = 0;
            y = 0;      //无参构造方法创建点0,0
        }
        public MyPoint(double x,double y){
            this.x = x;
            this.y = y;
        }                //有参构造方法
        
        //成员变量x和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;
        }
        public static double distance(MyPoint n,MyPoint k) {     //创建方法传入n,k
            double x1 = n.getX();
            double x2 = k.getX();
            double y1 = n.getY();
            double y2 = k.getY();                    //将值赋值给x1x2y1y2;
            return  Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));  //利用公式计算
        }
            
        }

    运行结果:

  • 相关阅读:
    一步一步教你使用Jmail实现邮件的接收与发送
    超级简单:使用FOR XML AUTO控制XML输出
    WF4.0工作流设计器快捷键
    糟糕编程的白痴指南
    WPF:全文翻译小工具
    获取为以逗号分隔列值的字符串
    WF 4.0 RC 学习资源
    这个杀手不太冷
    《Expert Cube Development with Microsoft SQL Server 2008 Analysis Services》读书笔记第九章:保护Cube(二)
    windows server 2008下无法检查到无线信号的解决方法
  • 原文地址:https://www.cnblogs.com/xushaohua/p/11561649.html
Copyright © 2011-2022 走看看