#return doubt

142 messages ยท Page 1 of 1 (latest)

turbid meadow
#

Here i want to determine if the given character is C, L, R, B, T or M. do I do it like this or do i use "CLRBTM".indexOf(c) ?? Idk if I need to create a string called CLRBTM before doing it tho. im stuck here for 10 mins

vernal harborBOT
#

<@&987246883653156906> please have a look, thanks.

turbid meadow
#

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

lofty zinc
#

i dont think it matters

turbid meadow
#

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

lofty zinc
#

here is another option Set.of('C', 'L', 'R', 'B', 'T', 'M').contains(c)

#

it depends what your teacher considers simplified

turbid meadow
#

i didnt learn that

#

i dont even know what that means

#

๐Ÿ˜ญ ๐Ÿ˜ญ

lofty zinc
#

okay so sets are collections without duplicates

#

you mostly use them to check for "membership"

turbid meadow
lofty zinc
#

so like "the set of all numbers from 1 to 10" has 1, 2, 3, 4, 5, 6,7 ,8 ,9, and 10

turbid meadow
lofty zinc
#

it has a small runtime overhead but it clearly communicates intent

lofty zinc
turbid meadow
#

i needed to create it or could simply write "CRLBTM".indexOf(c) ?

#

idk if i can do that

lofty zinc
#

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

turbid meadow
lofty zinc
#

it doesn't matter here

turbid meadow
#

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

lofty zinc
#

i'm fine with the acronym "yk" but its not adding anything to your sentences. Its like a textual tic, stop it

lofty zinc
#

when you put a string literal in your code like "abc" the java compiler puts it in what we call the "constant pool"

lofty zinc
#

when you do new String java needs to actually make a new string object, so that will be more work

turbid meadow
lofty zinc
#

yeah

#

new String( is pretty rare to need

turbid meadow
lofty zinc
#

modern computers are quite silly

turbid meadow
#

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

stable oasis
#

when u start trying to write things in weird ways to "maybe it makes it faster", ur doing things wrong

lofty zinc
#

you are having a mental spiral over a single line of code

stable oasis
#

write readable and understandable code

turbid meadow
turbid meadow
#

thats why I did it like this

#

and not the "CRLTBM"

#

cuz it made more sense to me like that

lofty zinc
#

what does CRLTBM come from? why those letters?

stable oasis
#

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

lofty zinc
#

@turbid meadow What does "R" stand for?

stable oasis
#
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

turbid meadow
turbid meadow
#

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

turbid meadow
#

before the function

#

i have the description of what it does

stable oasis
#

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

turbid meadow
#

i always use indexOf(c) >= 0

stable oasis
#

(interesting advantage of the EnumSet btw, its internally implemented using bitmasks. so its also the fastest implementation)

turbid meadow
#

cuz the index is from 0 to the length of it

#

why people use -1?

stable oasis
#

bc the javadoc of indexOf says it returns -1 if it couldnt be found

stable oasis
#

so != -1 and >= 0 is identical in practice

#

in this case

turbid meadow
#

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

stable oasis
#

ah they decided to have u implement the board with strings

#

that explains why u did what u did

turbid meadow
stable oasis
#

instead of some Tile[][] board

turbid meadow
#

they always decide what i have to implement

stable oasis
#

with an enum

#

ok

#

u shouldnt nest ifs like that btw, ur making hadouken

turbid meadow
stable oasis
#
if (first
  && second
  && third
  && fourth) {
  ...
}
#
  1. 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 ๐Ÿ‘

turbid meadow
#

so how would i separate those ifs

turbid meadow
#

tysm bro

#

u alr helped a lot

minor torrent
#
  1. 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?

minor torrent