Lecture Notes for CS 150 Fall 2009; Version 0.5

Size: px
Start display at page:

Download "Lecture Notes for CS 150 Fall 2009; Version 0.5"

Transcription

1 for CS 150 Fall 2009; Version 0.5 Draft! Do not distribute without prior permission. Copyright by Mark Holliday Comments, corrections, and other feedback appreciated Chapter 2: Using the Class Section 1: Method Signatures and Overloading Section 2: Literals and Return Values Section 3: Losing Return Values Section 4: Variable Declaration Statements Section 5: Assignment Statements Section 6: Implications of Reference Variables Section 7: Methods Section 8: Method Cascading and Composition There are two predefined objects that we will use significantly in the course. One is the PrintStream object referenced by the variable System.out and the other is the InputStream object referenced by the variable System.in. The PrintStream class has only two methods that we will use: println which we have already used and print. The difference is that print does not insert a newline character into the output. In other words, on the screen, a print method call does not move the cursor down to the next line. In contrast, the println method does. In fact, println is pronounced "print line". We will never use the InputStream class directly since it does not have the method we want to use for reading input. The method we want to use is called nextline and is a method of the Scanner class. In chapter 3 we learn how to create a Scanner object from an InputStream object. What this chapter is about is the class. The class is important in Java programs and since we are not going to discuss primitive types (such as the int type that represents integers) until chapter 5, the class is one of the main examples with which we will work. In using the class we will not be using predefined 1

2 objects; we will creating new objects. However, the way that we will be creating objects is a special way that just works with the class. The more general way to create objects will be discussed in the next chapter. In this chapter we will use the class to create objects and to practice using many of the methods that come with the class. We also discuss further the important idea of variables. Section 1: Method Signatures and Overloading Above we said that PrintStream has a method named println and a method named print. Actually, that is not quite true; PrintStream has two methods that are both named println as well as a method named print. To explain how this is possible we have to be more precise about how methods are characterized. As described in Chapter 1, a method is called by the general format reference. methodname ( argumentlist) where there is a reference to the object that has the method, followed by a dot, followed by the name of the method, followed by a left parenthesis, followed by list of arguments, followed by a right parenthesis. The reference makes clear which object is being used and each object is an instance of some class. Thus, we know we are using one of the methods associated with a particular class. Of the methods for that class if two have the same name, then the argument list must be used to distinguish between them. Such a situation is called an example of method overloading. The exact rule is that two methods of the same class must 1) have different names or 2) have the same name and a different number of arguments or 3) have the same name and the same number of arguments but at least one pair of corresponding arguments have different types. For example, if one method has a reference to a as its first argument and the other method has a reference to a PrintStream as its first argument, then the methods are distinct. In short the requirement is that the methods must have different signatures where a method's signature is its name and argument list. Here is an example using the three methods of the PrintStream class for printing. class PrintExample { public static void main([] args) { System.out.println("A string, then next line,"); System.out.println("and then a blank line."); 2

3 } } System.out.println(); // causes move to next line System.out.print("Just a string,"); System.out.print(" all on the same line."); System.out.println(); System.out.println("Hi"); The output is A string, then next line, and then a blank line. Just a string, all on the same line. Hi. Section 2: Literals and Return Values A literal is a sequence (or ) of characters between double quotes. We have used them as the argument to the println method of the PrintStream class. literals can be used in other situations and even as an argument there is more going on behind the scenes then we have discussed. The class is special in that it is the only class that has literal values. When a literal is used what really is happening is that 1. an instance of the class (that is, a object) is created, 2. that object is given the value of the literal, and 3. a reference to that object is also created with the literal representing the reference For example, the literal "wcu" creates the diagram shown in Figure 2.1. The new object has the value "wcu" inside it and "wcu" is also thought of as representing the reference to that object. 3

