Docsity

Array Lists - Homework 8 | Programming I | COP 2210, Study notes of Computer Science

Toggle sidebar
bg1
Computer Programming I Instructor: Greg Shaw
COP 2210
Programming Assignment #8
(ArrayLists)
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Array Lists - Homework 8 | Programming I | COP 2210 and more Study notes Computer Science in PDF only on Docsity!

Computer Programming I Instructor: Greg Shaw COP 2210

Programming Assignment

(ArrayLists)

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.
  1. Output will be a series of reports --- one report for each of the 20 weeks.
  2. Each weekly report should begin with an appropriate heading, including the week number.

19 Swordfish 20 Tapirs Regiment.java /* File: Regiment.java

  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person. / /*
  • A class that corresponds to a regiment - in terms of the military. / public class Regiment { private int number ; // number of the regiment private String name ; // name of the regiment private int strength ; // number of soldiers per regiment public static final int R5 = 30 ; // 5th regiment added 30 men weekly public static final int R0 = 100 ; // All other regiments add 100 men /*
  • Construction of the regiment with a number, name, and strength.
  • @param regimentNumber the number of the regiment
  • @param regimentName the name of the regiment
  • @param regimentPower the number of soldiers per regiment */ public Regiment(int regimentNumber, String regimentName, int regimentPower) { number = regimentNumber ; // number of the regiment name = regimentName ; // name of the regiment strength = regimentPower ;
  • Access the number of the regiment.
  • @return the number of the regiment / public int getNumber() { return number ; } /*
  • Access the name of the regiment.
  • @return the name of the regiment / public String getName() { return name ; } /*
  • Access the number of soldiers per regiment.
  • @return the number of soldiers per regiment / public int getStrength() { return strength ; } /*
  • Add the number of soldiers to a regiment.
  • @param morePower Add more soldiers to the regiment */ public void plusStrength(int morePower) { strength += morePower ; } } Army.java

// If it's the fifth regiment, add 30 - not of 100 if ( regiment.getName().equals("Elephants") ) { // Add 30 soldiers, not 100 soldiers regiment.plusStrength(Regiment.R5) ; } else // Otherwise { // Add 100 soldiers to the regiment regiment.plusStrength(Regiment.R0) ; } } } /**

  • Reports the names, regiment numbers, and number of soldiers
  • @pram w the week number for the report
  • @return report the String containing all the data */ public String report(int w) { String report = "" ; // Report nothing special for now sir // Begin the weekly report report += " General Name’s Army: Week " + (w + 0)
  • " \n\n"
  • " Number Regiment Name Strength
    n"
  • " ====== ================ ======
    n" ; for (int i = 0 ; i < regiments.size() ; i++) { Regiment regiment = regiments.get(i) ; // Get next Regiment object int number = regiment.getNumber() ; // Get regiment's number

String name = regiment.getName() ; // Get regiment's name int strength = regiment.getStrength() ; // Get regiment's strength // Heap up all the data to initialized string, report report += Align.center(number, 14) + Align.center(name, 16)

  • Align.center(strength, 14) + "\n" ; } report += "\n" ; return report ; } /**
  • Find the regiment with the most strength in the Army.
  • @return fronts String at the bottom of the report */ public String maxStrength() { int Max = 0 ; // Initializing empty value for later String fronts = "" ; // Again, empty and useless for now; proceed // Extract the first item on the list Regiment first = regiments.get(0) ; // Now extract this regiment's strength int initialMax = first.getStrength() ; if (regiments.size() == 1) // If there's only one regiment { // Command it to go to the front line already fronts = "Regiment " + first.getName() + " will be on the front"
  • " line this week with " + initialMax + "

list (Only Once!) regiments.remove(first) ; } // If Max belongs to the very first strength if (Max == initialMax) { // Then show the following message about the first regiment fronts = "Regiment " + first.getName() + " will be on"

  • " the front line this week with "
  • first.getStrength() + " soldiers!"
  • "\n\nFAREWELL SOLDIERS!\t\t\t\t\t"
  • "END OF WEEKLY REPORT\n\n"; return fronts ; } // Otherwise, if the Max belongs to the current strength else if (Max == nextStrength) { // Then show the following message about the following regiments fronts = "Regiment " + regiment.getName() + " will be on"
  • " the front line this week with "
  • regiment.getStrength() + " soldiers!"
  • "\n\nFAREWELL SOLDIERS!\t\t\t\t\t"
  • "END OF WEEKLY REPORT\n\n"; return fronts ; } } } return fronts ; } }

ArmyOut.java /* File: ArmyOut.java

  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person. / import java.io.File ; import java.io.IOException ; import java.util.Scanner ; /*
  • A class to read a list of regiments and calculate statistics of the army. */ public class ArmyOut { public static void main(String[] args) throws IOException { Army list = new Army() ; // Create a list // Input the file for the assignment and begin to scan through the data Scanner armory = new Scanner( new File("regiments.txt") ) ; System.out.println("Name (\u00A7U0#)") ; String line ; // The 20 regiments int number = 0 ; // number is saving room String name = "" ; // name is saving room // For each regiment that is in the army, add each one to a list for (int a = 0 ; a < 20 ; a++) { // Read each regiment's line line = armory.nextLine() ;

String frontLine = list.maxStrength() ; System.out.println(frontLine) ; } } }