创建一个生成约会服务的java程序

时间:2014-03-01 02:07:28

标签: java eclipse methods constructor

我有一份家庭作业,要求我创建约会服务。我有大约70%的计划完成,但我陷入了僵局。我很确定我的方法和构造函数类是正确的(如果不是那么请在上面纠正我),但我仍然坚持实现类。我不确定我是如何使用main方法启动两个人的。以下是给我的指示:

  • description - 标识特征的字符串
  • rating - 1到10之间的整数,表示一个人对另一个人的这种特征的渴望
  • 编写一个构造函数,将特征描述设置为给定字符串,并将评级设置为零以表明尚未确定它
  • 编写一个私有方法isValid(aRating),如果给定的评级有效,则返回true,即介于1和10之间
  • 编写方法setRating(aRating),如果有效,则将评级设置为aRating 编写方法setRating(),从键盘读取评级,坚持用户提供的评级有效
  • getDescription - 返回此特征的描述
  • getRating - 返回此特征的评级
  • getCompatability(Characteristics otherRating) - 返回两个匹配特征的兼容性度量,如果描述不匹配,则返回零
  • getCompatibilityMeasure(Characteristics otherRating) - 一种私有方法,使用以下公式将兼容性度量作为double值返回: [m = 1 - (r1-r2)*(r1-r2)/ 81]当两个等级都非零时;如果任一评级为零,则m为零。 (回想下面的练习5,构造函数将评级设置为零,表明它尚未确定。)
  • isMatch(Characteristics otherRating) - 一个私有布尔方法,如果描述匹配则返回true
  • 实现一个名为Dating Service的类,它实例化两个人,Chris和Pat,并使用您定义的所有方法测试它们的兼容性。

以下是我的方法类的代码。

    import java.util.Scanner; 
    public class Characteristic {
    public String description; //initialization of instance variables
    public int rating; 
    public Characteristic(String description){ //this is the constructor with a String parameter. It sets the rating at zero as instructed. 
    this.description= description; 
    rating=0;
    }
        private boolean isValid( int aRating){ //this is the method that sets the rating to true when it stays within the limit
    if(aRating >= 0 || aRating <= 10){
    return true;
        }
    else{
        return false;
        }
    }
        public void setRating(int aRating){ //This sets arating and rating as one and the same when arating comes back as true
    if(isValid(rating)==true)
    rating=aRating;
        }
    public void setRating(){
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Enter the rating for " +getDescription());
    rating= keyboard.nextInt();  //this sets the rating based on user input
    }
    public int getRating(){
    return rating; 
    }
    public String getDescription(){
    Scanner kb = new Scanner(System.in);
    System.out.println("Please enter the description.");
    String Description= kb.nextLine();
    return Description; //this lets the user input a description which is only supposed to be a simple string word
    }

    public String getDescription1(){
    return description; 
    }

    private boolean isMatch(Characteristic otherRating){ //This boolean determines if the two are a match or not
    if(getDescription().equals(otherRating.getDescription())){
        return true;
    }
    else{
        return false;
    }}

    private double getCompatabilityMeasure(Characteristic otherRating){ //This determines the numbered "compatability" of the two people
    if(this.rating ==0 && otherRating.rating==0){
    return 0; 
    }
    else{
    double m = 1-((otherRating.rating - this.rating)*(otherRating.rating-          this.rating))/(81); 
    return m;
    }}


    public double getCompatability(Characteristic otherRating){
    if(isMatch(otherRating) == false){
        return 0;
    }
    else{
        return getCompatabilityMeasure(otherRating); 
    }
    } 

1 个答案:

答案 0 :(得分:1)

如果要实例化上述Characteristic类的两个实例,可以执行以下操作:

Characteristic c1 = new Characteristic("Description"); // Using constructor with one String.

c2变量执行相同操作。然后你可以调用这样的方法:

c1.setRating(10);