From a84c20912851e5b009a8b2c23644d0206ad801cf Mon Sep 17 00:00:00 2001 From: Jiayong Date: Fri, 8 Jul 2022 22:54:56 -0400 Subject: [PATCH 1/9] initial setup --- .idea/.gitignore | 3 +++ .idea/compiler.xml | 13 ++++++++++++ .idea/jarRepositories.xml | 20 +++++++++++++++++++ .idea/misc.xml | 12 +++++++++++ .idea/vcs.xml | 6 ++++++ docs/hamurabi.java | 4 +++- pom.xml | 16 +++++++++++++++ .../org/hamurabiGLJ/hamurabi/Hammurabi.java | 6 ++++++ 8 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fbd5ba5 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ea0b3a9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/docs/hamurabi.java b/docs/hamurabi.java index 9947af3..7dadc73 100644 --- a/docs/hamurabi.java +++ b/docs/hamurabi.java @@ -1,3 +1,4 @@ +/* import java.io.IOException; import java.util.Scanner; @@ -153,4 +154,5 @@ public static void main(String[] args) throws IOException { a.newYear(); a.finished(); } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5791aac --- /dev/null +++ b/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + org.hamurabiGLJ + hamurabi + 1.0-SNAPSHOT + + + 18 + 18 + + + \ No newline at end of file diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java new file mode 100644 index 0000000..a7c406b --- /dev/null +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -0,0 +1,6 @@ +package org.hamurabiGLJ.hamurabi; + +public class Hammurabi { + + +} From 34f3bddc2b3494cec4a6f6726c762a560d407bbd Mon Sep 17 00:00:00 2001 From: Jiayong Date: Fri, 8 Jul 2022 22:58:51 -0400 Subject: [PATCH 2/9] move test file --- HammurabiTest.java | 125 ------------------ pom.xml | 8 ++ .../HammurabiTest.java | 125 ++++++++++++++++++ 3 files changed, 133 insertions(+), 125 deletions(-) delete mode 100644 HammurabiTest.java create mode 100644 src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java diff --git a/HammurabiTest.java b/HammurabiTest.java deleted file mode 100644 index 5cf1cc9..0000000 --- a/HammurabiTest.java +++ /dev/null @@ -1,125 +0,0 @@ -package hammurabi; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class HammurabiTest { - - Hammurabi ham; - - boolean about(double expected, double actual) { - return actual > 0.90 * expected && actual < 1.10 * expected; - } - - @Before - public void setUp() throws Exception { - ham = new Hammurabi(); - } - - @Test - public final void testPlagueDeaths1() { - int number_of_plagues = 0; - for (int i = 0; i < 10000; i++) { - int deaths = ham.plagueDeaths(100); - if (deaths > 0) { - number_of_plagues += 1; - } - } - int percentPlagues = number_of_plagues / 100; - assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", - about(1500, number_of_plagues)); - } - - @Test - public final void testPlagueDeaths2() { - int deaths = 0; - for (int i = 0; i < 10000; i++) { - deaths = ham.plagueDeaths(100); - if (deaths > 0) break; - } - assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", - 50, deaths); - } - - @Test - public final void testStarvationDeaths() { - int deaths = ham.starvationDeaths(100, 1639); - assertEquals("Wrong number of starvations deaths.", 19, deaths); - deaths = ham.starvationDeaths(100, 2500); - if (deaths < 0) { - fail("You starved a negative number of people!"); - } - } - - @Test - public final void testUprising() { - assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); - assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); - } - - @Test - public final void testImmigrants() { - int imm = ham.immigrants(10, 1200, 500); - assertEquals("Wrong number of immigrants.", 25, imm); - } - - @Test - public final void testHarvest() { - int[] yield = new int[7]; - for (int i = 0; i < 1000; i++) { - int harvest = ham.harvest(1); - assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); - yield[harvest] += 1; - } - for (int j = 1; j <= 6; j++) { - assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); - } - } - - @Test - public final void testGrainEatenByRats1() { - int infestations = 0; - for (int i = 0; i < 1000; i++) { - int eaten = ham.grainEatenByRats(100); - if (eaten > 0) { - infestations += 1; - } - } - int percentInfestations = infestations / 100; - assertTrue("Number of rat infestations is about " + percentInfestations + - ", not about 40%.", about(400, infestations)); - } - - @Test - public final void testGrainEatenByRats2() { - int percent = 0; - int[] counts = new int[31]; - for (int i = 0; i < 10000; i++) { - percent = ham.grainEatenByRats(100); - if (percent == 0) continue; - counts[percent] += 1; - assertTrue("Rats ate " + percent + "% of your grain, not 10% to 30%.", - percent >= 10 && percent <= 30); - } - for (int j = 11; j < 30; j++) { - assertTrue("Rats never ate " + j + "% of your grain.", counts[j] > 0); - } - } - - @Test - public final void testNewCostOfLand() { - int[] cost = new int[24]; - for (int i = 0; i < 1000; i++) { - int price = ham.newCostOfLand(); - assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); - cost[price] += 1; - } - for (int j = 17; j <= 23; j++) { - assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); - } - } - -} - diff --git a/pom.xml b/pom.xml index 5791aac..08c1865 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ org.hamurabiGLJ hamurabi 1.0-SNAPSHOT + + + junit + junit + 4.13.1 + test + + 18 diff --git a/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java new file mode 100644 index 0000000..5b2d92f --- /dev/null +++ b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java @@ -0,0 +1,125 @@ +package org.hamurabiGLJ.hamurabi; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class HammurabiTest { + + Hammurabi ham; + + boolean about(double expected, double actual) { + return actual > 0.90 * expected && actual < 1.10 * expected; + } + + @Before + public void setUp() throws Exception { + ham = new Hammurabi(); + } + +// @Test +// public final void testPlagueDeaths1() { +// int number_of_plagues = 0; +// for (int i = 0; i < 10000; i++) { +// int deaths = ham.plagueDeaths(100); +// if (deaths > 0) { +// number_of_plagues += 1; +// } +// } +// int percentPlagues = number_of_plagues / 100; +// assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", +// about(1500, number_of_plagues)); +// } +// +// @Test +// public final void testPlagueDeaths2() { +// int deaths = 0; +// for (int i = 0; i < 10000; i++) { +// deaths = ham.plagueDeaths(100); +// if (deaths > 0) break; +// } +// assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", +// 50, deaths); +// } +// +// @Test +// public final void testStarvationDeaths() { +// int deaths = ham.starvationDeaths(100, 1639); +// assertEquals("Wrong number of starvations deaths.", 19, deaths); +// deaths = ham.starvationDeaths(100, 2500); +// if (deaths < 0) { +// fail("You starved a negative number of people!"); +// } +// } +// +// @Test +// public final void testUprising() { +// assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); +// assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); +// } +// +// @Test +// public final void testImmigrants() { +// int imm = ham.immigrants(10, 1200, 500); +// assertEquals("Wrong number of immigrants.", 25, imm); +// } +// +// @Test +// public final void testHarvest() { +// int[] yield = new int[7]; +// for (int i = 0; i < 1000; i++) { +// int harvest = ham.harvest(1); +// assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); +// yield[harvest] += 1; +// } +// for (int j = 1; j <= 6; j++) { +// assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); +// } +// } +// +// @Test +// public final void testGrainEatenByRats1() { +// int infestations = 0; +// for (int i = 0; i < 1000; i++) { +// int eaten = ham.grainEatenByRats(100); +// if (eaten > 0) { +// infestations += 1; +// } +// } +// int percentInfestations = infestations / 100; +// assertTrue("Number of rat infestations is about " + percentInfestations + +// ", not about 40%.", about(400, infestations)); +// } +// +// @Test +// public final void testGrainEatenByRats2() { +// int percent = 0; +// int[] counts = new int[31]; +// for (int i = 0; i < 10000; i++) { +// percent = ham.grainEatenByRats(100); +// if (percent == 0) continue; +// counts[percent] += 1; +// assertTrue("Rats ate " + percent + "% of your grain, not 10% to 30%.", +// percent >= 10 && percent <= 30); +// } +// for (int j = 11; j < 30; j++) { +// assertTrue("Rats never ate " + j + "% of your grain.", counts[j] > 0); +// } +// } +// +// @Test +// public final void testNewCostOfLand() { +// int[] cost = new int[24]; +// for (int i = 0; i < 1000; i++) { +// int price = ham.newCostOfLand(); +// assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); +// cost[price] += 1; +// } +// for (int j = 17; j <= 23; j++) { +// assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); +// } +// } + +} + From 97c54a95dac2ee7313af7b5500ff4d205458dc01 Mon Sep 17 00:00:00 2001 From: Jiayong Date: Fri, 8 Jul 2022 23:05:52 -0400 Subject: [PATCH 3/9] add skeleton --- .../java/org/hamurabiGLJ/hamurabi/Hammurabi.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java index a7c406b..0d9a5f0 100644 --- a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -1,6 +1,19 @@ package org.hamurabiGLJ.hamurabi; +import java.util.*; + public class Hammurabi { + Random rand = new Random(); // this is an instance variable + Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) { // required in every Java program + new Hammurabi().playGame(); + } + void playGame() { + // declare local variables here: grain, population, etc. + // statements go after the declations + } + //other methods go here } From befb0bdf07de70b2d8c5336266000080ad45d555 Mon Sep 17 00:00:00 2001 From: Jiayong Date: Fri, 8 Jul 2022 23:18:01 -0400 Subject: [PATCH 4/9] add skeleton methods --- .../org/hamurabiGLJ/hamurabi/Hammurabi.java | 31 +++ .../HammurabiTest.java | 204 +++++++++--------- 2 files changed, 133 insertions(+), 102 deletions(-) diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java index 0d9a5f0..678cbd9 100644 --- a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -15,5 +15,36 @@ void playGame() { // statements go after the declations } + int getNumber(String message) { + while (true) { + System.out.print(message); + try { + return scanner.nextInt(); + } + catch (InputMismatchException e) { + System.out.println("\"" + scanner.next() + "\" isn't a number!"); + } + } + } + int askHowManyAcresToBuy(int price, int bushels) {return 0;} //other methods go here + int askHowManyAcresToSell(int acresOwned) {return 0;} + + int askHowMuchGrainToFeedPeople(int bushels) {return 0;} + + int askHowManyAcresToPlant(int acresOwned, int population, int bushels) {return 0;}; + + int plagueDeaths(int population) {return 0;} + + int starvationDeaths(int population, int bushelsFedToPeople) {return 0;} + + boolean uprising(int population, int howManyPeopleStarved) {return false;} + + int immigrants(int population, int acresOwned, int grainInStorage) {return 0;} + + int harvest(int acres, int bushelsUsedAsSeed) {return 0;} + + int grainEatenByRats(int bushels) {return 0;} + + int newCostOfLand() {return 0;} } diff --git a/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java index 5b2d92f..075fe21 100644 --- a/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java +++ b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java @@ -18,108 +18,108 @@ public void setUp() throws Exception { ham = new Hammurabi(); } -// @Test -// public final void testPlagueDeaths1() { -// int number_of_plagues = 0; -// for (int i = 0; i < 10000; i++) { -// int deaths = ham.plagueDeaths(100); -// if (deaths > 0) { -// number_of_plagues += 1; -// } -// } -// int percentPlagues = number_of_plagues / 100; -// assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", -// about(1500, number_of_plagues)); -// } -// -// @Test -// public final void testPlagueDeaths2() { -// int deaths = 0; -// for (int i = 0; i < 10000; i++) { -// deaths = ham.plagueDeaths(100); -// if (deaths > 0) break; -// } -// assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", -// 50, deaths); -// } -// -// @Test -// public final void testStarvationDeaths() { -// int deaths = ham.starvationDeaths(100, 1639); -// assertEquals("Wrong number of starvations deaths.", 19, deaths); -// deaths = ham.starvationDeaths(100, 2500); -// if (deaths < 0) { -// fail("You starved a negative number of people!"); -// } -// } -// -// @Test -// public final void testUprising() { -// assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); -// assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); -// } -// -// @Test -// public final void testImmigrants() { -// int imm = ham.immigrants(10, 1200, 500); -// assertEquals("Wrong number of immigrants.", 25, imm); -// } -// -// @Test -// public final void testHarvest() { -// int[] yield = new int[7]; -// for (int i = 0; i < 1000; i++) { -// int harvest = ham.harvest(1); -// assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); -// yield[harvest] += 1; -// } -// for (int j = 1; j <= 6; j++) { -// assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); -// } -// } -// -// @Test -// public final void testGrainEatenByRats1() { -// int infestations = 0; -// for (int i = 0; i < 1000; i++) { -// int eaten = ham.grainEatenByRats(100); -// if (eaten > 0) { -// infestations += 1; -// } -// } -// int percentInfestations = infestations / 100; -// assertTrue("Number of rat infestations is about " + percentInfestations + -// ", not about 40%.", about(400, infestations)); -// } -// -// @Test -// public final void testGrainEatenByRats2() { -// int percent = 0; -// int[] counts = new int[31]; -// for (int i = 0; i < 10000; i++) { -// percent = ham.grainEatenByRats(100); -// if (percent == 0) continue; -// counts[percent] += 1; -// assertTrue("Rats ate " + percent + "% of your grain, not 10% to 30%.", -// percent >= 10 && percent <= 30); -// } -// for (int j = 11; j < 30; j++) { -// assertTrue("Rats never ate " + j + "% of your grain.", counts[j] > 0); -// } -// } -// -// @Test -// public final void testNewCostOfLand() { -// int[] cost = new int[24]; -// for (int i = 0; i < 1000; i++) { -// int price = ham.newCostOfLand(); -// assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); -// cost[price] += 1; -// } -// for (int j = 17; j <= 23; j++) { -// assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); -// } -// } + @Test + public final void testPlagueDeaths1() { + int number_of_plagues = 0; + for (int i = 0; i < 10000; i++) { + int deaths = ham.plagueDeaths(100); + if (deaths > 0) { + number_of_plagues += 1; + } + } + int percentPlagues = number_of_plagues / 100; + assertTrue("Number of plagues is about " + percentPlagues + ", not about 15%.", + about(1500, number_of_plagues)); + } + + @Test + public final void testPlagueDeaths2() { + int deaths = 0; + for (int i = 0; i < 10000; i++) { + deaths = ham.plagueDeaths(100); + if (deaths > 0) break; + } + assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", + 50, deaths); + } + + @Test + public final void testStarvationDeaths() { + int deaths = ham.starvationDeaths(100, 1639); + assertEquals("Wrong number of starvations deaths.", 19, deaths); + deaths = ham.starvationDeaths(100, 2500); + if (deaths < 0) { + fail("You starved a negative number of people!"); + } + } + + @Test + public final void testUprising() { + assertTrue("Should have had an uprising!", ham.uprising(1000, 451)); + assertFalse("Should not have had an uprising!", ham.uprising(1000, 449)); + } + + @Test + public final void testImmigrants() { + int imm = ham.immigrants(10, 1200, 500); + assertEquals("Wrong number of immigrants.", 25, imm); + } + + @Test + public final void testHarvest() { + int[] yield = new int[7]; + for (int i = 0; i < 1000; i++) { + int harvest = ham.harvest(1, 1); + assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); + yield[harvest] += 1; + } + for (int j = 1; j <= 6; j++) { + assertTrue("You never have a yield of " + j + " bushels per acre.", yield[j] > 0); + } + } + + @Test + public final void testGrainEatenByRats1() { + int infestations = 0; + for (int i = 0; i < 1000; i++) { + int eaten = ham.grainEatenByRats(100); + if (eaten > 0) { + infestations += 1; + } + } + int percentInfestations = infestations / 100; + assertTrue("Number of rat infestations is about " + percentInfestations + + ", not about 40%.", about(400, infestations)); + } + + @Test + public final void testGrainEatenByRats2() { + int percent = 0; + int[] counts = new int[31]; + for (int i = 0; i < 10000; i++) { + percent = ham.grainEatenByRats(100); + if (percent == 0) continue; + counts[percent] += 1; + assertTrue("Rats ate " + percent + "% of your grain, not 10% to 30%.", + percent >= 10 && percent <= 30); + } + for (int j = 11; j < 30; j++) { + assertTrue("Rats never ate " + j + "% of your grain.", counts[j] > 0); + } + } + + @Test + public final void testNewCostOfLand() { + int[] cost = new int[24]; + for (int i = 0; i < 1000; i++) { + int price = ham.newCostOfLand(); + assertTrue("Illegal cost of land: " + price, price >= 17 && price <= 23); + cost[price] += 1; + } + for (int j = 17; j <= 23; j++) { + assertTrue("You never have a land cost of " + j + " bushels per acre.", cost[j] > 0); + } + } } From 38dfa73ad7f9b5cc3c1d08f1bf2940dd5262eedc Mon Sep 17 00:00:00 2001 From: Jiayong Date: Fri, 8 Jul 2022 23:33:22 -0400 Subject: [PATCH 5/9] add playGame local variables --- src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java index 678cbd9..da660f5 100644 --- a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -6,13 +6,19 @@ public class Hammurabi { Random rand = new Random(); // this is an instance variable Scanner scanner = new Scanner(System.in); + public static void main(String[] args) { // required in every Java program new Hammurabi().playGame(); } void playGame() { // declare local variables here: grain, population, etc. - // statements go after the declations + Integer grain = 2800; + Integer population = 100; + Integer land = 1000; + Integer landValue = 19; + + // statements go after the declarations } int getNumber(String message) { From 5a791ac988c79cf417d9268a4c1ec18e716cd3aa Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Sat, 9 Jul 2022 10:35:52 -0400 Subject: [PATCH 6/9] test --- .../org/hamurabiGLJ/hamurabi/Hammurabi.java | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java index da660f5..4d05f17 100644 --- a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -32,25 +32,48 @@ int getNumber(String message) { } } } - int askHowManyAcresToBuy(int price, int bushels) {return 0;} + int askHowManyAcresToBuy(int price, int bushels) { + return 0; + } //other methods go here - int askHowManyAcresToSell(int acresOwned) {return 0;} + int askHowManyAcresToSell(int acresOwned) { + int AcresToSell = getNumber("How many acres of land would you like to sell? "); + return 0; + } - int askHowMuchGrainToFeedPeople(int bushels) {return 0;} + int askHowMuchGrainToFeedPeople(int bushels) { + return 0; + } - int askHowManyAcresToPlant(int acresOwned, int population, int bushels) {return 0;}; + int askHowManyAcresToPlant(int acresOwned, int population, int bushels) { + return 0; + }; - int plagueDeaths(int population) {return 0;} + int plagueDeaths(int population) { + return 0; + } - int starvationDeaths(int population, int bushelsFedToPeople) {return 0;} + int starvationDeaths(int population, int bushelsFedToPeople) { + return 0; + } - boolean uprising(int population, int howManyPeopleStarved) {return false;} + boolean uprising(int population, int howManyPeopleStarved) { + return false; + } - int immigrants(int population, int acresOwned, int grainInStorage) {return 0;} + int immigrants(int population, int acresOwned, int grainInStorage) { + return 0; + } - int harvest(int acres, int bushelsUsedAsSeed) {return 0;} + int harvest(int acres, int bushelsUsedAsSeed) { + return 0; + } - int grainEatenByRats(int bushels) {return 0;} + int grainEatenByRats(int bushels) { + return 0; + } - int newCostOfLand() {return 0;} + int newCostOfLand() { + return 0; + } } From 1768ead95141dd2c8967d92c533d810293d45a01 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Sat, 9 Jul 2022 12:25:55 -0400 Subject: [PATCH 7/9] how many acres to sell --- .../org/hamurabiGLJ/hamurabi/Hammurabi.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java index 4d05f17..85bce78 100644 --- a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -5,6 +5,7 @@ public class Hammurabi { Random rand = new Random(); // this is an instance variable Scanner scanner = new Scanner(System.in); + private int land; public static void main(String[] args) { // required in every Java program @@ -19,6 +20,7 @@ void playGame() { Integer landValue = 19; // statements go after the declarations + System.out.println(askHowManyAcresToSell(123)); } int getNumber(String message) { @@ -26,19 +28,24 @@ int getNumber(String message) { System.out.print(message); try { return scanner.nextInt(); - } - catch (InputMismatchException e) { + } catch (InputMismatchException e) { System.out.println("\"" + scanner.next() + "\" isn't a number!"); } } } - int askHowManyAcresToBuy(int price, int bushels) { + + int askHowManyAcresToBuy(int price) { return 0; } + //other methods go here int askHowManyAcresToSell(int acresOwned) { - int AcresToSell = getNumber("How many acres of land would you like to sell? "); - return 0; + int acresToSell = getNumber("How many acres of land would you like to sell?\n"); + while (acresToSell > acresOwned) { + System.out.println("O Hammurabi, but you only have " + land + " acres of land!\n"); + acresToSell = getNumber("How many acres of land would you like to sell?\n"); + } + return acresToSell; } int askHowMuchGrainToFeedPeople(int bushels) { @@ -46,8 +53,9 @@ int askHowMuchGrainToFeedPeople(int bushels) { } int askHowManyAcresToPlant(int acresOwned, int population, int bushels) { + int acresToPlant = getNumber("How many acres"); return 0; - }; + } int plagueDeaths(int population) { return 0; From 0d62b55735b07052f56ebfd9c3549991f251edf8 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Sat, 9 Jul 2022 12:42:21 -0400 Subject: [PATCH 8/9] added changes from other branches --- .../org/hamurabiGLJ/hamurabi/Hammurabi.java | 57 ++++++++++++++++--- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java index 85bce78..9f5546e 100644 --- a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -33,12 +33,20 @@ int getNumber(String message) { } } } - - int askHowManyAcresToBuy(int price) { - return 0; + // Jiayong + int askHowManyAcresToBuy(int price, int bushels) { + int maxAcres = bushels / price; + String msg = "How many acres of land would you like to buy?"; + int input = getNumber(msg); + while (input > maxAcres) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!", + input * price, bushels); + System.out.println(newMsg); + input = getNumber(msg); + } + return input; } - - //other methods go here + // Linda int askHowManyAcresToSell(int acresOwned) { int acresToSell = getNumber("How many acres of land would you like to sell?\n"); while (acresToSell > acresOwned) { @@ -47,17 +55,48 @@ int askHowManyAcresToSell(int acresOwned) { } return acresToSell; } - + // Gychu int askHowMuchGrainToFeedPeople(int bushels) { - return 0; + String msg = "How many bushels of grain do you want to feed your people?\n"; + int input = getNumber(msg); + while (input > bushels) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!", + input, bushels); + System.out.println(newMsg); + input = getNumber(msg); + } + return input; } int askHowManyAcresToPlant(int acresOwned, int population, int bushels) { - int acresToPlant = getNumber("How many acres"); - return 0; + String msg = "How many acres do you wish to plant?\n"; + int input = getNumber(msg); + + while (true) { + if (input > acresOwned) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d acres but we only have %!", input, acresOwned); + System.out.println(newMsg); + input = getNumber(msg); + continue; + } + if (input / 10 > population) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d people but we only have %d!", input/10, population); + System.out.println(newMsg); + input = getNumber(msg); + continue; + } + if (input > bushels / 2) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!", input*2, bushels); + System.out.println(newMsg); + input = getNumber(msg); + continue; + } + return input; + } } int plagueDeaths(int population) { + return 0; } From 2f637ccbe3655bbf4cc0e4129bb1c97d21ae2986 Mon Sep 17 00:00:00 2001 From: "Meilin Li (Linda)" Date: Sat, 9 Jul 2022 13:42:45 -0400 Subject: [PATCH 9/9] methods done --- .../java/org/hamurabiGLJ/hamurabi/Hammurabi.java | 16 +++++++++------- .../org.hamurabiGLJ.hamurabi/HammurabiTest.java | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java index 9f5546e..ae6ca9d 100644 --- a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -20,7 +20,6 @@ void playGame() { Integer landValue = 19; // statements go after the declarations - System.out.println(askHowManyAcresToSell(123)); } int getNumber(String message) { @@ -96,7 +95,9 @@ int askHowManyAcresToPlant(int acresOwned, int population, int bushels) { } int plagueDeaths(int population) { - + if (rand.nextInt(101) <= 15) { + return population / 2; + } return 0; } @@ -105,15 +106,16 @@ int starvationDeaths(int population, int bushelsFedToPeople) { } boolean uprising(int population, int howManyPeopleStarved) { - return false; + return (((double)howManyPeopleStarved / population) >= 0.45); } int immigrants(int population, int acresOwned, int grainInStorage) { - return 0; + return (20 * acresOwned + grainInStorage) / (100 * population) + 1; } - int harvest(int acres, int bushelsUsedAsSeed) { - return 0; + int harvest(int acres) { + int yield = rand.nextInt(1,7); + return acres * yield; } int grainEatenByRats(int bushels) { @@ -121,6 +123,6 @@ int grainEatenByRats(int bushels) { } int newCostOfLand() { - return 0; + return rand.nextInt(17,24); } } diff --git a/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java index 075fe21..278b653 100644 --- a/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java +++ b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java @@ -69,7 +69,7 @@ public final void testImmigrants() { public final void testHarvest() { int[] yield = new int[7]; for (int i = 0; i < 1000; i++) { - int harvest = ham.harvest(1, 1); + int harvest = ham.harvest(1); assertTrue("Illegal harvest per acre: " + harvest, harvest > 0 && harvest <= 6); yield[harvest] += 1; }