20202318 2021-2022-1 《数据结构与面向对象程序设计》实验三报告

20202307 2021-2022-1 《数据结构与面向对象程序设计》实验三报告

课程:《程序设计与数据结构》
班级: 2023
姓名: 柳星宇
学号:20202318
实验教师:王志强
实验日期:2021年10月7日
必修/选修: 必修

 

1.实验内容

  • 初步掌握单元测试和TDD

  • 理解并掌握面向对象三要素:封装、继承、多态

  • 初步掌握UML建模

  • 完成蓝墨云上 (1)-(5)实验。

(1)参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习

(2)参考http://www.cnblogs.com/rocedu/p/4837092.html,以TDD的方式研究学习StringBuffer

(3)参考https://www.cnblogs.com/rocedu/p/4472842.html,对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式

(4)以TDD的方式开发一个复数类Complex

(5)使用StarUML对实验二中的代码进行建模

 

 

 

实验一

public class shiyan1 {
public static String percentage2fivegrade(int grade){
//如果成绩小于0,转成“错误”
if ((grade < 0))
return "错误";
//如果成绩小于60,转成“不及格”
else if (grade < 60)
return "不及格";
//如果成绩在60与70之间,转成“及格”
else if (grade < 70)
return "及格";
//如果成绩在70与80之间,转成“中等”
else if (grade < 80)
return "中等";
//如果成绩在80与90之间,转成“良好”
else if (grade < 90)
return "良好";
//如果成绩在90与100之间,转成“优秀”
else if (grade <= 100)
return "优秀";
//如果成绩大于100,转成“错误”
else
return "错误";
}
}

 

实验二

public class shiyan2 {

public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
}
}

public class test1 {
public class MyUtilTest {
public static void main(String[] args) {
//测试出错情况
if(shiyan1.percentage2fivegrade(-10) != "错误")
System.out.println("test failed 1!");
else if(shiyan1.percentage2fivegrade(115) != "错误")
System.out.println("test failed 2!");
else
System.out.println("test passed!");
}
}
}

public class test3 {
public static void main(String [] args){
StringBuffer buffer = new StringBuffer(20);
buffer.append('S');
buffer.append("tringBuffer");
// System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
// System.out.println(buffer.indexOf("tring12345"));
System.out.println("buffer = " + buffer.toString());
System.out.println(buffer.length());
}
}

public class test4 {
public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
// System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
// System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
System.out.println(buffer.length());
}
}

 

 

 

 

 

 

 

 实验三

public abstract class shiyan4 {
abstract public void DisplayValue();
}
class Integer extends shiyan4 {
int value;
Integer() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Short extends shiyan4 {
short value;
Short() {
value = (short) 20202318;
}
public void DisplayValue(){
System.out.println (value);
}
}
// Pattern Classes
abstract class Factory {
abstract public shiyan4 CreateDataObject();
}
class IntFactory extends Factory {
public shiyan4 CreateDataObject(){
return new Integer();
}
}
class ShortFactory extends Factory {
public shiyan4 CreateDataObject(){
return new Short();
}
}
//Client classes
class Document {
shiyan4 pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
class MyDoc {
static Document d;
public static void main(String[] args) {
d = new Document(new ShortFactory());
d.DisplayData();
}
}

 实验四

 public class test5 { // 用于测试复数类
public static void main(String[] args) {
System.out.println("请用户输入第一个复数的实部和虚部:");
shiyan3 data1 = new shiyan3();
System.out.println("请用户输入第二个复数的实部和虚部:");
shiyan3 data2 = new shiyan3();
// 以下分别为加减乘除
shiyan3 result_add = data1.add(data2);
shiyan3 result_sub = data1.sub(data2);
shiyan3 result_mul = data1.mul(data2);
shiyan3 result_div = data1.div(data2);
result_add.print();
result_sub.print();
result_mul.print();
result_div.print();
}
}

import java.util.Scanner;
public class shiyan3 { // 复数类
double real; // 实部
double image; // 虚部
shiyan3(){ // 不带参数的构造方法
Scanner input = new Scanner(System.in);
double real = input.nextDouble();
double image = input.nextDouble();
Complex(real,image);
}
private void Complex(double real, double image) { // 供不带参数的构造方法调用
// TODO Auto-generated method stub
this.real = real;
this.image = image;
}
shiyan3(double real,double image){ // 带参数的构造方法
this.real = real;
this.image = image;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImage() {
return image;
}
public void setImage(double image) {
this.image = image;
}
shiyan3 add(shiyan3 a){ // 复数相加
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = real + real2;
double newImage = image + image2;
shiyan3 result = new shiyan3(newReal,newImage);
return result;
}
shiyan3 sub(shiyan3 a){ // 复数相减
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = real - real2;
double newImage = image - image2;
shiyan3 result = new shiyan3(newReal,newImage);
return result;
}
shiyan3 mul(shiyan3 a){ // 复数相乘
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = real*real2 - image*image2;
double newImage = image*real2 + real*image2;
shiyan3 result = new shiyan3(newReal,newImage);
return result;
}
shiyan3 div(shiyan3 a){ // 复数相除
double real2 = a.getReal();
double image2 = a.getImage();
double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);
double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);
shiyan3 result = new shiyan3(newReal,newImage);
return result;
}
public void print(){ // 输出
if(image > 0){
System.out.println(real + " + " + image + "i");
}else if(image < 0){
System.out.println(real + "" + image + "i");
}else{
System.out.println(real);
}
}
}

 

 

 

实验五

 

 

 

 

 

 

 

 

 

 

其他(感悟、思考等)

感觉本次任务难度较大,感觉自己无法跟着老师给的网址一步步走下去,越到后边越感到力不从心,以至于有一些消极对待的情绪。不过,更让我感到恐惧的是自己无法只靠自己跟上大家的步伐,于是我虚心请教老师、舍友、学长等可以给我帮助的人。到现在,我基本能照葫芦画瓢地写完一些基础的java代码。更重要的是端正学习的态度,严于律己,把每个任务完成好。

 

posted @ 2021-10-12 22:20  柳星宇  阅读(43)  评论(0编辑  收藏  举报