• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with display routine  RSS feed

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working on a project that I have to be able to add, remove, search for, and display flowers in the "pack".  I can't for the life of me figure out how to get it to print correctly.  Generally I'll try to display the array, but nothing returns.  It just brings up the prompt to make another choice (add, remove, search, display).  Any tips are greatly appreciated!

P.S.  I modeled it after the code I found through a search on here, but I still can't figure it out.



My flower class:


 
Junilu Lacar
Sheriff
Posts: 10880
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you setting i to 1 on line 163? That statement is inside a for-loop that uses i as its loop index. You're essentially putting the loop back on square 1 every time through. If you were playing Snakes and Ladders, it's like landing on a snake that goes back to the first square no matter what you roll on the dice.

As for your algorithm, you should try to do it manually with pen and paper first. Once you figure out the strategy that way, it's easier to code it out.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Why are you setting i to 1 on line 163? That statement is inside a for-loop that uses i as its loop index. You're essentially putting the loop back on square 1 every time through. If you were playing Snakes and Ladders, it's like landing on a snake that goes back to the first square no matter what you roll on the dice.

As for your algorithm, you should try to do it manually with pen and paper first. Once you figure out the strategy that way, it's easier to code it out.


So it resets for the next flower to count?  That's what I'm trying to accomplish anyway.


I haven't actually tried writing code down with pen and paper, but that's what I have trouble the most, translating the idea in my head into code.
 
Tony Docherty
Bartender
Posts: 3210
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:I haven't actually tried writing code down with pen and paper, but that's what I have trouble the most, translating the idea in my head into code.

Don't write code with pen and paper, write out in your native language how you would solve it and then once you have a clear description of how to solve the problem translate that to code.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get what you're saying though.  It needs to be outside of the for loop.
 
Carey Brown
Saloon Keeper
Posts: 2540
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lines 67-70 are redundant, the constructor already assigns those attributes. Remove those lines.

And while we're on the subject, does your Flower class really need any setters? Once you set the attributes in the constructor, what part of the program's logic requires that an attribute can be modified later?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Lines 67-70 are redundant, the constructor already assigns those attributes. Remove those lines.

And while we're on the subject, does your Flower class really need any setters? Once you set the attributes in the constructor, what part of the program's logic requires that an attribute can be modified later?


I thought they were redundant, but I wasn't 100% sure.  I wrote the flower class first and I wasn't sure what  I was going to need so I made them all.  The directions mention creating getters and mutators in the flower class so I was trying to implement them. This is a project that will be continuous, though, so I might need them later.
 
Carey Brown
Saloon Keeper
Posts: 2540
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second half of this expression will always be false because you're comparing a String to null.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overall, though, I'm feeling pretty discouraged because everyone else seems to be turning everything in on time and understanding.  Where else can I get help besides my teacher and book?  Anyone know of ay good sites?  I already went though the Java tutorial on codecacemy.  Any thing else like that out there?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:The second half of this expression will always be false because you're comparing a String to null.


I should be comparing it to flowerName, right?
 
Carey Brown
Saloon Keeper
Posts: 2540
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:
Carey Brown wrote:The second half of this expression will always be false because you're comparing a String to null.


I should be comparing it to flowerName, right?

No.
You have
Which is the same as
Replacing null in the second half of the expression would be
Which as you can see doesn't make any sense, it will always be true.

On line 170 you are incrementing 'count'. What is it you are counting? Null flower names?
 
Carey Brown
Saloon Keeper
Posts: 2540
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To step back a bit from the problem. You have an array of flowers, some of which may be null (empty slot), and possibly more than one array slot containing the same flower name (e.g. "Rose").

You want to step through the array and for each non-null entry print the flower name and a count of other entries with the same name. Here's the rub, once you have found one Rose and counted all the other Roses, what's to keep you from displaying Rose again when you continue to step through the array? You need some way to identify that you've already processed all the Rose entries so that you don't process them again.

