20155210 潘滢昊 Java第三次实验

Java第三次实验

实验内容

  • 在IDEA中使用工具(Code->Reformate Code)把代码重新格式化
  • 在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例
  • 完成重构内容的练习,下载搭档的代码,至少进行三项重构
  • 以结对的方式完成Java密码学相关内容的学习

实验代码

1:无实验代码,主要学习了IDEA下code列表中的功能

2:

import junit.framework.TestCase;
import org.junit.Test; /**
* Created by liugaole on 17-5-3.
*/
public class ComplexTest extends TestCase { @Test public void testA() throws Exception{ Complex c=new Complex(); Complex c1=new Complex(3,5); Complex c2=new Complex(6,7); c1.Print(); c2.Print(); System.out.println("这两复数和为:"); System.out.println((c.jia(c1, c2).a+"+"+c.jia(c1, c2).b+"i").toString()); System.out.println("这两复数差为:"); System.out.println(c.jian(c1, c2).a+"+"+c.jian(c1, c2).b+"i"); System.out.println("这两复数乘积为:"); System.out.println(c.cheng(c1, c2).a+"+"+c.cheng(c1,c2).b+"i"); System.out.println("这两复数相除为:"); System.out.println(c.chu(c1, c2).a+"+"+c.chu(c1,c2).b+"i"); } @Test public void testB() throws Exception{ Complex c=new Complex(); Complex c1=new Complex(5,8); Complex c2=new Complex(2,3); c1.Print(); c2.Print(); System.out.println("这两复数和为:"); System.out.println((c.jia(c1, c2).a+"+"+c.jia(c1, c2).b+"i").toString()); System.out.println("这两复数差为:"); System.out.println(c.jian(c1, c2).a+"+"+c.jian(c1, c2).b+"i"); System.out.println("这两复数乘积为:"); System.out.println(c.cheng(c1, c2).a+"+"+c.cheng(c1,c2).b+"i"); System.out.println("这两复数相除为:"); System.out.println(c.chu(c1, c2).a+"+"+c.chu(c1,c2).b+"i"); } @Test public void testC() throws Exception{ Complex c=new Complex(); Complex c1=new Complex(6,2); Complex c2=new Complex(1,3); c1.Print(); c2.Print(); System.out.println("这两复数和为:"); System.out.println((c.jia(c1, c2).a+"+"+c.jia(c1, c2).b+"i").toString()); System.out.println("这两复数差为:"); System.out.println(c.jian(c1, c2).a+"+"+c.jian(c1, c2).b+"i"); System.out.println("这两复数乘积为:"); System.out.println(c.cheng(c1, c2).a+"+"+c.cheng(c1,c2).b+"i"); System.out.println("这两复数相除为:"); System.out.println(c.chu(c1, c2).a+"+"+c.chu(c1,c2).b+"i"); }
@Test public void testD() throws Exception{ Complex c=new Complex(); Complex c1=new Complex(5,2); Complex c2=new Complex(1,3); c1.Print(); c2.Print(); System.out.println("这两复数和为:"); System.out.println((c.jia(c1, c2).a+"+"+c.jia(c1, c2).b+"i").toString()); System.out.println("这两复数差为:"); System.out.println(c.jian(c1, c2).a+"+"+c.jian(c1, c2).b+"i"); System.out.println("这两复数乘积为:"); System.out.println(c.cheng(c1, c2).a+"+"+c.cheng(c1,c2).b+"i"); System.out.println("这两复数相除为:"); System.out.println(c.chu(c1, c2).a+"+"+c.chu(c1,c2).b+"i"); }
}

3:

我重构的是N.java

我首先给他进行了格式化,然后把乘法表的范围从2-9扩大到1-9,最后我又添加了上3角的乘法表

public class N{
public static void main(String[] args){
for(int j=1;j<10;j++){
for(int i=1;i<=j;i++){
System.out.printf("%d*%d=%2d ",j,i,i*j);}//变为1-9的乘法表
System.out.println();
}
System.out.println();
for(int j=9;j>=1;j--){
for(int i=1;i<=j;i++){
System.out.printf("%d*%d=%2d ",j,i,i*j);}//变为上三角乘法表
System.out.println();
}
}
}

4:

import java.io.*;
import javax.crypto.*;
public class Skey_DES{
public static void main(String args[])
throws Exception{
KeyGenerator kg=KeyGenerator.getInstance("DESede");
kg.init(168);
SecretKey k=kg.generateKey( );
FileOutputStream f=new FileOutputStream("key1.dat");
ObjectOutputStream b=new ObjectOutputStream(f);
b.writeObject(k);
}
}
import java.io.*;
import java.security.*;
public class Skey_kb{
public static void main(String args[]) throws Exception{
FileInputStream f=new FileInputStream("key1.dat");
ObjectInputStream b=new ObjectInputStream(f);
Key k=(Key)b.readObject( );
byte[ ] kb=k.getEncoded( );
FileOutputStream f2=new FileOutputStream("keykb1.dat");
f2.write(kb);
// 打印密钥编码中的内容
for(int i=0;i<kb.length;i++){
System.out.print(kb[i]+",");
}
}
}
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SDec{
public static void main(String args[]) throws Exception{
// 获取密文
FileInputStream f=new FileInputStream("SEnc.dat");
int num=f.available();
byte[ ] ctext=new byte[num];
f.read(ctext);
// 获取密钥
FileInputStream f2=new FileInputStream("keykb1.dat");
int num2=f2.available();
byte[ ] keykb=new byte[num2];
f2.read(keykb);
SecretKeySpec k=new SecretKeySpec(keykb,"DESede");
// 解密
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte []ptext=cp.doFinal(ctext);
// 显示明文
String p=new String(ptext,"UTF8");
System.out.println(p);
}
}

代码截图

已上传到蓝墨云班课

实验时间表

题目思考时间 代码编辑时间 代码调试时间
时间 120min 120min 60min
上一篇:如何在linux环境安装数据库


下一篇:C++复习1.内存管理的知识