%%%%%%%%%%%%%%%%%%%%NOTE%%%%%%%%%%%%%%%%%%%%%%%%%%% % Before you start doing these exercises read % carefully the following notes. % % Read IOCheatSheet.txt to see how to input strings % from the console. % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%OPERATIONS ON STRINGS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% String s // declare a variable of type String s.length() // returns the number of characters in s s.equals(s1) // returns true if s and s1 are the same string s.charAt(n) // returns the char value at position n s.substring(n, m) // returns the substring between n and m (excluding char at position m) s.substring(n) // returns the substring from position n to the end of the string s.indexOf(s1) // returns index within s of first occurrence of string s1, otherwise -1 s.trim() // removes spaces from string s Integer.parseInt(s) // returns the integer corresponding to s Double.parseDouble(s) // returns the double corresponding to string s Character.getNumericValue(c) // returns integer corresponding to char c String.valueOf(c) // returns the string representation of char c %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%Examples%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% String s = "Hello"; String s1 = "He"; String s2 = "12"; String s3 = "12.2"; char c = '5'; s.length() //returns the integer 5 s.equals(s1) // returns the boolean false s.charAt(1) // returns the char 'e' s.substring(0, 2) // returns the String "He" s.substring(3) // returns the String "lo" s.indexOf(s1) // returns the integer 0 Integer.parseInt(s2) // returns the integer 12 Double.parseDouble(s3) // returns the double 12.2 Character.getNumericValue(c) // returns integer 5 String.valueOf(c) // returns the String "5" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%ENJOY%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Ex.1 ---- Ask the user for their name. Then display their name to prove that you can recall it. Ask them for their age. Then display what their age would be five years from now. Then display what their age would be five years ago. Example: Hello. What is your name? John Hi, John! How old are you? 30 Did you know that in five years you will be 35 years old? And five years ago you were 25! Imagine that! Ex.2 ---- Make a simple numeric calculator. It should prompt the user for three numbers. Then add the numbers together and divide by 2. Display the result. Your program must support numbers with decimals and not just integers. Ex.3 ---- Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle of the "out" string, e.g. "<>". Hint: use str.substring(i, j) to extract the String starting at index i and going up to but not including index j. Ex.4 ------ Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". Ex.5 ---- Given a string of odd length, return the string length 3 from its middle index, so "Candy" yields "and". The string length will be at least 3. Ex. 6 ---- Given a string, return a new string made of 3 copies of the first 2 chars of the original string. The string may be any length. If there are fewer than 2 chars, use whatever is there. Ex. 7 ----- Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi". Ex.8 ------ Given two strings, append them together (known as "concatenation") and return the result. However, if the strings are different lengths, omit chars from the longer string so it is the same length as the shorter string. So "Hello" and "Hi" yield "loHi". The strings may be any length. Ex.9 ------ Given a string, if one or both of the first 2 chars is 'x', return the string without those 'x' chars, and otherwise return the string unchanged. This is a little harder than it looks.