| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1013 人关注过本帖
标题:(求助) 2 classes
只看楼主 加入收藏
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
结帖率:25%
收藏
 问题点数:0 回复次数:4 
(求助) 2 classes
(1) The Policy Class

Define a public class called Policy which has a private static/class variable
called NEXT_POLICY_NUMBER which is an int (initially set to 1) representing
the number to be given to the next policy created. Create the following attributes
in the class as well:

    • a private attribute called policyNumber of type int that identifies the policy     with a unique integer.
    • a protected attribute called amount that contains the amount (a float) of coverage for the policy.

Create a public constructor which takes a single float parameter and uses it to set the amount variable. The constructor sets the policyNumber such that each created policy has a unique number and also updates the class variable appropriately.

Create public get methods for your attributes as well as a public toString() method that returns a String with the following format (use String.format() for both the policy number and amount):
 Policy: 0001 amount: $320.00

Create a public instance method called isExpired() which always returns false. Now test your code with this program:
程序代码:
public class PolicyTestProgram {
    public static void main(String args[]) {
          System.out.println(new Policy(320)); // displays Policy: 0001 amount: $320.00
          System.out.println(new Policy(500.1f)); // displays Policy: 0002 amount: $500.10
          System.out.println(new Policy(0)); // displays Policy: 0003 amount: $0.00
          System.out.println(new Policy(320).isExpired()); // displays false
     }
}



(2) The DepreciablePolicy Class
Now we will make some subclasses to represent different types of policies. Define a
public class called DepreciablePolicy as a subclass of Policy which has a private
attribute called rate of type float which represents the rate at which the policy
depreciates each time a claim is made. For example, a rate of 0.10 means that the
value of the policy (i.e., the amount) depreciates 10% each time a claim is made (we
will discuss the making of claims in the next section). Implement these:

• a public constructor which takes a float representing the amount and another float representing
the rate. This constructor should use inheritance by calling the constructor from the superclass to
set the amount and policyNumber.

• a public get method for the rate.

• a public toString() method that returns a String with the following format:

      DepreciablePolicy: 0001 amount: $320.00 rate: 10.0%

You MUST make use of inheritance by calling the toString() from the superclass (You will have to
make a change to the Policy class’ toString() method as well. Also, use String.format() again to
display the rate.

• an instance method called isExpired() which returns true if the amount of this policy has
depreciated to 0. You should be able to write this method WITHOUT any IF statements … try.

• an instance method called depreciate() which reduces the amount of the policy by the rate
percentage. For example, if the amount is $100 and the rate is 0.10, then the amount after this
method is called once should be $90.



搜索更多相关主题的帖子: classes 
2009-11-22 12:47
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
回复 楼主 lyf3368
第一题我写了代码,可是有EXCEPTIONS, 帮我看一下怎样修改,第二题不会写

程序代码:
public class Policy {
    private static int  NEXT_POLICY_NUMBER = 1;
    private int  policyNumber;
    protected float  amount;
    
    //Constructors
    public Policy(float a) {
        this.policyNumber = 0;
        this.amount = a;
        this.policyNumber = NEXT_POLICY_NUMBER;  
        NEXT_POLICY_NUMBER++;         
    }
    
    //Get method
    public int getPolicyNumber() { return  this.policyNumber; }
    public float getAmount() { return  this.amount; }
    

    
    //Set method
    public void setPolicyNumber(int newPolicyNumber) { this.policyNumber = newPolicyNumber; }
    public void setAmount(float a) { this.amount = a; }
    
    
    //toString Method
    public String toString() {
        return ("Policy:" + String.format("%,04i", this.policyNumber) + "amount:" + "$" + String.format ("%,3.2f", this.amount));
    }
        
        
    public boolean isExpired(){
        return false;
    }  

 出现exceptions的地方是 formating

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i'
    at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2606)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2634)
    at java.util.Formatter.parse(Formatter.java:2480)
    at java.util.Formatter.format(Formatter.java:2414)
    at java.util.Formatter.format(Formatter.java:2367)
    at java.lang.String.format(String.java:2769)
    at Policy.toString(Policy.java:27)
    at java.lang.String.valueOf(String.java:2826)
    at (PrintStream.java:771)
    at PolicyTestProgram.main(PolicyTestProgram.java:3)

Process completed.


[ 本帖最后由 lyf3368 于 2009-11-22 12:52 编辑 ]
2009-11-22 12:50
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
哈哈,我想到答案了,不用了
2009-11-23 14:12
haizeng
Rank: 2
来 自:xdpsj.com
等 级:等待验证会员
帖 子:61
专家分:36
注 册:2009-11-30
收藏
得分:0 
   哈哈  小问题

[url=http://www./]石料生产线[/url]
[url=http://www.]破碎机[/url]
2009-11-30 09:57
zhuyunshen
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:212
专家分:455
注 册:2009-11-22
收藏
得分:0 
呵呵
2009-11-30 10:02
快速回复:(求助) 2 classes
数据加载中...
 
   



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

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