• Post Reply Bookmark Topic Watch Topic
  • New Topic

hi this is part of my StateProvince and Country class and wondering how do I do this?

 
Robert Gagno
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) use foreach loops instead of for loops, and (2) change your Arrays to ArrayLists, (3)change lab 8b to use only annoymous objects when adding State Province Objects to the deck.

Add the following methods to the Country class:

1. public void displayAllStateProvinces(), which loops through the country and prints each StateProvince objects' getDetails() method.

2. public int howManyStartWith(String subString) which takes a string(e.g. "Alabama" and "Alaska" would mean 2 is returned). Hint use the startswith() string method

here is the class I need help with all these instructions I have provided





 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36031
432
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert,
Welcome to CodeRanch! What specifically are you stuck on? Right now, it reads like the raw homework assignment.
 
Dwayne Barsotta
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to keep some type of collection of the "StateProvince" objects created and loop through each one.  You can have a collection in the class file itself that has all the objects created or you can have the main class that created the objects hold the collection and loop through them.  As your code is right now there is no way to loop through all the objects of "StateProvince". 

Your second issue is the same, you need to work with a collection of objects of type "StateProvince"

Right now your code needs to use:

StateProvince Ontario = new StateProvince("Ontario", "Toronto", 500);
StateProvince Alberta= new StateProvince("Alberta", "Edmonton", 745);

But none are stored in a collection:

you could have a list:

List<StateProvince> myList = new ArrayList<StateProvince>;
myList.add(Ontario );
myList.add(Alberta);

then you could loop through myList getting the reply's back you want.

My code might be a littke off, googke the proper way to make lists and such.  I don't know the idea of the complete program so I can't tell you if the Class of "StateProvince" should contain your collection or your main class.  I am thinking the main class should. 
 
Campbell Ritchie
Marshal
Posts: 53171
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

You are doing it the hard way with those arrays. Try array initialisers: they have the advantage that there is no risk of overrunning the size of the array, nor of leaving a null somewhere by mistake. I hope you can see there is repeated code in those two methods; that is a signal that you shou‍ls refactor that code by making it a method in its own right.
There is another problem, that the same arrays appear in a different class. That is a signal that you shou‍ld refactor that code by making it a method in its own right. And the only way you can make it into a method rather than two methods is by putting it in a class of its own.
There is a style problem in the third method I quoted. Look at the old Sun style guide. Don't write
if (something) return true; else return false;
Write
return something;
Also test that method carefully and see what happens for populations of 4, 23, 38, 999999999 and −999999999.
G'day, cobber! What will happen in your setCapital method if you pass "Sydney" as an argument? Do you permit Australian provinces?
 
Knute Snortum
Sheriff
Posts: 3329
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have several setters that look like this:

That isn't going to work.  You need to declare a parameter.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!