《实验1:熟悉开发环境、类与对象的设计和使用(教案)》由会员分享,可在线阅读,更多相关《实验1:熟悉开发环境、类与对象的设计和使用(教案)(10页珍藏版)》请在金锄头文库上搜索。
1、实验1:熟悉开发环境、类与对象的设计和使用实验目的:1、 熟悉eclipse开发环境。2、 熟练使用eclipse向导生成程序框架。3、 掌握类及类的成员、构造方法、创建对象、方法调用、引用等概念。4、 初步了解代码重构,并进行相应重构的设计;了解抽象类、接口的相关概念。实验内容:一、熟悉开发环境,完成完成简单面向对象程序设计。1.新建源程序文件Student.java具体操作步骤:在Project Explorer面板中,选择其中一个已经存在Java Application项目,如AppPors,并于右键弹出菜单中选择New-Class。出现New Java Class对话框。在其中Name
2、的文本输入框中键入待定义的类的名字:Student;并勾选public static void main(String args)选项框。注意,对话框中其它选项可不作改动。点击Finish按钮。A通过输入的方式获得“张三”import java.io.*;import java.util.Scanner;public class Student String name; public Student(String name)/类one的构造方法 this.name=name;public void showName() System.out.println(Hello!+name); publ
3、ic static void main(String args) Scanner scan = new Scanner(System.in);String s= scan.next();Student h= new Student(s); h.showName(); B通过字符串的形式获得“张三”public class Student String name; public Student(String name) this.name=name;public void showName() System.out.println(Hello!+name); public static void
4、 main(String args) String s= 张三;Student h= new Student(s); h.showName(); 2、在划线处填写适当代码、完成程序功能。3.按下述要求进行程序设计:保留上述Point类,并新增类Line。其中:l Line的field成员为:private Point p1;private Point p2;l Line的method成员为:1.平移两点的X坐标,平移值为shiftpublic void shiftX(int shift)2.计算两点之间的距离public double getLength()其中可使用Math类的方法:返回nu
5、m的n次方:方法原型为:static double pow(double num,double n)返回num的平方根:方法原型为:static double sqrt(double num)l Line的构造方法为:Line(Point p1, Point p2)类设计完成后,在main方法中创建Line的对象,并通过对该对象的方法调用,计算出点(0,0)和点(2,2)之间的距离,并将该线在X轴方向平移6.输出平移后的两个点的X值。public class Operation public static void main(String args) Point p1 = new Point(
6、0,0);Point p2 = new Point(2,2);Line line = new Line(p1,p2);System.out.println(距离为:+line.getLength();line.shiftX(6);System.out.println(终点移动6后的X值为:+p2.getX();System.out.println(距离为:+line.getLength();class Lineprivate Point p1;private Point p2;Line(Point p1, Point p2)this.p1.s = p1.s;this.p2 = p2;publi
7、c double getLength()return Math.sqrt(Math.pow(p2.getY()-p1.getY(),2) + Math.pow(p2.getX()-p1.getX(),2) ) ;public void shiftX(int shift) p1.setX(p1.getX()+shift); p2.setX(p2.getX()+shift);class Pointprivate int x,y;Point(int x,int y)this.x = x;this.y = y;public int getX()return x;public int getY()ret
8、urn y;public void setX(int xvalue)x=xvalue;距离为:2.8284271247461903终点移动6后的X值为:8距离为:2.8284271247461903可改进:采用Point类增加方法public void shiftX(int x)class Pointprivate int x,y;Point(int x,int y)this.x = x;this.y = y;public int getX()return x;public int getY()return y;public void setX(int xvalue)x=xvalue;publ
9、ic void shift(int x,int y)this.x=this.x+x;this.y=this.y+y;public void shiftX(int x)this.x=this.x+x;public void shiftY(int y)this.y=this.y+y;class Lineprivate Point p1;private Point p2;Line(Point p1, Point p2)this.p1 = p1;this.p2 = p2;public double getLength()return Math.sqrt(Math.pow(p2.getY()-p1.ge
10、tY(),2) + Math.pow(p2.getX()-p1.getX(),2) ) ;public void shiftX(int shift) p1.shiftX(shift); p2.shiftX(shift);继续改进:增加一个抽象类Shapeabstract class Shapeabstract void shift(int x,int y);abstract void shiftX(int x);abstract void shiftY(int y);重构程序:class Line extends Shapeabstract class Shapeclass Point ext
11、ends Shape源程序如下:public class testpublic static void main(String args) Point p1 = new Point(0,0);Point p2 = new Point(2,2);Line l = new Line(p1,p2);System.out.println(l.getLength();l.shift(2,2);System.out.println(l.getLength();abstract class Shapeabstract void shift(int x,int y);abstract void shiftX(
12、int x);abstract void shiftY(int y);class Point extends Shapeprivate int x,y;void shiftX(int x)this.x=this.x+x;void shiftY(int y)this.y=this.y+y;void shift(int x,int y)this.x=this.x+x;this.y= this.y+y;Point(int x,int y)this.x = x;this.y = y;public int getX()return x;public int getY()return y;public v
13、oid setX(int xvalue)x=xvalue;class Line extends Shapeprivate Point p1;private Point p2;void shiftY(int y)p2.shiftY(y);void shift(int x,int y)p1.shift(x,y);p2.shift(x,y);Line(Point p1,Point p2)this.p1= p1;this.p2= p2;public void shiftX(int shift)p1.shiftX(shift);public double getLength()return Math.sqrt(Math.pow (p2.getX()-p1.getX(),2)+