《面向对象程序设计与C++》实验报告
年级、专业、班级 | 2011级计科4班 2011级计科4班 2011级计科4班 | 姓名 | XX | |||
实验题目 | 基于命令行的学生成绩管理系统 | |||||
实验时间 | 2013.10.16~2013.10.23 | 实验地点 | A主414 | |||
实验成绩 | 实验性质 | □验证性 □设计性 ■综合性 | ||||
教师评价: □算法/实验过程正确; □源程序/实验内容提交 □程序结构/实验步骤合理; □实验结果正确; □语法、语义正确; □报告规范; 其他: 评价教师签名: | ||||||
一、实验目的 运用面向对象程序设计思想,使用Java集合框架,实现对学生成绩的管理。 | ||||||
二、实验项目内容 1、设计学生类接口; 2、使用集合框架,实现学生成绩的插入、通过名字查询、显示各科成绩排名列表、显示各科成绩通过学生学号的排名列表。 3、将内存中的集合框架对象进行持久化,下一次运行系统时可直接调用。 | ||||||
3、实验过程或算法(源程序) package filemanager; import java.io.*; import java.security.*; import java.util.*; pto.*; public class FileManager { public static void main(String[] args) throws Exception { MainMenu mainmenu=new MainMenu(); mainmenu.StartMenu(); } } class CreateFile{ CreateFile() throws IOException{ Scanner s=new Scanner(System.in); System.out.println("Where to create?(D:/aaa/...)"); String (); File file=new File(CurrentDirectory); ists()){ System.out.println("Enter the name:"); String (); File f = new File(CurrentDirectory+File.separator+Name) ;// 实例化File类的对象 f.mkdir() ; System.out.println("success to create!"); }else System.out.println("the directory isn't exist!"); } } class DeleteFile{ File path; public DeleteFile(File path){ this.path=path; } public void Delete(File path){ File filelist[]=path.listFiles(); int listlen=filelist.length; for(int i=0;i<listlen;i++){ if(filelist[i].isDirectory()){ Delete(filelist[i]); } else { filelist[i].delete(); } } path.delete();//删除当前目录 } void Success(){ System.out.println("Success to delete!!:"); } } class EnterDirectory{ EnterDirectory(String path) throws IOException{ Runtime().exec(" " + path); } } class ListDirectory{ ListDirectory(String path){ // String luj=null;//路径 //如果dir不以文件分隔符结尾,自动添加文件分隔符 if(!dsWith(File.separator)){ path = path + File.separator; } File dirFile = new File(path); //如果dir对应的文件不存在,或者不是一个文件夹则退出 if(!ists() || (!dirFile.isDirectory())){ System.out.println("List failed!cannot find the directory:"+path); 基于java的课程设计题目 } //list方法返回该目录下的所有文件(包括目录)的文件名,文件名不含路径信息 System.out.println(path+" contains below directorise or files"); String[] files = dirFile.list(); for(int i = 0; i < files.length; i++){ System.out.println(files[i]); } } } class CopyFile{ CopyFile(String source, String dest) throws IOException { File in = new File(source); File out = new File(dest); FileInputStream inFile = new FileInputStream(in); FileOutputStream outFile = new FileOutputStream(out); byte[] buffer = new byte[1024]; int i = 0; while ((i = ad(buffer)) != -1) { outFile.write(buffer, 0, i); }//end while inFile.close(); outFile.close(); System.out.print("Success to copy!"); } } class CopyDirectory{ public void copyFolder(String oldPath, String newPath) { try { (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 File a=new File(oldPath); String[] file=a.list(); File temp=null; for (int i = 0; i < file.length; i++) { dsWith(File.separator)){ temp=new File(oldPath+file[i]); } else{ temp=new File(oldPath+File.separator+file[i]); } if(temp.isFile()){ FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath + "/" + (Name()).toString()); byte[] b = new byte[1024 * 5]; int len; while ( (len = ad(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if(temp.isDirectory()){//如果是子文件夹 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); } } } catch (Exception e) { System.out.println("复制整个文件夹内容操作出错"); e.printStackTrace(); } } public void Success(){ System.out.println("Success to copy the directory!"); } } class TestDES { Key key; public TestDES(){} public TestDES(String str) { getKey(str);//生成密匙 } /** * 根据参数生成KEY */ public void getKey(String strKey) { try { KeyGenerator _generator = Instance("DES"); _generator.init(new Bytes())); this.key = _ateKey(); _generator = null; } catch (Exception e) { throw new RuntimeException("Error initializing SqlMap class. Cause: " + e); } } /* * 文件file进行加密并保存目标文件destFile中 * * @param file 要加密的文件 如c:/ * @param destFile 加密后存放的文件名 如c:/加密后文件.txt */ public void encrypt(String file, String destFile) throws Exception { //【具体用法见主函数】 Cipher cipher = Instance("DES"); // cipher.init(Cipher.ENCRYPT_MODE, getKey()); cipher.init(Cipher.ENCRYPT_MODE, this.key); InputStream is = new FileInputStream(file); OutputStream out = new FileOutputStream(destFile); CipherInputStream cis = new CipherInputStream(is, cipher); byte[] buffer = new byte[1024]; int r; while ((r = ad(buffer)) > 0) { out.write(buffer, 0, r); } cis.close(); is.close(); out.close(); } /* * 文件采用DES算法解密文件 * * @param file 已加密的文件 如c:/加密后文件.txt * * @param destFile * 解密后存放的文件名 如c:/ test/解密后文件.txt */ public void decrypt(String file, String dest) throws Exception { Cipher cipher = Instance("DES"); cipher.init(Cipher.DECRYPT_MODE, this.key); InputStream is = new FileInputStream(file); OutputStream out = new FileOutputStream(dest); CipherOutputStream cos = new CipherOutputStream(out, cipher); byte[] buffer = new byte[1024]; int r; while ((r = is.read(buffer)) >= 0) { cos.write(buffer, 0, r); } cos.close(); out.close(); is.close(); } void DES() throws Exception { System.out.println("inuput encrypted password: "); Scanner scan = new Scanner(System.in); String Line(); //以上两行用来实现字符串的输入。 TestDES td = new TestDES(td1); //【对称加密的密码所在】 System.out.println("encrypt: inuput source file and target file: "); String (); String (); td.encrypt(sourcee, targete); //加密 System.out.println(" encryption is complete!"); System.out.println("decrypt:inuput decryptioned password:"); Scanner scanf = new Scanner(System.in); String Line(); TestDES tdd = new TestDES(td2); System.out.println("decrypt: inuput source file and target file: "); String (); String (); tdd.decrypt(sourced,targetd); //解密 System.out.println(" decryption is complete"); } } class MainMenu{ void StartMenu() throws Exception{ System.out.println("Choose what to do:"); System.out.println("'1':Create a directory"); System.out.println("'2':Delete a directory"); System.out.println("'3':Enter a directory"); System.out.println("'4':List a directory"); System.out.println("'5':Copy a file"); System.out.println("'6':Copy a directory"); System.out.println("'7':Encrypt a file"); System.out.println("'0':Exit"); Scanner s=new Scanner(System.in); int Int(); while(true){ switch(a){ case 1: System.out.println("you choose 1"); new CreateFile(); break; case 2: System.out.println("you choose 2"); System.out.println("input the directory name that you want to delete:"); File pathtodele=new ()); ists()){ DeleteFile DeleteDirectory=new DeleteFile(pathtodele); DeleteDirectory.Delete(DeleteDirectory.path); DeleteDirectory.Success(); }else System.out.println("Cannot find the directory:"+pathtodele); break; case 3: System.out.println("you choose 3"); System.out.println("input the directory that you want to enter:"); String (); new EnterDirectory(pathtoenter); break; case 4: System.out.println("you choose 4"); System.out.println("input the directory name that you want to list:"); String (); new ListDirectory(pathtolist); break; case 5: System.out.println("you choose 5"); System.out.println("input the source file and the target file:"); String (); String (); new CopyFile(sourcef,destf); break; case 6: System.out.println("you choose 6"); CopyDirectory copydirectory= new CopyDirectory(); System.out.println("input the source directory:"); String (); System.out.println("input the target directory:"); String (); pyFolder(sourcedir,destdir); copydirectory.Success(); break; case 7: System.out.println("you choose 7"); TestDES testdes=new TestDES(); testdes.DES(); break; case 0: System.out.println("you choose to exit"); it(0); default: System.out.print("error"); } Int(); } } } | ||||||
4、实验结果及分析和(或)源程序调试过程 | ||||||
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论