Issue
I keep getting the error message
method clear in class Screen cannot be applied to given types;
required: boolean
found: no arguments
reason: actual and formal argument list differ in length
For clear(),
I’m trying to do an exercise from a workbook that wants me to use the following headers and fill out the methods and constructor. The actual function of clear() doesn’t matter, just that I use the headers and call the clear method if xRes x yRes > 2000000)
The question,
Exercise 3.55 Given the following class (only shown in fragments here),
public class Screen
{
public Screen(int xRes, int yRes)
{ ...
}
public int numberOfPixels()
{ ...
}
public void clear(boolean invert)
{ ...
}
}
write some lines of Java code that create a Screen object. Then call its clear
method if (and only if) its number of pixels is greater than two million. (Don’t
worry about things being logical here; the goal is only to write something that is
syntactically correct—i.e., that would compile if we typed it in.)
My code,
/**
* Write a description of class Screen here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Screen
{
// instance variables - replace the example below with your own
private int xRes;
private int yRes;
private boolean invert;
/**
* Constructor for objects of class Screen
*/
public Screen(int xRes, int yRes, boolean invert)
{
// initialise instance variables
this.xRes = xRes;
this.yRes = yRes;
if((xRes * yRes) > 2000000)
{
clear(); // this is where i get my error
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int numberOfPixels()
{
// put your code here
return xRes * yRes;
}
public void clear(boolean invert)
{
this.invert = invert;
}
}
Solution
The clear metod has a strange implementation right now as it doesn’t clear anything but maybe you will get to fixing that later.
To call the method in it’s current form you need to provide an argument of true or false.
Like this:
clear(true);
Answered By – Johan Nordlinder
Answer Checked By – Willingham (BugsFixing Volunteer)