简体   繁体   中英

Having large amounts of trouble with calling a method from another class

My question: I keep getting an error... (multiple actually..)
" method getYear in class Appointment cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length "

I cannot for the life of me, figure out how to fix this. If anyone could give me some help, i've already tried spending a few hours on it with no progress...

import java.util.Scanner;

public class Appointment
{
  protected int year = 0;
  protected int month = 0;
  protected int day = 0;
  protected String description = "";

  public Appointment(int year,int month ,int day, String description)
  {
    this.year = year;
    this.month = month;
    this.day = day;
    this.description = description;
  }

  public static void main(String[] args)
  {
    Appointment[] appointments = new Appointment[4];
    appointments[0] = new Daily(2011, 8, 13, "Brush your teeth.");
    appointments[1] = new Weekly(2012, 2, 3, "Buy groceries.");
    appointments[2] = new Monthly(2012, 5, 20, "Visit grandma.");
    appointments[3] = new Onetime(2012, 11, 22, "Dentist appointment.");
    //set all appointments as necessary.........
    System.out.println("Enter a date (year month day) to list "
                         + "appointments: ");
    Scanner in = new Scanner(System.in);
    int year = in.nextInt();
    int month = in.nextInt();
    int day = in.nextInt();
    for (int i = 0; i < appointments.length; i++)
    {
      if (appointments[i].occursOn(year, month, day))
      {
        System.out.println(appointments[i]);
      } // end if loop
    } //end for loop
  }//end main

  public int getYear(int year)
  {
    int year2 = 0;
    year2 = year;

    return year2;
  }

  public int getMonth(int month)
  {
    int month2 = 0;
    month2 = month;
    return month2;
  }

  public int getDay(int day)
  {
    int day2 = 0;
    day2 = day;
    return day2;
  }     

  public boolean occursOn(int year,int month,int day)
  {
    year = 0;
    month = 0;
    day = 0;
    return false;
  }

  public String toString()
  {
    String nothing = "";
    return nothing; // change this.  Needs to return appointments for that specific day combination.
  }
}

That is my appointment class... and the OTHER file is this...

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 Weekly appointment
 */
public class Weekly extends Appointment
{

 /**
Initializes appointment for a given date
@param year the year
@param month the month
@param day the day
@param description the text description of the appointment
*/
public Weekly(int year, int month, int day, String description)
{
super(year, month, day, description);
}

/**
Determines if the appointment occurs on the same weekday
 @param year the year
@param month the month
@param day the day
@return true if day matches the base appointment weekdate date and is
later than the base appointment
*/
public boolean occursOn(int year, int month, int day)
{
    if (year < getYear())
    {
      return false;
    }
    if (month < getMonth() && year == getYear())
    {
       return false;
    }
    // we need to determine if the appointment is on the same day of the
    // week, the GregorianCalendar class is useful for that
    GregorianCalendar today = new GregorianCalendar(year, month, day);
    GregorianCalendar appointment = new GregorianCalendar(getYear(),
                                                      getMonth(), getDay());

    return today.get(Calendar.DAY_OF_WEEK) == appointment
      .get(Calendar.DAY_OF_WEEK);
  }
 }

Thank you everyone for your help!!! (FIXED!)

In your Weekly class you have:

if (year < getYear())

You're calling getYear() with no arguments, but it's defined in Appointment to take one int argument.

public int getYear(int year)
{
  int year2 = 0;
  year2 = year;

  return year2;
}

With that definition, you need to pass an int to the method when you call it.

You could also change the implementation of getYear() so that it doesn't take an argument, which would be preferred. Your current implementation seems to just return the same value you pass in. It should probably return the value this.year that's set in the constructor.

public boolean occursOn(int year, int month, int day)
{
    if (year < ***getYear()***)
    {
      return false;
    }
    if (month < getMonth() && year == ***getYear()***)
    {
       return false;
    }
    // we need to determine if the appointment is on the same day of the
    // week, the GregorianCalendar class is useful for that
    GregorianCalendar today = new GregorianCalendar(year, month, day);
    GregorianCalendar appointment = new GregorianCalendar(***getYear()***,
                                                      getMonth(), getDay());

    return today.get(Calendar.DAY_OF_WEEK) == appointment
      .get(Calendar.DAY_OF_WEEK);
  }

check function calls in *...!

there you are calling functions which are not defined anywhere in the code pasted by you.

there is function which expects an argument, that is why you are getting the error saying argument is expected but not provided..

Your function getYear() is declared like:

public int getYear(int year)

So you have to call it with an int value. The same happen with functions getDay() and getMonth() .

Your method is:

public int getYear(int year)
  {
    int year2 = 0;
    year2 = year;

    return year2;
  }

Your call is:

getYear()

You're missing the method argument

All your get methods are not obeying OOP model. So at first learn OOP and then Java. However for now change the body of all get methods to just return this.year , return this.day ,... and so on.

As now you are just setting and getting the value which is not correct.

A method defined like this needs exactly one single parameter when you want to call it:

public int getYear(int year)
{
    int year2 = 0;
    year2 = year;
    return year2;
}

For example, getYear(2) . If you call it with no parameters, as in getYear() , the compiler cannot understand what you mean and reports an error.

The getYear method now appears to not do much, but that is another topic. Good luck :)

actual and formal argument lists differ in length

I cannot for the life of me, figure out how to fix this.

Either you can't read, or you just do not know what an actual and a formal argument list is.

 public R method(int arg1, float arg2)
                 ^^^^^^^^^^^^^^^^^^^^     formal argument list
          ^^^^^^                          method name
        ^                                 return type

So, method in our example has a formal argument list of length 2: one int, and one float. Since Java is a type safe language (to some degree, at least), it won't allow you to call this method with any number of arguments, except if they are 2. Furthermore, the first argument must be int (or autoconvertable to int), and the second one must be a float or convertable to float.

Here are some examples, figure out which ones are correct:

method(1, 5.3f)
method(5.3f, 1)
method()
method(1)
method(5.3f)
method("this", "not")
method(a, b, c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM