Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Similar presentations


Presentation on theme: "Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."— Presentation transcript:

1 Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

2 Week 2 Topics 2.1.1 Types and Variables 2.1.2 The Assignment Operator 2.1.3 Objects, Classes, and Methods 2.1.4 Method Parameters and Return Values 2.1.5 Number Types

3 2.1.1 Types and Variables In Java, every value has a type Example: “Hello World” has the type String, the object System.out has the type PrintStream, and the number 13 has the type int (integer) Use variables to store values that you want to use at a later time int luckyNbr = 13;

4 2.1.1 Types and Variables Cont. System.out.println(luckyNbr); Above is the same as System.out.println(13); A variable is a storage location in the computer’s memory that has a type, a name, and a contents The value must match the type: String greeting = 13; // NO!

5 2.1.1 Types and Variables Cont. An identifier is the name of a variable, method or class, and the following are firm rules that Java enforces: Identifiers can be made up of letters, digits and the underscore character, but cannot start with a digit. 1greeting is not legal. You cannot use other symbols such as ? or %. hello! is not legal.

6 2.1.1 Types and Variables Cont. Spaces are not permitted inside identifiers. lucky Nbr is not a legal identifier. You cannot use Java reserved words such as public Identifiers are case sensitive, thus, greeting, GREETING and Greeting are different

7 2.1.1 Types and Variables Cont. There are some conventions that you should follow also (even though will compile if you violate): Variable and method names should start with a lowercase letter, such as luckyNbr Class names should start with an Uppercase letter. Examples: Employee, Point, PrintQueue

8 2.1.1 Types and Variables Cont. When deciding on a name for a variable, you should make a choice that describes the purpose of the variable. For example, the variable name greeting is a better identifier than the name g.

9 2.1.2 The Assignment Operator Use the assignment operator (=) to initialize the value or change the value of a variable int luckyNbr = 13; To change the value of the variable, simply assign the new value: luckyNbr = 12;

10 2.1.2 The Assignment Operator Cont. It is an error to use a variable that is not initialized int luckyNbr; System.out.println(luckyNbr); // NO! We will cover this topic later on in the course, but note that the assignment operator is NOT used to test for equality

11 2.1.3 Objects, Classes, and Methods A class is a model of an entity needed to address a problem Examples: Employee, Encryptor, PrintQueue, Point, PurchaseOrder A class defines the attributes (data) and the actions (methods) of the entity The attributes (data) of a class are nouns: firstName, message, xCoordinate

12 2.1.3 Objects, Classes, and Methods Cont. The actions (methods) of a class are verbs: setName, getXcoord, encryptMessage Methods are routines (called functions in some languages) that contain logic A method is a sequence of instructions that accesses the data of an instance of a class

13 2.1.3 Objects, Classes, and Methods Cont. An object is a specific instance of a class Think of a class as a mold, and an object as the manufactured widget A method may or may not receive input The format for calling a method is: objectVariable.methodName(inputs)

14 2.1.3 Objects, Classes, and Methods Cont. String greeting = “Hello World!”; The class is type String, the object is pointed to by the variable greeting int n = greeting.length(); length is a method defined by the String class The value of n is 12

15 2.1.3 Objects, Classes, and Methods Cont. Note that the method is called on the object variable: greeting.length() The String class length method does not receive input System.out.println(“Hello World!”); The println (print line) method does receive input

16 2.1.3 Objects, Classes, and Methods Cont. String river = “Mississippi”; String bigRiver = river.toUpperCase(); bigRiver will contain value “MISSISSIPPI” System.out.length(); // NO! The PrintStream class (which System.out belongs to) does not contain a length method

17 2.1.3 Objects, Classes, and Methods Cont. Every object belongs to a class The class defines the methods for an object The methods form the public interface for the class A class also defines a private implementation, describing the data and the instructions in the methods, these are hidden from the programmer (black box)

18 2.1.4 Methods Parameters and Return Values A parameter is an input to a method System.out.println(greeting); The string greeting is a parameter to the println method The implicit parameter of a method call is the object on which the method is invoked The System.out PrintStream object is an implicit parameter of println

19 2.1.4 Methods Parameters and Return Values Cont. The return value of a method is a result that the method has computed for use by the code that called it int n = greetings.length(); You can also use the return value as a parameter of another method System.out.println(greeting.len gth());

20 2.1.4 Methods Parameters and Return Values Cont. Not all methods return a value, such as println String newRiver = river.replace(“issipp”, “our”); The above method call has one implicit parameter (“Mississippi”, the value of river), two explicit parameters, and the return value “Missouri”

21 2.1.4 Methods Parameters and Return Values Cont. When a method is defined in a class, the definition specifies the types of the explicit parameters and the return value public int length() public String replace(String target, String replacement) A method name is overloaded if a class has more than one method with the same name but different parameter types

22 2.1.4 Methods Parameters and Return Values Cont. public void println(String output) public void println(int output) public void println(double output) public void println()

23 2.1.5 Number Types Java has separate types for integers and floating-point numbers When a floating-point number is divided or multiplied by 10, only the position of the decimal point changes; it “floats” Use the type double to represent a floating-point number such as 1.3 or -0.33333

24 2.1.5 Number Types Cont. Use an integer (int) for quantities that can never have a fractional part Advantage of an int is that they take less storage space, are processed faster, and don’t cause rounding errors There are several other number types (less commonly used than int or double) that we cover in Week 5

25 2.1.5 Number Types Cont. Do not use commas when writing numbers in Java. For example, 13,000 must be written as 13000 To write numbers in exponential format, use En. For example 1.3 x 10 to the -4 power (.00013) is written as 1.3E-4. In Java numbers are not objects and number types are not classes Number types are called primitive types

26 2.1.5 Number Types Cont. You can combine numbers with operators such as + or - To multiple two numbers use the * operator, to divide use / * and / have higher precedence than + or – If x = 2 and y = 3, then x + y * 2 = 8 Use parentheses to change the precedence: (x + y) * 2 = 10

27 Reference: Big Java 2 nd Edition by Cay Horstmann 2.1.1 Types and Variables (section 2.1 in Big Java) 2.1.2 The Assignment Operator (section 2.2 in Big Java) 2.1.3 Objects, Classes, and Methods (section 2.3 in Big Java) 2.1.4 Method Parameters and Return Values (section 2.4 in Big Java) 2.1.5 Number Types (section 2.5 in Big Java)


Download ppt "Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."

Similar presentations


Ads by Google