A simple way to identify those that you've already processed is by setting the array element to null once they have been counted. This works well because your loop logic will already step over null entries. On the other hand you cannot be modifying the array that you'll be needing later for other operations. This is where making a copy of your array, as you've done with searchFlowers, allows you to modify the elements of searchFlowers without damaging the main flowers array.

Does this make sense?
 
Liutauras Vilda
Marshal
Posts: 3829
203
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny,

1. I have a feeling that you're not in the right direction putting so much code in the constructor of the class Assignment2. Constructor is meant to construct object but not interact with user during the app lifetime. Might better have a similar construct to this:
As you see I've made Assignment2 constructor private, and probably that isn't so bad, because you don't want to instantiate this class, it is responsible to give a go for you app.

2. Having said that, now it doesn't look right to have all those "other methods" in this class as addFlower, removeFlower and so on. What is that something where you adding/removing/searching/... flowers? FlowerBucket? FlowerPack? Whenever that might be, but you surely don't add flowers to your Assignment2 (even though teacher (in case of 'she') might would be happy about that). So, methods should really belong to a particular thing and it should make sense. Currently you have some sort of disconnected things. For me helps to try to relate things with a real world examples, finally, this is what the applications are about.

As for example imagine you have a Garage and a Car. If you need to change a gear, you'd expect to be able to do that with a car, so, in which class you'd put method changeGear() ? Of course to a Car class. Which thing can accept cars for repairing? Yeah - garage, not a car, so if you were have a method acceptForRepairWork(uaz), you'd expect this method to be in a Garage class. Now, look again to your Assignment2 class - it isn't quite right, agree? That is why you already have a Flower class and all related methods in it. That means you need another class for the purpose of adding/removing/..., look to instructions what they say about that.

Better, do you have and can share instructions with us? Interesting to see what they say about those things.

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Johnny Vazquez wrote:
Carey Brown wrote:The second half of this expression will always be false because you're comparing a String to null.


I should be comparing it to flowerName, right?

No.
You have
Which is the same as
Replacing null in the second half of the expression would be
Which as you can see doesn't make any sense, it will always be true.

On line 170 you are incrementing 'count'. What is it you are counting? Null flower names?


Rhetorical question?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Johnny,

1. I have a feeling that you're not in the right direction putting so much code in the constructor of the class Assignment2. Constructor is meant to construct object but not interact with user during the app lifetime. Might better have a similar construct to this:
As you see I've made Assignment2 constructor private, and probably that isn't so bad, because you don't want to instantiate this class, it is responsible to give a go for you app.

2. Having said that, now it doesn't look right to have all those "other methods" in this class as addFlower, removeFlower and so on. What is that something where you adding/removing/searching/... flowers? FlowerBucket? FlowerPack? Whenever that might be, but you surely don't add flowers to your Assignment2 (even though teacher (in case of 'she') might would be happy about that). So, methods should really belong to a particular thing and it should make sense. Currently you have some sort of disconnected things. For me helps to try to relate things with a real world examples, finally, this is what the applications are about.

As for example imagine you have a Garage and a Car. If you need to change a gear, you'd expect to be able to do that with a car, so, in which class you'd put method changeGear() ? Of course to a Car class. Which thing can accept cars for repairing? Yeah - garage, not a car, so if you were have a method acceptForRepairWork(uaz), you'd expect this method to be in a Garage class. Now, look again to your Assignment2 class - it isn't quite right, agree? That is why you already have a Flower class and all related methods in it. That means you need another class for the purpose of adding/removing/..., look to instructions what they say about that.

Better, do you have and can share instructions with us? Interesting to see what they say about those things.



We aren't supposed to change his code.  I see what you're saying, but I've never created a run method like that before.

