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

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

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

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

本期题目:

自定义异常(20分钟)

需求描述:

自定义一个异常类NoThisSoundException和Player类,在Player类的play()方法中使用自定义异常,要求如下:

  1. NoThisSoundException继承自Exception类,类中有一个无参和一个接收一个string类型参数的构造方法,构造方法中都是用super关键字调用父类的构造方法。
  2. Player类中定义一个play(int index)方法,方法接收一个int类型的参数,表示播放歌曲的索引,当index>10时,play()方法用throw关键字抛出NoThisSoundException异常,创建一场对象时,调用的有参构造方法,传入“您播放的歌曲不存在”。
  3. 在测试类中创建Player对象,并调用play()方法测试自定义的NoThisSoundException异常,使用try...catch语句捕获异常,调用NoThisSoundException的getMessage()方法打印异常提示信息。

考查知识点:

  • 面向对象的继承特性
  • 自定义异常类的实现
  • 异常处理的相关知识点

参考知识点:《java基础入门》第4章

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

全部评论
异常类: /**  *   */ package com.finersoft.nowcoder05; /**  * 名称:播放异常类<br/>  * 说明:<br/>  * 作者:筱茜 链接:https://www.nowcoder.com/discuss/70619 来源:牛客网  *   * 需求描述:  *   * 自定义一个异常类NoThisSoundException和Player类,在Player类的play()方法中使用自定义异常,要求如下:  *   * NoThisSoundException继承自Exception类,类中有一个无参和一个接收一个string类型参数的构造方法,构造方法中都是用super关键字调用父类的构造方法。  * Player类中定义一个play(int  * index)方法,方法接收一个int类型的参数,表示播放歌曲的索引,当index>10时,play()方法用throw关键字抛出NoThisSoundException异常,创建一场对象时,调用的有参构造方法,传入“您播放的歌曲不存在”。  * 在测试类中创建Player对象,并调用play()方法测试自定义的NoThisSoundException异常,使用try...catch语句捕获异常,调用NoThisSoundException的getMessage()方法打印异常提示信息。  *   * 考查知识点:  *   * 面向对象的继承特性 自定义异常类的实现 异常处理的相关知识点  *   * 参考知识点:《java基础入门》第4章<br/>  *   * @author Finersoft  * @date 2018年3月27日  */ public class NoThisSoundException extends Exception {     /**      *       */     private static final long serialVersionUID = 5303011482203106325L;     /**      * 无参构造方法,懒人用      */     public NoThisSoundException() {         super("没有一首歌能表达我忧郁的心情");     }     /**      * 带参构造方法      * @param msg      */     public NoThisSoundException(String msg) {         super( msg);     } } 播放器类和测试方法: /**  *   */ package com.finersoft.nowcoder05; /**  * 名称:播放器类<br/>  * 说明:<br/>  * 作者:筱茜 链接:https://www.nowcoder.com/discuss/70619 来源:牛客网  *   * 需求描述:  *   * 自定义一个异常类NoThisSoundException和Player类,在Player类的play()方法中使用自定义异常,要求如下:  *   * NoThisSoundException继承自Exception类,类中有一个无参和一个接收一个string类型参数的构造方法,构造方法中都是用super关键字调用父类的构造方法。  * Player类中定义一个play(int  * index)方法,方法接收一个int类型的参数,表示播放歌曲的索引,当index>10时,play()方法用throw关键字抛出NoThisSoundException异常,创建一场对象时,调用的有参构造方法,传入“您播放的歌曲不存在”。  * 在测试类中创建Player对象,并调用play()方法测试自定义的NoThisSoundException异常,使用try...catch语句捕获异常,调用NoThisSoundException的getMessage()方法打印异常提示信息。  *   * 考查知识点:  *   * 面向对象的继承特性 自定义异常类的实现 异常处理的相关知识点  *   * 参考知识点:《java基础入门》第4章<br/>  *   * @author Finersoft  * @date 2018年3月27日  */ public class Player {     /**      * 歌曲索引开始      */     static int startIndex = 0 ;     /**      * 歌曲索引结束       */     static int endIndex = 10 ;     /**      * 播放音乐      * @param index      * @throws NoThisSoundException      */     public static void play(int index) throws NoThisSoundException {         if (index > startIndex && index <= endIndex) {             System.out.println("我在唱一首歌,一首简单的歌");         } else {             throw new NoThisSoundException("您播放的歌曲不存在");         }     }     /**      * 测试方法      * @param args      */     public static void main(String[] args) {         try {             play(10);             play(11);         } catch (NoThisSoundException e1) {             System.out.println(e1.getMessage());         } catch (Exception e) {             //好的异常习惯是最后要加一个上面异常处理不了时怎么处理的操作             System.out.println("其他异常" + e.getMessage());         }         finally {             //System.out.println("扫尾操作");         }         System.out.println("播放完毕");     } }
点赞 回复 分享
发布于 2018-03-27 11:50
package all_test; class NoThisSoundException extends Exception{     private static final long serialVersionUID = 1972549693000089438L;     public NoThisSoundException() {         super();     }          public NoThisSoundException(String msg) {         super(msg);     } } class Player{     static final int start=0;     static final int end=10;          public static void play(int index) throws NoThisSoundException{         if(index<start||index>end) {             throw new NoThisSoundException("你播放的歌曲不存在");         }         System.out.println("那一天,人们想起了被咸鱼支配的恐惧");     } } public class Test_5 {     public static void main(String[] args) {         try {             System.out.println("歌名:咸鱼突刺");             Player.play(5);             System.out.println("\n歌名:当咸鱼有了梦想");             Player.play(11);         }catch(NoThisSoundException e1) {             System.out.println(e1.getMessage());         }catch(Exception e2) {             System.out.println(e2.getStackTrace());         }finally {             System.out.println("\n咸鱼翻身也逃不出咸鱼的命运");         }         System.out.println("\n咸鱼挂了,完结撒花");     } }
点赞 回复 分享
发布于 2018-03-28 21:04
package com.niuke_1; class NoThisSoundException extends Exception{ public NoThisSoundException(){ super(); } public NoThisSoundException(String x){ super(x); } } class Player{ public void player(int index) throws NoThisSoundException{ if (index>10){ NoThisSoundException no = new NoThisSoundException("您播放的歌曲不存在"); throw no; } } } public class demo_5 { public static void main(String[] args){ Player pl = new Player(); try{ pl.player(21); }catch (NoThisSoundException no){ System.out.println(no.getMessage()); } } }
点赞 回复 分享
发布于 2018-04-11 16:50
/测试类/ public class Test { public static void main(String[] args){ Player p = new Player(); try { p.play(4); p.play(14); } catch (NoThisSoundException e) { System.out.println(e.getMessage()); }catch(Exception e){ System.out.println(e.getMessage()); } finally{ //System.out.println(""); } System.out.println("播放完成"); } } /异常类 NoThisSoundException / public class NoThisSoundException extends Exception{ public NoThisSoundException() { super(); } public NoThisSoundException(String str){ super(str); } } /Player类 / public class Player{ static void play(int index) throws NoThisSoundException{ if(index <= 10){ System.out.println("一首歌。。。"); }else{ throw new NoThisSoundException("您播放的歌曲不存在"); } } } 截图
点赞 回复 分享
发布于 2018-03-30 15:02
class NoThisSoundException extends Exception{     public NoThisSoundException(){         super();     }     public NoThisSoundException(String str){         super(str);     } } class Player{     public void play(int index) throws NoThisSoundException {         if(index>10) {             throw new NoThisSoundException("您播放的歌曲不存在");}         } } public class example {     public static void main(String[] args) {         Player ella=new Player();         try{             ella.play(11);}catch (NoThisSoundException e) {                 // TODO: handle exception                 System.out.println(e.getMessage());             }         }     }
点赞 回复 分享
发布于 2018-03-30 10:07
public class NoThisSoundException extends Exception{          public NoThisSoundException() {         // TODO Auto-generated constructor stub         super();     }          public NoThisSoundException(String msg) {         // TODO Auto-generated constructor stub         super(msg);     } } import org.junit.Test; public class Player {          public void play(int index) throws NoThisSoundException{         if(index>10){             throw new NoThisSoundException("您播放的歌曲不存在!");         }     }          @Test     public void test(){         Player player = new Player();         try {             player.play(12);         } catch (NoThisSoundException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }     } }
点赞 回复 分享
发布于 2018-03-29 14:05
package exercise1; class NoThisSoundException extends Exception{     NoThisSoundException() {         // TODO Auto-generated constructor stub         super();     }          NoThisSoundException(String ms){         super(ms);     } } class Player{     private int Max = 10;     Player(){         super();     }     public void play(int index)throws NoThisSoundException{         if(index>Max)             throw new NoThisSoundException("您播放的歌曲不存在");         System.out.println("正常播放");     } } public class five {    public static void main(String[] args){        Player player = new Player();        try{            player.play(3);            player.play(11);        }catch(NoThisSoundException e){            System.out.println(e.getMessage());        }    } }
点赞 回复 分享
发布于 2018-03-28 17:27
package com.company; /** * 问题: * 作者:筱茜 * 链接:https://www.nowcoder.com/discuss/70619 * 来源:牛客网 * * 自定义一个异常类NoThisSoundException和Player类,在Player类的play()方法中使用自定义异常,要求如下: * NoThisSoundException继承自Exception类,类中有一个无参和一个接收一个string类型参数的构造方法, * 构造方法中都是用super关键字调用父类的构造方法。 * Player类中定义一个play(int index)方法,方法接收一个int类型的参数,表示播放歌曲的索引, * 当index>10时,play()方法用throw关键字抛出NoThisSoundException异常,创建一场对象时, * 调用的有参构造方法,传入“您播放的歌曲不存在”。 * 在测试类中创建Player对象,并调用play()方法测试自定义的NoThisSoundException异常,使用try...catch语句捕获异常,调用NoThisSoundException的getMessage()方法打印异常提示信息。 * * 考查知识点: * 面向对象的继承特性 * 自定义异常类的实现 * 异常处理的相关知识点 * * 参考知识点:《java基础入门》第4章 * * Solution: * @author zhudky * @version v1.0.0 2018/3/27 * */ //自定义异常 class NoThisSoundException extends Exception { public NoThisSoundException(){ super(); } public NoThisSoundException(String message){ super(message); } } //Player类 class Player { public void play(int index) throws NoThisSoundException{ if(index>10){ throw new NoThisSoundException("您播放的歌曲不存在"); } System.out.println("正常播放。。。。"); } } //测试类 public class Main { public static void main(String[] args) { Player player = new Player(); try{ System.out.println("点歌:1"); player.play(1); System.out.println("点歌:11"); player.play(11); }catch (Exception e){ System.out.println(e.getMessage()); }finally { System.out.println("放完了"); } } } 结果 点歌:1 正常播放。。。。 点歌:11 您播放的歌曲不存在 放完了
点赞 回复 分享
发布于 2018-03-27 21:31
package com.nowcode.www.demo; public class Play {     private int num;     public static final int MAX = 10;     public Play(int num) {         super();         this.num = num;     }     public Play() {         super();     }     public void play(int index) throws NoThisSoundException {         if (index > MAX) {                 throw new NoThisSoundException("index > MAX");         }else {             System.out.println("play " + index + "song ");         }     }     public static void main(String[] args) throws NoThisSoundException {         Play play = new Play();         try {         play.play(9);         play.play(11);         } catch (NoThisSoundException e) {             System.out.println("23333,出错");         }          } } package com.nowcode.www.demo; public class NoThisSoundException extends Exception{     public NoThisSoundException(){         super();     }     public NoThisSoundException(String string){         super(string);     }      }
点赞 回复 分享
发布于 2018-03-27 14:06

相关推荐

已注销:bro不如吃顿疯狂星期四
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务