Java类'月' - 仍未获得回报......以及其他一些事情

时间:2015-05-10 01:00:55

标签: java arrays

新的一周,新任务 - 我需要帮助的新麻烦!作业是: 写一个名为Month的类。该类应该有一个名为monthNumber的int字段,该字段包含月份的编号。例如,1月将是1,2月将是2,依此类推。该类还应该有一个名为lastMonthCreated的静态字段,该字段包含上次构造的月份数。另外,提供以下方法:

将monthNumber字段设置为1的无参数构造函数。 一个构造函数,它接受月份的数量作为参数。它应该将monthNumber字段设置为作为参数传递的值。如果传递的值小于1或大于12,则构造函数应将monthNumber设置为1。 接受月份名称的构造函数,例如“January”或“February”作为参数。它应该将monthNumber字段设置为正确的对应值。 接受int参数的setMonthNumber方法,该参数分配给monthNumber字段。如果传递的值小于1或大于12,则该方法应将monthNumber设置为1。 一个getMonthNumber方法,它返回monthNumber字段中的值。 一个getMonthName方法,它返回月份的名称。例如,如果monthNumber字段包含1,则此方法应返回“January”。 一个getLastMonthCreated方法,它返回lastMonthCreated字段中的值。 一个toString方法,它返回与getMonthName方法相同的值。 一个equals方法,它接受Month对象作为参数。如果参数对象包含与调用对象相同的数据,则此方法应返回true。否则,它应该返回false。 接受Month对象作为参数的greaterThan方法。如果调用对象的monthNumber字段大于参数的monthNumber字段,则此方法应返回true。否则,它应该返回false。 一个lessThan方法,它接受Month对象作为参数。如果调用对象的monthNumber字段小于参数的monthNumber字段,则此方法应返回true。否则,它应该返回false。 通过使用一组三个测试程序MonthDemo1.java,MonthDemo2.java和MonthDemo3.java演示Month类,可以从此分配链接下面的MonthDemo文件夹下载。

基本上我有一个很好的解决方案(希望是一个很好的解决方案),当我编译时我会遇到很多错误 - 我将它们从48降到了6.

错误:

  

第47行:意外类型monthNumber =(monthName [(index + 1)++]);

^错误指向索引 - 所以我尝试创建一个名为index的变量并在使用它之前初始化,但没有改变。 必需:变量 type:value

  

第91行:不兼容的类型,如果(monthName = monthName [index])

,则无法将String转换为String []

^我得到了这句话,但不知道如何更改它以获得我需要的东西。

^同一行再次出现相同错误

  