4 "wcu" "wcu" Figure 2.1. The object and reference created by the literal "wcu". Once we have a reference to an object we can use that reference to call a method of that object. That is what we did with System.out.println("Hi"); since System.out is a variable holding a reference to a PrintStream object and println is a method that PrintStream objects have. The only difference is that in the case of "wcu" the reference is not being held by a variable. Instances of the class are immutable. The value within a object can not be changed after the object is created. To simulate changing a object, it is necessary to create a new object that has a value which is a modification of the value of the original object. A object can have a value which is an empty sequence of characters. The literal for this object is ""; that is, the double quote for the start of the immediately followed by the double quote which is the end of the. A variable that is not holding a reference to a object is different from a variable that is holding a reference to a object that is the empty. Figure 2.2a illustrates a variable that is not holding a reference to an object and Figure 2.2b illustrates a variable that reference a object that is the empty. 4

5 foo foo (a) (b) "" Figure 2.2. Memory diagram for a) an empty variable, and b) a variable holding a reference to a object that is an empty. One method of the class is called touppercase. We can call that method by "wcu".touppercase() Figure 2.3 shows this method call as the wavy arrow going from the reference "ibm" to the method touppercase. The wavy arrow has empty parentheses next to it, to indicate that there are no arguments. The "ibm" that is the reference is not inside a rectangle since it is not in a variable. 5

6 "wcu".touppercase() arguments reference method name "wcu" () "wcu" touppercase "WCU" Figure 2.3. The diagram for the method call "wcu".touppercase(). So "wcu".touppercase() is a new example since it illustrates a method from the class while before we have just talked about methods of the PrintStream class. It is also new in that the reference is created from a literal instead of being held within a predefined variable. It is also new in that it illustrates how literals automatically create objects. It also is new in that it is our first method that has a return value. All of the println and print methods just perform their action and then execution returns to the calling code. The touppercase method adds a new feature called a return value. A return value means that when execution returns to the calling code, a value is returned also. The value that the touppercase method of the class returns is a reference to a object. With respect to method overloading all that matters is the method name and the argument list. However, whether a method returns a value and if it returns a value, what type of value it returns, is an important aspect of the description of a method. Here is a table [page 29 of AW] describing the methods we have so far introduced. 6

7 Class Method Return value Argument List PrintStream println None None PrintStream println None One argument which is a reference to a object PrintStream print None One argument which is a reference to a object touppercase Reference to a object none Section 3: Losing Return Values When a method call returns a value something has to be done with that value or will it be lost. In particular, the following code fragment is a very common bug for beginning Java programmers. System.out.println( "The following method call returns a value."); "cs150 is wonderful".touppercase(); System.out.print( "However, nothing is done with the return value "); System.out.println("so it is lost."); The whole point of the touppercase method is to create an object which is an all uppercase version of the that it is a method of. However, if the returned reference to that object is not used, then there is no way of accessing the new object. So how can the returned value be used? It can be used immediately or it can be saved for later use. One way to use it immediately is to make that reference be an argument to another method call. Here is such an example System.out.println("cs150 is wonderful".touppercase()); This example will display on the screen CS150 IS WONDERFUL In this example, the return value from the touppercase method call is being used as 7

8 the argument to the println method call. This works since the type of the return value is reference to and that is the type that is required for the argument to the println method. In understanding the above example, or any example using a return value, the key is to think of the return value as replacing the method call that generates the return value. So think of the above example as having the effect of System.out.println( reference to a object ); where reference to a object is not Java code but just stands for the reference that is returned from the method call. This way of using a reference immediately is called method composition. A second way of using a return value which is a reference immediately is to use it to call a method of the new object. This is called method concatenation and we will see some examples of this later in this chapter. There are other ways of using a return value immediately which are especially useful when we look at return values which are of primitive types. Primitive types are discussed in Part B of these notes. Section 4: Variable Declaration Statements Instead of using a return value immediately, many times it is smart to save it and then later use it. Once it is saved it can be used over and over again. The way to store a return value is to store it in a variable. A variable has a name (its identifier) and can hold a value. One kind of a value is a reference to an object. So far the only variable that we have seen is the predefined variable System.out. We can create our own variables using a variable declaration statement. Such a statement has the form of the type of the variable followed by the name of the variable. An example of the simplest version is stringvalue; // declares one variable named stringvalue //that can hold a reference to a object // but is not currently holding a reference Several variables can be declared in a single declaration statement as long as they are all 8

