#Codes

77 messages · Page 1 of 1 (latest)

vale karma
#

How can i write this codes?

opaque needleBOT
#

This post has been reserved for your question.

Hey @vale karma! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

vale karma
#

i wrote this but i can't continue

#

How will i identify to videoGameList

#

and the methods

grave sage
#

there are two methods

vale karma
grave sage
#

its a VideoGame[]

vale karma
#

private VideoGame[] videoGameList;

#

is it?

grave sage
#

its an array yes

polar tendon
#
public class ShoppingSite {
  private String storeName;
  private int videoGameCount;
  private int totalGameStock;
  private VideoGame[] videoGameList = new VideoGame[totalGameStock]; // reference to a VideoGame class somewhere
  
  public ShoppingSite(String storeName) {
    this.storeName = storeName;
  }

  public String getStoreName(){
    return this.storeName;
  }

  public void addVideoGame(VideoGame videoGame){
    videoGameList[this.videoGameCount + 1] = videoGame;
    this.videoGameCount++

    // maybe add check if videoGameCount > totalGamesStock then can't add more videoGame's
  }
}
#

Think this could work?

#

This assumes that totalGameStock is the maximum number of games in the store and videoGameCount is how many games are in the stock room

vale karma
#

how can i test "totalgameStock"

#

when i add the new game it has to be increase

polar tendon
#

Is this a school project?

vale karma
#

nope i wanna make a practice

polar tendon
#

Looking at the error message, there seems to be an error on line 15 of ShoppingSite.java

vale karma
#

it is " videoGameList[this.videoGameCount++] = videoGame;
"

polar tendon
#

So an array is immutable, you can't keep updating the length of videoGameList. Can I see a screenshot of ShoppingSite.java to try and fix the error

#

I think the error is it needs to be:

vale karma
polar tendon
#
  videoGameList[this.videoGameCount + 1] = videoGame;
#

yes this should fix your error ^

vale karma
polar tendon
#

Also, in your constructor function you need to instantiate what videoGameCount and totalGameStock are, and then move the ```java
new VideoGame[this.totalGameStock];

polar tendon
# vale karma isn't it same already?

You normally use ++ if you want to add one to the value. It might return the value + 1 but for readability I would say ```java
this.videoGameCount + 1

vale karma
#

like that?

polar tendon
#

You still need to instantiate this in the constructor. I feel as though if you are struggling with this and you aren't doing this as part of education then you should rewatch your fundamentals as it is covered well in the freecodecamp java course (https://www.youtube.com/watch?v=A74TOX803D0) or if you are in education you should wait until you get further or talk to your lecturer

Learn the Java programming language in this complete beginner's course. You will learn all the main features of Java (version 17) and how to use Java for your own projects.
🔗 Code along on Replit: https://join.replit.com/java-students

✏️ Farhan Hasin Chowdhury created this course.

🏗 Replit provided a grant that make this course possible.

🔗 h...

▶ Play video
vale karma
#

but how , i already instantiate constructor called " ShoppingSite(String storeName),

#

wil

#

will i have to create one more?

polar tendon
#

You need to instantiate all of the variables in there preferably however your class diagram doesn't ask for any other inputs except for a String. As far as I can tell you haven't attempted to do this by yourself and have just copied and pasted what I have given. I recommend you go back to the fundamentals to understand how this all works. I'm not trying to be mean I think this project may just be a bit too advanced for you currently

vale karma
#

yep i got it

#

u helped me

#

thank u

opaque needleBOT
# vale karma thank u

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

vale karma
#

but i have to ask one more thing

#

as you say " your class diagram doesn't ask for any other inputs except for a String". So, how ahaha

#

im not asking the code

#

but how it can be possible

polar tendon
#

I assume there must be a hard coded value for these variables, which would mean they don't need to be in the constructor, however without knowing these values I can't tell you if this is the solution. If I were making this as a practice project I would just include these values in the constructor

#

Where did you find this diagram? It seems like a fun challenge

vale karma
#

yep my friend gave

#

actually i wrote the other codes

#

can i show you

#

like that

#

i didn't understand only "addVideoGame

#

my friend gave me the print

#

it has to be like this

#

1m

#

i did everything actually but i couldn't write the last sentence

polar tendon
#

Right okay, looking at what the output is I would scrap what I told you lol. I would be interested in how your friend designed the videoGameList as it is a list with unknown size so I would use an ArrayList<VideoGame> however the typing is VideoGame[] so you must know how big the videoGameList is?

#

or I guess you could make videoGameList's size some large number

vale karma
#

i guess it doesn't matter for him

vale karma
polar tendon
#

Show code

vale karma
#

i am trying arraylist now

#

how it could be?

#

i created a arraylist and i used ".set" command

polar tendon
#

Instead of using the .set command you can use the .add command to append to the end.

#

also your videoGameCount is now wrong. to get the total you would need to loop through your videoGameList and get the total of all the VideoGame's getGameStock's()

vale karma
#

oh thank u

opaque needleBOT
# vale karma oh thank u

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

vale karma
#

thank u very much for everythin

#

g