DOC PREVIEW
UW-Madison CS 302 - CS 302 Lecture Notes

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Scope v Every variable and parameter that is declared has a scope which is the area of a program where the variable can be used v A variable’s scope begins at its declaration and continues until the closing curly brace “}” of the scope to which it was declared. (The closing curly brace could be for a method, block of code, etc.) v Local Scope: • Variables declared in methods are “local” variables • Parameters are variables to a method, and therefore are local to the method • Local variables cannot be accessed outside of the method to which they are declared v Nested Scope: • Defined: One scope WITHIN another scope • All method’s scopes are nested inside a class’ scope. • Compound statements (e.g., if, while, else blocks) form new scopes that are nested inside a method’s scope (and possibly further nested inside other compound statements’ scopes) • Variables declared inside a scope (whether nested or not) are NOT accessible outside that scope • Variables declared in an outer scope are accessible in any inner scopes • class data members belong to the class’ scope and are available to all inner scopes (i.e., methods). However, if a data member is non-static, it is NOT accessible by static methods of the class.v Example #1: class Parameters { public static void main (String[] args) { int x = 0; System.out.println (“x in main: ” + x); whatHappensToX (x); System.out.println (“x in main: ” + x); } public static void whatHappensToX (int x) { x = 10; System.out.println (“x in whatHappensToX: ” + x); } }v Example #2: class Scope { private static int foo = 3; private static int goo = 5; public static void main (String[] args) { int foo = 5; int goo = 3; for (int i = 0; i < 4; i++) { foo = foo(foo); print (i, foo, goo); goo = goo(goo); print (i, foo, goo); } } private static int foo (int goo) { goo = goo + foo; foo = foo – goo; return goo; } private static int goo (int foo) { foo = foo + goo; goo = goo – foo; return foo; } private static void print (int i, int f, int g) { System.out.println (“i = ” + i + “, f = ” + f + “, g = ” + g + “, foo = ” + foo + “, goo = ” + goo); }Character Primitive Type v Like arithmetic primitive data types, except the value is a single character v Character Literals (aka Character Constants): symbols enclosed in single quotes e.g.: ‘a’ ‘1’ ‘$’ ‘+’ ‘Z’ v Syntax for Character Variables: Declaration: char <variable name>; Assignment: <variable name> = ‘<character>’; • char is a Java reserved word • <character> is any unicode character • ‘<character>’ can be substituted for any expression that evaluates to a char. • The open quote ‘ and the close quote ’ must be on the same line v Examples: char c; c = ‘a’ char charVal = ‘#’; char menuChoice = getMenuChoice (); /* getMenuChoice() returns a char */ char c1, c2, c3, c4 = ‘8’ v Unicode: The international standard coding scheme for characters (Contains the ASCII coding scheme)Character Primitive Type...continued v Characters have a numerical value. • Can do simple arithmetic on characters char c = ‘a’ + 1; // the value of c is ‘b’ • Can use logic on characters if (‘a’ < ‘b’) {...} • Can cast between characters and integers print (“The ASCII code of c is “ + (int)‘c’); print (“The char with ASCII code of 35 is “ + (char) 35); v Write a code fragment that prints to standard output (System.out) the alphabet in either all lowercase or all uppercase letters. for (char c = ‘a’; c <= ‘z’; c++) { if (c != ‘z’) { System.out.print (c + “, ”); } else { System.out.println (c); } }Strings v String Literals • Any set of characters within double quotation marks • Can have any number of characters within a string • The open quote “ and the close quote ” must be on the same line • The string literal begins with the first character after the open quote and ends with the last character before the end quote • Examples: A typical String: “I love Java!” A zero character String: “” • Certain characters need an escape sequence in order to be a part of the string. An escape sequence is a backslash followed by a character. \” prints “ within the string \\ prints \ within the string \’ prints ‘ within the string \n prints a carriage return (aka newline) within the string \t prints a tab space within the string Example: print (“This is a \”quote\”\nand this” + “is a backslash\t\\”); output: This is a “quote” and this is a backslash \Strings...continued v Concatenation Operator: + One can concatenate (i.e., merge) the following using the concatenation operator: • two Strings: “Java is “ + “Fun!”; • a String and a Primitive Type: “The sum of 1+2 is “ + (1+2); • a String and another Object “This is a Fish: “ + fish1; v toString() Method ALL objects inherit a toString() method from the Object class. This method “converts” an object into a string. Whenever an object is concatenated with a String (or used as an argument to such methods as printLine()), the object’s toString() method is called. When creating an instantiable class, it is always a good idea to define a toString() method that will override the inherited toString() method. Example of a toString() method: public String toString () { return (“Name: “ + fishName + “; Food: “ + foodPoints + “ ; Position: “ + position); }Strings...continued v String Objects Strings (including String Literals) are all instances (i.e., objects) of the String class defined in the java.lang package. Therefore, they are declared and created in the same way as other objects: Declaration: String <object name>; Creation: <object name> = new String (<String Literal>); Because String objects are used so frequently, a shortcut syntax for declaring and creating is included in Java syntax: String <object name> = <String Literal>; Examples: String s; s = new String (“Hello World!”); String name = new String (“David Schneider”); String joke = “There once was a man from Nantucket...”; String input = inBox.getString (“Enter a


View Full Document

UW-Madison CS 302 - CS 302 Lecture Notes

Download CS 302 Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 302 Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 302 Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?