9 of the same type. For example, first, second, third; // declares three variables named first, second, // and third all of these variables can hold a // reference to a object but // currently none of them is holding a reference This second form of variable declarations has some limitations. As discussed later there are actually three kinds of variables: fields, parameters, and local variables. The examples we are currently considered are all local variables. This second form of variable declarations where the type is listed once for several variables is not legal Java syntax when declaring a variable that is a parameter. Trying to use this form when declaring several parameters (that is, in a parameter list) is a common bug for beginning students. For fields and local variables this second form of variable declarations is legal Java. However, programming style guidelines restrict its use. In particular, our departmental guidelines prohibit its use for fields. The reason is that each field is important enough to warrant a separate declaration statement. Our guidelines allow this form for local variables, but only three variables can be declared in a single such statement. The reason is that declaring many variables in a single statement makes the code more difficult to read. There is a third form for variable declaration statements that we will see after introducing assignment statements. This third form includes an initialization as part of the declaration. Section 5: Assignment Statements The most common way to cause a variable to hold a value is by using an assignment statement. In particular an assignment statement can be used to save the return value from a method call into a variable. Assignment statements are important and also, unfortunately, students sometimes have trouble reading them correctly at first. An assignment statement has four parts which are in order from left to right: a left-hand side (LHS), an equal sign, a right-hand side (RHS), and a semi-colon. The key point is that you need to read an assignment statement from right-to-left, not leftto-right. This is what often confuses beginning students since we are all used to reading left-to-right. You read the RHS first. The RHS is an expression. The concept of an 9

10 expression is very important and the details will be discussed later. For now all we need to know is that an expression is something that evaluates to a value. The one expression that we have been using is a method call. The value that a method call expression evaluates to is the return value of that method call. The LHS side must be a variable since the point of an assignment statement is to cause the value which is the RHS to become the new value held by the variable which is the LHS. The equal sign in the middle is called the assignment operator since it is thought of as doing the actual assigning; that is, the actual placing of the RHS value into the LHS variable. To help the student remember that the equal sign means assignment and not equality, I read the equal sign as the word gets. Thus, in the example. stringvalue; stringvalue = "how are you?"; The second statement is read as The variable stringvalue gets the "how are you?" In this example, recall that the "how are you?" literal causes a object to be created that holds the value "how are you?" and the literal also represents a reference to that object. Thus the right-hand side is a reference to a object. The assignment causes the variable stringvalue to be assigned, or get, a copy of that reference. Part a) of Figure 2.4 shows the memory diagram for this example after just the right-hand side has executed. Part b) of Figure 2.4 shows the memory diagram after the entire statement has executed. "How are you?" stringvalue "How are you?" (a) "How are you?" (b) "How are you?" 10

11 Figure 2.4. a) The memory diagram after the right-hand side of the statement has executed. b) The memory diagram after the assignment statement stringvalue = "how are you?"; has completed execution. Another example assignment statement has a method call as the RHS. In this case, the RHS evaluates to the return value of that method call. Here is an example, stringvalue = "hello".touppercase(); Figure 2.5 is the memory diagram showing the result of the execution of this statement. This example illustrates how the return value from a method call can be saved in a variable. "hello" "hello" "hello" "hello" "hello" "hello" touppercase "HELLO" : stringvalue "HELLO" (a) (b) (c) Figure 2.5. The object diagram for the assignment statement stringvalue = "hello".touppercase();. a) shows the calling of the method, b) shows the situation after the return from the method call, and c) shows the situation after the assignment of the return value to the variable stringvalue. The RHS must evaluate to a value so having a method call being the RHS only makes sense if the method returns a value. For example, have a println method call as a RHS does not make sense since there is no return value to then assign to the LHS. stringvalue = System.out.println(); 11

