Docsity

Java Program for Russo-Japanese War: Army Organization and Regiments - Prof. Gregory Shaw, Study notes of Computer Science

Toggle sidebar
bg1
Russo-Japanese War Assignment (8) (Copy and Paste when necessary)
regiments.txt
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
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
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Java Program for Russo-Japanese War: Army Organization and Regiments - Prof. Gregory Shaw and more Study notes Computer Science in PDF only on Docsity!

Russo-Japanese War Assignment (8) (Copy and Paste when necessary) regiments.txt 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 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 strength to the Army. / public void addStrength() { // Traverse the list of regiments to update the list accordingly // (i + 1) is the regiment number for (int i = 0 ; i < regiments.size() ; i++) { Regiment regiment = regiments.get(i) ; // 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

// 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. */

package assignments ; 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() ; // Create a Scanner object associated with String "line" Scanner regiment = new Scanner(line) ; // Extract tokens from each current regiment's line number = regiment.nextInt() ; // Get regiment numbers name = regiment.next() ; // Get regiment names // Add the strengths of each regiment before organizing a regiment int strength = 1050 - (a + 1) * 50 ; // 1st Strengths Equation