public class Gunfight { public static void main(String[] args) { int i, num, max, Max, nBottlesConsumed = 1, maxBottlesAllowed = 3; // check # of arguments from the command line if(args.length == 0) { System.out.println("Please enter at least one parameter: 'java Gunfight [nBottlesConsumedDrunk] [maxBottlesAllowedAllowed]' "); System.exit(-1); } // convert the 1_st paramater which is a string to integer if ((num = Integer.parseInt(args[0])) > 0) nBottlesConsumed = num; // convert the 2_nd paramater if it exists if ((args.length > 1) && (max = Integer.parseInt(args[1])) > 0) maxBottlesAllowed = max; /* * create(construct or instantiate) an object (instance) called * sweatyPost with an argument passed to the constructor of * a class named WesternTown */ WesternTown sweatyPost = new WesternTown("Northwestern America"); // WesternTown sweatyPost = new WesternTown(); sweatyPost.saloons = 2; sweatyPost.sheriffs = 1; sweatyPost.troublemakers = 5; /* * create(construct or instantiate) an object (instance) called * terry from the constructor of a class named Humans */ Humans terry = new Humans(); terry.whiskeyPreference = "Straight"; terry.wasNamed("Terry"); /* * maurice is a Villain which is extended (subcalss, inherit) * from Humans class */ Villain maurice = new Villain(); maurice.hatColor = "black"; maurice.mustacheColor = "red"; maurice.horseName = "Beer Gut"; maurice.whiskeyPreference = "Jack Daniels"; maurice.wasNamed("Maurice"); for (i = 0; i < nBottlesConsumed; i++) maurice.drinkWhiskey(); num = maurice.howDrunkAmI(); // if (num = maurice.howDrunkAmI()) /* * Maurice will behave badly only if * the villain drink more than maxBottlesAllowed */ if (num > maxBottlesAllowed) { /* * Assignment: write a subclass named Sheriff which * inherits superclass named Humans. * Now assuming Mary decides to attend police academy * to become a Sheriff. How would you instantiate her? */ Humans mary = new Humans(); mary.horseName = "Midnight"; mary.whiskeyPreference = "Straight"; mary.wasNamed("Mary"); System.out.println(maurice.whatIsYourName() + " drank " + maurice.howDrunkAmI() + " bottles of " + maurice.whiskeyPreference + "in an old town of " + sweatyPost.location); maurice.tieUpDamsel(terry); System.out.println(mary.whatIsYourName() + " rode a horse named " + mary.horseName + " came to rescue."); } else { if (num > 1) { // drank more than 1 bottle System.out.println(maurice.whatIsYourName() + " was riding a horse named \"" + maurice.horseName + "\" and jumping up and down."); /* * if this mean guy drank more than maxBottlesAllowedAllowed * we set Max = 0 to force this guy to drink plenty * of water - see "do while" loop */ if (nBottlesConsumed > maxBottlesAllowed) Max = 0; else Max = 1; do { maurice.drinkWater(); num = maurice.howDrunkAmI(); } while (num > Max); System.out.println("Is " + maurice.whatIsYourName() + " drunk?"); if (maurice.areYou("Jack")) System.out.println("Yes. num: " + num); else { if (maurice.areYou("Maurice")) System.out.println("Not sure. num: " + num); else System.out.println("Yes. num: " + num); } } else System.out.println(maurice.whatIsYourName() + " was board."); } System.out.println("\n\t\tThe End."); } }