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

Slides:



Advertisements
Similar presentations
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
JavaScript, Third Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2 types, variables, methods Pages Horstmann.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Basic Elements of C++ Chapter 2.
Computer Science 101 Introduction to Programming.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 1 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Classes CS 21a: Introduction to Computing I First Semester,
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
Working With Objects Tonga Institute of Higher Education.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables.
Chapter 2 Basic Computation
BASIC ELEMENTS OF A COMPUTER PROGRAM
Console Output, Variables, Literals, and Introduction to Type
Chapter 3: Using Methods, Classes, and Objects
IDENTIFIERS CSC 111.
Chapter 2 Using Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to C++ Programming
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Classes CS 21a: Introduction to Computing I
© A+ Computer Science - Variables & Data Types © A+ Computer Science - ©A+ Computer Science
Chapter 2 Variables.
Use a variable to store a value that you want to use at a later time
Variables in C Topics Naming Variables Declaring Variables
Each object belongs to a class
Parameter: an input to a method
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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

Week 2 Topics Types and Variables The Assignment Operator Objects, Classes, and Methods Method Parameters and Return Values Number Types

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;

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!

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.

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

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

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.

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;

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

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

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

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)

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

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

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

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)

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

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());

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”

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

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()

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

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

2.1.5 Number Types Cont. Do not use commas when writing numbers in Java. For example, 13,000 must be written as 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

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

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