Now that we’ve helped out our friend and his sister they have invited us over for dinner to talk about what improvements can be made.  We find out many things about the young boy, we even find out that his name is Alexander and his sister’s name is Elizabeth. Elizabeth tells us a about the day of her accident when she was climbing a tree and a branch broke causing her to fall to the ground giving her these near fatal wounds leaving her bed-ridden.  Finally, we start talking about the improvements they would like made to make Elizabeth’s life a little happier.
Alexander finds himself searching through his pack for specific traits of a flower. Some days Elizabeth wants only red flowers or only flowers with thorns as she likes to peel them off. Surely we can help them out!
• Create a flower object that has specific traits (name, color, presence of thorns and smell)
• These flower objects must be able to stay in his pack (Use an array of length 25)
• Be able to add, remove, display  and search these flowers – by name (We can drop the sorting for now)
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:To step back a bit from the problem. You have an array of flowers, some of which may be null (empty slot), and possibly more than one array slot containing the same flower name (e.g. "Rose").

You want to step through the array and for each non-null entry print the flower name and a count of other entries with the same name. Here's the rub, once you have found one Rose and counted all the other Roses, what's to keep you from displaying Rose again when you continue to step through the array? You need some way to identify that you've already processed all the Rose entries so that you don't process them again.

A simple way to identify those that you've already processed is by setting the array element to null once they have been counted. This works well because your loop logic will already step over null entries. On the other hand you cannot be modifying the array that you'll be needing later for other operations. This is where making a copy of your array, as you've done with searchFlowers, allows you to modify the elements of searchFlowers without damaging the main flowers array.

Does this make sense?



I think this makes sense, but I'm at work currently.  I'm going to re-read it this afternoon and get back to you.
 
Liutauras Vilda
Marshal
Posts: 3829
203
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:We aren't supposed to change his code.

Instructions wrote:1. Create a flower object
...
2. These flower objects must be able to stay in his pack (Use an array of length 25)

Here it is. You need a FlowerPack class, it has to be able to accommodate certain number of flowers.
 
Junilu Lacar
Sheriff
Posts: 10880
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, Johnny, your teacher is getting kind of heavy on the drama there with that problem statement. What is he, moonlighting as a writer for the Lifetime channel? 

The easiest, most straightforward way I can think of to do this is through a two-step process. You basically want to create a histogram and the best way to do that is to use a Map. In this case, it would be a Map<String, Integer>. The key of the map would be the flower name and the value would be the count. You go through the array of flowers, either putting in an initial count or updating the existing count that's in the map as you go. When you're done collecting the data about your array of flowers, then you just go through the map and print out its entries, I.e., the flower names and their corresponding counts.

The procedure is much more complicated if you need to use only arrays because you'd have to create two arrays for the histogram: one to hold the unique names of the flowers in the original array, and another parallel array if int to hold the counts for the corresponding flower name. Then you'd need a variable to keep track of how many unique flower names you've found so far, as well as another loop to search the array of unique names to see if you already found it before.

You can trace through this manually on paper. Again, when you do this manually, don't write code but instead use boxes and words and little picture and other things that will help you visualize the *process*, not the code.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Johnny Vazquez wrote:We aren't supposed to change his code.

Instructions wrote:1. Create a flower object
...
2. These flower objects must be able to stay in his pack (Use an array of length 25)

Here it is. You need a FlowerPack class, it has to be able to accommodate certain number of flowers.


I have an array with a length of 25.  He doesn't want us to use a third class because he only wants us to turn in Assignment2.java and Flower.java. 

Unless I'm mis-understanding what you're saying.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The easiest, most straightforward way I can think of to do this is through a two-step process. You basically want to create a histogram and the best way to do that is to use a Map. In this case, it would be a Map<String, Integer>.

[MODERATOR EDIT: deleted irrelevant parts of long quote]


Is Map apart of array Lists?  We haven't gone over array lists yet going to be adding them on the next assignment.
 
Liutauras Vilda
Marshal
Posts: 3829
203
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:He doesn't want us to use a third class because he only wants us to turn in Assignment2.java and Flower.java. 

Unless I'm mis-understanding what you're saying.
No, probably it's me who misunderstood these instructions (more of a idle talk honestly).

