题集4~6的总结

我的第二次博客~~~🔢


目录:  1.前言

            2.设计与分析

           3.采坑心得

           4.改进建议

           5.总结


1.前言


①三次题目集的知识点:

  •   正则表达式的使用 --字符串的处理 --分割、信息的提取
  • Array List
  • Hashmap
  • 类的继承 子类与父类
  • Collections.sort()排序方法的使用 (compareTo()方法)
  • 实现接口 
  • 抽象类
  • 方法的覆盖和重写
  • 类的多态
  • 类与类之间的关系
  • 题目设计思路的采取--采用恰当的设计思路可以事半功倍

② 题量

    题量不大,一次作业最多只有三个题,比前三次题集的题量还少些,继续保持!  

③ 难度

   第四次题集难度其实不大,校园角色类设计-1这道题主要考察的是类与类之间的继承,掌握好继承,就能很快解决,很好上手。设计一个学生类和它的一个子类——本科生类这道题更不必说了,代码思路全都在题目中清楚给出,根据题意能很快写出。相比之下,最有难度的一道是,答题判题程序-4,是前三次题集的迭代,这次增加的内容是选择题和填空题,有答案部分正确的可能性,可能性增加,需要考虑的方面也骤增,不过有前几次的经验在身上也能解决。🥳🥳🥳

    第五次题集,共三道题,有俩道题是阅读程序进行改错,其实把错误代码copy到eclipse中显示出错误,再加上调试也就基本上差不多了。但是其中一道多改了一处错误反而不对,这里向室友求助了。🤣本次题集最有挑战性的一道是家居强电电路模拟程序-1,对于电路我不太熟。看完题目更不太熟了哈哈哈🤣🤣🤣。后面也是在与同学交流中终于得到解决。🥳🥳🥳

     第六次题集,只有一题--家居强电电路模拟程序-2,写这道题的时候,我大改了家居强电电路模拟程序-1的代码,我发现其实设备两根引脚其实并没有什么作用,我采用计算电阻来分配电压的方法来计算亮度、转速。这样思路更加清晰。写这次迭代让我学着处理多个类,受益匪浅。😎😎😎


2.设计与分析


这里我对答题判题程序-4、家居强电电路模拟程序-1和家居强电电路模拟程序-2进行了代码分析,其余的在前言中有些赘述,简单的就不多说啦。🙇‍♀️

  • 答题判题程序-4

(类图设计):

在第三次的基础上我新增了选择题,填空题的类,判分情况有完全正确,部分正确和错误,把对应的题目处理放入相应的类中分情况而定。根据题型不同,分不同的情况来判分,最主要的就是要多方考虑、分类各种不同的情况。这要求逻辑清晰,考虑周到。🔢🔢🔢

  • 家居强电电路模拟程序-1

(类图设计):

比起前面答题判题程序这个题目的类建的多,采用继承和多态,而且新增了接口的实现,还用上了Collections.sort()的排序方法,便捷了许多操作。这个题目写的时候很崩溃,一直觉得自己看不懂,第一遍写的时候过测试点很勉强,本来还一个测试点还没过,后面在同学坚持不懈的帮助下终于过了。但后面在补练中,copy过去最后几个测试点竟然没过,又根据我后面写的家居强电电路模拟程序-2的代码改了下,才顺利通过全部测试点。

  • 家居强电电路模拟程序-2

(类图设计):

这次迭代多加了一个落地扇类,一个并联类,继承和多态已经能熟练使用,复杂度更高。🤡🤡🤡

 


3.踩坑心得


  • 非零返回:数组越界问题---常见问题:数组遍历时访问了指向null的对象

          解决方法:用eclipse调试运行后能给出出错的具体位置,然后对症下药就行啦。

这次写题集过程中,这个问题出现不多,只见过一俩次,比较前三次写的答题判题程序的迭代,有很大的改善。真的很不错!🤟🤟🤟

  • 代码设计思路的恰当选择:家居强电电路模拟程序-1的时候真的很犯难,看不懂设备的两根引脚干嘛用的,总觉得题目给了一定要用,一直对引脚上做文章,导致我一度带偏,写代码写的很煎熬,很多次想放弃 。后面写家居强电电路模拟程序-2这道题目的时候,发现只要根据各个部分的电阻来分配电压再计算亮度或者转速就可以了,完全可以不需要用到设备的俩根引脚,不一定要设置引脚的输入电压、输出电压。所以写家居强电电路模拟程序-1的时候,测试点过的很勉强,写完家居强电电路模拟程序-2后,又再2的基础上删去一些部分,改动输入匹配部分去过补练的测试点就很简单了。

         下面是我写家居强电电路模拟程序-1第一次和作业补练的源代码对比:

