



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Portions of this handout by Eric Roberts and Patrick Young
private String addCommasToNumericString(String digits)
public void run() { while (true) { String digits = readLine("Enter a numeric string: "); if (digits.length() == 0) break; println(addCommasToNumericString(digits)); } }
public String removeAllOccurrences(String str, char ch)
removeAllOccurrences("This is a test", 't') returns "This is a es" removeAllOccurrences("Summer is here!", 'e') returns "Summr is hr" removeAllOccurrences("---0---", '-') returns "0"
public void run() { Rational r = new Rational(1, 2); r = raiseToPower(r, 3); println("r ^ 3 = " + r); }
private Rational raiseToPower(Rational x, int n) Rational result = new Rational(1); for (int i = 0; i < n; i++) { result = result.multiply(x); } return result; }
Diagram at this point
Portions of this handout by Eric Roberts and Patrick Young
private String addCommasToNumericString(String digits) { String result = ""; int len = digits.length(); int nDigits = 0; for (int i = len - 1; i >= 0; i--) { result = digits.charAt(i) + result; nDigits++; if (((nDigits % 3) == 0) && (i > 0)) { result = "," + result; } } return result; }
private String removeAllOccurrences(String str, char ch) { String result = ""; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) != ch) { result += str.charAt(i); } } return result; }
**private String removeAllOccurrences(String str, char ch) { while (true) { int pos = str.indexOf(ch); if (pos >= 0) { str = str.substring(0, pos)
1000 1 2
nu m den 1020 1 1
nu m den 1040 1 2
nu m den
r 1000^ FFC
heap
n
1060 1 4
nu m den
stack i 3^ FFB result
1080 1 8
nu m den
x 1000^ FFB
garbage