



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The quiz 1 results for csis 110 lecture 9, with statistics such as mean, standard deviation, and median. It also covers the reading assignment for chapter 3, including reference types, string operations, and class methods. The homework problems are listed, and the lab exercise is explained.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Quiz 1 Summary Possible 25 Mean 20 SD 2. Median 20. Max 24 Comment - on the last problem, it asked for a sequence of statements, not a program - you didn't need to define a class or main for this problem. Chapter 3 Reading: Last time: section 3.1 - 3.3, and 3. This time: Section 3. Homework Problems 3.2, 3.13, 3.16, 3.18, 3.21 - You don't need to 'explain' your answers. Due Wednesday, Feb 2. Reference Types Last time we started looking at reference types Example: String What's this look like in memory? String phrase = "My dog has fleas."; Instead of storing the value in the memory location, Java stores a reference to the value. We say that an object variable 'references' or 'points to' an object of that type. Thus, a String variable references an object whose type is String. We also say that phrase is an 'instance of a String'. When we declare an variable of a reference type, that variable is an 'instance of the type'. Uninitialized references
Declare a reference variable without giving it an initial value: Scanner stdin; Rectangle box; Point somePoint; Operator new stdin = new Scanner( System.in ); box = new Rectangle( x, y, width, height ); new allocates memory for an object ('instance') of the type. In these declarations, the variable references ("points to") a newly constructed object. Assignment We can use assignment with reference variables. But, it's not the same as with primitive variables. Rectangle box = new Rectangle( x, y, width, height ); Rectangle box2 = box; Draw picture Garbage Collection String word1 = "Happy"; String word2 = "Birthday"; What happens if we have the statement? word1 = word2; What happens to "Happy"? String Operations Concatenation (+) Look at program that prints out first and last names - Example1.java First, notice the nextLine() method that is part of the Scanner class. It gets all of the characters typed by the user until they press the enter key. What is going on in the println statement?
Note that these operations only access the value of the String. None of the methods change the String's value. Thus Strings are immutable. This is unusual - most types have operations that change an object's value. We'll see this later. Class methods The above methods all require an instance of a String. They operate on a particular, specific String. These are called instance methods - they require an instance of the type to operate on. Java also provides class methods - these are methods that provide some sort of service related to the class, but they do not require an instance of the class. The String class provides a set of methods named valueOf. The valueOf methods produce a String from a primitive type. int aValue = -45; double pi = 3.1416; char letter = 'Q'; String thisOne = String.valueOf( aValue ); String piString = String.valueOf( pi ); String it = String.valueOf( letter ); Important distinction - class methods use the class name. Instance methods use an object name (variable). int value = 1234 String aString = String.valueOf( value ); double pi = 3.1416. aString = String.valueOf( pi ); int value = 1234; int l = String.valueOf( value ).length(); Same result as int value = 1234 String s = String.valueOf( value ); int l = s.length(); Lab Exercise Write a program that asks the user for a telephone number in the form (xxx) yyy-yyyy. Your program should print out: The area code is xxx.
The local phone number is yyy-yyyy. Your program should be as robust as possible to extra spaces at the beginning or end of the phone number. You can use the Scanner.nextLine() function to read the input.