Java SE,OOP,对象,类

时间:2012-11-01 14:41:33

标签: java

您好我堆叠哪种方式很好解决以下问题? 我想要填写或检索最长的评论,我不知道你有什么想法吗?

public class Day {
    private Date date;
    private int temperature;
    private String comments ;
    Weather weather;
   Day( Date date, int temperature, String comments){
       this.date=date; 
            this.temperature=temperature;
       this.comments=comments;
   }
 public Date getDate(){
     return  date;
 }
    public void setDate(Date date){
               this.date=date;
    }
    public int getTemperature(){
        return  temperature;

    }
    public  void setTemperature(int temperature){
        this.temperature=temperature;

    }
    public String getComments(){
        return  comments;

    }
    public void setComments(String comments){
        this.comments=comments;
    }
    public Boolean longestComment(String longcom){
        StringTokenizer st=new StringTokenizer(comments);
       String s;
           st.countTokens();
        if(st.hasMoreTokens()){
            return true;
        }
        return  false;
    }

        public void testComent(String longcom){
            if  (longestComment(longcom)){
             System.out.println("The longest comment is:" +longcom);
            }
        }

       public  static  void main (String args []){
           Calendar c=new GregorianCalendar();

             Day day=new Day(c.getTime(),20,"Today is normal temperature");
                day.testComent("Tomorrow is going to be the highest degree for ever in this summer");
           day.testComent("Yesterday was the coldest temperature");
           day.testComent("Next week is going to be the coldest temperature ever");
       }
}

我想查看哪篇评论是最长的,请一些建议

2 个答案:

答案 0 :(得分:0)

通过这种方法,您最终可以使用最长注释的Day对象,并且您的testComment方法将打印最长注释。这是你期望的吗

public boolean longestComment(String longcom){

    if(longcom.length() > this.comments.length()){
        this.comments = longcom;
        return true;
    }
    return  false;
}

答案 1 :(得分:0)

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;

public class Day {
   private Date date;
   private int temperature;
   private ArrayList<String> comments = new ArrayList<String>() ;
   Weather weather;

   Day( Date date, int temperature, String comment){
      this.date=date; 
      this.temperature=temperature;
      this.comments.add(comment);
   }
   public Date getDate(){
      return  date;
   }
   public void setDate(Date date){
      this.date=date;
   }
   public int getTemperature(){
      return  temperature;

   }
   public  void setTemperature(int temperature){
      this.temperature=temperature;

   }
   public ArrayList<String> getComments() {
      return comments;

   }
   public void addComment(String comment){
      this.comments.add(comment);
   }
   public String longestComment() {
      int length = 0;
      String longestComment = "";
      for ( String comment : comments )
      {
         if ( comment.length() > length )
         {
            length = comment.length();
            longestComment = comment;
         }
      }
      return longestComment;
   }


   public  static  void main (String args []){
      Calendar c=new GregorianCalendar();

      Day day=new Day(c.getTime(),20,"Today is normal temperature");
      day.addComment("Tomorrow is going to be the highest degree for ever in this summer");
      day.addComment("Yesterday was the coldest temperature");
      day.addComment("Next week is going to be the coldest temperature ever");
      System.out.println("the longest comment is:");
      System.out.println( day.longestComment() );
   }
}

输出:

the longest comment is: 
Tomorrow is going to be the highest degree for ever in this summer