| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 880 人关注过本帖
标题:JAVA运用的练习题求助
只看楼主 加入收藏
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:10 
JAVA运用的练习题求助
1.修改这个method to throw exception in two different case
- 存货不足
- quantity 范围(1...500)
2.修改 driver class to catch and handle the exceptions

程序代码:
import java.until.*;
import java.util.Scanner;
public class Part {

 private String ID;

 private String name;

 private int stLevel;

 private int roLevel;

 private double uPrice;

 public Part(String ID, String name, int sL, int rL, double uP){
  this.ID = ID;
  this.name = name;
  stLevel=sL;
  roLevel=rL;
  uPrice = uP;

 }

 public String getID(){return ID;}

 public int getSLevel(){return stLevel;}

 public void replenish(int qty){
  stLevel += qty;

 }

 public double supply(int qty){
  if (stLevel<qty) return -1.0;
  stLevel-=qty;
  if(stLevel<roLevel)
   System.out.println("Place order for" + ID);
  return qty*uPrice;

 }
}
class TestPart{

 public static void man(String args[]){
  Part P=new Part("s123", "Axle", 12, 80, 250.0);
  Scanner sc=new Scanner(System.in);
  boolean done=false;
  do{
   System.out.print("Enter qty:");
   int qty=sc.nextInt();
   double amt= p.supply(qty);
   if(amt<0){
    System.out.println("Insufficient Qty");
    System.out.println("Curr.stock="+p.getSLevel());
   }
   else{
    System.out.println("Cost for supplying"+p.getID()+"=" + amt);
    done = true;
   }
  }while(!done);

 }
}

搜索更多相关主题的帖子: JAVA 练习题 
2010-05-22 08:42
liubangchuan
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 15:03
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
原句是这样的:insufficient stock
2010-05-22 15:39
liubangchuan
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 16:32
liubangchuan
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 16:39
wtuaimmmm
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 17:26
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
程序代码应该没错的,题目是这样的
~recall that in our part class the supply method returns -1 when there are insufficient stock.
~this is not a good design, as there can be many dfifferent reasons why this method. for example, the quantity requested may be -ve or too large(above preset limt).
~in this case, throwing an exception is more suitable when one or more error states are detected. unlike returning a value(such as -1) it can explicitly indicate the cause of error.
!now modify the supply method to throw exception in two different cases
- insufficient stock
- incorrect quantity requested(assume valid quantity is in range 1...500
~modify the driver class to catch and handle the exceptions.
2010-05-22 17:40
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
这个好像是提示还是什么,没看懂
changes needed for supply method

~when the quantity requested <= 0 or >500 throw an excption object as in: throw new Exception("Invalid amount");
~when the sock level is less than quantity requested throw as in: throw new Exception("Insufficient Stock");
~Change the method header to : public double supply(int qty) throws Exception


Changes needed in the calling method
~Place the try-catch within a loop which repeats until done is true. Place the call to supply in a try block as in:
try{
  double amt=p.supply(qty);
  System.out.println("Cost for supplying" + p.getID() + "=" + amt);
  done=true;
}

~Catch the Exception and handle it as in:
-Display the exception
-extract the exception message saving it in a string
-Depending on this message perform appropriate actions
catch(Exception e){
   System.out.println(e);
   String cause=e.getMessage();
   if(cause.indexOf("Insuff")>= 0){
   ..... ....

else if(cause.indexOf("Invalid") >= 0)
   ...
}
}
2010-05-22 17:48
liubangchuan
该用户已被删除
收藏
得分:30 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 22:10
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
在class TestPart里 e代表什么
2010-05-23 08:48
快速回复:JAVA运用的练习题求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040304 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved