西北工业大学·面向对象编程实验三
3.1
题目说明:给定Project类,在ProjectArray中实现缺失功能,并在测试类TestProjectArray中运行成功显示“All tests passed”(具体说明,见文章末尾Unit3-1文档)
public class ProjectArray {
/**
* Creates an array with three objects of class {@link Project}.
* <p>
* The first element of the array is the object
* <code>first</code>, the second element of the array is
* the object <code>second</code>, and the third element of the
* array is the object <code>third</code>.
* </p>
*
* @param first a {@link Project} object.
* @param second a {@link Project} object.
* @param third a {@link Project} object.
* @return an array with the objects <code>first</code>,
* <code>second</code>, and <code>third</code>.
*/
public static Project[] makeArray(Project first, Project second,
Project third) {
if(first!=null&&second!=null&&third!=null) {
Project[] project=new Project[3];
project[0]=first;
project[1]=second;
project[2]=third;
return project;
}else {
return null; // REMOVE; USED SO THIS FILE COMPILES
}
}
/**
* Creates a new array from the specified array of {@link Project}
* objects.
* <p>
* The elements in the new array have the same order as those in
* the specified array.
* </p>
*
* @param array an array that contains objects of class {@link Project}.
* @return a <i>new</i> array of the objects in the specified array.
*/
public static Project[] copyArray(Project[] array) {
if(array!=null) {
Project[] project=new Project[array.length];
for(int i=0;i<array.length;i++) {
/*Project temproject = new Project(array[i].getId(),array[i].getName(),array[i].getPrice());
project[i]= project;*/
project[i]=array[i];
}
return project;
}else {
return null;
}
}
/**
* Returns the {@link Project} object with the specified ID.
*
* @param array an array that contains objects of class {@link Project}.
* @param id an Project ID.
* @return The {@link Project} object with the specifed
* ID. Returns <code>null</code> if there are no Projects
* in the specified array with the specifed ID.
*/
public static Project getProject(Project[] array, int id) {
if(array.length==0) {
return null;
}else {
for(Project project : array) {
if(project.getId()==id) {
return project;
}
}
return null;
}
}
/**
* Returns the number of Projects whose price is Lower than the specified
* dollar amount.
*
* @param array an array that contains objects of class {@link Project}.
* @param amount a dollar amount.
* @return the number of Projects whose price is Lower than the
* specified dollar amount.
*/
public static int countLowerPrice(Project[] array, double amount) {
if(array.length==0||amount==0) {
return 0; // REMOVE; USED SO THIS FILE COMPILES
}else {
int num=0;
for(Project project : array) {
if(project.getPrice()<amount) {
num++;
}
}
return num;
}
}
/**
* Returns the sum of the price of the Projects in the specified
* array.
*
* @param array an array that contains objects of class {@link Project}.
* @return the sum of the price of the Projects in the specified
* array.
*/
// for-each loop
public static double sumPrice(Project[] array) {
if(array.length==0) {
return 0; // REMOVE; USED SO THIS FILE COMPILES
}else {
double sumprice=0;
for(Project project : array) {
sumprice=sumprice+project.getPrice();
}
return sumprice;
}
}
/**
* Obtains the Lowest price in the specified array.
*
* @param array an array that contains objects of class {@link Project}.
* @return the Lowest price in the specified array.
*/
// for-each loop
public static double getLowestPrice(Project[] array) {
if(array.length==0) {
return 0; // REMOVE; USED SO THIS FILE COMPILES
}else {
double low=array[0].getPrice();
for(Project project : array) {
if(low>project.getPrice()) {
low=project.getPrice();
}
}
return low;
}
}
/**
* Increases the Price of every Project in the specified array by the
* specified amount.
*
* @param array an array that contains objects of class {@link Project}.
*/
// for-each loop
public static void increaseAll(Project[] array, double amount) {
for(Project project : array) {
double price = project.getPrice();
project.setPrice(price+amount);
}
}
/**
* Returns a string representation of the specified
* {@link Project} array.
* <p>
* Uses the method <code>toString</code> in class <code>Project</code>
* to obtain the string representation of each object in the array.
* </p>
* A new line character ( \n ) separates the string representations
* of each <code>Project</code> object. For example:
* </p>
* <pre>
* Project[102,cruise,68250.0]\n
* Project[101,domestic,36000.0]\n
* Project[103,outbound,92175.0]
* </pre>
* <p>
* Note that the string returned does <i>not</i> end with a new line
* character (\n).
* </p>
* <p>
* This method assumes that every element in the specified array
* contains a valid reference to an <code>Project</code> object.
* </p>
*
* @param array an array that contains objects of class {@link Project}.
* @return the string representation of the specified array
*/
public static String displayAll(Project[] array) {
String string="";
for(Project project : array) {
string = string + project.toString();
if(!project.equals(array[array.length-1])) {
string=string + "\n";
}
}
return string;
}
}
3.2.1
题目说明:在这个题目中你将实现复试系统,该系统向用户提供四种选择:
根据用户选择,系统能够以纯文本、HTML或XML三种格式显示学生信息。
复试系统的部分类图如下:
你需要实现接口 StudentsFormatter 、类 FushiSystem 、类 PlainTextStudentsFormatter 、类 HTMLStudentsFormatter 和类 XMLStudentsFormatter 。请按照类图和下面的描述信息实现复试系统。部分代码已经实现,在 FushiSystem 文件夹中,这些类无需更改。你需要使用单例模式和策略模式来实现复试系统显示学生信息的代码。
代码如下(单一实例模型采用懒汉式):
1.StudentsFormatter接口:
public interface StudentsFormatter {
/**
* @param studentCatalog 创建接口方法的形式参数
*
* @return 接口方法返回String对象
*/
String formatStudents(StudentCatalog studentCatalog);
}
2.PlainTextStudentsFormatter类:
public class PlainTextStudentsFormatter implements StudentsFormatter{
/**
* singletonInstance 作为调用formatStudents方法的通道
*/
private static PlainTextStudentsFormatter singletonInstance=null;
/**
* singletonInstance的无参构造方法
*/
private PlainTextStudentsFormatter(){
}
/**
* @param studentCatalog 创建接口方法的形式参数
*
* @return s 返回输出字符串
*/
@Override
public String formatStudents(StudentCatalog studentCatalog) {
String s="\nStudent Catalog\n";
for(Student student : studentCatalog.students){
s=s+student.getId()+"_";
s=s+student.getName()+"_";
for(TestItem testitem : student.getExamPaper().testitem){
s=s+testitem.getTest().getCode();
if(!testitem.equals(student.getExamPaper().getTestItem(9))){
s=s+"_";
}
}
s=s+"\n";
}
return s;
}
/**
* @return singletonInstance 当singletonInstance为空时创建并返回
*/
public static PlainTextStudentsFormatter getSingletonInstance() {
if(singletonInstance==null){
singletonInstance=new PlainTextStudentsFormatter();
}
return singletonInstance;
}
}
输出样例:
Student Catalog
201921300_吴广胜_E004_P008_E001_M003_M001_P004_M008_P005_P007_E003
2019213002_陈盛典_E008_M007_P002_M002_E007_P007_P006_E009_P003_M010
2019213003_刘子豪_M004_P002_P009_P007_M007_E005_M003_E006_P001_E002
2019213004_仇历_P004_P007_M004_P002_E003_E007_P009_M006_M001_E004
2019213005_郑西泽_P005_E007_P004_E004_P007_M006_M004_E009_P009_M001
3.HTMLStudentsFormatter类:
public class HTMLStudentsFormatter implements StudentsFormatter{
/**
* singletonInstance 作为调用formatStudents方法的通道
*/
private static HTMLStudentsFormatter singletonInstance=null;
/**
* singletonInstance的无参构造方法
*/
public HTMLStudentsFormatter(){
}
/**
* @param studentCatalog 传入一个需要输出信息的学生列表
*
* @return s 返回输出字符串
*/
@Override
public String formatStudents(StudentCatalog studentCatalog) {
String s="\n<html>\n";
s=s+" <body>\n";
s=s+" <center><h2>学生目录</h2></center>\n";
for(Student student : studentCatalog.students){
s=s+" <hr>\n";
s=s+" <h4>"+student.getId()+" "+student.getName()+"</h4>\n";
s=s+" <blockquote>\n";
for(TestItem testitem : student.getExamPaper().testitem){
s=s+" "+"<"+testitem.test.getCode()+"> <"+testitem.getTest().getTitle()+"> <br>\n";
}
s=s+" </blockquote>\n";
}
s=s+" </body>\n";
s=s+"</html>";
return s;
}
/**
* @return singletonInstance 当singletonInstance为空时创建对象
*/
public static HTMLStudentsFormatter getSingletonInstance() {
if(singletonInstance==null){
singletonInstance=new HTMLStudentsFormatter();
}
return singletonInstance;
}
}
输出样例:
4.XMLStudentsFormatter类
public class XMLStudentsFormatter implements StudentsFormatter{
/**
* singletonInstance 作为调用formatStudents方法的通道
*/
private static XMLStudentsFormatter singletonInstance=null;
/**
* singletonInstance无参构造方法
*/
public XMLStudentsFormatter() {
}
/**
* @param studentCatalog 创建接口方法的形式参数
*
* @return s 返回输出字符串
*/
@Override
public String formatStudents(StudentCatalog studentCatalog) {
String s="\n";
s=s+"<StudentCatalog>\n";
for(Student student : studentCatalog.students){
s=s+" <student id=\"<"+student.getId()+">\" name=\"<"+student.getName()+">\">\n";
s=s+" <ExamPaper>\n";
for(TestItem testitem : student.getExamPaper().testitem){
s=s+" <Test code=\"<"+testitem.getTest().getCode()+">\"><"+testitem.getTest().getTitle()+"></Test>\n";
}
s=s+" </ExamPaper>\n";
s=s+" </Student>\n";
}
return s;
}
/**
* @return singletonInstance 当singletonInstance为空时创建并返回
*/
public static XMLStudentsFormatter getSingletonInstance() {
if(singletonInstance==null){
singletonInstance=new XMLStudentsFormatter();
}
return singletonInstance;
}
}
输出样例:
3.2.2
使用工厂模式实现3.2.1的输出,我利用的使抽象类实现简单工厂模式
代码如下:
1.StudentsFormatter抽象类
public abstract class StudentsFormatter {
/**
* @param studentCatalog 创建抽象方法的形式参数
*
* @return 抽象方法返回String对象
*/
abstract String formatStudents(StudentCatalog studentCatalog);
}
2.Factory类
public class Factory {
public StudentsFormatter createFormatter(int operate){
StudentsFormatter studentsFormatter=null;
switch(operate){
case 0:
studentsFormatter=PlainTextStudentsFormatter.getSingletonInstance ();
case 1:
studentsFormatter=HTMLStudentsFormatter.getSingletonInstance ();
case 2:
studentsFormatter=XMLStudentsFormatter.getSingletonInstance ();
}
return studentsFormatter;
}
}
然后将fushiSystem中的run函数对应部分改成如下图所示:
实验结束!!