java的第六次作业

 

第一题

一.题目:

编写一个类Computer,类中含有一个求n的阶乘的方法。

将该类打包,并在另一包中的java文件App.java中引入包,在主类中定义Computer类的对象,调用求n的阶乘的方法。

 

二.代码及注释:

1.computer类:

package qiujiecheng; //创建一个包
public class computer {    //定义一个类名

/**
* @param args
*/

// TODO Auto-generated method stub
public long getqiujiecheng(long x){  //求阶乘的方法
long t=1;
for(int i=1;i<=x;i++){
t*=i;
}
return t;
}
}

2.App类:

package APP;//创建一个包
import java.util.Scanner; 导入包
import qiujiecheng.computer;
public class app {  //定义一个类名

/**
* @param args
*/
public static void main(String[] args) {    //制定主方法
// TODO Auto-generated method stub
System.out.println("请输入一个整数:"); //进行输入
Scanner reader=new Scanner(System.in); //利用Scanner类创造对象
computer k=new computer();  //创建对象
long t=reader.nextLong(); //输入t
System.out.println(k.getqiujiecheng(t));  //通过类名访问类方法
}

}

三.运行结果:

 

第二题:

一.题目:

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

(1)两个私有变量x和y表示坐标值;

(2)成员变量x和y的访问器和修改器;

(3)无参构造方法创建点(0,0);

(4)一个有参构造方法,根据参数指定坐标创建一个点;

(5)distance方法(static修饰)返回参数为MyPoint类型的两个点对象之间的距离。

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

二. 代码及注释:

 
  
package cn.edu.ccut.pr;//创建一个包

public class MyPoint { //定义一个类名
     double x;  //建立横坐标
     double y;//建立纵坐标
    /**
     * @param args
     */
    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(double x,double y){
        this.x=x;
        this.y=y;
    }
static double distance(MyPoint p1,MyPoint p2){//计算两者之间的距离
    double x1=p1.getX();
    double x2=p2.getX();
    double Y1=p1.getY();
    double Y2=p1.getY();
    return Math.sqrt((x1-x2)*(x1-x2)+(Y1-Y2)*(Y1-Y2));//返回距离值
}



    public static void main(String[] args){//制定主方法
        MyPoint p1=new MyPoint(6,2);//创建p1对象
        MyPoint p2=new MyPoint(8,56);//创建p2对象
        double distance=MyPoint.distance(p1,p2);
        System.out.println("两点间距离为:"+distance);//输出两点间距离
    }
    }

三.运行结果:

 

转载于:https://www.cnblogs.com/shuang123/p/11544477.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您解答。 首先,我们需要在布局文件中添加一个 RelativeLayout 控件: ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 这里添加九宫格子的布局 --> </RelativeLayout> ``` 然后,我们需要在 RelativeLayout 中添加九宫格子的布局。这里我们采用 ImageView 控件作为九宫格子,每个格子的大小相同,可以采用权重的方式实现。 ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image1" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_weight="1" android:src="@drawable/image1" /> <ImageView android:id="@+id/image2" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_alignParentTop="true" android:layout_toRightOf="@id/image1" android:layout_toEndOf="@id/image1" android:layout_weight="1" android:src="@drawable/image2" /> <ImageView android:id="@+id/image3" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_alignParentTop="true" android:layout_toRightOf="@id/image2" android:layout_toEndOf="@id/image2" android:layout_weight="1" android:src="@drawable/image3" /> <ImageView android:id="@+id/image4" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_below="@id/image1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_weight="1" android:src="@drawable/image4" /> <ImageView android:id="@+id/image5" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_below="@id/image2" android:layout_toRightOf="@id/image4" android:layout_toEndOf="@id/image4" android:layout_weight="1" android:src="@drawable/image5" /> <ImageView android:id="@+id/image6" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_below="@id/image3" android:layout_toRightOf="@id/image5" android:layout_toEndOf="@id/image5" android:layout_weight="1" android:src="@drawable/image6" /> <ImageView android:id="@+id/image7" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_below="@id/image4" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_weight="1" android:src="@drawable/image7" /> <ImageView android:id="@+id/image8" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_below="@id/image5" android:layout_toRightOf="@id/image7" android:layout_toEndOf="@id/image7" android:layout_weight="1" android:src="@drawable/image8" /> <ImageView android:id="@+id/image9" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="5dp" android:layout_centerInParent="true" android:layout_below="@id/image6" android:layout_toRightOf="@id/image8" android:layout_toEndOf="@id/image8" android:layout_weight="1" android:src="@drawable/image9" /> </RelativeLayout> ``` 在上述布局中,我们使用了 `android:layout_weight` 属性来实现九宫格子的等分布局,每个 ImageView 控件的权重都设置为 1,这样每个格子的大小就相等了。同时,我们使用了 `android:layout_centerInParent` 属性来使每个格子都居中显示,使用了 `android:layout_alignParentTop` 和 `android:layout_alignParentLeft`(或 `android:layout_alignParentStart`)属性来使第一个格子位于 RelativeLayout 控件的左上角,使用了 `android:layout_below`、`android:layout_toRightOf` 和 `android:layout_toEndOf` 属性来设置每个格子的位置。 最后,我们可以在 Java 代码中获取每个 ImageView 控件,并为它们设置点击事件,以实现九宫格的功能。 ```java ImageView image1 = findViewById(R.id.image1); ImageView image2 = findViewById(R.id.image2); ImageView image3 = findViewById(R.id.image3); ImageView image4 = findViewById(R.id.image4); ImageView image5 = findViewById(R.id.image5); ImageView image6 = findViewById(R.id.image6); ImageView image7 = findViewById(R.id.image7); ImageView image8 = findViewById(R.id.image8); ImageView image9 = findViewById(R.id.image9); image1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理第一个格子的点击事件 } }); image2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理第二个格子的点击事件 } }); // 其他格子的点击事件同理 ``` 好了,以上就是使用相对布局实现九宫格的方法。希望对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值