12 // WRONG! Not legal Java since the RHS does not // evaluate to a value We have now seen three kinds of Java statements: method call statements, variable declaration statements, and assignment statements. There is a third form of variable declaration statements that is a combination of a variable declaration and an assignment statement. Here is an example name = "John Smith"; This is called a declaration with an initialization. The value on the right-hand side is assigned to the variable that is declared on the left-hand side. As mentioned earlier, variables are useful since they can be used to store a value and reuse that value later. The only kind of value we have seen so far is a reference. Thus, for example [page 30 of AW] instead of System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); we can do line = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); System.out.println(line); System.out.println(line); System.out.println(line); which is quite a bit shorter. In this example the variable line is holding a reference to a object. That reference can be used to call a method on that object. For example, greeting = "Hello"; biggreeting; // Figure 2.6a shows after this statement biggreeting = greeting.touppercase(); // Figure 2.6b shows after this statement System.out.println(bigGreeting); Figure 2.6 shows the memory diagrams for this code fragment after the declaration of the variable biggreeting and the assignment to variable biggreeting. 12

13 greeting biggreeting greeting biggreeting () "hello" "hello" touppercase "HELLO" "hello" (a) "hello" (b) Figure 2.6. a) Memory diagram after declaration of variable biggreeting, and b) memory diagram after assignment to variable biggreeting. All the variables should have the word before the variable name, but it is not shown due to limited space. Section 6: Implications of Reference Variables When a variable is declared, it is declared to be of a particular type (such as or PrintStream). For all the variables that we are currently considering, that type is a class. When the type of a variable is a class, then the value held by the variable is a reference to an instance of that class or is the special value called null. The null value will be discussed later. Variables that can hold references (that is, have a class as their type) are called reference variables. In this section we discuss some implications of the fact that reference variables can hold references. The first point is that a variable can only hold a reference for an object of the variable's type. It is illegal to try to assign to the variable a reference to another kind of object. s; PrintStream p; s = System.out; // WRONG! since s can only hold a reference to a // object and PrintStream holds a // reference to a PrintStream object 13

14 p = "Hello"; // WRONG! Since p can only hold a reference // to a PrintStream // object and "Hello" is a reference to a // object s = "Hello"; // fine p = System.out; // fine The second point is that the reference that a variable is holding can change and that a variable can only hold one reference at a time. t; t = "Springtime"; t = "Wintertime"; Figure 2.7 shows the memory diagram after the last of these statements. Notice that the object "Springtime" has no reference pointing to it. If an object has no reference pointing to it, then there is no way for the program to access that object. The Java virtual machine will eventually detect such objects and delete them. That process is called automatic garbage collection. Automatic garbage collection is an important difference from C++. In C++ garbage collection has to be done explicitly by the programmer. "SpringTime" "WinterTime" : t Figure 2.7. The memory diagram after the statements where variable t has its value changed. The third point is that even though a variable can only reference one object at a time, one object may have several references to it simultaneously. Consider this fragment s, t; s = "Springtime"; t = s; 14

15 Figure 2.8 shows the memory diagram after the execution of these statements. "SpringTime" : s : t Figure 2.8. The memory diagram for the code fragment where s and t both hold references to the same object. A fourth point is that variables are independent so that two variables both hold references to the same object, does not mean that changes to one of those variables will change the other variable. For example, consider the following code fragment. Figure 2.9 shows the memory diagram after the execution of this fragment. s, t; s = "Inventory"; t = s; s = "Payroll"; : t : s "Inventory" "Payroll" Figure 2.9. The memory diagram for the code fragment where two variables are referencing the same object and then one of the variables has its reference changed. The last point is that when a variable is used in an assignment statement, how the variable is used depends on whether the variable is on the left hand side or the right hand side. If it is on the right hand side, then when the statement is executed the content of the variable (which is a reference) is retrieved. If the variable is on the left hand side, then when the 15

