【Java】实验二
1、设计一个名为figure的图形软件包(package)。包中包含三角形、矩形、圆三个类。要求:(1)每个类都要构造方法并为成员设置get和set方法;(2)每个类都要有计算周长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有main方法的类中编写代码,分别使用图形软件包中的三个类,生成三个对象,并打印出其周长和面积。
Triangle.java
1 package com.ly.figure;
2 public class Triangle{
3 private double edge1;
4 private double edge2;
5 private double edge3;
6
7 public Triangle(double eg1,double eg2,double eg3){
8 edge1=eg1;
9 edge2=eg2;
10 edge3=eg3;
11 }
12 public double getEdge1(){
13 return edge1;
14 }
15 public double getEdge2(){
16 return edge2;
17 }
18 public double getEdge3(){
19 return edge3;
20 }
21 public void setEdge(double eg1,double eg2,double eg3){
22 edge1 = eg1;
23 edge2 = eg2;
24 edge3 = eg3;
25 }
26 public double Perimeter(){
27 return edge1+edge2+edge3;
28 }
29 public double Square(){
30 double s = (edge1+edge2+edge3)/2;
31 double area=Math.sqrt((double)(s*(s-edge1)*(s-edge2)*(s-edge3)));
32 return area;
33 }
34 };
Rectangle
1 package com.ly.figure;
2 public class Rectangle {
3 private double length;
4 private double width;
5
6 public Rectangle(double len,double wid){
7 length=len;
8 width=wid;
9 }
10 public double getLength(){
11 return length;
12 }
13 public double getWidth(){
14 return width;
15 }
16 public void setLength(double len){
17 length=len;
18 }
19 public void setWidth(double wid){
20 width=wid;
21 }
22 public double Perimeter(){
23 return 2*(length+width);
24 }
25 public double Square(){
26 return length*width;
27 }
28 }
Circle.java
1 package com.ly.figure;
2 public class Circle {
3 private static final double PI = 3.141596;
4 private double radius;
5
6 public Circle(double r){
7 radius=r;
8 }
9 public double getRadius(){
10 return radius;
11 }
12 public void setRadius(double r){
13 radius=r;
14 }
15 public double Perimeter(){
16 return (2*PI*radius);
17 }
18 public double Square(){
19 return PI*(radius*radius);
20 }
21 }
FigureTest.java
1 package com.ly.test;
2 import com.ly.figure.*;
3
4 public class FigureTest {
5 public static void main(String[] args) {
6 //三角形
7 Triangle triangle = new Triangle(3,3,2);
8 System.out.println("三角形的三边分别为:" + triangle.getEdge1() + "," + triangle.getEdge2() + "," + triangle.getEdge3());
9 System.out.println("三角形的周长为:" + triangle.Perimeter());
10 System.out.println("三角形的面积为:" + triangle.Square());
11
12 //长方形
13 Rectangle rectangle = new Rectangle(5,4);
14 System.out.println("长方形的长宽分别为:" + rectangle.getLength() + "," + rectangle.getWidth());
15 System.out.println("长方形的周长为:" + rectangle.Perimeter());
16 System.out.println("长方形的面积为:" + rectangle.Square());
17
18 //圆
19 Circle circle = new Circle(3);
20 System.out.println("圆的半径为:" + circle.getRadius());
21 System.out.println("圆的周长为:" + circle.Perimeter());
22 System.out.println("圆的面积为:" + circle.Square());
23 }
24 }
运行结果:
三角形的三边分别为:3.0,3.0,2.0
三角形的周长为:8.0
三角形的面积为:2.8284271247461903
长方形的长宽分别为:5.0,4.0
长方形的周长为:18.0
长方形的面积为:20.0
圆的半径为:3.0
圆的周长为:18.849576
圆的面积为:28.274364
2、编写类Factorial,为其添加两个静态方法(方法名自定义)。其中一个使用递归计算n的阶乘,一个使用非递归计算n的阶乘。构造main方法进行测试。
Factorial.java
1 package com.ly.factorial;
2
3 public class Factorial {
4 Factorial(){}
5
6 //非递归方式
7 public long factorial_1(int n){
8 long result = 1;
9 if(n<0)
10 return -1;
11 for(int i=1;i<=n;i++){
12 result = result*i;
13 }
14 return result;
15 }
16
17 //递归方式
18 public long factorial_2(int n){
19 if(n<0)
20 return -1;
21 if(n==0)
22 return 1;
23 else{
24 return n*factorial_2(n-1);
25 }
26 }
27 }
Main.java
1 package com.ly.factorial;
2 import java.util.Scanner;
3
4 public class Main {
5 public static void main(String[] args) {
6 Factorial fac = new Factorial();
7
8 int n;
9 Scanner in = new Scanner(System.in);
10 System.out.print("请输入一个整数n:");
11 n = in.nextInt();
12
13 System.out.println("---------------------------");
14 System.out.print("使用非递归方法计算:" + fac.factorial_1(n));
15 System.out.println();
16 System.out.print("使用递归方法计算:" + fac.factorial_2(n));
17 }
18 }
运行结果:
测试一:
请输入一个整数n:0
使用非递归方法计算: 1
使用递归方法计算: 1
测试二:
请输入一个整数n:5
使用非递归方法计算: 120
使用递归方法计算: 120
测试三:
请输入一个整数n:20
使用非递归方法计算: 2432902008176640000
使用递归方法计算: 2432902008176640000
3、按照要求使用Java进行编码。
1) 设计一个教师类Teacher,属性有编号(no)、姓名(name)、年龄(age)、所属学院(seminary),为这些属性设置相应的get和set方法;
2) 为Teacher类添加方法equals;(当两个教师对象的no相同时返回true)
3) 为Teacher类添加方法toString,通过该方法可以返回“编号为**、姓名为**、年龄为**的**学院老师”形式的字符串。
4) 构造main方法进行测试。
Teacher.java
1 package com.ly.teacher;
2
3 public class Teacher{
4 private int no;
5 private String name;
6 private int age;
7 private String seminary;
8
9 Teacher(int no,String name,int age,String seminary){
10 this.no = no;
11 this.name = name;
12 this.age = age;
13 this.seminary = seminary;
14 }
15 public int getNo(){
16 return no;
17 }
18 public String getName(){
19 return name;
20 }
21 public int getAge(){
22 return age;
23 }
24 public String getSeminary(){
25 return seminary;
26 }
27 public void setNo(int no){
28 this.no = no;
29 }
30 public void setName(String name){
31 this.name = name;
32 }
33 public void setAge(int age){
34 this.age = age;
35 }
36 public void setSeminary(String seminary){
37 this.seminary = seminary;
38 }
39
40 public boolean equal(Object o){
41 boolean result=false;
42 Teacher t = (Teacher)o;
43 if(this.no==t.no){
44 return true;
45 }
46 return result;
47 }
48 public String toString(){
49 return "编号:"+no+"\n"
50 +"姓名:"+name+"\n"
51 +"年龄:"+age+"\n"
52 +"学院:"+seminary+"\n";
53 }
54 }
Main.java
1 package com.ly.teacher;
2
3 public class main {
4 public static void main(String[] args) {
5 Teacher t1 = new Teacher(1001,"Blanche",30,"College of Information Science and Engineering");
6 Teacher t2 = new Teacher(1002,"Tom",32,"College of Information Science and Engineering");
7 Teacher t3 = new Teacher(1003,"Linyu",31,"College of Information Science and Engineering");
8
9 System.out.println(t1.toString());
10 System.out.println(t2.toString());
11 System.out.println(t3.toString());
12
13 System.out.println("*--------------------------------------------------*");
14 t3.setNo(t1.getNo());
15 System.out.println("*--------------教师t3的编号被修改-----------------*");
16 System.out.println("*--------------------------------------------------*");
17 System.out.println();
18 System.out.println(t3.toString());
19
20 System.out.println("教师t3与教师t1是否相同?");
21 System.out.println(t3.equal(t1));
22 }
23 }
运行结果:
编号:1001
姓名:Blanche
年龄:30
学院:College of Information Science and Engineering
编号:1002
姓名:Tom
年龄:32
学院:College of Information Science and Engineering
编号:1003
姓名:Linyu
年龄:31
学院:College of Information Science and Engineering
*--------------------------------------------------*
*--------------教师t3的编号被修改-----------------*
*--------------------------------------------------*
编号:1001
姓名:Linyu
年龄:31
学院:College of Information Science and Engineering
教师t3与教师t1是否相同?
true
4、设计一个带表头的单链表(链表中的元素属于同一类型对象,但对象的类型可以随意),提供以下操作:
(1)insert:在某个位置插入对象;
(2)delete:在某个位置删除对象;
(3)delete:删除链表中与x相同的元素;
(4)size:返回当前链表中对象的个数;
(5)isEmpty:判断链表是否为空;
(6)traverse:遍历链表,打印出所有的元素;
(7)getData:取得某个位置的对象。构造main函数进行测试。
Node.java
1 package com.ly.simplelist; 2 3 public class Node { 4 private Object element = new Object(); 5 private Node next; 6 7 public Node(Object element,Node next){ 8 this.element = element; 9 this.next = next; 10 } 11 public void setElement(Object o){ 12 this.element = o; 13 } 14 public void setNextNode(Node nextNode){ 15 this.next = nextNode; 16 } 17 public Object getElement(){ 18 return element; 19 } 20 public Node getNextNode(){ 21 return next; 22 } 23 public void display(){ 24 System.out.println(this.getElement()); 25 } 26 }
SimpleList.java
1 package com.ly.simplelist; 2 3 public class SimpleList { 4 private Node head; 5 private int size = 0; 6 7 SimpleList(){ 8 this.head = new Node(null,null); 9 } 10 public boolean isEmpty(){ 11 if(this.size == 0) 12 return true; 13 else 14 return false; 15 } 16 17 //返回当前链表中元素的个数 18 public int getSize(){ 19 return this.size; 20 } 21 22 //在index位置插入元素 23 public void insert(int index,Object element){ 24 if(index<0 || index>this.size){ 25 System.out.println("插入越界!"); 26 return; 27 } 28 Node node = new Node(element,null); 29 Node temp = head; 30 //查找第index个节点的前一个节点 31 for(int i=0;i<index;i++){ 32 temp = temp.getNextNode(); 33 } 34 node.setNextNode(temp.getNextNode()); 35 temp.setNextNode(node); 36 this.size++; 37 } 38 39 //在index位置删除对象 40 public void delete(int index){ 41 if(index<0 || index>this.size){ 42 System.out.println("删除位置越界!"); 43 return; 44 } 45 Node temp = head; 46 for(int i=0;i<index;i++){ 47 temp = temp.getNextNode(); 48 } 49 temp.setNextNode(temp.getNextNode().getNextNode()); 50 this.size--; 51 } 52 53 //删除链表中与x相同的元素 54 public void delete(Object x){ 55 if(this.size == 0){ 56 System.out.println("当前链表为空!"); 57 return; 58 } 59 60 int flag = 0; 61 Node temp = head; 62 for(int i=0;i<this.size;i++){ 63 temp = temp.getNextNode(); 64 if(temp == x){ 65 flag = 1; 66 temp.setNextNode(temp.getNextNode().getNextNode()); 67 this.size--; 68 } 69 } 70 if(flag==0){ 71 System.out.println("结点" + x + "不存在!"); 72 } 73 } 74 75 //遍历链表 76 public void traverse(){ 77 if(this.size == 0){ 78 System.out.println("当前链表为空!"); 79 return; 80 } 81 Node temp = head; 82 while(temp.getNextNode() != null){ 83 temp = temp.getNextNode(); 84 temp.display(); 85 } 86 } 87 88 //取得index位置的对象 89 public Object getData(int index){ 90 if(index<0 || index>this.size-1){ 91 System.out.print("查询位置越界!"); 92 return null; 93 } 94 Node temp = head; 95 for(int i=0;i<=index;i++){ 96 temp = temp.getNextNode(); 97 } 98 return temp.getElement(); 99 } 100 }
Main.java
1 package com.ly.simplelist; 2 3 public class Main { 4 public static void main(String[] args) { 5 /* 6 Node node = new Node("Node test",null); 7 Node node2 = new Node("123",null); 8 node.setNextNode(node2); 9 node.display(); 10 node2.display(); 11 */ 12 13 String str1 = "string1"; 14 String str2 = "string2"; 15 String str3 = "string3"; 16 int number1 = 100; 17 int number2 = 200; 18 double db1 = 1.0; 19 double db2 = 2.0; 20 21 22 SimpleList list = new SimpleList(); 23 list.insert(0, str1); 24 list.insert(1, str2); 25 list.insert(2, str3); 26 list.insert(3, number1); 27 list.insert(4, number2); 28 29 System.out.println("新建链表:"); 30 list.traverse(); 31 32 System.out.println("链表是否为空?" + list.isEmpty()); 33 34 System.out.println("显示链表长度:" + list.getSize()); 35 System.out.println(); 36 37 System.out.println("向链表中index位置插入新元素:"); 38 list.insert(3, db1); 39 list.insert(4, db2); 40 list.traverse(); 41 42 System.out.println(); 43 System.out.println("删除链表中index位置的元素:"); 44 list.delete(2); 45 list.delete("adc"); 46 list.traverse(); 47 48 System.out.println(); 49 System.out.println("显示链表index位置的元素:"); 50 System.out.println(list.getData(2)); 51 } 52 53 }
运行结果:
新建链表:
string1
string2
string3
100
200
链表是否为空?false
显示链表长度:5
向链表中index位置插入新元素:
string1
string2
string3
1.0
2.0
100
200
删除链表中index位置的元素:
结点adc不存在!
string1
string2
1.0
2.0
100
200
显示链表index位置的元素:
1.0

Triangle.java
浙公网安备 33010602011771号