{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

03_lewis_409167_labMan03

03_lewis_409167_labMan03 - Chapter 3 Using Classes and...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
34 Chapter 3: Using Classes and Objects Chapter 3: Using Classes and Objects Lab Exercises Topics Lab Exercises String Class Prelab Exercises Working with Strings Random Class Rolling Dice Math Class Computing Distance Formatting Classes Formatting Output Enumerated Types Playing with Cards Wrapper Classes Experimenting with the Integer Class Panels Nested Panels
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 3: Using Classes and Objects 35 Prelab Exercises Sections 3.2-3.5 These exercises focus on the String, Random, and Math classes defined in the Java Standard Class Library. The main concepts are in the text in sections 3.2-3.5. The goals of the lab are for you to gain experience with the following concepts: Declaring a variable to be a reference to an object—for example, the following declares the variable quotation to be a reference to a String object: String quotation; Declaring a variable to be a reference to an object and creating the object ( instantiating it) using the new operator—for example, String quotation = new String("I think, therefore I am."); Random generator = new Random(); Invoking a method to operate on an object using the dot operator —for example, quotation.length() invokes the length method which returns the length of the quotation String or quotation.toLowerCase() quotation except all letters are lower case. These invocations would be used in a program in a place appropriate for an integer (in the first case) or a String (in the second case) such as an assignment statement or a println statement. Invoking static or class methods—these are methods that are invoked using the class name rather than an object name. The methods in the Math class are static methods (basically because we don't need different instances of Math whereas there are lots of different String objects). Examples are Math.sqrt(2) (which returns the square root of 2) and Math.pow(3, 2) (which returns 3 2 ) Importing the appropriate packages—usually when you use classes from a library you need to put the import declaration at the top of your program. The exception is for classes defined in the java.lang package (this includes String and Math) which is automatically imported into every Java program.
Image of page 2
36 Chapter 3: Using Classes and Objects Exercises 1. Fill in the blanks in the program below as follows: (Section 3.2, especially the example in Listing 3.1, should be helpful): (a) declare the variable town as a reference to a String object and initialize it to "Anytown, USA". (b) write an assignment statement that invokes the length method of the string class to find the length of the college String object and assigns the result to the stringLength variable (c) complete the assignment statement so that change1 contains the same characters as college but all in upper case (d) complete the assignment statement so that change2 is the same as change1 except all capital O's are replaced with the asterisk (*) character.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern