标签:xtend code ret stc 信息 scanner art http +=
一、实验内容
任务一
1.参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装alibaba 插件,解决代码中的规范问题。
2.在IDEA中使用工具
(Code->Reformate Code)
把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能。提交截图,加上自己学号水印。
public class CodeStandard { 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()); if(buffer.capacity()<20) buffer.append("1234567"); for(int i=0; i<buffer.length();i++) System.out.println(buffer.charAt(i)); } }
实验步骤
1.安装alibaba插件
File->Settings->Plugins->Marketplace
2.完成代码格式化
编码规约扫描
这是扫描后的代码,有两处问题,原因都是在if/else/for/while/do语句中未使用大括号
这是完成格式化之后的代码
3.Code菜单使用
我觉得比较好用的是
Override Methodshe 和 Surround With
任务二
在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;
提交搭档项目git log的截图,包含上面git commit的信息,并加上自己的学号水印信息。
1.搭档项目加入
通过邀请,可以看到搭档已加入
2.下载搭档代码
//搭档代码
public class Complex { double RealPart=0; double ImagePart=0; public Complex(){} public Complex(double RealPart,double ImagePart){ this.RealPart=RealPart; this.ImagePart=ImagePart; } public double getRealPart(){ return RealPart; } public double getImagePart(){ return ImagePart; } public String toString(){ String s = ""; double r=RealPart; double i=ImagePart; if(r==0&&i==0){ s="0"; } else if(r==0&&i!=0){ s=i+"i"; } else if(r!=0&&i==0){ s=r+""; } else if(r!=0&&i<0){ s=r+""+i+"i"; } else { s=r+"+"+i+"i"; } return s; } public boolean equals(Object obj){ if(this==obj){ return true; } else return false; } public Complex ComplexAdd(Complex a){ return new Complex(RealPart+a.getRealPart(),ImagePart+a.getImagePart()); } public Complex ComplexSub(Complex a){ return new Complex(RealPart-a.getRealPart(),ImagePart-a.getImagePart()); } public Complex ComplexMulti(Complex a){ double r=RealPart*a.getRealPart()-ImagePart*a.getImagePart(); double i =ImagePart*a.getRealPart()+RealPart*a.getImagePart(); return new Complex(r,i); } public Complex ComplexDiv(Complex a){ double r=(RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart); double i=(ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart); return new Complex(r,i); } }
测试代码
import junit.framework.TestCase; public class ComplexTest extends TestCase { private Complex a = new Complex(1, 5); private Complex b = new Complex(2, -3); public void testEquals() { assertTrue(a.equals(a)); assertFalse(b.equals(a)); } public void testToString() { assertEquals("1.0+5.0i", a.toString()); assertEquals("2.0-3.0i", b.toString()); } public void testComplexAdd() { assertEquals("3.0+2.0i", a.complexadd(b).toString()); assertEquals("4.0-6.0i", b.complexadd(b).toString()); } public void testComplexSub() { assertEquals("-1.0+8.0i", a.complexsub(b).toString()); assertEquals("0", b.complexsub(b).toString()); } public void testComplexMulti() { assertEquals("-24.0+10.0i",a.complexmulti(a).toString()); assertEquals("17.0+7.0i",a.complexmulti(b).toString()); } public void testComplexDiv() { assertEquals("0.38461538461538464+13.0i",a.complexdiv(a).toString()); } }
3.Junit测试截图
4.代码提交
-在虚拟机里进入主目录
输入git clone 搭档码云链接
-然后主目录下就有了你搭档的仓库
-进入仓库,输入调试好的代码
git add . ->git commit -m "" ->git push
可以看到代码已经上传成功
任务三
http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA
完成重构内容的练习,下载搭档的代码,至少进行三项重构,提交重构后代码的截图,加上自己的学号水印。提交搭档的码云项目链接。
1.重构
用搭档的Complex为例
重构前代码见任务二
重构中的问题
1.命名未采用驼峰形式
2.重写的方法没有加入@Override
3.类没有添加创建者信息
//重构后代码 import org.jetbrains.annotations.Contract; /** * @author 20175120pyc */ @SuppressWarnings("ALL") public class Complex { double realpart = 0; double imagepart = 0; public Complex() { } Complex(double realpart, double imagepart) { this.realpart = realpart; this.imagepart = imagepart; } public double getRealPart() { return realpart; } private double getImagepart() { return imagepart; } @Override public String toString() { String s = ""; double r = realpart; double i = imagepart; if (r == 0 && i == 0) { s = "0"; } else if (r == 0 && i != 0) { s = i + "i"; } else if (r != 0 && i == 0) { s = r + ""; } else if (r != 0 && i < 0) { s = r + "" + i + "i"; } else { s = r + "+" + i + "i"; } return s; } @Override @Contract(value = "null -> false", pure = true) public boolean equals(Object obj) { if (this == obj) { return true; } else { return false; } } public Complex complexadd(Complex a) { return new Complex(realpart + a.getRealPart(), imagepart + a.getImagepart()); } public Complex complexsub(Complex a) { return new Complex(realpart - a.getRealPart(), imagepart - a.getImagepart()); } public Complex complexmulti() { return complexmulti(); } public Complex complexmulti(Complex a) { double r = realpart * a.getRealPart() - imagepart * a.getImagepart(); double i = imagepart * a.getRealPart() + realpart * a.getImagepart(); return new Complex(r, i); } Complex complexdiv(Complex a) { double r = (realpart * a.imagepart + imagepart * a.realpart) / (a.imagepart * a.imagepart + a.realpart * a.realpart); double i = (imagepart * a.imagepart + realpart * a.realpart) / (a.realpart * a.realpart + a.realpart * a.realpart); return new Complex(r, i); } }
代码规约扫描截图
搭档码云链接:https://gitee.com/zxjacdl/zxj175221/tree/master/20175120pyc
任务四
以结对的方式完成Java密码学相关内容的学习,结合重构,git,代码标准。
提交学习成果码云链接和代表性成果截图,要有学号水印。
import java.util.Scanner; /** * @author 20175120pyc */
@SuppressWarnings("All") public class Caeser { /** 定义密钥字母表 */ private String alphabet; /** 定义密钥 key */ private int key; public Caeser(String alphabet, int key) { // 根据不同的字母表和不同的密钥生成一个新的凯撒算法,达到通用的目的 super(); this.alphabet = alphabet; this.key = key; } public String encode(String from) { //凯撒加密算法,传入明文字符串,返回一个密文字符串 String to = ""; for (int i = 0; i < from.length(); i++) { to += alphabet.charAt((alphabet.indexOf(from.charAt(i))+key)%alphabet.length()); } return to; } public static void main(String[] args) { Caeser caeser = new Caeser("abcdefghijklmnopqrstuvwxyz", 3); Scanner scanner = new Scanner(System.in); System.out.println("请输入要加密的字符串"); //输入字符串 String str =scanner.nextLine(); //调用加密方法进行加密 String result = caeser.encode(str); // 得到结果 System.out.print(result); } }
运行截图
二、实验过程中的问题与解决
问题:添加创建者信息时,完成添加后仍然显示格式错误
解决:
/** * @author 20175120pyc */ //在这里添加 @SuppressWarnings("All")
标签:xtend code ret stc 信息 scanner art http +=
原文地址:https://www.cnblogs.com/1751-pyc/p/10802915.html