新的一周,新的任务-我需要帮助的新麻烦!分配是:
编写一个名为Month的类。该类应具有一个名为monthNumber的int字段,该字段保存月份数。例如,一月将为1,二月将为2,依此类推。该类还应该具有一个名为lastMonthCreated的静态字段,该字段包含上次构造的月份数。此外,提供以下方法:
一个无参数的构造函数,将monthNumber字段设置为1。
接受月份数作为参数的构造函数。它应该将monthNumber字段设置为作为参数传递的值。如果传递的值小于1或大于12,则构造函数应将monthNumber设置为1。
接受月份名称(例如“ January”或“ February”)作为参数的构造函数。应该将monthNumber字段设置为正确的对应值。
一个setMonthNumber方法,该方法接受一个int参数,该参数分配给monthNumber字段。如果传递的值小于1或大于12,则该方法应将monthNumber设置为1。
一个getMonthNumber方法,该方法返回monthNumber字段中的值。
一个getMonthName方法,该方法返回月份的名称。例如,如果monthNumber字段包含1,则此方法应返回“ January”。
一个getLastMonthCreated方法,该方法返回lastMonthCreated字段中的值。
一个toString方法,该方法返回与getMonthName方法相同的值。
接受Month对象作为参数的equals方法。如果参数对象与调用对象具有相同的数据,则此方法应返回true。否则,它应该返回false。
接受Month对象作为参数的GreaterThan方法。如果调用对象的monthNumber字段大于参数的monthNumber字段,则此方法应返回true。否则,它应该返回false。
接受Month对象作为参数的lessThan方法。如果调用对象的monthNumber字段小于参数的monthNumber字段,则此方法应返回true。否则,它应该返回false。
通过使用一组三个测试程序MonthDemo1.java,MonthDemo2.java和MonthDemo3.java来演示Month类,可以从此分配链接下面的MonthDemo文件夹中下载它们。
基本上,我对此有所了解(希望是一个不错的),并且在编译时遇到了很多错误-我将其错误从48降到了6。
错误:
第47行:意外的类型monthNumber =(monthName [(index + 1)++]);
^错误指向索引-因此我尝试创建一个名为index的变量并在使用它之前进行初始化,但没有更改。
必需:变量
类型:值
第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;
}
}
演示1
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);
}
}
}
演示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);
}
}
演示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());
}
}
最佳答案
我只能说你很困惑。您的代码有几个问题:
构造函数没有返回类型public String Month(String monthName[])
。
您不需要两个整数monthNumber
和MonthNumberInt
。
班级的字段必须使用驼峰式大写字母。例如,MonthNumberInt
应该为monthNumberInt
。
我已经解决了一些逻辑问题。您需要循环创建新的Month
实例,因为您不能每月使用同一实例。
在发布到StackOverflow
上之前,代码应具有良好的格式,以便其他人可以看一下。
这是重写的作品:
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: JanuaryMonth 1 is monthName: FebruryMonth 2 is monthName: MarchMonth 3 is monthName: AprilMonth 4 is monthName: MayMonth 5 is monthName: JuneMonth 6 is monthName: JulyMonth 7 is monthName: AugustMonth 8 is monthName: SeptemberMonth 9 is monthName: OctoberMonth 10 is monthName: NovemberMonth 11 is monthName: December