【牛客带你学编程Java方向】项目练习第2期(截止2.27)


Java项目练习:第2期
练习时间:2月6日-2月27日(2周,除去春节)
活动规则:
  • 每一期一个项目,届时会开新帖发布     
  • 学员直接将答案提交到该贴评论区即可     
  • 两周后,公布导师参考答案     
  • 导师评选出当期最佳代码(将被设置为精彩回复

奖励:牛客大礼包一份(牛客定制水杯 牛客定制笔 牛客定制程序员徽章 滑稽抱枕)
参与方式:直接将你的代码回复到本帖评论区

-----------------------------------------------------

本期题目:

笔记本(30分钟)

需求描述:
  • 能存储记录
  • 不限制能存储的记录条数
  • 能知道已经存储的记录的数量
  • 能查看每一条记录
  • 能删除一条记录
  • 能列出所有的记录

考察知识点:
  • 面向对象基础
  • 类的封装特性
  • 基本集合类的使用

参考知识点:《java基础入门》第3章和第7章


参与方式:直接将你的代码回复到本帖评论区

全部评论
package notebook; /**  * 利用LinkedHashMap容器完成笔记本功能  * 学炜写于2018.2.8  * 贴出代码以及输出结果  */ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; public class notebook {     public static void main(String[] args) {             LinkedHashMap<Integer,String> linkedHashMap = new LinkedHashMap<Integer,String>();//新建一个笔记本(利用LinkedHashMap容器来存储记录与索引)              //能存储记录,并且不限制能存储的记录条数             linkedHashMap.put(1,"谢谢");             linkedHashMap.put(2,"牛客");              linkedHashMap.put(3,"带我");              linkedHashMap.put(4,"学习");              linkedHashMap.put(5,"JAVA");              //能知道已经存储的记录的数量              int count=linkedHashMap.size();              System.out.println("笔记本记录的数量为:"+count);              //查看某条记录                           System.out.println("查看到的第1条记录内容为:"+linkedHashMap.get(1));              //删除某条记录              System.out.println("删除的第1条记录内容为:"+linkedHashMap.remove(1));              //重新加回第一条记录              //列出所有的记录              Iterator<Entry<Integer, String>> iter = linkedHashMap.entrySet().iterator();              while(iter.hasNext()){                  Map.Entry<Integer, String> entry = iter.next();                    System.out.println("第"+entry.getKey()+"条记录内容为:"+entry.getValue());              }     }      } //控制台输出的内容如下:  /*  笔记本记录的数量为:5  查看到的第1条记录内容为:谢谢  删除的第1条记录内容为:谢谢  第2条记录内容为:牛客  第3条记录内容为:带我  第4条记录内容为:学习  第5条记录内容为:JAVA  */
点赞 回复 分享
发布于 2018-02-08 00:52
import java.util.ArrayList;import java.util.List; public class NoteBook { private List<String> list; public NoteBook(){ list = new ArrayList(); } public NoteBook(List<String> list){ this.list = list; } //存储记录 //不限制能存储的记录条数 public String save(String s){ if (list.add(s)) { return "存储成功"; } return "存储失败"; } //获取记录条数 public int getSum() { return list.size(); } //查看每条记录 public String getAnyOne(int i) { if (i > 0 && i < list.size()) { return list.get(i); } return "没有此记录"; } public String getAnyOne() { if (list.size() == 0) { return "没有此记录"; } return list.get(0); } //删除记录 public String deleteOne() { if (list.size() == 0) { return "没有此记录"; } return list.remove(0); } public String deleteOne(int i) { if (i > 0 && i < list.size()) { list.remove(i); return "已经移除"; } return "没有此记录"; } public void showAll() { list.forEach(s-> System.out.println(s)); } }
点赞 回复 分享
发布于 2018-02-08 22:13
package notebook; import java.util.LinkedHashMap; public class notebook {     public static void main(String[] args) {         LinkedHashMap<Integer,String> notebook=new LinkedHashMap<Integer,String>();         //存储记录         notebook.put(1, "Happy");         notebook.put(2, "New");         notebook.put(3, "Year");         notebook.put(4, "Everyone");         //获取存储记录的数量         int num=notebook.size();         System.out.println("存储记录的数量为:"+num);         //查看某一条记录         System.out.println("第1条记录为:"+notebook.get(1));         //列出所有记录         for(int i=0;i<num;i++)         {             System.out.println(notebook.get(i));         }         //删除某一条记录         notebook.remove(2);         //列出所有记录         for(int i=0;i<num;i++)         {             System.out.println(notebook.get(i));         }    } }
点赞 回复 分享
发布于 2018-02-08 22:45
import java.util.*; public class NoteBook {     private ArrayList<String> Records;     private int savedNumbersOfRecords;     public NoteBook(){         Records= new ArrayList<String>();         savedNumbersOfRecords=0;     }     //增加一条记录     public int saveRecord(String string){         Records.add(string);         return ++savedNumbersOfRecords;     }     //获取记录数目     public int getSavedNumbersOfRecords(){         return savedNumbersOfRecords;     }     //获取某条记录     public String getRecordByIndex(int index){         return Records.get(index);     }     //删除某条记录     public int deleteRecordByIndex(int index){         Records.remove(index);         return --savedNumbersOfRecords;     }     //列出所有记录     public void listAllRecords(){         System.out.println("笔记本记录列表:");         for(int i=0;i<savedNumbersOfRecords;i++){             System.out.println(this.getRecordByIndex(i));         }         System.out.println("笔记本列表打印完毕");     }     public static void main(String[] args){         NoteBook noteBook= new NoteBook();         //添加记录         noteBook.saveRecord("第一条记录");         noteBook.saveRecord("第二条记录");         noteBook.saveRecord("第三条记录");         //获取记录总数         System.out.println("获取记录总数:"+noteBook.getSavedNumbersOfRecords());         //列出记录列表         noteBook.listAllRecords();         //获取第二条记录         System.out.println("获取第二条记录:"+noteBook.getRecordByIndex(1));         //删除第二条记录         noteBook.deleteRecordByIndex(1);         //列出记录列表         noteBook.listAllRecords();     } }
点赞 回复 分享
发布于 2018-02-09 18:18
import java.util.ArrayList;import java.util.List; public class NoteBook { private List<String> list; public NoteBook(){ list = new ArrayList(); } public NoteBook(List<String> list){ this.list = list; } //存储记录 //不限制能存储的记录条数 public String save(String s){ if (list.add(s)) { return "存储成功"; } return "存储失败"; } //获取记录条数 public int getSum() { return list.size(); } //查看每条记录 public String getAnyOne(int i) { if (i > 0 && i < list.size()) { return list.get(i); } return "没有此记录"; } public String getAnyOne() { if (list.size() == 0) { return "没有此记录"; } return list.get(0); } //删除记录 public String deleteOne() { if (list.size() == 0) { return "没有此记录"; } return list.remove(0); } public String deleteOne(int i) { if (i > 0 && i < list.size()) { list.remove(i); return "已经移除"; } return "没有此记录"; } public void showAll() { list.forEach(s-> System.out.println(s)); } public static void main(String[] ages){ NoteBook noteBook = new NoteBook(); noteBook.save("记录0"); noteBook.save("记录1"); noteBook.save("记录2"); noteBook.save("记录3"); System.out.println("插入结果:"+noteBook.save("记录4")); System.out.println("删除结果:"+noteBook.deleteOne()); System.out.println("获得条目: "+noteBook.getSum()); System.out.println("列出一条: "+noteBook.getAnyOne(1)); noteBook.showAll(); } }
点赞 回复 分享
发布于 2018-02-09 22:48
/**  * 利用I/O编程写的比较功能简单的记事本  */ package notepad; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextArea; public class Notepad extends JFrame implements ActionListener{  //定义需要的组件、  JTextArea jta=null;  //定义菜单条  JMenuBar jmb=null;  //定义菜单条里面的菜单选项  JMenu ju=null;  //定义菜单选项里面的内容  JMenuItem jmi=null;  JMenuItem jmi1=null;     public static void main(String args[]){      Notepad np=new Notepad();     }  //定义构造方法用来初始化组件  public Notepad(){   jta=new JTextArea();   jmb=new JMenuBar();   ju=new JMenu("文件");   jmi=new JMenuItem("打开");   //给文本域注册监听   jmi.addActionListener(this);   //添加一个打开命令   jmi.setActionCommand("open");   jmi1=new JMenuItem("保存");   jmi1.addActionListener(this);   //添加一个保存命令   jmi1.setActionCommand("save");   //将文本域添加到容器里面去   this.add(jta);   //将菜单条添加到文本域里面   this.setJMenuBar(jmb);   //将菜单选项添加到菜单条里面   jmb.add(ju);   //给菜单选项添加内容     ju.add(jmi);      ju.add(jmi1);   //设置大小和默认关闭方式   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //设置窗体大小   this.setSize(500, 500);   //设置可见性   this.setVisible(true);  }  /* (non-Javadoc)   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)   */  @Override  public void actionPerformed(ActionEvent arg0) {   // TODO Auto-generated method stub   if(arg0.getActionCommand().equals("open")){    //使用JFileChooser来弹出文件选择面板    JFileChooser jfc=new JFileChooser();    //给文件面板起一个名字    jfc.setDialogTitle("请选择文件");    //选择默认打开方式    jfc.showOpenDialog(null);    //用来得到文件路径    String path=jfc.getSelectedFile().getPath();    //显示    jfc.setVisible(true);    FileReader fr=null;    BufferedReader br=null;    try {     fr=new FileReader(path);     br=new BufferedReader(fr);     //建立一个字符串用来存储读到的字节     String s="";     //用一个字符串来存储最终结果     String allcon="";     while((s=br.readLine())!=null){      allcon=allcon+s+"\r\n";      //将字符串写到文本区域里面      jta.setText(allcon);     }    } catch (Exception e) {     // TODO Auto-generated catch block     e.printStackTrace();    }finally{     try {      fr.close();      br.close();     } catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();     }        }           }else if(arg0.getActionCommand().equals("save")){    //使用JFileChooser来弹出保存面板    JFileChooser jfc=new JFileChooser();    //给文件设置一个标题    jfc.setDialogTitle("文件另存为........");    //设置文件默认方式显示    jfc.showSaveDialog(null);    jfc.setVisible(true);    FileWriter fw=null;    FileReader fr=null;    BufferedWriter bw=null;    BufferedReader br=null;    //用来得到文件路径    String paths=jfc.getSelectedFile().getPath();    try {    // fr=new FileReader(jfc.get);    // fw=new FileWriter(paths);    // bw=new BufferedWriter(fw);     bw.write(this.jta.getText());      //  String s="";     //   String allstring=jta.getText();     //   while(bw.!=null){      //   bw.write(s);     //   }         } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }finally{     try {      bw.close();      fw.close();          } catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();     }    }   }  } }
点赞 回复 分享
发布于 2018-02-19 10:23
import java.util.ArrayList; import java.util.Iterator; import java.util.List; interface Notepad {  // 能存储记录  // 不限制能存储的记录条数  public void addNote(String note);  // 能知道已经存储的记录的数量  public int getNoteLength();  // 能查看每一条记录  public String getOne(int position);  // 能删除一条记录  public String deleteOne(int position);  // 能按笔记内容删除一条笔记  public String deleteOne(String note);  // 能列出所有的记录  public String getAllNotes(); } public class NotepadbyList implements Notepad {  private List<String> notes;  // 能存储记录  // 不限制能存储的记录条数  public void addNote(String note) {   notes.add(note);  }  // 能知道已经存储的记录的数量  public int getNoteLength() {   return notes.size();  }  // 能查看每一条记录  public String getOne(int position) {   return notes.get(position);  }  // 能删除一条记录  public String deleteOne(int position) {   return notes.remove(position);  }  // 能按笔记内容删除一条笔记  public String deleteOne(String note) {   Iterator<String> iterator = notes.iterator();   while (iterator.hasNext()) {    String str = iterator.next();    if (str.equals(note)) {     iterator.remove();     return note;    }   }   return "";  }  // 能列出所有的记录  public String getAllNotes() {   StringBuilder sb = new StringBuilder();   for (String string : notes) {    sb.append(string);   }   return sb.toString();  }  // 构造函数  public NotepadbyList() {   notes = new ArrayList<String>();  }  // 测试方法  public static void main(String args[]) {   NotepadbyList np = new NotepadbyList();   np.addNote("好好学习");// 添加笔记   np.addNote("天天向上");// 添加笔记   np.addNote("少玩游戏");// 添加笔记   np.addNote("多刷牛客");// 添加笔记   System.out.println(np.getNoteLength());// 这儿应该输出4   System.out.println(np.getOne(3));// 这儿应该输出多刷牛客   System.out.println(np.getAllNotes());// 这儿应该输出好好学习天天向上少玩游戏多刷牛客   System.out.println(np.deleteOne(0));// 这儿应该输出好好学习   System.out.println(np.deleteOne("天天向上"));// 这儿应该输出天天向上   np.addNote("好好学习");// 添加笔记   np.addNote("天天向上");// 添加笔记   System.out.println(np.getAllNotes());// 这儿应该输出少玩游戏多刷牛客好好学习天天向上  } }
点赞 回复 分享
发布于 2018-02-26 21:44
import java.util.ArrayList; public class Notepad {  public String item;  public ArrayList list;  public Notepad() {   list=new ArrayList();   this.item=null;  }  public Notepad(String item){   list=new ArrayList();   list.add(item);   this.item=item;  }   public void add(String item) {   list.add(item);  };  public void check() {   System.out.println(list);  };  public void check(int index) {   if(index>getcount()) {System.out.println("超出记录范围");}   else System.out.println(index+"位置处的记录是:"+list.get(index));}  public int getcount() {   return list.size();  }  public void dele(String item) {list.remove(item);};  public static void main(String [] args) {   Notepad note=new Notepad("hello");   note.add("hi");   int cc=note.getcount();   System.out.println(cc);   note.check();   note.dele("hi");   note.add("I am");   note.add("Lemon");   note.check();   note.check(2);   note.check(10);  }
点赞 回复 分享
发布于 2018-03-09 09:49
package com.niuke_1; import java.util.ArrayList;import java.util.List; public class demo_2 { private List<String> notes = new ArrayList<String>(); public void add(String note){ notes.add(note); } public void add(int location,String note){ notes.add(location,note); } public int getSize(){ int size=notes.size(); return size; } public String getNote(int index){ return notes.get(index); } public void removeNote(int index){ notes.remove(index); } public String[] list(){ int size=getSize(); String[] result_set = new String[size]; notes.toArray(result_set); for (int i = 0; i < notes.size(); i++) { result_set[i]=notes.get(i); } return result_set; } public static void main(String[] args) { // String[] a = new String[2];// a[0] = "first";// a[1] = "second"; demo_2 notebook = new demo_2(); notebook.add("好好学习"); notebook.add("天天向上"); notebook.add(2,"走上人生巅峰"); System.out.println(notebook.getSize()); // System.out.println(notebook.getNote(0));// notebook.removeNote(1); String nb[]=notebook.list(); for (String s:nb) { System.out.println(s); }// for (int i=0; i<nb.length; i++){// System.out.println(nb[i]);// } }}
点赞 回复 分享
发布于 2018-04-11 16:26

相关推荐

10-16 14:02
南京大学 Java
淘天转正给我挂了,意料之中,但还是很伤心,我很怀疑自己&nbsp;你很优秀,可惜我们今年最不缺的就是优秀的人&nbsp;我是呢种很容易自我伤害的人,如果我的朋友背叛了我,我第一反应不是愤怒,而是自责,责备自己认人不清&nbsp;陌生的城市很大,大到没有容纳自己的地方&nbsp;出门的路口拐过去也不知道会到哪里去&nbsp;这一切就像一个巨大的他妈的袋鼠,狠狠的给了我一拳&nbsp;我觉得这样不好,我这样的人不适合呆在互联网,我的一个同学就很适合,我原以为她不适合的&nbsp;有一天面试,面试官问我,如果明天项目就要上线了,今天出现了一个重大严重bug,加班也解决不了,你怎么办?&nbsp;我叹了口气,想了想说,准备一下简历,晚上加班投一波&nbsp;最近秋招运气很差,和...
榨出爱国基因:很简单,我进厂不就是了,说完,他的气息不再掩饰,显露而出,再回流水线,竟是大专巅峰修为!一瞬间,流水线再次一寂,只见他挥手间就飞出三只蛊虫,一转苦力蛊,二转牛马蛊,三转吗喽蛊! 有趣,真有趣,精彩,实在是精彩。 我乃大专巅峰!谁敢叼我 谁能叼我! 又听他低吟: 电子厂中寒风吹 流水线上大神归 无休倒班万人退,本科悔而我不悔! 夜深, 他牢牢占据工位,转身低眉道:不过是些许夜班罢了
点赞 评论 收藏
分享
一个帅气潇洒的豆子:小公司
点赞 评论 收藏
分享
刻苦的花生米刷了100道题:项目经历里面多写点技术点
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务