Java SE OOP、类、对象、方法

Java SE OOP、类、对象、方法,java,oop,Java,Oop,大家好,我需要帮助。我必须每天上课,每节课都有自己的场地。 上课日及其字段(日期、温度和备注)。使用字段(季节和注释)对天气进行分类。主要问题是:找到并显示以下数据:平均温度、最高温度日、注释最长日。所以我已经很容易地写出了这两个类的基本结构,但是用逻辑结构,我可以写一些 建议或帮助如何解决或改进我是编程新手。 今天是上课日: public class Day { private Date date; private int temperature; private Ar

大家好,我需要帮助。我必须每天上课,每节课都有自己的场地。 上课日及其字段(日期、温度和备注)。使用字段(季节和注释)对天气进行分类。主要问题是:找到并显示以下数据:平均温度、最高温度日、注释最长日。所以我已经很容易地写出了这两个类的基本结构,但是用逻辑结构,我可以写一些 建议或帮助如何解决或改进我是编程新手。 今天是上课日:

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");
        day.addComment("In the early years of the ice era there was only highest degree of temperature for ever");
        System.out.println("the longest comment is:");
        System.out.println( day.longestComment() );
    }
}
公共课日{
私人日期;
温度;
private ArrayList comments=new ArrayList();
天气;;
日期(日期、内部温度、字符串注释){
这个日期=日期;
这个。温度=温度;
this.comments.add(comment);
}
公共日期getDate(){
返回日期;
}
公共作废设置日期(日期){
这个日期=日期;
}
公共int getTemperature(){
返回温度;
}
公共空隙设定温度(内部温度){
这个。温度=温度;
}
公共ArrayList getComments(){
返回评论;
}
公共void addComment(字符串注释){
this.comments.add(comment);
}
公共字符串longestComment(){
整数长度=0;
字符串longestComment=“”;
for(字符串注释:注释)
{
if(comment.length()>length)
{
长度=comment.length();
最长评论=评论;
}
}
回复最长的评论;
}
公共静态void main(字符串参数[]){
日历c=新的公历日历();
Day Day=新的一天(c.getTime(),20,“今天是正常温度”);
addComment(“明天将是今年夏天有史以来最高的学位”);
addComment(“昨天是最冷的温度”);
addComment(“下周将是有史以来最冷的温度”);
addComment(“在冰河时代的早期,只有最高的温度有史以来”);
System.out.println(“最长的注释是:”);
System.out.println(day.longestcoment());
}
}
下面是课堂上的天气:

public class Weather {
private  String season;
    private String comments;
        Day [] day;

     public   Weather(String season, String comments, Day [] day){
         this.comments=comments;
         this.season=season;
         this.day=day;
       }

   public static void main (String args []){


   }
    public String getSeason(String season){
        return season;
    }
    public void setSeason(String season){
                    this.season=season;
    }
    public String getComments(String comments){
        return comments;
    }
          public void setComments(String comments){
              this.comments=comments;
          }
    public Day getDay(Day  day){
        return day ;
    }
    public void setDay(Day [] day){
        this.day=day;
    }
    public int averageTemp(int aver){
    for(int t=0; t<day.length;t++){
        if (aver==((day.length)/2)){

          return day[t].getTemperature();
    }
    }
        return 0;
    }
  public void maxTemp(){
      int max=0;

      for (int i=0; i<day.length;i++){

      }

  }
    public void findLongestComment(String comment){

   System.out.println("Comment\""+comment+"\":");
}
公共类天气{
私人弦乐季;
私有字符串注释;
第[]天;
公共天气(连续季节、连续评论、第[]天){
这个.评论=评论;
这个季节=季节;
这个.天=天;
}
公共静态void main(字符串参数[]){
}
公共字符串季节(字符串季节){
回归季节;
}
公众假期(弦乐季){
这个季节=季节;
}
公共字符串getComments(字符串注释){
返回评论;
}
公共void setComments(字符串注释){
这个.评论=评论;
}
公众日(日){
回归日;
}
公共无效设定日(第[]天){
这个.天=天;
}
公共整数平均温度(整数平均){

对于(int t=0;t平均温度可能更像:

public int averageTemp()
{
  int totalTemp=0;
  for (Day day1 : day)
  {
    totalTemp+=day1.getTemperature;
  }
  return totalTemp/day.length;
}

对于max temp,您对最长注释的定义是正确的。只需使用相同的逻辑:循环遍历所有条目,将每个条目与迄今为止找到的最高值进行比较,如果该值较高,则成为迄今为止的最高值。

日期是使用java.util.Date或JavaID中的日历声明的。day是一天,或day Instant的数组ces.如果是后者,请将变量命名为
days
,而不是
day
,因为数组包含多个元素。您还应该重新考虑平均值的概念。使用java.util.calendar我的问题是如何编写逻辑部分,以便查找和显示以下数据:平均温度、最高温度的日期how我可以在main方法中打印这个块吗?System.out.println(“Average temp=“+averageTemp”);这就是你想要的吗?不,我想在main方法中提供如下对象引用:public static void main(String args[]){Calendar c=new GregorianCalendar();Weather Weather=new Weather(“夏季”、“酷热季节”);weather.addTemp(49);weather.addTemp(42);weather.addTemp(45);weather.addTemp(46);System.out.println(weather.averageTemp());System.out.println(c.getTime());System.out.println(weather.maxTemp());}好的,看起来不错。你对你在评论中的内容有意见吗?杰伊,你能帮我完全解决这个问题吗?