实验2 面向对象程序设计(6学时)
二、实验内容
1.设计一个教师类Teacher(属于cn.net.sdkd包),要求:
1)属性有编号(int no)、姓名(String name)、年龄(int age)、所属学院(String 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.sd;
import cn.net.sdkd.Teacher;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class TeacherManagement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Teacher> teachers = new ArrayList<>();
while(true)
{
System.out.println("是否输入? Y/y");
String flag = sc.next();
if(flag.equals("Y")||flag.equals("y"))
{
int no = sc.nextInt();
String name = sc.next();
int age = sc.nextInt();
String seminary = sc.next();
Teacher t1 = new Teacher(no,name,age,seminary);
teachers.add(t1);
}
else
{
break;
}
}
System.out.println("输入要查找的老师");
int no = sc.nextInt();
String name = sc.next();
int age = sc.nextInt();
String seminary = sc.next();
Teacher t2 = new Teacher(no,name,age,seminary);
search(teachers,t2);
}
public static void search(ArrayList<Teacher> t2,Teacher t1)
{
boolean flag = false;
Iterator<Teacher> it = t2.iterator();
while(it.hasNext())
{
if(it.next().name.equals(t1.name))
flag = true;
}
if(!flag)
System.out.println("没有符合条件的老师");
else
System.out.println(t1.toString());
}
}
package cn.net.sdkd;
import javax.swing.*;
import java.util.Comparator;
public class Teacher implements Comparable<Teacher>, Comparator<Teacher> {
public int no;
public String name;
public int age;
public String seminary;
@Override
public int compare(Teacher teacher, Teacher t1) {
if(teacher.no<t1.no)
return 1;
else if(teacher.no==t1.no)
return 0;
else
return -1;
}
@Override
public boolean equals(Object obj) {
Teacher teacher = (Teacher) obj;
if(this.no==teacher.no)
return true;
else
return false;
}
@Override
public String toString() {
return "编号为"+this.no+",姓名为"+this.name+",年龄为"+this.age+"的"+this.seminary+"学院老师";
}
public Teacher(int no, String name, int age, String seminary) {
this.no = no;
this.name = name;
this.age = age;
this.seminary = seminary;
}
@Override
public int compareTo(Teacher teacher) {
if(this.no<teacher.no)
return 1;
else if(this.no == teacher.no)
return 0;
else
return -1;
}
public Teacher() {
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSeminary() {
return seminary;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
}
2.设计一个带表头的双向链表(链表中数据的具体类型可以随意),提供以下方法:(1)insert:在某个位置插入对象;(2)insert:在链表的最后插入对象;(2)delete:在某个位置删除对象;(3)delete:删除链表中与x相同的元素;(4)size:返回当前链表中对象的个数;(5)isEmpty:判断链表是否为空;(6)traverse:遍历链表,打印出所有的元素;(7)getData:取得某个位置的对象。构造main函数进行测试。
package Test;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
class Node {
int data;
Node next;
Node front;
//next:后继 front:前驱
public Node() {
data = 0;
next = null;
front = null;
}
public Node(int e, Node pre, Node next) {
this.data = e;
this.front = pre;
this.next = next;
}
public void setData(int a) {
this.data = a;
}
public Object getData() {
return this;
}
}
public int size;
public Node head;
public Node rear;
public Main() {
this.size = 0;
head = new Node();
rear = new Node(0, head, null);
head.next = rear;
}
//尾插法
public void insert(int e) {
Node t = new Node(e, null, null);
rear.front.next = t;
t.front = rear.front;
t.next = rear;
rear.front = t;
size++;
}
public void insert(int x, int e) {
Node t = new Node(e, null, null);
Node f = head;
for (int i = 0; i < x; i++) {
f = f.next;
}
t.next = f;
t.front = f.front;
f.front.next = t;
f.front = t;
size++;
}
public void delete(int x) {
Node f = head.next;
while (f.next != null) {
if (f.data == x) {
f.front.next = f.next;
f.next.front = f.front;
}
f = f.next;
}
}
public void delete(int pos, Main t) {
Node f = head;
for (int i = 0; i < pos; i++) {
f = f.next;
}
f.front.next = f.next;
f.next.front = f.front;
}
public boolean isEmpty() {
if (size == 0)
return true;
else
return false;
}
public int size() {
return size;
}
public void traverse() {
Node f = head.next;
while (f.next != null) {
System.out.print(f.data+" ");
f = f.next;
}
}
public static void main(String[] args) {
Main str = new Main();
str.insert(1);
str.insert(2);
str.insert(3);
str.insert(2,4);
str.insert(5);
str.insert(6);
System.out.println("size = " + str.size);
str.traverse();
System.out.println();
str.delete(2);
str.traverse();
System.out.println();
str.delete(3, str);
str.traverse();
}
}
3.使用一维数组编码实现一个栈(Stack)类,要求提供以下操作:(1)boolean isEmpty():判断栈当前是否为空;(2)入栈操作void push(obj):把数据元素obj插入堆栈;(3)出栈操作Object pop():出栈,并返回删除的数据元素;(4)Object getTop():取堆栈当前栈顶的数据元素并返回;(5)利用Stack类实现一个方法:输入一个正整数,输出该整数所对应的二进制数。
class stack{
private Object[] stack;
private int need;
stack(int n)
{
stack = new Object[n];
for(int i = 0;i < n;i++)
{
stack[i] = null;
}
need = n;
}
public boolean isEmpty()
{
if(need == stack.length)
return true;
else
return false;
}
void push(Object obj)
{
if(need == 0)
{
System.out.println("空间已满!");
return;
}
else
{
stack[need-1] = obj;
need--;
}
}
Object pop()
{
Object object = stack[need];
need++;
stack[need] = null;
return object;
}
Object getTop()
{
return stack[need];
}
int GetTwo()
{
return Integer.parseInt((String) stack[need],2);
}
}
4.利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法:(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。(7)Matrix transpose():返回当前矩阵的转置矩阵;(8)getMax():返回矩阵中的最大值及其所在行和列;(9)print():以行和列的形式打印出当前矩阵。**
import java.text.DecimalFormat;
import java.util.*;
class Matrix{
private int row;//行
private int col;//列
//private double value;
double [][]Data;
public Matrix(int row, int col,double [][]Data) {
this.row = row;
this.col = col;
// this.value = value;
this.Data = Data;
}
public void setMatrix(int row , int col, double value) {
this.Data[row - 1][col - 1] = value;
}
public double getMatrix(int row, int col) {
return Data[row - 1][col - 1] ;
}
public int width() {
return row;
}
public int height() {
return col;
}
public Matrix add(Matrix b) {
if(this.width() != b.width() && this.height() != b.height()) {
return null;
}
double add[][] = new double[this.row][this.col];
for(int i = 0;i<col;i++) {
for(int j = 0;j<row;j++) {
add[i][j] = this.Data[i][j] + b.Data[i][j];
}
}
Matrix another = new Matrix(this.col,this.row,add);
System.out.println("after add:");
return another;
}
public Matrix multiply(Matrix b) {
if(this.col != b.row) {
return null;
}
double mul[][] = new double[this.row][b.col];
double temp = 0;
for(int i = 0;i<this.row;i++) {
for(int k = 0;k<b.col;k++) {
for(int j = 0;j<this.col;j++)
{
temp += this.Data[i][j] * b.Data[j][k];
}
mul[i][k] = temp;
temp = 0;
}
}
Matrix another = new Matrix(this.row, b.col, mul);
System.out.println("after multiply:");
return another;
}
public Matrix transpose() {
double tran[][] = new double[this.row][this.col];
for(int i = 0;i<this.row;i++) {
for(int j = 0;j<this.col;j++) {
tran[j][i] = this.Data[i][j];
}
}
Matrix another = new Matrix(this.col,this.row,tran);
System.out.println("after transpose:");
return another;
}
public String toString() {
DecimalFormat df = new DecimalFormat("0");
String result = "";
//result += df.format(Data[0][0]);
for(int i = 0;i<this.row;i++) {
result += df.format(Data[i][0]);
for(int j = 1;j<this.col;j++) {
result += " " + df.format(Data[i][j]);
}
result += "\n";
}
return result;
}
}
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int row = scan.nextInt();
int col = scan.nextInt();
System.out.println("row:" + row +" column:" + col);
//set value
double data[][] = new double[row][col];
for(int i = 0;i<row;i++) {
for(int j = 0;j<col;j++) {
double d = scan.nextDouble();
data[i][j] = d;
}
}
Matrix matrix = new Matrix(row,col,data);
int srow = scan.nextInt();
int scol = scan.nextInt();
double sv = scan.nextDouble();
System.out.println("after set value:");
matrix.setMatrix(srow, scol, sv);
System.out.print(matrix);
//get value
DecimalFormat df = new DecimalFormat("0");
int vrow = scan.nextInt();
int vcol = scan.nextInt();
System.out.print("value on (" +vrow + "," + vcol +"):");
System.out.println(df.format(matrix.getMatrix(vrow, vcol)));
//add
int addrow = scan.nextInt();
int addcol = scan.nextInt();
double addMatrix[][] = new double[addrow][addcol];
for(int i = 0;i<addrow;i++) {
for(int j = 0;j<addcol;j++) {
double ad = scan.nextDouble();
addMatrix[i][j] = ad;
}
}
Matrix add = new Matrix(addrow,addcol,addMatrix);
System.out.print(matrix.add(add));
// //mul
int mulrow = scan.nextInt();
int mulcol = scan.nextInt();
double mulMatrix[][] = new double[mulrow][mulcol];
for(int i = 0;i<mulrow;i++) {
for(int j = 0;j<mulcol;j++) {
double mu = scan.nextDouble();
mulMatrix[i][j] = mu;
}
}
Matrix mul = new Matrix(mulrow,mulcol,mulMatrix);
//System.out.print(matrix.add(add));
System.out.print(matrix.multiply(mul));
//transpose
System.out.print(matrix.transpose());
}
}