• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Regiment and Army Class  RSS feed

 
Jose Castellanos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

I have written a class for my programming assignment. When I run the test class it shows all of the regiments, their initial values, and the first report. However, the if statement at the bottom of the test class isn't working how I want it to. I want it to make more reports until there are no regiments left. Here are the specifications for the assignment.

I. Problem Statement

The following story is told about the Russian army at the time of the Russo-Japanese War:

It seems that 20 regiments were in a continuous process of formation. The first had 1000 men, the second had 950, the third 900, and so on down to the twentieth regiment, which garrisoned only 50. During each week, 100 men were added to each regiment, and at week's end, the largest regiment was sent off to the front.

Apparently, the commander of the 5th regiment was a wonderful chess player. To delay his eventual trip to the front, the General of the Army (who happened to be his chess partner) sent him only thirty new recruits each week (instead of 100).

Write a Java program that tells which regiment is sent to the front each week, shows the status of the remaining regiments, and determines exactly how long it takes for the chess-playing commander to go to the front.


II. Specifications

1. Create a class to represent a regiment. Each regiment knows its regiment number, name, and strength (number of men).

2. Create another class to represent an army (an army is a collection of regiments). Your army class will feature separate methods to do each of the following:

• add a regiment to the list
• update the list (i.e., add new men to each regiment)
• find the largest regiment
• print a report

3. Your test class will read the regiment number and name for each regiment from a data file, until eof, compute the number of initial recruits for the regiment, create a Regiment object, and add it to the list. Then, for each week of the simulation, it will then call the methods that update the list and print the report.

4. Output will be a series of reports --- one report for each of the 20 weeks.

5. Each weekly report should begin with an appropriate heading, including the week number.


6. The report should state which regiment is being shipped out that week, and data on the remaining regiments (i.e., those which have not yet gone to the front) should be in column form, neatly aligned, with headings. For each remaining regiment print the regiment number, name, and the current strength of the regiment.

Here is the Regiment and Army class:




Here is the Test class:


Here are the contents of the text file that was used as input:
1 Aardvarks
2 Begonias
3 Chrysanthemums
4 Dhalias
5 Elephants
6 Ferrets
7 GilaMonsters
8 Hyraxes
9 Ibex
10 Jackyls
11 KimodoDragons
12 Lemurs
13 Marigolds
14 Nonames
15 Opossums
16 Porcupines
17 Quahogs
18 Rhododendrons
19 Swordfish
20 Tapirs

The if statement at the end of the test class only works for one update. I want it to continue to show reports up until the last regiment is left. Any help would be greatly appreciated and thank you so much for your time!
 
Frankie Law
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am reading the assignment correctly, I think you need to reconsider part 1, where it says army strength (men) starts at 1000.

1 Aardvarks (1000 man)
2 Begonias (950)
3 Chrysanthemums (900)
4 Dhalias (850)
5 Elephants (800)
...

In the RegimentTester, you need to simulate a 20 week period.

Does that give you some hints?
 
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
Jose Castellanos wrote:
The if statement at the end of the test class only works for one update. I want it to continue to show reports up until the last regiment is left. Any help would be greatly appreciated and thank you so much for your time!

If you want to do something multiple times, what kind of Java control flow statement should you use?

A few things about your code:

1.  Standard naming convention for Java programs: methods should start with a lowercase letter. You have a number of methods that don't follow this convention and it makes your code confusing to read.

2.  Your code is inconsistently formatted, particularly the indentation of statements within control flow statements. If you are using an IDE like Eclipse or NetBeans, there's a keyboard shortcut for that: Ctrl+Shift+F.  I suggest you start using it often.

3.  Why does your update method build a String for reporting? That not only duplicates the functionality that's already in another method but it also takes away focus from what it should be doing, which is to just update the Regiment objects that are in the list.

4.  On line 7, what is the purpose of initializing the regiment strength to 1050? That's not even a number that's given in the problem statement: "The first had 1000 men, the second had 950, the third 900, and so on down to the twentieth regiment, which garrisoned only 50."  Where did you get 1050?  That looks like a kludge to me.
 
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
The problem statement says:  "the 5th regiment"

Yet, you write this in your program:

You defined three fields in your Regiment class:

Which of these three fields matches the information that the problem statement provides? Does your code check the most appropriate field?

As a matter of style, if your class is already named "Regiment," does it really make sense to prefix its attributes with "reg"?  On your driver's license, does it say "Driver's Name", "Driver's Address", "Driver's Date of Birth", "Driver's Height", "Driver's Color of Eyes", "Driver's Gender", and most silly of all, "Driver's Driver's License Number"?  Do you see what I'm getting at?
 
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
Why would you define methods like the ones below in your Regiment class?

Methods of a class should apply to all instances of that class. Those first two are specific to certain instances of the Regiment class. That third one shouldn't even be in the Regiment class at all.

Imagine your Tester class as being the Division Headquarters. It is the Division HQ that organizes the various regiments that belong to that division and assigns men to each of them. Does that make sense? Does that tell you what change you should make to your program with respect to assigning Regiment strength?
 
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
@OP wrote this:

This is actually a fairly common "code smell" that has a documented refactoring: http://refactoring.com/catalog/parameterizeMethod.html
 
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
I wrote:
Imagine your Tester class as being the Division Headquarters. It is the Division HQ that organizes the various regiments that belong to that division and assigns men to each of them. Does that make sense? Does that tell you what change you should make to your program with respect to assigning Regiment strength?

Another alternative, which is the one I actually ended up using in my solution, is to have the Army class be responsible for assigning men to regiments as they get added to the Army's list of regiments.

To give you an idea of what you should expect your program to output, here's part of what my solution prints out:
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another alternative which I would suggest is that printing a Regiment is the responsibility of that Regiment object. You shou‍ld implement that by overriding the toString() method. The String#format() method may help you achieve the nice columns.
 
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
Campbell Ritchie wrote:Another alternative which I would suggest is that printing a Regiment is the responsibility of that Regiment object. You shou‍ld implement that by overriding the toString() method. The String#format() method may help you achieve the nice columns.

At first blush, this seems reasonable. However, some problems quickly become apparent when you try to implement it.

One problem is that the formatting logic for the required report gets spread out between two classes and maintenance becomes a little more difficult. Formatting a report header is obviously not the responsibility of a Regiment object so that responsibility has to be assigned elsewhere. Yet, the formatting of the header is closely tied to the formatting of a line item in the report. Parameterizing the format string might appear to help but then there's an implicit "knowledge" of the contents of the format string that needs to be coded into the call to System.out.printf(). Maybe if the format string had some kind of token replacement power, that is, if you could specify something like "${it.number} \t${it.name} \t${it.strength} \n" it wouldn't be so bad but there's nothing in Java like that as far as I know..

The fact is, Regiment is a domain object and it is very seldom, if ever, appropriate for a domain object to be responsible for formatting itself for purposes of display, whether that be on screen or in a report or some other medium. Since this is a small program and the Army class has most of the information needed to generate the report, it is actually better to have the Army class be responsible for all the report formatting, as the original specifications dictate.

Edit: Of course, you could take all of it a step farther and create a RegimentReport class that's responsible for creating the report but for a program this small, having the Army class do the reporting is good enough.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!