0% found this document useful (0 votes)
138 views3 pages

String Exercise

This document contains instructions for a string exercise in Java. It provides sample code and asks the student to write additional code to manipulate and analyze strings, including determining string lengths, extracting substrings, converting case, finding indexes of characters, and more. The student is asked to write methods to display integer values of characters, format a string with upper and lowercase halves, and output a name in reverse order.

Uploaded by

johndoe21718
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views3 pages

String Exercise

This document contains instructions for a string exercise in Java. It provides sample code and asks the student to write additional code to manipulate and analyze strings, including determining string lengths, extracting substrings, converting case, finding indexes of characters, and more. The student is asked to write methods to display integer values of characters, format a string with upper and lowercase halves, and output a name in reverse order.

Uploaded by

johndoe21718
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

OOP

String Exercise

September 2013

You need to look up String class in the java class library to answer the following: 1. Given the following local variable declarations:
String a = "abc"; String s = a; String t;

What is the value of the following expressions (or ERROR)? __________ s.length() __________ t.length() __________ 1 + a __________ a.toUpperCase() __________ "Tomorrow".indexOf("r") __________ "Tomorrow".lastIndexOf('o') __________ "Tomorrow".substring(2,4) __________ (a.length() + a).startsWith("a") __________ s == a __________ a.substring(1,3).equals("bc")

2. Assume the following:


String s, t, h, a; String n, e; int i; h = "Hello"; s = " How are you? a = "abc"; n = null; e = "";

";

Give the values of the following expressions, or illegal. __________ h.length() __________ h.substring(1) __________ h.toUpperCase() __________ h.toUpperCase().toLowerCase() __________ h.indexOf("H") __________ h.startsWith("ell") __________ "Tomorrow".indexOf("o") __________ "Tomorrow".indexOf("o", 3) __________ "Tomorrow".lastIndexOf('o')

OOP

String Exercise

September 2013

__________ "Tomorrow".substring(2,4) __________ a.length() + a __________ "a = \"" + a + "\"" __________ (a.length() + a).startsWith("a") __________ a.length() + a.startsWith("a") __________ a.substring(1,3).equals("bc") __________ a.substring(1,3) == "bc" __________ "a".compareTo("c") __________ "a".compareTo("A") __________ s.trim().charAt(2)

Coding time: 3. What does the following code print? System.out.println( "*\n**\n***\n****\n*****" );

4. Here's a peek ahead. In this chapter you learned about integers and the data type int. Java can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. Every character has a corresponding integer representation. The set of characters a computer uses and the corresponding integer representations for those characters is called that computers character set. You can indicate a character value in a program by simply enclosing that character in single quotes as with 'A'. You can determine the integer equivalent of a character by preceding that character with (int)this is called a cast (as we have already seen in previous examples). (int) 'A' The following statement would output a character and its integer equivalent System.out.println( "The character " + 'A' + " has the value " + (int) 'A' ); When the preceding statement executes, it displays the character A and the value 65 (from the so- called Unicode character set) as part of the string. Write method that displays the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols. At a minimum, display the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character.

OOP

String Exercise

September 2013

5. Write a Java method that reads a string from the keyboard, and outputs the string such that the first half is all lowercase and the second half is all uppercase. If, for instance, the string "HelloWorld" is given, the output will be "HELLOworld".

6. Write a method that prompts for the first name and then for the last name of a person. The program should then output the complete name spelled backwards, using a space as separator between first name and last name. You are to submit question 1- 3 on moodle. Create a class called StrExercise and write all the methods for #4-6. Run your program for me by the end of the class.

You might also like