题集5:(仅挑出有区别的部分)

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String s=input.nextLine();  //接收连接信息
		ArrayList<String> circuit=new ArrayList<>();
		ArrayList<String> operate=new ArrayList<>();
		Pattern p1 = Pattern.compile("\\[(.*)\\s+(.*)\\]");
		Pattern p2 = Pattern.compile("#(.*)");
        Matcher m1 ;
        Matcher m2;
		while(!s.equals("end")) {
			m1= p1.matcher(s);
			m2=p2.matcher(s);
			if(m1.matches()) {
				circuit.add(m1.group(1));
				circuit.add(m1.group(2));
			}
			if(m2.matches()) {
				operate.add(m2.group(1));
			}
			s=input.nextLine();
		}
		Concatenation first=new Concatenation();
		Pattern p3 = Pattern.compile("(K|F|L|B|R|D)(\\d+)-(1|2)");
		Matcher m3;
		Equipment e;
		for(int i=0;i<circuit.size();i++) {
			m3=p3.matcher(circuit.get(i));
			if(m3.matches()) {
				if(m3.group(3).equals("1")) {
					switch(m3.group(1)) {
					case "K":
						e=new Switch(Integer.parseInt(m3.group(2)));
						first.addEquipment(e);
						break;
					case "F":
						e=new Binning(Integer.parseInt(m3.group(2)));
						first.addEquipment(e);
						break;
					case "L":
						e=new Continuou(Integer.parseInt(m3.group(2)));
						first.addEquipment(e);
						break;
					case "B":
						e=new Incandescent(Integer.parseInt(m3.group(2)));
						first.addEquipment(e);
						break;
					case "R":
						e=new Fluorescent(Integer.parseInt(m3.group(2)));
						first.addEquipment(e);
						break;
					case "D":
						e=new Fan(Integer.parseInt(m3.group(2)));
						first.addEquipment(e);
						break;
					}
				}
			}
		}//
		Pattern p4 = Pattern.compile("(K|F|L)(\\d+)(.*)");
		Matcher m4;
		for(int i=0;i<operate.size();i++) {
			m4=p4.matcher(operate.get(i));
			if(m4.matches()) {
				switch(m4.group(1)) {
				case "K":
					for(int j=0;j<first.equipment.size();j++) {
						if(first.equipment.get(j)instanceof Switch&&first.equipment.get(j).number==Integer.parseInt(m4.group(2))) {
							((Switch) first.equipment.get(j)).changge();
							break;
						}
					}
					break;
				case "F":
					for(int j=0;j<first.equipment.size();j++) {
						if(first.equipment.get(j)instanceof Binning) {
							if(m4.group(3).equals("+")) {
								((Binning) first.equipment.get(j)).upbin();
							}else {
								((Binning) first.equipment.get(j)).downbin();
							}
							break;
						}
					}
					break;
				case "L":
					for(int j=0;j<first.equipment.size();j++) {
						if(first.equipment.get(j)instanceof Continuou) {
							Pattern p5 = Pattern.compile(":(.*)");
							Matcher m5=p5.matcher(m4.group(3));
							if(m5.matches()) {
								((Continuou) first.equipment.get(j)).setbin(Double.parseDouble(m5.group(1)));
							}
						}
					}
				}
			}
		}
		double v=220.0;//记录当时电位差
		for(int i=0;i<first.equipment.size();i++) {  
			if(first.equipment.get(i)instanceof Switch) {
				((Switch) first.equipment.get(i)).setoutput(v);
				v=((Switch) first.equipment.get(i)).getv();
				((Switch) first.equipment.get(i)).show();
			}
		}
		for(int i=0;i<first.equipment.size();i++) {  
			if(first.equipment.get(i)instanceof Binning) {
				((Binning) first.equipment.get(i)).setoutput(v);
				v=((Binning) first.equipment.get(i)).getv();
				((Binning) first.equipment.get(i)).show();
			}
		}
		for(int i=0;i<first.equipment.size();i++) {  
			if(first.equipment.get(i)instanceof Continuou) {
				((Continuou) first.equipment.get(i)).setoutput(v);
				v=((Continuou) first.equipment.get(i)).getv();
				((Continuou) first.equipment.get(i)).show();
			}
		}
		for(int i=0;i<first.equipment.size();i++) {  
			if(first.equipment.get(i)instanceof Incandescent) {
				((Incandescent) first.equipment.get(i)).setlight(v);
				((Incandescent) first.equipment.get(i)).show();
			}
		}
		for(int i=0;i<first.equipment.size();i++) {  
			if(first.equipment.get(i)instanceof Fluorescent) {
				((Fluorescent) first.equipment.get(i)).setlight(v);
				((Fluorescent) first.equipment.get(i)).show();
			}
		}
		for(int i=0;i<first.equipment.size();i++) {  
			if(first.equipment.get(i)instanceof Fan){
				((Fan) first.equipment.get(i)).setw(v);
				((Fan) first.equipment.get(i)).show();
			}
		}
	}
}
class Equipment {
	double pins1=0.0;//引脚1输入引脚
	double pins2=0.0;//引脚二输出引脚
	int number;
	public Equipment(int number) {
		super();
		this.number = number;
	}
	public double getv() {
		return Math.abs(pins1-pins2);
	}
}
class Switch extends Equipment{

