java第十二周作业
l、定义一个点类Point,包含2个成员变量x、y分 别表示x和y坐标,2个构造器Point和Point(int x0,yO),以及一个movePoint (int dx,int dy)方法实 现点的位置移动,创建两个Point对象pl、p2,分 别调用movePoint方法后,打E印plp2的坐标。[ 必作题]
package kg522; import java.util.Scanner; public class kg5221 { static int p=0; static int o=0; public static void di() { Scanner input=new Scanner(System.in); System.out.println("1:移动"); System.out.println("2:查询位置"); System.out.println("3:退出系统"); int b=input.nextInt(); if(b==1) move(); if(b==2) wz(); if(b==3) System.out.println("您已退出,欢迎下次使用"); } public static void wz() { System.out.println("点的位置在"+"("+o+","+p+")"); } public static void move() { System.out.println("请输入移动的方向"); System.out.println("8为向上"); System.out.println("2为向下"); System.out.println("4为向左"); System.out.println("6为向右"); System.out.println("输入5终止移动"); Scanner input=new Scanner(System.in); for(int c=0;c<10000000;c++){ int a=input.nextInt(); if(a==2){ System.out.println("向下移动"); o=o-1; } if(a==4){ System.out.println("向左移动"); o=o+1; } if(a==6){ System.out.println("向右移动"); p=p+1; } if(a==8){ System.out.println("向上移动"); p++; } if(a==5) break; } System.out.println("终止移动"); System.out.println("1:返回首页"); System.out.println("2:继续移动"); int z=input.nextInt(); if(z==1) di(); if(z==2) move(); } public static void main(String[] args) { System.out.println("恭喜您创建了一个点"); di(); } }
2、定义一个矩形类Rectangle:(知识点:对象的 创建和使用)[必做题]
2.1定义三个方法:getArea(求面积、getPer()求 周长,showAll()分别在控制台输出长、宽、面积、周长。
2.2有2个属性:长length、宽width
2.3通过构造方法Rectangle(int width, int length), 分别给两个属性赋值
2.4创建一个Rectangle对象,并输出相关信息
package oop; public class cfx{ int m; int z; int l; int k; public cfx(int m,int z,int l,int k){ this.k=k; this.l=l; this.m=k*l; this.z=2*l+2*k; } public void show(){ System.out.println("长"+l+"宽"+k+"面积"+k*l+"周长"+(2*l+2*k)); } public void mz(){ m=k*l; System.out.println("面积"+m); } public void zc(){ z=2*l+2*k; System.out.println("周长为"+z); } public cfx(int l,int k) { this.k=k; this.l=l; } }
package oop; public class cfx1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub cfx c=new cfx(3, 4); c.show(); c.zc(); c.mz(); } }
3、定义一个笔记本类,该类有颜色(char)和cpu 型号(int)两个属性。[必做题]
3.1无参和有参的两个构造方法,有参构造方法可 以在创建对象的同时为每个属性赋值;
3.2输出笔记本信息的方法
3.3然后编写一个测试类,测试笔记本类的各个 方法。
package oop; public class book { char c; int b; int j; public book(int b,char c,int j){ this.c=c; this.b=b; this.j=j; } public void show(){ System.out.println("颜色为"+c+"型号"+b+"单价"+j); } public book(){ } }
package oop; public class book2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub book m=new book(5, '黄', 2); book m1=new book(); m1.b=1; m1.c='红'; m1.j=1; m.show(); m1.show(); } }