Tuesday, March 25, 2008

Programming Fundamental Assigment 1 Code

import java.util.Scanner;
import java.util.Random;
import java.util.*;

public class UnifiedDomination
{
private Random rng = new Random(); // Our random number generator
private Scanner input = new Scanner(System.in); // Our keyboard reader

private int australianTerritory1Armies = 4;
private int australianTerritory2Armies = 2;
private int australianTerritory3Armies = 2;
private int australianTerritory4Armies = 1;
private int africanTerritory1Armies = 3;
private int africanTerritory2Armies = 2;
private int africanTerritory3Armies = 3;
private int africanTerritory4Armies = 2;

private String australianPlayerName; // Name of Australian player
private String africanPlayerName; // Name of African player


private String name;
public UnifiedDomination()
{
// Your code goes here.
// Use separate methods and follow design principles to achieve
// a high design mark.

getName();
currentArmies();
battle();
battle();
currentArmies();
}
private void getName()
{
System.out.print("Enter name of player for Australia: ");
australianPlayerName = input.nextLine();
System.out.print("Enter name of player for African: ");
africanPlayerName = input.nextLine();
}
private void currentArmies()

{ System.out.println();
System.out.println("Australia has "+ australianArmies() + " armies.");
System.out.println("African has " + africanArmies() + " armies.");
System.out.println("In total, there are " + totalArmies() + " armies.");
System.out.println();
}
private int australianArmies()
{
return australianTerritory1Armies + australianTerritory2Armies
+ australianTerritory3Armies +australianTerritory4Armies;
}
private int africanArmies()
{
return africanTerritory1Armies + africanTerritory2Armies
+ africanTerritory3Armies + africanTerritory4Armies ;
}
private int totalArmies()
{
return australianArmies() + africanArmies() ;
}
private void battle()
{
int myRandomNumber1 = rng.nextInt(2);
australianTerritory1Armies -= myRandomNumber1;
int myRandomNumber2 = rng.nextInt(2);
africanTerritory3Armies -= myRandomNumber2;
System.out.println( australianPlayerName + " 's Australia Territory 1 fights "
+ africanPlayerName + " 's African Territory 3");


}
// Don't touch this line, leave it here ;-)
public static void main(String[] args) { new UnifiedDomination(); }
}

No comments: