arrays - Java - error: constructor "constructor name" in class "class name" cannot be applied to given types; -
before asking question want make things clear. first, new java , programming in general. second, first post please go easy on me if did wrong. finally, do not want specific solutions assignment in responses post. issues me figure out. want explanation why test code wont compile/run. better understand issue paste assignment information, driver class given, class code accessed driver class. compiler error have shown in title, since vague, here screenshot of exact errors i'm getting.
the following assignment:
you design class “lostpuppy.java” represents puppy lost in multi-floor building contains same number of rooms on each floor. during instantiation (or creation) of object of class, each room on each floor initialized empty (you use space ‘ ‘ character purpose) , random room chosen puppy lost. purpose, character “p” placed in random location. further details on constructor listed below.
an object of class used game 2 players take turns searching puppy, 1 room @ time until unfortunate little canine found. instantiation of object , search performed “driver” program has been provided allowing have concentrate on developing class (the driver program in file “puppyplay.java”)
fields (of course, fields private):
a character (char) array named myhidingplaces. represents building rows floors , columns rooms on each floor (this building has unusual numbering system; floors , rooms both start @ zero).
two integers hold floor , room puppy lost, named myfloorlocation , myroomlocation.
a char named mywinner assigned player’s character when player finds puppy (the driver program uses digits ‘1’ , ‘2’ more distinguish players puppy).
a boolean named myfound set true when puppy found.
constructor:
receives 2 integer parameters user’s input number of floors , rooms of building in puppy lost.
the constructor instantiates 2d array “myhidingplaces” character array first parameter rows (thefloors) , second parameter columns (therooms).
initialize myhidingplaces’ cells, each contain space ‘ ‘ (done single quotes)
- set myfloorlocation (floor puppy on) randomly based using first parameter
- set myroomlocation (room puppy in) randomly based using second parameter
- set myhidingplaces[myfloorlocation][myroomlocation] char ‘p’
- set mywinner single space
- set myfound false
methods:
roomsearchedalready receives floor , room searched , returns true if room has been searched, false otherwise.
puppylocation receives floor , room searched , returns true if floor , room puppy lost, false otherwise. method should not change of fields.
indicesok receives floor , room searched , returns true if floor , room values within array indices range, false otherwise (used check these indices not cause error when applied array).
numberoffloors returns how many floors in building (the first floor starts @ zero).
numberofrooms returns how many rooms on each floor of building (the first room starts @ 0 , floors have same number of rooms).
searchroom receives floor , room searched , current player (as char type) , returns true if puppy found, false otherwise. if puppy not found searchroom sets myhidingplaces array @ received floor , room location received player value (a ‘1’ or ‘2’) or, when found, sets mywinner field current player , sets myfound true.
tostring displays current hidingplaces array , it’s contents except location of puppy remains hidden until he/she found @ point tostring called (by driver) , both player found puppy , ‘p’ displayed in same cell….
now, , perhaps awkward portion of tostring output. normally, when displaying 2d array, [0][0] cell displayed in upper left corner matrices. however, because puppy decided lost in building , not matrix, make more visual sense have first floor (row 0) displayed on bottom, second floor above it… , top floor, well… on top! save words, closely @ sample run provided on next page. output should same seen on next page in sample run.
here driver program:
import java.util.random; import java.util.scanner; /** * program used driver program play game * class lostpuppy. not used grading! * * puppy lost in multi-floor building represented in class * lostpuppy.class. 2 players take turns searching building * selecting floor , room puppy might be. * * @author david schuessler * @version spring 2015 */ public class puppyplay { /** * driver program play lostpuppy. * * @param theargs may contain file names in array of type string */ public static void main(string[] theargs) { scanner s = new scanner(system.in); lostpuppy game; int totalfloors; int totalrooms; int floor; int room; char[] players = {'1', '2'}; int playerindex; boolean found = false; random rand = new random(); { system.out.print("to find puppy, need know:\n" + "\thow many floors in building\n" + "\thow many rooms on floors\n\n" + " please enter number of floors: "); totalfloors = s.nextint(); system.out.print("please enter number of rooms on floors: "); totalrooms = s.nextint(); s.nextline(); // consume previous newline character // start game: create lostpuppy object: game = new lostpuppy(totalfloors, totalrooms); // pick starting player playerindex = rand.nextint(2); system.out.println("\nfloor , room numbers start @ 0 '0'"); { { system.out.println("\nplayer " + players[playerindex] + ", enter floor , room search separated space: "); floor = s.nextint(); room = s.nextint(); //for testing, use random generation of floor , room //floor = rand.nextint(totalfloors); //room = rand.nextint(totalrooms); } while (!game.indicesok(floor, room) || game.roomsearchedalready(floor, room)); found = game.searchroom(floor, room, players[playerindex]); playerindex = (playerindex + 1) % 2; system.out.println("\n[" + floor + "], [" + room + "]"); system.out.println(game.tostring()); s.nextline(); } while (!found); playerindex = (playerindex + 1) % 2; system.out.println("great job player " + players[playerindex] +"!"); system.out.println("would find puppy [y/n]? "); } while (s.nextline().equalsignorecase("y")); } } finally, here test code:
import java.util.random; import java.util.scanner; public class lostpuppy { char[][] myhidingplaces; int myfloorlocation; int myroomlocation; char mywinner; boolean myfound; random random = new random(); public void lostpuppy(int thefloors, int therooms) {// ^ void issue , removed code(answered 7/14/2015 on stack overflow) char[][] myhidingplaces = new char[thefloors][therooms]; (int = 0; < thefloors; i++) { (int j = 0; j < therooms; j++) { myhidingplaces[i][j] = ' '; } } myfloorlocation = random.nextint(thefloors); myroomlocation = random.nextint(therooms); myhidingplaces[myfloorlocation][myroomlocation] = 'p'; mywinner = ' '; myfound = false; } public boolean roomsearchedalready(int floor, int room) { if (myhidingplaces[floor][room] == '1' || myhidingplaces[floor][room] == '2') { return true; } else { return false; } } public boolean puppylocation(int floor, int room) { if (myhidingplaces[floor][room] == 'p') { return true; } else { return false; } } public boolean indicesok(int floor, int room) { if (floor <= myhidingplaces.length || room <= myhidingplaces[0].length) { return true; } else { return false; } } public int numberoffloors() { return myhidingplaces.length - 1; } public int numberofrooms() { return myhidingplaces[0].length - 1; } public boolean searchroom(int floor, int room, char player) { if (myfound = true) { mywinner = player; myfound = true; return true; } else { myhidingplaces[floor][room] = player; return false; } } public string tostring() { return "this test"; } }
i not want specific solutions assignment in responses post. issues me figure out. want explanation why test code wont compile/run.
this line
game = new lostpuppy(totalfloors, totalrooms); won't compile because haven't defined constructor expect 2 int arguments (totalfloors , totalrooms).
in order fix this, declare in class lostpuppy constructor such
public lostpuppy(int totalfloors, int totalrooms) { //do here paremeters.. } this line
while (!game.indicesok(floor, room) won't compile because haven't defined indicesok method in lostpuppy class.
create method such as
public boolean indicesok(int floot, int room) { //return conditions }
Comments
Post a Comment