	public void setoutput(double v) {//当开关状态为1时,输出引脚电位等于输入电位。
		if(state==1) {
			pins2=v;
		}else {
			pins2=0;
		}
	}

}
class Continuou extends Equipment{

	public void setoutput(double v) {
		pins2=bin*v;
	}
}

作业补练中:

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String s = input.nextLine(); // 接收连接信息
		ArrayList<String> sb = new ArrayList<>();
		ArrayList<Equipment> bigchuane = new ArrayList<>();
		ArrayList<Switch> switchs = new ArrayList<>();
		ArrayList<Binning> binnings = new ArrayList<>();
		ArrayList<Continuou> continuous = new ArrayList<>();
		ArrayList<Fan> fans = new ArrayList<>();
		ArrayList<Fluorescent> fluorescents = new ArrayList<>();
		ArrayList<Incandescent> incandescents = new ArrayList<>();
		while (!s.equals("end")) {
			sb.add(s); // 储存好了连接信息。
			s = input.nextLine();
		}
		 Pattern p1 = Pattern.compile("\\[(.*) (.*)\\]");
		Pattern p3 = Pattern.compile("#(K|F|L)(\\d+)(.*)");
		for (int i = 0; i < sb.size(); i++) {
			Matcher m1 = p1.matcher(sb.get(i));
			Matcher m3 = p3.matcher(sb.get(i));
			if (m1.find()) {
				Pattern p = Pattern.compile("(K|F|L|B|R|D)(\\d+)-1");
				Matcher m = p.matcher(sb.get(i));
				while (m.find()) {
					Equipment e;
					switch (m.group(1)) {
					case "K":
						e = new Switch(Integer.parseInt(m.group(2)));
						bigchuane.add(e);
						switchs.add((Switch) e);
						break;
					case "F":
						e = new Binning(Integer.parseInt(m.group(2)));
						bigchuane.add(e);
						binnings.add((Binning) e);
						break;
					case "L":
						e = new Continuou(Integer.parseInt(m.group(2)));
						bigchuane.add(e);
						continuous.add((Continuou) e);
						break;
					case "B":
						e = new Incandescent(Integer.parseInt(m.group(2)));
						bigchuane.add(e);
						incandescents.add((Incandescent) e);
						break;
					case "R":
						e = new Fluorescent(Integer.parseInt(m.group(2)));
						bigchuane.add(e);
						fluorescents.add((Fluorescent) e);
						break;
					case "D":
						e = new Fan(Integer.parseInt(m.group(2)));
						bigchuane.add(e);
						fans.add((Fan) e);
						break;
					}
				}
			} else {// 处理操作信息 从大串联中遍历
				if (m3.find()) {
					switch (m3.group(1)) {
					case "K":
						for (int j = 0; j < bigchuane.size(); j++) {
							if (bigchuane.get(j).number == Integer.parseInt(m3.group(2))
									&& bigchuane.get(j) instanceof Switch) {
								((Switch) bigchuane.get(j)).changge();
								break;
							}
						}
						break;
					case "F":
						for (int j = 0; j < bigchuane.size(); j++) {
							if (bigchuane.get(j).number == Integer.parseInt(m3.group(2))
									&& bigchuane.get(j) instanceof Binning) {
								((Binning) bigchuane.get(j)).sbin(m3.group(3));
								break;
							}
						}
						break;
					case "L":
						for (int j = 0; j < bigchuane.size(); j++) {
							if (bigchuane.get(j).number == Integer.parseInt(m3.group(2))
									&& bigchuane.get(j) instanceof Continuou) {
								((Continuou) bigchuane.get(j)).setbin(Double.parseDouble(m3.group(3).substring(1)));
								break;
							}
						}
						break;
					}
				}
			} // 处理好了操作信息。
		} // 处理电路信息完毕,可以输出。 首先进行分配电压!
		// ...  //
		Collections.sort(switchs);
		Collections.sort(binnings);
		Collections.sort(continuous);
		Collections.sort(fans);
		Collections.sort(fluorescents);
		Collections.sort(incandescents);
		for (int i = 0; i < switchs.size(); i++) {
			switchs.get(i).show();
		}
		for (int i = 0; i < binnings.size(); i++) {
			binnings.get(i).show();
		}
		for (int i = 0; i < continuous.size(); i++) {
			continuous.get(i).show();
		}
		for (int i = 0; i < incandescents.size(); i++) {
			incandescents.get(i).show();
		}
		for (int i = 0; i < fluorescents.size(); i++) {
			fluorescents.get(i).show();
		}
		for (int i = 0; i < fans.size(); i++) {
			fans.get(i).show();
		}
	}
}
class Fan extends Equipment implements Comparable<Fan>{
	int w=0;
	public void setw(double v,double r) {
		v=(20/r)*v;   //计算它这一部分电压
		if(Math.abs(v-0)< 1e-8||v<80) {
			w=0;
		}else if(v>=150.0) {
			w=360;
		}else if(v==80.0){
			w=80;
		}else {
			double k=(360-80)/(150-80);
			double b=80-k*80;
			w=(int)(k*v+b);
		}
	}
}
  • if(m.find())与while(m.find())在分组时的的区别

