西北工业大学#面向对象编程实验#实验三

西北工业大学·面向对象编程实验三

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函数对应部分改成如下图所示:
在这里插入图片描述

实验结束!!

  • 28
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
面向对象编程是一种程序设计思想,它以对象为基本单位,通过对象之间的交互来完成程序的功能。在Java中,面向对象编程是一种重要的编程范式,也是西北工业大学计算机科学与技术专业的基础课程之一。 在面向对象编程的范式中,一切皆为对象对象是程序的基本单位,它可以拥有属性和行为。属性是对象的状态和特征,行为是对象能够执行的操作。通过封装、继承和多态等特性,可以将对象之间的关系和功能进行抽象和组织,从而更好地设计和实现程序。 在西北工业大学面向对象编程基础课程中,学生会学习Java语言的基本语法和面向对象编程的基本概念。他们会学习如何定义类和对象,如何实现封装和继承,以及如何应用多态和接口等概念。此外,他们还会学习如何使用Java的标准类库和相关工具,来进行程序的开发和调试。 学生在学习面向对象编程基础的过程中,会通过实践课程设计和项目实践,来提高他们的编程能力和问题解决能力。他们会学习如何设计和实现简单的面向对象的程序,如学生成绩管理系统、图书管理系统等,从而更好地理解和掌握面向对象编程的基础知识和方法。 通过面向对象编程基础课程的学习,学生将能够掌握Java语言的基本语法和面向对象编程的基本概念,提高他们的编程能力和问题解决能力,为以后的学习和工作打下扎实的基础。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值