16 statement is executed, the variable is used as the storage place to hold the result of evaluating the right hand side of the statement. Section 7: Methods So far we have only used one method, touppercase, of the class. We now introduce several other methods. The table [page 38 of AW] below lists these methods. touppercase tolowercase Method Returns Arguments Reference to a object Reference to a object None None length A number None trim concat substring substring Reference to a object Reference to a object Reference to a object Reference to a object None Reference to a object A number Two numbers The length method returns how many characters are in the. The trim method removes space characters and tab characters from the beginning and end of the. The concat method creates a new that is the concatenation of the current and the that is the argument. Here is a fragment illustrating these three methods. s = " blue "; // there are two spaces before the "b" and // after the "e" System.out.println(s.length()); s = s.trim(); System.out.println(s); System.out.println(s.length()); s = s.concat(s); 16

17 System.out.println(s); // Figure 2.10 shows the result of the last // two statements The output of this fragment is 8 blue 4 blueblue Of these three methods perhaps the most interesting is concat since it takes a reference to a as an argument and creates a new. Figure 2.10 is the memory diagram for the second to last statement in the above fragments. That statement illustrates concatenation. : s : s ( ) "blue" "blue" "blueblue" concat Figure The memory diagram for the statements illustrating concatenation. The characters that form a can be specified by position. The first character in the s is at position 0, the second character is at position 1, and the last character is a position s.length() - 1. The position number is called the index of that character. The two substring methods use these position numbers in creating a new. The substring method with one argument creates a new that is the substring of the current that starts at the position specified by the integer argument and goes to the end of the current. The substring method with two arguments creates a new that is the substring of the current that starts at the position specified by the first argument and goes to, but does not include, the position specified by the second 17

18 argument. Here is a code fragment and figure, Figure 2.11, illustrating the one argument substring method. s, t; s = "hamburger"; t = s.substring(3); : s (3) : t "hamburger" substring "burger" Figure Memory diagram for the fragment illustrating the one argument substring method. Here is a code fragment and figure, Figure 2.12, illustrating the two argument substring method. s, t; s = "hamburger"; t = s.substring(3, 7); 18

19 : s "hamburger" (3, 7) substring : t "burg" Figure Memory diagram illustrating the two argument substring method. Section 8: Method Cascading and Composition We have considered three kinds of statements: method call statements, variable declaration statements, and assignment statements. It is possible to construct method call statements that involve more than one method call. There are two ways to do this: method cascading and method composition. This section describes these two ways. Sometimes invoking more than one method in a single statement is a good idea and sometimes it is not The advantage is that the code is shorter which means that the programmer does not have to write as much and does not have to create some extraneous variables. The disadvantage is that the code is shorter which means that the code can sometimes be less readable. When shorter code is an improvement is subjective. In any case, it is useful to know how to use method cascading and composition to shorten a code fragment. Of the two approaches method composition is more widely used and usually more readable. Method cascading is only possible when the first method called returns a reference. In this case, the returned reference can be immediately used to call a method of that object referenced. Here is an example code fragment and corresponding figure, Figure 2.13, not using method cascading. first = "Blue"; second = "Green"; third = "Red"; temp, last; // use the name last instead of final since // final is a reserved word 19

20 // not using method cascading temp = first.concat(second); last = temp.concat(third); // last is "BlueGreenRed" first second (second) concat "Blue" "Green" temp (third) "BlueGreen" concat third "Red" "BlueGreenRed" last Figure Memory diagram for fragment without method cascading. All the variables should have the word before the variable name, but it is not shown due to limited space. Here is the same code fragment and corresponding figure, Figure 2.14, but with method cascading. The use of the variable temp is avoided. The reference returning from first.concat(second) is immediately used to call the concat method of the new object. first = "Blue"; second = "Green"; third = "Red"; last; // using method cascading last = first.concat(second).concat(third); // last is "BlueGreenRed" 20