If you explicitly been told to submit two files, assuming in both there need to be only by one class defined, then you are right in the approach, here isn't much to misunderstand "wants us to turn in Assignment2.java and Flower.java". Sorry for confusion.
 
Junilu Lacar
Sheriff
Posts: 10880
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:Is Map apart of array Lists?  We haven't gone over array lists yet going to be adding them on the next assignment.

I don't know why your teacher wouldn't give you an example of this first but oh well ...

Here's what a pencil-paper visualization might look like:

The array of flowers:
012345678910...
RoseCarnationRoseRoseLilyCarnationLilyTulipMarigoldCarnationMarigold...

Your histogram:

unique names:
0123...
RoseCarnationLilyTulip...

counts:
0123...
3321...


The first rows in each of those tables, the ones with the consecutive numbers in them starting from 0 is the array index. The second row is what the array actually holds. If you use these tables and go back over the process I describe, maybe you'll get a better picture of what you need to do in your code.

EDIT: To make it even easier for you to visualize the histogram:

[0][1][2][3]...
namesRoseCarnationLilyTulip...
counts3321...

Again, names and counts would have to be two separate arrays, one holding Strings for the unique flower names and the other one holding ints for the corresponding counts. They are parallel because elements in either array that have the same index go together.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Here's what a pencil-paper visualization might look like: ...

[MODERATOR EDIT: deleted irrelevant parts of long quote]

I'll give it a shot this afternoon!  This is technically my second semester of Java, but my first semester was 3 years ago and I haven't used Java in between.  Maybe that's why he hasn't shown us (thinks we already know)?  I'm having a hard time remember all the basic things I learned.
 
Knute Snortum
Sheriff
Posts: 3557
86
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to refresh your basic Java knowledge, this tutorial is a good place to start.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:If you need to refresh your basic Java knowledge, this tutorial is a good place to start.


Thanks, I'll definitely go over it again and again.

I was working on this project last night.  I think I'm making a little progress, but we'll see when I re-finish the methods.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been busy, so I have only just come across this discussion. You may have changed it since you posted, but why have you got all the code about selecting options in a constructor?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I have been busy, so I have only just come across this discussion. You may have changed it since you posted, but why have you got all the code about selecting options in a constructor?


It is code that was given by my professor and we aren't allowed to change it.  We need to turn in an Assignment2.java and a Flower.java. 
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would always prefer assignments where the candidates have to write all their own code. It would be better to have that code in a different method, in fact maybe even in a different class, but you cannot change it now.
 
Liutauras Vilda
Marshal
Posts: 3829
203
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:I think I'm making a little progress
Please show us. We should have solved it by now, it isn't very complicated task. You just might need hint or two.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a buddy who knows some java, but definitely not an expert (better than me).  We ended up re-writing my whole Assignment2 class.  Do you think this is better? Or should I build off my original?

I seem to be able to add and display flowers now (but I thought my display routine would be simpler, seems kind of long).  Searching and removing flowers doesn't quite work.  When trying to remove a flower it says flower not found 2x then gives me a null pointer exception.  Is this because I didn't fill the array completely?  When I add a lily then search for the lily I get

Exception in thread "main" Flower not found.
Flower found!

java.lang.NullPointerException
at Assignment2.searchFlowers(Assignment2.java:130)
at Assignment2.<init>(Assignment2.java:38)
at Assignment2.main(Assignment2.java:7)

 
Junilu Lacar
Sheriff
Posts: 10880
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When searching, you don't account for the case where the array element you're accessing is null. Line 130 will throw a NullPointerException when the element is null.
 
Junilu Lacar
Sheriff
Posts: 10880
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your logic for searching for a flower is all wrong. Let's ignore that you're not accounting for nulls in the array for now and assume that the array will be full of valid Flower objects. The logic in the loop basically looks at each flower and immediately prints out either "Flower found!" or "Flower not found."  So if you have 25 flowers that don't match the flower being searched for, that code will print "Flower not found." 25 times.  Is that really what you want?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!