Java 实验:面向对象程序设计

1.     设计一个名为figure的图形软件包(package)。包中包含三角形、矩形、圆三个类。要求:(1)每个类都要构造方法并为成员设置get和set方法;(2)每个类都要有计算周长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有main方法的类中编写代码,分别使用图形软件包中的三个类,生成三个对象,并打印出其周长和面积。

import java.util.Scanner;
import figure.Circle;
import figure.Rectangle;
public class Figure {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入长方形长度:");
		double h = sc.nextDouble();
		
		System.out.println("请输入长方形宽度:");
		double w = sc.nextDouble();
		
		Rectangle r = new Rectangle(h,w);
		System.out.println("长方形面积为"+r.getSqu()+",周长为"+r.getCir()); 
		
		System.out.println("请输入圆形半径:");
		double cc = sc.nextDouble();

		Circle c = new Circle(cc);
		System.out.println("圆形面积为"+c.getSqu()+",周长为"+c.getCir());
		sc.close();
		
	}
}

package figure;

public class Circle {
	private double rad;
	private static double pi = 3.14;
	
	public Circle(double r){
		rad = r;
	}

	public double getSqu(){
		
		return pi * rad * rad;

	}

	public double getCir(){

	  return 2 * pi * rad;

	}
}

package figure;

public class Rectangle {
	private double width;
	private double height;
	
	public Rectangle(double w,double h){
		width = w;
		height = h;
	}

	public double getSqu(){
		
		return width * height;

	}

	public double getCir(){

	  return 2 * (width + height);

	}
}


2.     设计一个教师类Teacher(属于cn.net.sdkd包),要求:

1)       属性有编号(int no)、姓名(String name)、年龄(int age)、所属学院(seminary),为这些属性设置相应的get和set方法。

2)       为Teacher类重写equals方法,要求:当两个教师对象的no相同时返回true。

3)       重写Teacher类的toString方法,通过该方法可以返回“编号为**、姓名为**、年龄为**的**学院老师”形式的字符串。

4)       由多个Teacher对象所形成的数组可以以两种方法排序(编号由低到高排序):1)使用Arrays.sort(Object[]a)方法;2)使用Arrays.sort(Object[] a,Comparator c)方法。

5)       再定义一个类TeacherManagement(属于cn.sd包),提供方法search,方法可以在一组给定的教师中,根据姓名(或年龄)返回等于指定姓名(或年龄)的教师的字符串信息,信息格式为:“编号为**、姓名为**、年龄为**的**学院老师”。如果没有满足条件的教师,则返回“没有符合条件的教师”。

6)       构造main方法进行测试。

package cn.net.sdkd;

public class Teacher {
    private int no;
    private int age;
    private String name;
    private String seminary;
    
	public Teacher(){
		
	}
	
	public Teacher(int no, String name, int age, String seminary) {
		this.no = no;
		this.name = name;
		this.age = age;
		this.seminary = seminary;
	}
    
    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }    
    public String getSeminary() {
        return seminary;
    }

    public void setSeminary(String seminary) {
        this.seminary = seminary;
    }
    public String toString() {
        return "编号为"+ this.no +",姓名为"+ this.name +",年龄为"+ this.age +"的"+ this.seminary +"学院老师";
    }

    public boolean equals(Object o) {
        if (!(o instanceof Teacher))
            return false;
        Teacher unit = (Teacher) o;
        return unit.no == no;
     }
    
}


 

package cn.sd;

import cn.net.sdkd.Teacher;
public class TeacherManagement {
	public static String search(Teacher[] teachers, String name){
		Teacher t = null;
		for(int i = 0;i < teachers.length;i++){
			if(teachers[i] != null && teachers[i].getName() == name){
				t = teachers[i];
				break;
			}
		}
		
		if(t != null){
			return t.toString();
		}else{
			return "没有符合条件的教师";
		}
		
	}
	
	public static String search(Teacher[] teachers, int age){
		Teacher t = null;
		for(int i = 0;i < teachers.length;i++){
			if(teachers[i] != null && teachers[i].getAge() == age){
				t = teachers[i];
				break;
			}
		}
		
		if(t != null){
			return t.toString();
		}else{
			return "没有符合条件的教师";
		}
	}
}


3.        设计一个带表头的双向链表(链表中数据的具体类型可以随意),提供以下方法:(1)insert:在某个位置插入对象;(2)delete:在某个位置删除对象;(3)delete:删除链表中与x相同的元素;(4)size:返回当前链表中对象的个数;(5)isEmpty:判断链表是否为空;(6)traverse:遍历链表,打印出所有的元素;(7)getData:取得某个位置的对象。构造main函数进行测试。

public class Node<T> {

    public T data;
    public Node<T> prev = null;
    public Node<T> next = null;

