CS 100: Lecture L12
March 4
Strings
import java.io.*; public class L12b { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); String s = "abcd"; int n = s.length(); System.out.println(s + " has length " + n); String t = "ab cd"; if (s.equals(t)) {System.out.println(s + " equals " + t);} else {System.out.println(s + " does not equal " + t);} t = "abcd"; if (s.equals(t)) {System.out.println(s + " equals " + t);} else {System.out.println(s + " does not equal " + t);} String r1 = "abc"; String r2 = "xyz"; r1 = r2; r2 = "def"; System.out.println("r1 = " + r1 + " r2 = " + r2); String s1 = "ab"; String s2 = "cd"; String s3 = 1 + s1 + 2 + s2 + 3.1; System.out.println(s3); String B = "aardvark"; int n1 = B.indexOf("a"); int n2 = B.lastIndexOf("a"); int n3 = B.indexOf("ar"); int n4 = B.indexOf("z"); System.out.println("n1 = " + n1 + " n2 = " + n2 + " n3 = " + n3 + " n4 = " + n4); String A1 = B.substring(4,7); String A2 = B.substring(4,4); String A3 = B.substring(4,5); System.out.println("A1 = " + A1 + " A2 = " + A2 + " A3 = " + A3); in.waitUntilEnter(); } } /* Output: abcd has length 4 abcd does not equal ab cd abcd equals abcd r1 = xyz r2 = def 1ab2cd3.1 n1 = 0 n2 = 5 n3 = 1 n4 = -1 A1 = var A2 = A3 = v */
Clarification of the Alias Concept Using the Disk class and the String class.
import java.io.*; public class ShowAliasIdea { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); // Let's create 2 disk objects: Disk D1 = new Disk(1,2,3); Disk D2 = new Disk(10,20,30); // If we "pretty print" the objects referred to by D1 and D2 // we see that they indeed refer to two separate disks: D1.println(); D2.println(); // Output from this: // xc = 1.0 yc = 2.0 r = 3.0 // xc = 10.0 yc = 20.0 r = 30.0 // Now the statement D2 = D1; // says that D2 now references the same object as D1. That is, D1 is // an alias for D2 and vice versa. Let's confirm this: D1.println(); D2.println(); // Output from this: // xc = 1.0 yc = 2.0 r = 3.0 // xc = 1.0 yc = 2.0 r = 3.0 // Even if we modify this object "through" D1, D2 still refers to // the same object as D1: D1.move(5,6); D1.println(); D2.println(); // Output from this: // xc = 5.0 yc = 6.0 r = 3.0 // xc = 5.0 yc = 6.0 r = 3.0 // Now strings are objects, so let us step through an analogous // sequence and see what happens. // Let's create 2 string objects: String s1 = "abc"; String s2 = "xyz"; System.out.println(s1); System.out.println(s2); // Output from this: // abc // xyz s2 = s1; // s2 now refers to the same string object as s1: System.out.println(s1); System.out.println(s2); // Output from this: // abc // abc // Now let's modify the string referenced by s1 and see if s2 // "follows along": s1 = s1 + "123"; System.out.println(s1); System.out.println(s2); // Output from this: // abc123 // abc // What's happened? The key is that the statement s1 = s1 + "123" // does NOT modify the object referenced by s1. Java does not permit // modification of string objects. A statement like s1 = s1 + "123" // is equivalent to the following more 'traditional' appeal to the String constructor: // // s1 = new String(s1+"123"). // // So s1 now refers to this new object while s2 continues to reference the // string object "abc". in.waitUntilEnter(); } }
Roman Numeral Calculations
// Methods for dealing with Roman numerals public class RN { // Yields the value of the Roman numeral specified by the first character // of r. It is assumed that r is a valid one or two character Roman numeral public static int valueOfShort(String r) { String First = r.substring(0,1); int s; // Determine the first character and assign its value to s without regard // to the second character. if (First.equals("I")) {s = 1;} else if (First.equals("V")) {s = 5;} else if (First.equals("X")) {s = 10;} else if (First.equals("L")) {s = 50;} else if (First.equals("C")) {s = 100;} else if (First.equals("D")) {s = 500;} else {s = 1000;} // Identify those situations where the value is to be negated boolean specialI = r.equals("IV") || r.equals("IX"); boolean specialX = r.equals("XL") || r.equals("XC"); boolean specialC = r.equals("CD") || r.equals("CM"); if (specialI || specialX || specialC ) {return -s;} else {return s;} }