Question:
java program - nearly done please help?
Vash
2011-10-10 16:47:31 UTC
I am doing a program that matches months with their number.

Expected output is

java MonthDemo1
Month 1 is January
Month 1 is January
Month 1 is January
Month 2 is February
Month 3 is March
Month 4 is April
Month 5 is May
Month 6 is June
Month 7 is July
Month 8 is August
Month 9 is September
Month 10 is October
Month 11 is November
Month 12 is December
%
To execute MonthDemo2, use the command:

% java MonthDemo2
Month 10 is October
Month 5 is May
October and May are NOT equal.
October is greater than May
October is NOT less than May
%
To execute MonthDemo3, use the command:

% java MonthDemo3
Month 3 is March
Month 12 is December
Month 1 is January
The last month created is 5
%

My program gets following errors
Month.java:12: illegal start of expression
public Month(String name)
^
Month.java:12: ')' expected
public Month(String name)
^
Month.java:12: illegal start of expression
public Month(String name)
^
Month.java:12: ';' expected
public Month(String name)
^
Month.java:81: ')' expected
if (month2.getMonthNumber() == monthNumb
^
Please help, my code is

public class Month
{
private int monthNumber;

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

public Month(String name)
{
if (name.equalsIgnoreCase("january")) monthNumber = 1;
else if (name.equalsIgnoreCase("february")) monthNumber = 2;
else if (name.equalsIgnoreCase("march")) monthNumber = 3;
else if (name.equalsIgnoreCase("april")) monthNumber = 4;
else if (name.equalsIgnoreCase("may")) monthNumber = 5;
else if (name.equalsIgnoreCase("june")) monthNumber = 6;
else if (name.equalsIgnoreCase("july")) monthNumber = 7;
else if (name.equalsIgnoreCase("august")) monthNumber = 8;
else if (name.equalsIgnoreCase("september"))monthNumber = 9;
else if (name.equalsIgnoreCase("october")) monthNumber = 10;
else if (name.equalsIgnoreCase("november")) monthNumber = 11;
else if (name.equalsIgnoreCase("december")) monthNumber = 12;
else monthNumber = 1;
}

public void setMonthNumber(int m)
{
if (m < 1 || m > 12)
System.out.println("Invalid Month Number");
else monthNumber = m;
}


public int getMonthNumber()
{
return monthNumber;
}
public String getMonthName()
{
String name;
switch (monthNumber)
{
case 1: name = "January";
break;
case 2: name = "February";
break;
case 3: name = "March";
break;
case 4: name = "April";
break;
case 5: name = "May";
break;
case 6: name = "June";
break;
case 7: name = "July";
break;
case 8: name = "August";
break;
case 9: name = "September";
break;
case 10: name = "October";
break;
case 11: name = "November";
break;
case 12: name = "December";
break;
default: name = "Unknown";
}
return name;
}
public String toString()
{
return getMonthName();
}
public boolean equals(Month month2)
{
boolean status;
if (month2.getMonthNumber() == monthNumb
status = true;
else
status = false;

return status;
}

public boolean greaterThan(Month month2)
{
boolean status;

if (monthNumber > month2.getMonthNumber())
status = true;

else
status = false;

return status;

}

public boolean lessThan(Month month2)
{
boolean status;
if (monthNumber < month2.getMonthNumber())
status = true;

else
status = false;

return status;

}
}
Four answers:
Vaibhav
2011-10-10 18:10:28 UTC
It should be

if( month2.getMonthNumber( )==monthNumber )
anonymous
2011-10-10 16:57:40 UTC
It is probably due to bad formatting, as far as I can tell change:



public Month(int m)

{

if (m < 1 || m > 12)

monthNumber = 1;

else

monthNumber = m;



to:



public Month(int m)

{

if (m < 1 || m > 12)

monthNumber = 1;

else

monthNumber = m;

}



and:



if (month2.getMonthNumber() == monthNumb



to:



if (month2.getMonthNumber() == monthNumb)
?
2017-03-03 13:30:56 UTC
After the line: equipment.out.println ("You dont have have been given to pay the £a hundred surcharge"); you will possibly desire to upload the line: equipment.out.println ("how many injuries have you ever had?"); you do no longer choose the quantity of injuries to be a double on account that it is not conceivable to have a fraction of an twist of destiny, it will be an entire quantity. So replace: injuries=enter.nextDouble(); with: injuries=enter.nextInt(); Your sturdy judgment is improper; it will stick on the 1st if assertion on account which you're saying 'if injuries is better than or comparable to a minimum of a million' which covers any importance better than one. you want to point if (injuries == 0) else if (injuries == a million) ... else if (injuries >= 6) it is now no longer displaying the full pondering the reality which you does no longer have a line that prints it out. upload a line that asserts: equipment.out.println("comprehensive insurance is " + comprehensive);
Zyzzyx
2011-10-10 16:48:57 UTC
What is the question?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...