21 first second (second) concat "Blue" "Green" (third) "BlueGreen" concat third "Red" "BlueGreenRed" last Figure Memory diagram for fragment with method cascading. Notice that the only difference in the figure is that the variable named temp does not exist. Instead the reference to the object holding the value "BlueGreen" is not inside a variable. All the variables should have the word before the variable name, but it is not shown due to limited space. Method composition also involves multiple method calls and also, like method cascading, requires the first method call to have a return value. Method composition is actually more widely applicable since the return value from the first method call does not have to be a reference. It can be another type of value (a primitive type value), but we will not discuss primitive types until later. Here are two code fragments and corresponding figures. The first fragment and figure, Figure 2.15, show the use of a temporary variable instead of method composition. The second fragment and figure, Figure 2.16, use method composition and avoid a temporary variable. first = "Blue"; second = "Green"; third = "Red"; temp, last; 21

22 // not using method composition temp = first.concat(second); last = third.concat(temp); // last is "RedBlueGreen" first second (second) concat "Blue" "Green" temp third (temp) concat "BlueGreen" "Red" "RedBlueGreen" last Figure Memory diagram for fragment without method composition. All the variables should have the word before the variable name, but it is not shown due to limited space. first = "Blue"; second = "Green"; third = "Red"; last; // using method composition last = third.concat(first.concat(second)); // last is "RedBlueGreen" 22

23 first second (second) concat "Blue" "Green" third ( ) concat "BlueGreen" "Red" "RedBlueGreen" last Figure Memory diagram for fragment with method composition. All the variables should have the word before the variable name, but it is not shown due to limited space. To get the final string to be BlueGreenRed using method composition replace the above last statement by last = first.concat(second.concat(third)); // last is "BlueGreenRed" 23

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) In previous chapters, methods were called with parameters that were primitive data types. This chapter discusses how to use object

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

STUDENT LESSON A10 The String Class

STUDENT LESSON A10 The String Class STUDENT LESSON A10 The String Class Java Curriculum for AP Computer Science, Student Lesson A10 1 STUDENT LESSON A10 The String Class INTRODUCTION: Strings are needed in many programming tasks. Much of

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

Use of objects and variables

Use of objects and variables Unit 2 Use of objects and variables Summary Objects and classes The class String Method invocation Variables Assignment References to objects Creation of objects: invocation of constructors Immutable and

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG 1 Notice Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CPS122 Lecture: From Python to Java last revised January 4, Objectives: Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output Last revised January 12, 2006 Objectives: 1. To introduce arithmetic operators and expressions 2. To introduce variables

More information

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Java s String Class. in simplest form, just quoted text. used as parameters to. This is a string So is this hi 1 Java s String Class in simplest form, just quoted text "This is a string" "So is this" "hi" used as parameters to Text constructor System.out.println 2 The Empty String smallest possible string made

More information

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents Variables in a programming language Basic Computation (Savitch, Chapter 2) TOPICS Variables and Data Types Expressions and Operators Integers and Real Numbers Characters and Strings Input and Output Variables

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Week 2: Data and Output

Week 2: Data and Output CS 170 Java Programming 1 Week 2: Data and Output Learning to speak Java Types, Values and Variables Output Objects and Methods What s the Plan? Topic I: A little review IPO, hardware, software and Java

More information

Notes from the Boards Set BN19 Page

Notes from the Boards Set BN19 Page 1 The Class, String There are five programs in the class code folder Set17. The first one, String1 is discussed below. The folder StringInput shows simple string input from the keyboard. Processing is

More information

Using Java Classes Fall 2018 Margaret Reid-Miller

Using Java Classes Fall 2018 Margaret Reid-Miller Using Java Classes 15-121 Fall 2018 Margaret Reid-Miller Today Strings I/O (using Scanner) Loops, Conditionals, Scope Math Class (random) Fall 2018 15-121 (Reid-Miller) 2 The Math Class The Math class

More information

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as: 11. Arrays An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example,

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

2.8. Decision Making: Equality and Relational Operators

2.8. Decision Making: Equality and Relational Operators Page 1 of 6 [Page 56] 2.8. Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. This section introduces a simple version of Java's if statement

More information

Appendix: Common Errors

