Java 0520 第五次课作业

223633_qg9t_3858331.png

package org.jsoft.quanxian;

public class Point {
		private int x;
		public int getX() {					//get方法
			return x;
		}
		public void setX(int x) {			//set方法
			this.x = x;
		}
		public int getY() {
			return y;
		}
		public void setY(int y) {
			this.y = y;
		}
		private int y;
		Point(int x,int y){					//构造方法
			this.x=x;
			this.y=y;
		}
		public void diaplay(){				//输出方法
			System.out.println("The point is at:"+"("+x+","+y+")");
		}
}
package org.jsoft.quanxian;

public class TestPoint {

	public static void main(String[] args) {
		Point point = new Point(100,42);			//创建对象
		point.diaplay();							//调用方法
	}

}

223907_bpa8_3858331.png

223934_4WvQ_3858331.png

package org.jsoft.quanxian;

public class Rectangle {
		private Point point;
		public Point getPoint() {					//get方法
			return point;
		}
		public void setPoint(Point point) {			//set方法
			this.point = point;
		}
		public void setPoint(int x,int y) {
			point.setX(x);
			point.setY(y);
		}
		public int getWidth() {
			return width;
		}
		public void setWidth(int width) {
			this.width = width;
		}
		public int getHeight() {
			return height;
		}
		public void setHeight(int height) {
			this.height = height;
		}
		private int width;
		private int height;
		Rectangle(Point p,int width,int height){		//构造方法修改点坐标
				point=p;
				point.setX(10);
				point.setY(4);
				this.width=width;
				this.height=height;
		}
		Rectangle(Point p,int x,int y,int width,int height){		//构造方法重载修改点坐标
				point=p;
				point.setX(x);
				point.setY(y);
				this.width=width;
				this.height=height;
		}
		Rectangle(Rectangle r){}
		Rectangle(){}
		public int getArea(){						//求面积方法
			return width*height;
		}
		public void display(){						//输出宽和长
			System.out.println("宽和长:"+width+","+height);
		}
}
package org.jsoft.quanxian;

public class TestRectangle {
			public static void main(String[] args) {
				Point p = new Point(100,42);			//创建点对象
				Rectangle r = new Rectangle();          //创建矩形对象无参			
				r.display();							//调用方法
				Rectangle r1 = new Rectangle(r);		//创建矩形对象引用类型参数
				r1.display();
				Rectangle r2 = new Rectangle(p,30,40);	//创建矩形对象引用类型参数和长宽
				r2.display();
				p.diaplay();
				System.out.println("面积为:"+r2.getArea());
				Rectangle r3 = new Rectangle(p,10,2,3,4);//创建矩形对象引用类型参数和点坐标和长宽
				r3.display();
				p.diaplay();				
				System.out.println("面积为:"+r3.getArea());
			}
}

224055_yEuW_3858331.png

选择题

1,B

2,A D

3,C

4,C

5,C

6,D

7,D

8,A

9,A

10,C

11,D

12,C

13,B

14,B

15,C

16,B

17,A D

18,D

19,C

20,C

填空题

1,true flase

2,1

3,private

4,new

5,方法 抽象类

6,interface  public abstract

7,private

8,String    StringBuffer

9,super

10,length()

11,方法名 返回值 参数列表 

12,345

13,def

14,46888

15,20

     0

     30

 编程题

1.求1~100之间不能被3整除数的和。(使用for循环结构)10分

package org.jsoft.test;

public class ProblemOne {
			public static void main(String[] args) {
				int num=0;
				for(int i=1;i<=100;i++){		//循环
					if(i%3!=0){					//判断不能被三整除
						num=num+i;				//累加
					}
				}
				System.out.println(num);		//输出
			}	
}

2.请写程序,实现对数组{ 25,24,12,76,101}从小到大的排序。(使用冒泡排序,并打印每次排序结果)10分

package org.jsoft.test;

public class ProblemTwo {

	public static void main(String[] args) {
				int [] a = {25,24,12,76,101};
				for(int i=0;i<a.length-1;i++){				//冒泡排序
					for(int j=0;j<a.length-1-i;j++){
						int temp=0;
						if(a[j]>a[j+1]){
							temp=a[j+1];
							a[j+1]=a[j];
							a[j]=temp;
						}
					}
				}
				for(int z=0;z<a.length;z++){				//循环输出
					System.out.print(a[z]+(" "));
				}
	}

}

3.已知函数  225503_FyQY_3858331.png

请设计一个方法实现上面函数,根据传入的值x的不同,返回对应的y值。

① 定义一个方法,接收一个int类型的参数,返回值为int类型。

② 在方法中使用个if else if else语句针对x的值进行三种情况判断。

③ 根据判断结果执行不同的表达式,将结果赋予变量y。

④ 在方法的最后返回y。

在main方法中调用设计好的方法,传入一个int型的值,将方法的返回值打印。

package org.jsoft.test;

public class ProblemThree {
	public static int count(int x){			//计算方法传整形参数
		int y;
		if(x>0){                            //判断x大于0					
			y=x+3;
		}else if(x==0){						//判断x等于0
			y=0;
		}else{								//判断x小于0
			y=x-1;
		}
		return y;							//返回y
	}
	public static void main(String[] args) {
		System.out.println(count(10));		//输出
	}

}

 

转载于:https://my.oschina.net/u/3858331/blog/1816963

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值