• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm stuck on a homework assignment  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help please.
I'm stuck here. My homework assignment instructions are

1) Create a Javabean called WeatherBean with 3 String attributes : morningWeather, noonWeather, and eveningWeather.
2) Create another javabean called RegionBean with 3 WeatherBean attributes: eastCoast, westCoast and realCoast.
3) Initialize weather for all regions at all times of day. Write a servlet to pass a RegionBean to a JSP that prints that noon weather on West Coast and morning weather on Real Coast.

So 1 and 2. Got it
#3 I'm confused by these instructions . "Initialize weather for all regions at all times of day".

I'm thinking on RegionBean for the setters say setWestCoast I need to get each of the weathers. ( getMorningWeather, getNoonWeather and getEveningWeather) but each of the coasts are a WeatherBean object so I'm not understanding how to get the weathers (noonWeather, morningWeather etc) which are on the WeatherBean from the RegionBean.


I have been trying to figure out different ways to make this work. In the RegionBean I have the issue that each of the coasts ( e.g. westCoast ) is a WeatherBean object.
I've been trying to figure this out. I keep thinking I need a way to initialize a variable to the weather getters ( getMorningWeather ) for example but since WeatherBean is a WeatherBean object and the getMorningWeather is a String object maybe I need something which can change WeatherBean object to a String object?

Am I close to being on the right track at all ?

I hope it's okay I post this question here rather than in the jsp forum. I thought this was more a java question not a jsp / servlet issue .



------------- Here is my WeatherBean -------------------------



----------- Here is my RegionBean --------------------------




---------------- Here is my WeatherServlet --------------------------------



 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Welcome to the Ranch!

Looks like you have got #1 pretty much under control.
#2 needs some more work
and you asked for help on #3

"Initialize weather for all regions at all times of day"
Basically I read this as meaning you need to set morning, noon and evening weather for all of the regions - east, west, real coast.
So you could then call regionBean.getEastCoast().getMorningWeather() - and have it return a value.
The question makes no mention of what the values might be - I think you have to make them up.


You normally do initialization in a constructor - I think this is where you need to focus your attention.
Take the Weather Bean.



Right now according to this code, weather is always "Cold and Rainy" in the morning, "Sunny" at noon, and "Cloudy" in the evening.
You would get this by calling new WeatherBean();
You could change this instead so that you provide the values as parameters to your constructor i.e. WeatherBean eastCoastWeather = new WeatherBean("Fog", "Cloud cover", "Showers")
Or after creating a new Weather bean, call "set" methods to change their values.
That will let you define "Weather" for one region.


Looking at your RegionBean constructor:



This constructor effectively does nothing right now. It sets each variable to... itself.
Similarly I would expect to have values passed in on the constructor, or call the setter methods like regionBean.setEastCoast(eastCoastWeather)


Hope this helps some,
cheers,
Stefan
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of the import statements in the first two classes are necessary. The one for JSTL StringLiteral, in particular, is strangely out of place and seems like it's a result of copy-paste coding. Don't import anything that's not relevant, otherwise it just causes confusion and doubt about your code.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW, this is a poorly thought-out exercise to begin with. It is not a good object-oriented design. The "design smell" there is the different qualifiers used for the fields: morning, noon, evening, east, west, real.

If anything, a WeatherBean would have these attributes: time (morning, noon, evening), weather (cold, warm, rainy, cloudy, sunny, etc.). Then you'd need a way to lump different WeatherBeans for all times in one day together.

Similarly, a RegionBean would have these attributes: region (east, west, real), weather (a collection of WeatherBeans, perhaps for a single day).

This design would make the requirement to "Initialize weather for all regions at all times of day" naturally break down into the separate tasks to "initialize weather for all times of day" and "initialize weather for all regions"

With this design in mind, the problem would be:

1. Create three RegionBean instances: one each for the East, West, and Real coasts.
2. For each of the RegionBean instances, initialize their weather for all times of the day (morning, noon, evening) by creating as many WeatherBean instances necessary to represent the weather for that region for a day
3. Make your JSP display the weather information for all available regions and all available times

You can still use the above to kind of figure out what you need to do with the poor design that you've been given. Basically, what you're missing is code (and a place to put that code) that creates RegionBean instances and WeatherBean instances and associates these with each other. Given what you have, the best place would probably be the servlet. That is, the servlet would have a method where the bean creation and association is done, then it puts these beans into one of the contexts available to a JSP (page, request, session, or application). The JSP then takes these beans out of the context and displays the information.

