Ver código fonte

(feat) Lab 7 - string formatting implemented

Plus placeholder for lab 6 exercise 9 (rock-paper-scissors)
main
Rob Hallam 1 ano atrás
pai
commit
992c14be3b
1 arquivos alterados com 50 adições e 0 exclusões
  1. +50
    -0
      week1.java

+ 50
- 0
week1.java Ver arquivo

@@ -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));
}
}
}

Carregando…
Cancelar
Salvar