#return doubt
142 messages ยท Page 1 of 1 (latest)
<@&987246883653156906> please have a look, thanks.
public static boolean isShipPiece(char c) {
return c == 'C' ||c == 'L'|| c == 'R' || c == 'B' || c == 'T' || c == 'M';
}
here's the code if someone wants to help
both pieces of code would work
i dont think it matters
but my teacher said the project's grade will be influenced if it could be simplified yk
and i cannot use more than 100 characters per line
and idk if i can use "CLRBTM" before .indexOf(c)
cuz i learned to create the string before and then call it with the method yk
yk?
here is another option Set.of('C', 'L', 'R', 'B', 'T', 'M').contains(c)
it depends what your teacher considers simplified
the thing is
i didnt learn that
i dont even know what that means
๐ญ ๐ญ
okay so sets are collections without duplicates
you mostly use them to check for "membership"
yk = you know
so like "the set of all numbers from 1 to 10" has 1, 2, 3, 4, 5, 6,7 ,8 ,9, and 10
oh ok i get it
the thing is
it has a small runtime overhead but it clearly communicates intent
say those sentences out loud and take off the "you know"
i needed to create it or could simply write "CRLBTM".indexOf(c) ?
idk if i can do that
you can do it any number of ways
you are just paranoid about your teacher
theres nothing i can say to you that will either read your teacher's mind or make you not afraid
nah i just want my project to be as efficient as it could possibly be
it doesn't matter here
i know that
but like
this one
that I used
feels way too much
thats why I thought I could create a string before
but idk if I really need to do String s = new String .....
and then put s.indexOf(c)
yk
or if I can just use "CLRBTM".indexOf
i'm fine with the acronym "yk" but its not adding anything to your sentences. Its like a textual tic, stop it
no you do not
when you put a string literal in your code like "abc" the java compiler puts it in what we call the "constant pool"
sorry
when you do new String java needs to actually make a new string object, so that will be more work
so more unecessary stuff created
so "CLRTBM" is the most efficient way of doing it
no clue. to know what is and isnt faster you need to benchmark
modern computers are quite silly
or should i call it the laziest way cuz its faster to type
and more logical
even tho
there are many logical ways of doing it
mine just took more to type with the same logic
when u start trying to write things in weird ways to "maybe it makes it faster", ur doing things wrong
you are having a mental spiral over a single line of code
write readable and understandable code
just because of what my teacher told me
gonna follow that mindset
thats why I did it like this
and not the "CRLTBM"
cuz it made more sense to me like that
what does CRLTBM come from? why those letters?
id say neither is really readable
to someone without context
smells like sth that should be an enum instead
of special characters
and then an enumset
@turbid meadow What does "R" stand for?
enum ShipmentType {
CARGO,
TRANSPORT,
ROAD,
...
}
Set<ShipmentType> shipTypes = EnumSet.of(ShipmentType.CARGO, ShipmentType.BOAT, ...);
if (shipTypes.contains(type)) { ... }
thats the direction this is probably going if u want to make it readable
but more context is needed indeed
right
cuz its a shipbattle project
C means circle (1 boat with 1 length)
L means left,
R right
B bottom
T top
M middle
X water
and in that code i wanted to check if its a shippiece
thats why its just CLRBTM
i mean, im giving it context in javadoc
before the function
i have the description of what it does
since this is likely just some mini personal project its whatever
but in "real code" u would model ur tiles with enums instead
and then do some of what i shared above
or at least use a Set.of('C', 'L', 'X', ...) and then contains on that
strings and chars are for text
ur not working with text here
hence it would confuse people without context
but again, in such a small mini project situation it doesnt matter at all
pick whichever u prefer and move on
๐
if we talk about performance it will not be measurable. we are talking about numbers so small (in the low nanoseconds area) that even calling the measurement functions will take longer. the difference vanishes below the possible measurement resolution
the string.indexOf "trick" is known enough that at least non-juniors wont be confused about it
for example
boolean isVoval(char c) { return "aeiou".indexOf(c) != -1; }
its what happens when people try to be "smart" instead of writing things the proper and readable way
but as said, this trick is established enough to be fairly common knowledge
"proper" would be private static final Set<Character> VOVELS = Set.of('a', 'e', 'i', 'o', 'u'); and then return VOVELS.contains(c);.
debatable though which one should be preferred in this particular situation
but especially if u have larger scenarios, more content in the set, ... and so on, latest then should u stop doing these "tricks" and use a proper set instead
just a simple question
i always use indexOf(c) >= 0
(interesting advantage of the EnumSet btw, its internally implemented using bitmasks. so its also the fastest implementation)
damnnnnnnn
bc the javadoc of indexOf says it returns -1 if it couldnt be found
is this battleship?
yeah
btw @stable oasis i need ur help
i dont want the solution
but a way of thinking about it
im stuck for like 3 hours seeing this
i have to do this in the project
i wrote this
but idk how to proceed with the 4th condition
ah they decided to have u implement the board with strings
that explains why u did what u did
yeah
instead of some Tile[][] board
they always decide what i have to implement
it has to be true and only true if those 4 conditions are matched
if (first
&& second
&& third
&& fourth) {
...
}
- is a bit too complex for me to just understand right away
an example would be helpful
i have to leave now though. but others can help ๐
ok
so how would i separate those ifs
np
tysm bro
u alr helped a lot
- For each clue (row or col), there are enough positions in the corresponding row or col to accommodate the ship pieces , AND the positions already filled in do not exceed the given clue.
You need to explain the game a bit more. Is this like battle ship? What do the clue digits represent?
They said yes earlier
Well as usual, OP only provides scant insight into the assignment. WTF do the clues represent. Says they are a digit.