Maybe the design was presented the way it was because you haven't been taught how to do loops in JSPs yet. If that's the case, then it's a reasonable first step but one that should be moved towards the better design that I suggested once you've learned about looping constructs in JSP, using JSTL, of course.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If anything, a WeatherBean would have these attributes: time (morning, noon, evening), weather (cold, warm, rainy, cloudy, sunny, etc.). Then you'd need a way to lump different WeatherBeans for all times in one day together.

Similarly, a RegionBean would have these attributes: region (east, west, real), weather (a collection of WeatherBeans, perhaps for a single day).

Given this design, more appropriate names for these classes would be something like TemporalWeather and RegionalDailyWeather. These are really "moment in time" concepts that you're dealing with. The "Bean" suffix is redundant, IMO, and puts you in a mindset of thinking too much about implementation when you really should be thinking about the high-level abstract ideas. In a real-world programming scenario, some teams (like mine) will discuss a name like TemporalWeather for a few minutes, trying to see if the name really fits the concept that's associated with it and if there's a better name to represent that concept.

I know this is all probably still above your head right now but keep this in mind as you progress further along because this will eventually help you write better programs: Good names tell you about the intent of the program, not the implementation.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thank You Stefan that helped a lot. I got things working. Just so confused by the terms.

Stefan Evans wrote:Hi. Welcome to the Ranch!

Looks like you have got #1 pretty much under control.
#2 needs some more work
and you asked for help on #3

"Initialize weather for all regions at all times of day"
Basically I read this as meaning you need to set morning, noon and evening weather for all of the regions - east, west, real coast.
So you could then call regionBean.getEastCoast().getMorningWeather() - and have it return a value.
The question makes no mention of what the values might be - I think you have to make them up.


You normally do initialization in a constructor - I think this is where you need to focus your attention.
Take the Weather Bean.



Right now according to this code, weather is always "Cold and Rainy" in the morning, "Sunny" at noon, and "Cloudy" in the evening.
You would get this by calling new WeatherBean();
You could change this instead so that you provide the values as parameters to your constructor i.e. WeatherBean eastCoastWeather = new WeatherBean("Fog", "Cloud cover", "Showers")
Or after creating a new Weather bean, call "set" methods to change their values.
That will let you define "Weather" for one region.


Looking at your RegionBean constructor:



This constructor effectively does nothing right now. It sets each variable to... itself.
Similarly I would expect to have values passed in on the constructor, or call the setter methods like regionBean.setEastCoast(eastCoastWeather)


Hope this helps some,
cheers,
Stefan
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:None of the import statements in the first two classes are necessary. The one for JSTL StringLiteral, in particular, is strangely out of place and seems like it's a result of copy-paste coding. Don't import anything that's not relevant, otherwise it just causes confusion and doubt about your code.


No they aren't. They came in when I was trying to figure out how to solve this issue and I just hadn't removed them but thank you.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:FWIW, this is a poorly thought-out exercise to begin with. It is not a good object-oriented design. The "design smell" there is the different qualifiers used for the fields: morning, noon, evening, east, west, real.

If anything, a WeatherBean would have these attributes: time (morning, noon, evening), weather (cold, warm, rainy, cloudy, sunny, etc.). Then you'd need a way to lump different WeatherBeans for all times in one day together.

Similarly, a RegionBean would have these attributes: region (east, west, real), weather (a collection of WeatherBeans, perhaps for a single day).

This design would make the requirement to "Initialize weather for all regions at all times of day" naturally break down into the separate tasks to "initialize weather for all times of day" and "initialize weather for all regions"

With this design in mind, the problem would be:

1. Create three RegionBean instances: one each for the East, West, and Real coasts.
2. For each of the RegionBean instances, initialize their weather for all times of the day (morning, noon, evening) by creating as many WeatherBean instances necessary to represent the weather for that region for a day
3. Make your JSP display the weather information for all available regions and all available times

You can still use the above to kind of figure out what you need to do with the poor design that you've been given. Basically, what you're missing is code (and a place to put that code) that creates RegionBean instances and WeatherBean instances and associates these with each other. Given what you have, the best place would probably be the servlet. That is, the servlet would have a method where the bean creation and association is done, then it puts these beans into one of the contexts available to a JSP (page, request, session, or application). The JSP then takes these beans out of the context and displays the information.

Maybe the design was presented the way it was because you haven't been taught how to do loops in JSPs yet. If that's the case, then it's a reasonable first step but one that should be moved towards the better design that I suggested once you've learned about looping constructs in JSP, using JSTL, of course.


Thank You! You are correct. We haven't gone over loops in JSP yet so that maybe why. I appreciate the help. I've gotten things to work thanks to you guys.!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!