From 992c14be3b14d7b4b50d7e91c3a3d82f7770bef7 Mon Sep 17 00:00:00 2001 From: Rob Hallam <0504004h@student.gla.ac.uk> Date: Wed, 20 Sep 2023 09:40:58 +0100 Subject: [PATCH] (feat) Lab 7 - string formatting implemented Plus placeholder for lab 6 exercise 9 (rock-paper-scissors) --- week1.java | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/week1.java b/week1.java index d99d9eb..90e59d4 100644 --- a/week1.java +++ b/week1.java @@ -7,6 +7,7 @@ class runLabs { new Lab4(); new Lab5(); new Lab6(); + new Lab7(); } } @@ -256,6 +257,7 @@ class Lab6 extends Lab { new six6(); new six7(); new six8(); + new six9(); } /** @@ -384,6 +386,12 @@ class Lab6 extends Lab { } + class six9 extends Exercise { + public six9() { + super(9); + System.out.println(String.format("TODO: ro sham bo once we have stdin working")); + } + } /* TOASK - is there a better way of returning multiple values in Java? à la python, ie: return (quotient, remainder) */ @@ -521,3 +529,45 @@ class Lab6 extends Lab { } } } + +/** + * String formatting + * + */ +class Lab7 extends Lab { + public Lab7() { + super(7); + writeTimesTable(8.24579, 10); + writeTimesTable(93.12279, 22); + writeTimesTable(193.12279, 22); + writeTimesTable(13.9, 122); + } + + /** + * @param num The number to write the times table for (0 <= num <= 99) + * @param limit How many multiples to show (0 <= limit <= 99) + */ + public void writeTimesTable(double num, int limit) { + final double MINNUM = 0; + final double MAXNUM = 99; + final int MINLIMIT = 0; + final int MAXLIMIT = 99; + + if ((num < MINNUM) || (num > MAXNUM)) { + System.err.println(String.format("Please specify a number between %.2f and %.2f (supplied: %.2f)", + MINNUM, MAXNUM, num)); + return; + } + + if ((limit < MINLIMIT) || (limit > MAXLIMIT)) { + System.err.println(String.format("Please specify a limit between %d and %d (supplied: %d)", + MINLIMIT, MAXLIMIT, limit)); + return; + } + for (int i = 0; i < (limit + 1); i++) { + // output specification: + // ~~.~~~ x ~~ = ~~~~~.~~~ + System.out.printf("%2.3f x %02d = %5.3f%n", num, i, (num*i)); + } + } + }