当前位置: 代码迷 >> Java相关 >> JAVA运用的练习题求助
  详细解决方案

JAVA运用的练习题求助

热度:209   发布时间:2010-05-22 08:42:34.0
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 07:03:51
jackyshen

等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
  得分:0 
原句是这样的:insufficient stock

----------------解决方案--------------------------------------------------------
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 08:32:12
liubangchuan
该用户已被删除
  得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 08:39:36
wtuaimmmm
该用户已被删除
  得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-22 09:26:52
jackyshen

等 级:论坛游民
帖 子: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.

----------------解决方案--------------------------------------------------------
这个好像是提示还是什么,没看懂
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 14:10:38
jackyshen

等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
  得分:0 
在class TestPart里 e代表什么

----------------解决方案--------------------------------------------------------
  相关解决方案