第92行:找不到符号返回getLastCreated;`

^我在顶部定义了这个但没有初始化 - 如果我这样做会有帮助吗?我尝试初始化为0并且为null并且出错。

93号线再次相同

基本上我正在寻找两件事,首先是关于如何处理这些错误的任何提示,以及一般来说,我是否正在使用这个,或者是否有需要改变的部分的想法让它起作用。如果您建议更改,您是否介意建议实际代码并解释原因,因为我真的很新,并希望从建议中学习而不仅仅是实施。

所有的帮助非常感谢 - 自从启动Java以来​​,我在这个论坛上学到了很多东西!

PS - 会改变我的方法上的情况以降低 - 只是让它们在我看上去更容易看到它们。

    public class Month

{
    private int monthNumber;
    String[] monthName = {
        "January", "Februry", "March",
            "April", "May", "June", "July", "August", "September",
            "October", "November", "December"
    }; //Months. 
    int MonthNumberInt = 0;
    public static String lastMonthCreated;


    /** 
A no-arg constructor that sets the monthNumber field to 1. 
*/
    public Month() {
        monthNumber = 1;
    }

    /** 
A constructor that accepts the number of the month as an argument. It                 
should set the monthNumber field to  
the value passed as the argument. If a value less than 1 or greater than      
12 is passed, the constructor  
should set monthNumber to 1. 
*/

    public Month(int monthNumber) {
        if ((monthNumber < 1) || (monthNumber > 12)) {
            monthNumber = 1;
        } else {
            monthNumber = monthNumber;
        }
    }

    /** 
A constructor that accepts the name of the month, such as "January" or      
"February" as an argument. It should  
set the monthNumber field to the correct corresponding value. 
*/

    public String Month(String monthName[]) {
        int index = 0;
        monthNumber = (monthName[(index + 1)++]);
    }

    /** 
A setMonthNumber method that accepts an int argument, which is assigned      
to the monthNumber field. If a value  
less than 1 or greater than 12 is passed, the method should set  
monthNumber to 1. 
*/

    public int setMonthNumber(int monthNumberInt) {
        if ((monthNumber < 1) || (monthNumber > 12)) {
            return monthNumber = 1;
        }
        monthNumberInt = monthNumber;
    }

    /** 
A getMonthNumber method that returns the value in the monthNumber field. 
*/

    public int getMonthNumber() {
        return monthNumber;
    }

    /** 
A getMonthName method that returns the name of the month. For example, if      
the monthNumber field contains 1,  
then this method should return "January". 
*/

    public String getMonthName() {
        return monthName[monthNumber - 1];
    }

    /** 
A getLastMonthCreated method that returns the value in the  
lastMonthCreated field. 
*/

    public String getLastMonthCreated() {
        for (int index = 0; index < monthName.length; index++) //Check through     
        the array. {
            if (monthName = monthName[index]) getLastCreated = monthName[index - 1];
            return getLastCreated;
        }
    }

    /** 
A toString method that returns the same value as the getMonthName method. 
*/
    public String toString() {
        String str = "monthName: " + getMonthName();
    }

    /** 
An equals method that accepts a Month object as an argument. If the      
argument object holds the same data as the  
calling object, this method should return true. Otherwise, it should      
return false. 
*/

    public boolean Equals(Month m1) //Month needs to go here.
    {
        if (monthNumber == m1.getMonthNumber()) return true;
        else return false;
    }

    /** 
A greaterThan method that accepts a Month object as an argument. If the      
calling object's monthNumber field  
is greater than the argument's monthNumber field, this method should      
return true. Otherwise, it should  
return false. 
*/

    public boolean GreatThan(Month m1) //Month needs to go here.
    {
        if (monthNumber > m1.monthNumber) return true;
        else return false;
    }

    /** 
A lessThan method that accepts a Month object as an argument. If the          
calling object's monthNumber field is  
less than the argument's monthNumber field, this method should return      
true. Otherwise, it should return  
false. 
*/

    public boolean LesserThan(Month m1) //Month needs to go here.
    {
        if (monthNumber < m1.monthNumber) return true;
        else return false;
    }
}

demo1的

 public class MonthDemo1
    {
    public static void main(String[] args)
    {
      // Use the no-arg constructor.
      Month m = new Month();
      System.out.println("Month " + m.getMonthNumber() +
                         " is " + m);
      // Set the month number to the values 0 through 12
      // (0 is invalid), and display the resulting month name.
      for (int i = 0; i <= 12; i++)
      {
         m.setMonthNumber(i);
         System.out.println("Month " + m.getMonthNumber() +
                         " is " + m);
      }
       }
    }

DEMO 2

public class MonthDemo2
    {
    public static void main(String[] args)
    {
      // Use the 2nd constructor to create two objects.
      Month m1 = new Month(10);
      Month m2 = new Month(5);
      System.out.println("Month " + m1.getMonthNumber() +
                         " is " + m1);
      System.out.println("Month " + m2.getMonthNumber() +
                         " is " + m2);

      // Test for equality.
      if (m1.equals(m2))
         System.out.println(m1 + " and " + m2 + " are equal.");
      else
         System.out.println(m1 + " and " + m2 + " are NOT equal.");

      // Is m1 greater than m2?
      if (m1.greaterThan(m2))
         System.out.println(m1 + " is greater than " + m2);
      else
         System.out.println(m1 + " is NOT greater than " + m2);

      // Is m1 less than m2?
      if (m1.lessThan(m2))
         System.out.println(m1 + " is less than " + m2);
      else
         System.out.println(m1 + " is NOT less than " + m2);
      }
     }

DEMO 3

public class MonthDemo3
    {
    public static void main(String[] args)
    {
      // Use the 3rd constructor to create three objects.
      Month m1 = new Month("March");
      Month m2 = new Month("December");
      Month m3 = new Month("Bad Month");
      System.out.println("Month " + m1.getMonthNumber() +
                         " is " + m1);
      System.out.println("Month " + m2.getMonthNumber() +
                         " is " + m2);
      System.out.println("Month " + m3.getMonthNumber() +
                         " is " + m3);

      Month m4 = new Month("May");
      System.out.println("The last month created" +
                         " is " + m4.getLastMonthCreated());
      }
    }

2 个答案:

答案 0 :(得分:2)

我只能说你很困惑。您的代码存在以下几个问题:

  1. 构造函数没有返回类型public String Month(String monthName[])
  2. 您不需要两个整数monthNumberMonthNumberInt
  3. 类的字段必须是驼峰字母。例如,MonthNumberInt应为monthNumberInt
  4. 我修复了一些逻辑问题。您需要在循环中创建Month的新实例,因为您不能每个月使用相同的实例。
  5. 在发布StackOverflow之前,代码应格式正确,以便其他人可以查看。
  6. 这是重写的作品:

    public class Month {
        String[] monthName = { "January", "Februry", "March",
                "April", "May", "June", "July", "August", "September",
                "October", "November", "December" }; //Months.
        int monthNumberInt = 0;
        public static String lastMonthCreated;
    
    
        /**
         A no-arg constructor that sets the monthNumber field to 1.
         */
        public Month()
        {
            monthNumberInt = 1;
        }
    
        /**
         A constructor that accepts the number of the month as an argument. It
         should set the monthNumber field to
         the value passed as the argument. If a value less than 1 or greater than
         12 is passed, the constructor
         should set monthNumber to 1.
         */
    
        public Month(int monthNumber)
        {
            if((monthNumber < 1 ) || ( monthNumber > 12)) {
            this.monthNumberInt = 1;
            } else {
                this.monthNumberInt = monthNumber;
            }
    
        }
    
        public Month(String monthName)
        {
            monthNumberInt = monthName.indexOf(monthName);
        }
    
        public int getMonthNumberInt() {
            return monthNumberInt;
        }
    
        public void setMonthNumberInt(int monthNumberInt) {
            this.monthNumberInt = monthNumberInt;
        }
    
        /**
         A toString method that returns the same value as the getMonthName method.
         */
        public String toString()
        {
           return  "monthName: " + monthName[monthNumberInt];
        }
    
        /**
         An equals method that accepts a Month object as an argument. If the
         argument object holds the same data as the
         calling object, this method should return true. Otherwise, it should
         return false.
         */
    
        public boolean Equals(Month m)//Month needs to go here.
        {
            if(this.monthNumberInt == m.getMonthNumberInt())
                return true;
            else
                return false;
        }
    
        /**
         A greaterThan method that accepts a Month object as an argument. If the
         calling object's monthNumber field
         is greater than the argument's monthNumber field, this method should
         return true. Otherwise, it should
         return false.
         */
    
        public boolean GreatThan(Month m1)//Month needs to go here.
        {
            if(monthNumberInt > m1.monthNumberInt)
                return true;
            else
                return false;
        }
    
        /**
         A lessThan method that accepts a Month object as an argument. If the
         calling object's monthNumber field is
         less than the argument's monthNumber field, this method should return
         true. Otherwise, it should return
         false.
         */
    
        public boolean LesserThan(Month m1)//Month needs to go here.
        {
            if(monthNumberInt < m1.monthNumberInt)
                return true;
            else
                return false;
        }
    }
    

    我测试了它:

    public static void main(String[] args) {
        Month m;
        // Set the month number to the values 0 through 12 // (0 is invalid), and display the resulting month name.
        for (int i = 0; i <12; i++) {
            m = new Month();
            m.setMonthNumberInt(i);
            System.out.println("Month " + m.getMonthNumberInt() + " is " + m);
        }
        }
    

    输出是:

    Month 0 is monthName: January Month 1 is monthName: Februry Month 2 is monthName: March Month 3 is monthName: April Month 4 is monthName: May Month 5 is monthName: June Month 6 is monthName: July Month 7 is monthName: August Month 8 is monthName: September Month 9 is monthName: October Month 10 is monthName: November Month 11 is monthName: December

答案 1 :(得分:0)

String[] monthName = {"January", "February",
            "March", "April", "May", "June", "July",
            "August", "September", "October", "November",
            "December"};

Calendar cal = Calendar.getInstance();
String month = monthName[cal.get(Calendar.MONTH)];