if(m.find())是字符串中一找到要检测的部分就返回true,不会接下去再次寻找剩余部分是否还有符合的。

while(m.find())是检测到后可以进入循环,退出循环后,还能接下去继续寻找匹配的部分,可以实现在一段字符串中多次匹配到要找到的部分。适合有格式要求的信息的多次提取。

从室友那里学习到的方法,在此之前我一直是只会使用if(m.find()),没有发现while(m.find())的便捷之处,哈哈哈哈学废了。🤙🤙🤙

 


4.改进建议


  • 代码规范性

其实这次写代码,规范性是有的,比上次好,也更加注意了代码不要过多的繁杂,越简单易懂是最好。

  • 类中的成员变量应该改为私有,使用private,并设置相应的get和set方法。

将所有成员属性设置为私有优点:可以自己控制读写权限;对于写权限,我们可以检测数据的有效性。即通过if判断输入是否是正确的。

因为这里是写题目,我为了方便处理,并没有把成员变量设置成私有,能减少的get和set方法也都省略了,这对于以后写代码总归是留下了不好的习惯,以后要多加改正,提高写代码的规范性,而不是为了写题而写代码。

这个问题上一次题集也出现了,还是没有改正哈哈哈。。。这次继续!!!


5.总结


    首先,通过这三次题集,我逐渐适应了Java专业课的学习,对作业都能有很好的应对,尽量多花时间在专业课的学习上,做到精益求精。

    其次,通过写这三次题集,我收获良多:

  • 我掌握了继承和多态,extends,instanceof能熟练运用。父类方法的重写和覆盖也学会了。
  • 我还学习接口的实现,接口是一种特殊的抽象类。CompareTo方法,Collections.sort()排序方法能用上,拓宽了我的知识面。
  • 又对正则表达式有了新认识,对正则表达式语法进行了补充和拓宽。
  • 我使用eclipse进行调试,能查出数据的错误,找到逻辑问题出错点,so good!

    然后,以下是我总结出来还需进一步学习和研究的地方:

  • 正则表达式 

正则表达式是常客了,次次都用,好好掌握,事半功倍!没有最好只有更好!咱们敲代码的就是干。。。

  • ArryList的熟练使用

这几次题集我都用到了Array List并且用的地方很多,但也存在着错误使用的地方,比如set()函数的错误使用,该多用,多注意!熟能生巧!!

  • 尽量减少代码复用部分

好的代码应该是简洁,复用性低的,这一点有待改善哦😁😁😁

    最后,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见,我有:

  1. pta提交后测试点的提示还是有必要的,一直不过的测试点没给提示要自己去一个一个的去排查,我是真的有点没耐心,无疑是大海捞针。上次有测试点提示都得折磨好久,才过,这次没提示更折磨人啊,而且类、设备、测试点也越来越多,球球给点提示吧~~~
  2. pta 测试样例能不能多设置几个!!! 样例多来几个也能帮助我们更好的理解题意啊啊啊啊啊~~~

好啦!我的第二次博客就写到这里!!!!撒花!!!💯