Appendix: Common Errors Appendix: Common Errors Appendix 439 This appendix offers a brief overview of common errors that occur in Processing, what those errors mean and why they occur. The language of error messages can often

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

STRINGS AND STRINGBUILDERS. Spring 2019

STRINGS AND STRINGBUILDERS. Spring 2019 STRINGS AND STRINGBUILDERS Spring 2019 STRING BASICS In Java, a string is an object. Three important pre-built classes used in string processing: the String class used to create and store immutable strings

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasketEnhanced { public static void main(string[]

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello

More information

What did we talk about last time? Examples switch statements

What did we talk about last time? Examples switch statements Week 4 - Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation

More information

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Creating

More information

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduce the Java programming

More information

Chapter 29B: More about Strings Bradley Kjell (Revised 06/12/2008)

Chapter 29B: More about Strings Bradley Kjell (Revised 06/12/2008) Chapter 29B: More about Strings Bradley Kjell (Revised 06/12/2008) String objects are frequently used in programs. This chapter provides extra practice in using them. Chapter Topics: Strings are Immutable

More information

CPS122 Lecture: From Python to Java

CPS122 Lecture: From Python to Java Objectives: CPS122 Lecture: From Python to Java last revised January 7, 2013 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

More information

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16 Intro to Strings Lecture 7 CGS 3416 Spring 2017 February 13, 2017 Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 1 / 16 Strings in Java In Java, a string is an object. It is not a primitive

More information

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017 Intro to Strings Lecture 7 COP 3252 Summer 2017 May 23, 2017 Strings in Java In Java, a string is an object. It is not a primitive type. The String class is used to create and store immutable strings.

More information

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015 More on Strings Lecture 10 CGS 3416 Fall 2015 October 13, 2015 What we know so far In Java, a string is an object. The String class is used to create and store immutable strings. Some String class methods

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming

More information

download instant at

download instant at 2 Introduction to Java Applications: Solutions What s in a name? That which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would

More information

Program Elements -- Introduction

Program Elements -- Introduction Program Elements -- Introduction We can now examine the core elements of programming Chapter 3 focuses on: data types variable declaration and use operators and expressions decisions and loops input and

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

Object Oriented Methods : Deeper Look Lecture Three

Object Oriented Methods : Deeper Look Lecture Three University of Babylon Collage of Computer Assistant Lecturer : Wadhah R. Baiee Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces,

More information

CS11 Java. Fall Lecture 1

CS11 Java. Fall Lecture 1 CS11 Java Fall 2006-2007 Lecture 1 Welcome! 8 Lectures Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7-8 Lab Assignments Made available on Mondays Due one week later Monday, 12 noon

More information

double float char In a method: final typename variablename = expression ;

double float char In a method: final typename variablename = expression ; Chapter 4 Fundamental Data Types The Plan For Today Return Chapter 3 Assignment/Exam Corrections Chapter 4 4.4: Arithmetic Operations and Mathematical Functions 4.5: Calling Static Methods 4.6: Strings

More information

SDKs - Eclipse. SENG 403, Tutorial 2

SDKs - Eclipse. SENG 403, Tutorial 2 SDKs - SENG 403, Tutorial 2 AGENDA - SDK Basics - - How to create Project - How to create a Class - Run Program - Debug Program SDK Basics Software Development Kit is a set of software development tools

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

Lecture 7: Classes and Objects CS2301

Lecture 7: Classes and Objects CS2301 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

2: Basics of Java Programming

2: Basics of Java Programming 2: Basics of Java Programming CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1 topics: introduction to java, part 1 cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 cis20.1-fall2007-sklar-leci.2 1 Java. Java is an object-oriented language: it is

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Walter Savitch Frank M. Carrano Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers

More information

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Table of Contents Date(s) Title/Topic Page #s. Abstraction Table of Contents Date(s) Title/Topic Page #s 9/10 2.2 String Literals, 2.3 Variables and Assignment 34-35 Abstraction An abstraction hides (or suppresses) the right details at the right time An object

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming s SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177 Programming Time allowed: THREE hours: Answer: ALL questions Items permitted: Items supplied: There is

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

BM214E Object Oriented Programming Lecture 8

BM214E Object Oriented Programming Lecture 8 BM214E Object Oriented Programming Lecture 8 Instance vs. Class Declarations Instance vs. Class Declarations Don t be fooled. Just because a variable might be declared as a field within a class that does

More information

COMP-202 Unit 5: Basics of Using Objects

COMP-202 Unit 5: Basics of Using Objects COMP-202 Unit 5: Basics of Using Objects CONTENTS: Concepts: Classes, Objects, and Methods Creating and Using Objects Introduction to Basic Java Classes (String, Random, Scanner, Math...) Introduction

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

String. Other languages that implement strings as character arrays

String. Other languages that implement strings as character arrays 1. length() 2. tostring() 3. charat() 4. getchars() 5. getbytes() 6. tochararray() 7. equals() 8. equalsignorecase() 9. regionmatches() 10. startswith() 11. endswith() 12. compareto() 13. indexof() 14.

More information

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4 Assignments Lecture 6 Complete for Project 1 Reading: 3.1, 3.2, 3.3, 3.4 Summary Program Parts Summary - Variables Class Header (class name matches the file name prefix) Class Body Because this is a program,

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x ); Chapter 5 Methods Sections Pages Review Questions Programming Exercises 5.1 5.11 142 166 1 18 2 22 (evens), 30 Method Example 1. This is of a main() method using a another method, f. public class FirstMethod

More information

Mobile App:IT. Methods & Classes

Mobile App:IT. Methods & Classes Mobile App:IT Methods & Classes WHAT IS A METHOD? - A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. -

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13 CHAPTER 2 Define a method vs. calling a method Line 3 defines a method called main Line 5 calls a method called println, which is defined in the Java library You will learn later how to define your own

More information

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic BIT 3383 Java Programming Sem 1 Session 2011/12 Chapter 2 JAVA basic Objective: After this lesson, you should be able to: declare, initialize and use variables according to Java programming language guidelines

More information

The compiler is spewing error messages.

The compiler is spewing error messages. Appendix B Debugging There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly. Compile-time errors are

More information

Using System.out.println()

Using System.out.println() Programming Assignments Read instructions carefully Many deduction on Program 3 for items in instructions Comment your code Coding conventions 20% of program grade going forward Class #23: Characters,

More information

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs are divided into classes 7 Class: public class 8 Class:

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017 Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017 1 1. Please obtain a copy of Introduction to Java Programming, 11th (or 10th) Edition, Brief

More information

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Strings, Strings and characters, String class methods. JAVA Standard Edition

Strings, Strings and characters, String class methods. JAVA Standard Edition Strings, Strings and characters, String class methods JAVA Standard Edition Java - Character Class Normally, when we work with characters, we use primitive data types char. char ch = 'a'; // Unicode for

More information

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2 Classes & Objects Topics Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2 Object-Oriented Programming Classes combine data and the methods (code) to manipulate the

More information

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1 A First Look At Java Didactic Module 13 Programming Languages - EEL670 1 Outline Thinking about objects Simple expressions and statements Class definitions About references and pointers Getting started

More information

Section 2: Introduction to Java. Historical note

Section 2: Introduction to Java. Historical note The only way to learn a new programming language is by writing programs in it. - B. Kernighan & D. Ritchie Section 2: Introduction to Java Objectives: Data Types Characters and Strings Operators and Precedence

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Chapter 8: Strings and Things

Chapter 8: Strings and Things Chapter 8: Strings and Things Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey Word Of Fortune Program this will be cleared when it's working 1-2 Chapter Topics Chapter 8 discusses

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 03 / 2015 Instructor: Michael Eckmann Today s Topics Finish up discussion of projected homeruns 162 as a constant (final) double vs. int in calculation Scanner

More information

CS 101 Fall 2006 Midterm 1 Name: ID:

CS 101 Fall 2006 Midterm 1 Name:  ID: You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure

More information