logo资料库

设计一个名为figure的图形软件包.doc

第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
资料共10页,剩余部分请下载后查看
实验 2 类和继承 1、设计一个名为 figure 的图形软件包(package)。包中包含三角形、矩形、圆三个类。要 求:(1)每个类都要构造方法并为成员设置 get 和 set 方法;(2)每个类都要有计算周 长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有 main 方法的类 中编写代码,分别使用图形软件包中的三个类,生成三个对象,并打印出其周长和面积。 代码如下: package figure; /*三角形类*/ public class Triangle { private float length1 = 9; private float length2 = 9; private float length3 = 9; public Triangle(int l1,int l2,int l3) { this.setLength1(l1); this.setLength2(l2); this.setLength3(l3); } public void setLength1(float length1) { this.length1 = length1; } public float getLength1() { return length1; } public void setLength2(float length2) { this.length2 = length2; } public float getLength2() { return length2; } public void setLength3(float length3) { this.length3 = length3; } public float getLength3() { return length3; } public double getArea() { double p=(length1+length2+length3)/2.0; return Math.sqrt(p*(p-length1)*(p-length2)*(p-length3)); }
public double getC() { return length1+length2+length3; } } package figure; /*矩形类*/ public class Rectangle { private float width = 8; private float length = 8; public Rectangle(float w, float l) { this.length = l; this.width = w; } public void setWidth(float width) { this.width = width; } public float getWidth() { return width; } public void setLength(float length) { this.length = length; } public float getLength() { return length; } public double getArea() { return length * width; } public double getC() { return 2 * (length + width); } } package figure; /*矩形类*/ public class Circular { private float len = 8; public Circular(float r) {
this.len = r; } public void setRadio(float radio) { this.len = radio; } public float getRadio() { return len; } public double getArea() { return Math.PI* Math.pow(len, 2); } public double getC() { return 2 * Math.PI * len; } } /*main 方法测试*/ package cn.edu.skd.javatest2; import figure.*; public class figure_test { public static void main(String[] args) { Triangle t = new Triangle(8, 8, 8); System.out.println("三角形的面积" + t.getArea()); System.out.println("三角形的周长" + t.getC()); Rectangle r = new Rectangle(8, 8); System.out.println("矩形的面积" + r.getArea()); System.out.println("矩形的周长" + r.getC()); Circular c = new Circular(8); System.out.println("圆形的面积" + c.getArea()); System.out.println("圆形的周长" + c.getC()); } } 运行结果如下: 2、编写类 Factorial,为其添加两个静态方法(方法名自定义)。其中一个使用递归计算 n 的阶乘,一个使用非递归计算 n 的阶乘。构造 main 方法进行测试。 代码如下: package cn.edu.skd.javatest2;
public class Factorial { public static void main(String[] args) { int n = 10; System.out.println("递归求解:10!=" + fac1(n)); System.out.println("非递归求解:10!=" + fac2(n)); } /* 递归 */ public static long fac1(int n) { if (n < 0) return -1; else if (n == 0 || n == 1) return 1; else return n * fac1(n - 1); } /* 非递归 */ public static long fac2(int n) { int sum = 1; if (n < 0) return -1; else if (n == 0 || n == 1) sum = 1; else for (int i = 1; i <= n; i++) { sum *= i; } return sum; } } 运行结果如下: 3、按照要求使用 Java 进行编码。 1) 设计一个教师类 Teacher,属性有编号(no)、姓名(name)、年龄(age)、所属学 院(seminary),为这些属性设置相应的 get 和 set 方法; 2) 为 Teacher 类添加方法 equals;(当两个教师对象的 no 相同时返回 true) 3) 为 Teacher 类添加方法 toString,通过该方法可以返回“编号为**、姓名为**、
年龄为**的**学院老师”形式的字符串。 4) 构造 main 方法进行测试。 代码如下: package cn.edu.skd.javatest2; public class Teach { main(String[] args) { public static void Teacher t1 = new Teacher("001", "戴宗", 18, "信息"); Teacher t2 = new Teacher("003", "宋江", 48, "信息"); Teacher t3 = new Teacher("003", "吴用", 39, "信息"); Teacher t4 = new Teacher("004", "林冲", 42, "信息"); if (t2.equals(t3)) { System.out.println(t2.getName() + "老师和" + t3.getName() + " 老师相同"); } else { System.out.println(t2.getName() + "老师和" + t3.getName() + " 老师不相同"); } if (t2.equals(t4)) { System.out.println(t2.getName() + "老师和" + t4.getName() + " 老师相同"); } else { System.out.println(t2.getName() + "老师和" + t4.getName() + "老 师不相同"); } System.out.println(t1.toString()); } } class Teacher { private String no;// 编号 private String name;// 姓名 private int age;// 年龄 private String seminary;// 所属院校 public Teacher(String no, String name, int age, String seminary) { this.no = no; this.name = name; this.age = age; this.seminary = seminary; } public void setNo(String no) { this.no = no; } public String getNo() {
return no; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setSeminary(String seminary) { this.seminary = seminary; } public String getSeminary() { return seminary; } public boolean equals(Teacher t) { if (this.no.toString().trim().equals(t.no.toString().trim())) return true; else return false; } public String toString() { return "编号为" + this.no + "、姓名为" + this.name + "、年龄为" + this.age + "的" + this.seminary + "的老师"; } } 运行结果如下: 4、设计一个带表头的单链表(链表中的元素属于同一类型对象,但对象的类型可以随意), 提供以下操作:(1)insert:在某个位置插入对象;(2)delete:在某个位置删除对象; (3)delete:删除链表中与 x 相同的元素;(4)size:返回当前链表中对象的个数;(5) isEmpty:判断链表是否为空;(6)traverse:遍历链表,打印出所有的元素;(7)getData: 取得某个位置的对象。构造 main 函数进行测试。 代码如下: package cn.edu.skd.javatest2;
public class LinkedList { private Node head; private int count; public LinkedList() { head = new Node(null);/* 初始化头结点 */ count = 0; } /* 在某个位置插入对象; */ public boolean insert(int i, Object o) { if (i > count || i < 0) { System.out.println("插入位置错误"); return false; } else if (i == 0) { head.setNext(new Node(o, head.getNext())); count++; return true; } else { Node n = head.getNext(); int j = 0; /* n 指向要插入位置的前一个位置 */ while ((n != null) && j < i - 1) { n = n.getNext(); j++; } n.setNext(new Node(o, n.getNext())); count++; return true; } } /* 在某个位置删除对象 */ public Object delete(int i) { if (count == 0) { return null; } if (i < 0 || i > count - 1) { return null; } else if (i == 0) {/* 删除第一个结点 */ /* 记录删除的对象,用于返回值 */ Object o = head.getNext().getData(); head.setNext(head.getNext().getNext());
count--; return o; } else {/* 删除其他结点 */ Node h = head;/* 记录头结点 */ int j = 0; while ((h.getNext() != null) && j < i) { h = h.getNext();/* h 指向要删除结点的前一个结点 */ j++; } Object o = h.getNext().getData(); h.setNext(h.getNext().getNext()); count--; return o; } } /* 删除链表中与 o 相同的元素 */ public Object delete(Object o) { if (this.head == null || this.head.getNext() == null) { return null; } Node n = head.getNext();/* n 指向第一个结点 */ Node p = head;/* 记录 n 指向结点的前一个结点 */ while (n != null) { if (n.getData().equals(o)) { p.setNext(n.getNext()); count--; return n.getData(); } else { p = n;/* 记录 n 指向结点的前一个结点 */ n = n.getNext();/* n 指向下一个结点 */ } } return null;/* 没有相同元素 */ } /* 返回当前链表中对象的个数 */ public int size() { return count; } /* 判断链表是否为空 */ public boolean isEmpty() { return count == 0 ? true : false;
分享到:
收藏