// This program contains several small examples discussed in the CSE143X // lecture that are relevant to the Bagels program. Each small example has // been turned into its own method. import java.util.*; public class Examples { public static void main(String[] args) { // make a string with 10 occurrences of "hello" String s = repeat("hello", 10); System.out.println("s = " + s); // sample call to replace the 'e' at index 1 with an asterisk s = replace(s, 1, "*"); System.out.println("s = " + s); // sample call on round1 to round a computation System.out.println("unrounded two thirds = " + 2.0 / 3.0); System.out.println("rounded two thirds = " + round1(2.0 / 3.0)); } // returns a string composed of times occurrences of text public static String repeat(String text, int times) { String result = ""; for (int i = 0; i < 10; i++) { result = result + "hello"; } return result; } // returns the string obtained by replacing the character at the given // index with the replacement text public static String replace(String s, int index, String replacement) { return s.substring(0, index) + replacement + s.substring(index + 1); } // returns the result of rounding n to 1 digit after the decimal point public static double round1(double value) { return (int) (10 * value + 0.5) / 10.0; } }