    public boolean equals(Node<T> node) {
        if (data.equals(node.data)) {
            return true;
        }
        return false;
    }

    public int hashCode() {
        return data.hashCode();
    }

    public String toString() {
        return data.toString();
    }
}


public class DoublyLinkedList<T> {

    private Node<T> head;
    private Node<T> tail;
    private int size;
    
    public DoublyLinkedList() {
        head = new Node<T>();
        tail = new Node<T>();
        size = 0;
    }

    /**
     * 删除某个位置的对象
     * @param index 删除位置
     */
    public void delete(int index) {
        if (isEmpty()) {
            System.out.println("The Doubly Linked List is empty");
        } else if(index < 0 || index > size - 1){
        	System.out.println("The wrong number");
        }else {
            Node<T> theNode = head.next;
            for (int i = 0;null != theNode && i < index;i++) {
            	theNode = theNode.next;
            }
            theNode.next.prev = theNode;
            theNode = theNode.next;
            size --;
        }
    }
    
    /**
     * 删除链表中与key值相同的元素
     * @param key 要删除的值
     */
    public void delete(T key) {
        if (isEmpty()) {
            System.out.println("The Doubly Linked List is empty");
        } else {
            Node<T> theNode = head.next;
            while (null != theNode) {
                if (theNode.data.equals(key)) {
                    theNode.next.prev = theNode;
                    theNode = theNode.next;
                    size --;
                    break;
                }
                theNode = theNode.next;
            }
        }
    }
    
    /**
     * 返回当前链表中对象的个数
     * @return 对象的个数
     */
	public int size(){
		return size;
	}
	
	/**
	 * 判断链表是否为空
	 * @return 链表是否为空
	 */
    public boolean isEmpty() {
        if (null == head.next || null == tail.prev) {
            return true;
        }
        return false;
    }
    /*
	public boolean isEmpty(){
		return 0 == size;
	}
	*/
    
    /**
     * 在链表的最后插入对象
     * @param node 要插入的对象
     */
    public void insert(Node<T> node) {
        if (null == tail.prev) {
            tail.prev = node;
            head.next = node;
        } else {
            tail.prev.next = node;
            node.prev = tail.prev;
            tail.prev = node;
        }
        size ++;
    }
    
    /**
     * 取得某个位置的对象
     * @param index 位置
     * @return 对象数据
     */
	public T getData(int index) {
		if(index < 0 || index > size - 1){
			return null;
		}
        Node<T> theNode = head.next;
        for (int i = 0;null != theNode && i < index;i++) {
        	theNode = theNode.next;
        }
     	return theNode.data;
	}
}


4.     一个公司有三种不同类型的员工,他们的薪水分别按年(计算方法:年薪*工作年数)、按月(计算方法:月薪*工作月数)、按周(计算方法:周薪*工作周数)结算。编写类Company,提供计算所有员工总薪水的方法getEarnings,该方法能够根据输入的一组员工(包含各类员工)返回这组员工的总薪水。

public class Company {
	public double getEarnings(Employee[] e){
		double salary = 0;
		for(int i = 0;e[i] != null;i++){
			salary += e[i].salary * e[i].time;
		}
		return salary;
	}
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、实验目的 掌握根据实际需求设计Java的方法; 掌握Java中继承的使用方法; 掌握Java中package的概念和使用方法; 掌握静态方法的定义和使用方法; 掌握Object中equals方法和toString方法的覆盖方法。 二、实验内容 1、设计一个名为figure的图形软件包(package)。包中包含三角形、矩形、圆三个。要求:(1)每个都要构造方法并为成员设置get和set方法;(2)每个都要有计算周长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有main方法的中编写代码,分别使用图形软件包中的三个,生成三个对象,并打印出其周长和面积。 2、编写Factorial,为其添加两个静态方法(方法名自定义)。其中一个使用递归计算n的阶乘,一个使用非递归计算n的阶乘。构造main方法进行测试。 3、按照要求使用Java进行编码。 设计一个教师Teacher属性有编号(no)、姓名(name)、年龄(age)、所属学院(seminary),为这些属性设置相应的get和set方法; 为Teacher重写equals方法;(当两个教师对象的no相同时返回true) 重写Teacher的toString方法,通过该方法可以返回“编号为**、姓名为**、年龄为**的**学院老师”形式的字符串。 构造main方法进行测试。 4、设计一个带表头的单链表(链表中的元素属于同一型对象,但对象的型可以随意),提供以下操作:(1)insert:在某个位置插入对象;(2)delete:在某个位置删除对象;(3)delete:删除链表中与x相同的元素;(4)size:返回当前链表中对象的个数;(5)isEmpty:判断链表是否为空;(6)traverse:遍历链表,打印出所有的元素;(7)getData:取得某个位置的对象。构造main函数进行测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值