10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

13-Jun-14 OOP features of Jeroo. Overview In this presentation we will discuss these topics: OOP terminology Jeroo syntax constructors methods.
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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Using Objects. Overview In this presentation we will discuss: –Classes and objects –Methods for objects –Printing results.
Introduction to Programming with Java, for Beginners
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Classes and Objects in Java
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Unit 3: Java Data Types Math class and String class.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Classes and Objects in Java
Primitive Data Types. Identifiers What word does it sound like?
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
LCC 6310 Computation as an Expressive Medium Lecture 2.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
The Basics Input / Output, and Primitive Data Types.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Programs and Models Almost all computer programs model some artifact – Artifact: product of civilization, either concrete or abstract, simple or complex.
Chapter 2 Basic Computation
Chapter 3 Syntax, Errors, and Debugging
Classes and Objects in Java
Introduction to Objects
OOP features of Jeroo 8-Nov-18.
Using Objects 21-Nov-18.
Introduction to Primitive Data types
Overview of Java 6-Apr-19.
Introduction to Primitives
Introduction to Primitives
Output Manipulation.
IAT 800 Foundations of Computational Art and Design
Just Enough Java 17-May-19.
LCC 6310 Computation as an Expressive Medium
OOP features of Jeroo 3-Jul-19.
Introduction to Primitive Data types
String Objects & its Methods
Introduction to Objects
Presentation transcript:

10-Jun-15 Using Objects

2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results

3 Classes and objects A class is the type of an object Just as a variable counter may have type int, Color.red has type Color Just as 5 is a literal of type int, "Hello" is a literal of type String There are exactly eight primitive types There are thousands of classes, and you can create more

4 Declarations You declare variables to hold primitive values like this: int classSize; double area; You declare variables to hold objects like this: Color uglyBrown; String myName;

5 Assignment statements An assignment statement has the form: variable = expression ; Examples: classSize = 40; area = pi * radius * radius; uglyBrown = new Color(175, 175, 30); myName = "David Matuszek";

6 Combining declaration and assignment Declaration and assignment can be combined into a single statement: int classSize = 40; String myName = "David Matuszek"; Color uglyBrown = new Color(175, 175, 30); You can only declare a variable once, but you can assign to it many times in many places A variable declared inside a method is declared only for that one method Hence, a method can declare a variable with the same name as a variable in another method—but it’s a different variable

7 Methods Primitives have operations, classes have methods You cannot define new primitives, but you can define new classes You cannot define new operations, but you can define new methods Here we will talk about using methods supplied by Java, not defining new ones

8 Data in classes and objects A class is the type of an object A class describes: How to make a new object of that class Example: new Color(175, 175, 30); What kind of data is in an object Example: a Color object contains three numbers representing the amount of red, green, and blue The methods of an object (the actions it can perform) Example: a Color object can tell you how much red it contains: int amount = myColor.getRed();

9 Sending messages to objects We don’t perform operations on objects, we “talk” to them This is called sending a message to the object A message looks like this: object. method ( extra information ) The object is the thing we are talking to The method is a name of the action we want the object to take The extra information is anything required by the method in order to do its job Examples: g.setColor(Color.pink); amountOfRed = Color.pink.getRed( );

10 Messages and methods Messages can be used to: Tell an object some information Ask an object for information (usually about itself) Request an object to do something Any and all combinations of the above A method is something inside the object that responds to your messages A message contains commands to do something Java contains thousands of classes, each typically containing dozens of methods When you program you use these classes and methods, and also define your own classes and methods

11 Messages to a Graphics If you have a Graphics, and its name is g, here are some things you can do with g: Tell it to use a particular color: g.setColor(Color.orange); Ask it what color it is using: Color currentColor = g.getColor(); Tell it to draw a line: g.drawLine(14, 23, 87, 5);

12 Messages to a Color Once you make a Color, you cannot change it; you can only ask it for information The last method doesn’t change the color; it makes a new color // Make a new purplish color Color myColor = new Color(100, 0, 255); // Ask how much blue is in it int amountOfBlue = myColor.getBlue(); // Ask the color for a brighter version of itself Color brightColor = myColor.brighter();

13 String A String is an object, but......because Strings are used so much, Java gives them some special syntax There are String literals: "This is a String" (Almost) no other objects have literals There is an operation, concatenation, on Strings: "Dave" + "Matuszek" gives "DaveMatuszek" In other respects, String s are just objects

14 String methods A String, like a Color, is immutable Once you create it, there are no methods to change it But you can easily make new Strings: myName = "Dave"; myName = "Dr. " + myName; This is kind of a subtle point; it will be important later, but you don’t need to understand it right away If s is the name of the string "Hello", then s.length() tells you the number of characters in String s (returns 5 ) s.toUpperCase() returns the new String "HELLO" ( s itself is not changed) But you can say s = s.toUpperCase();

15 String concatenation + usually means “add,” but if either operand (thing involved in the operation) is a String, then + means concatenation If you concatenate anything with a String, you are actually concatenating a “string version” of that thing For example, you can concatenate a String and a number: System.out.println("The price is $" + price);  + as the concatenation operator is an exception to the rule: Primitives have operations, Objects have methods

16 Data in classes A class describes objects. It describes: How to construct an object of that class, the kind of data in an object, and the messages that the object can understand A class can also contain its own data, which is the same for any object of that class Constants are often provided this way Examples: class Color contains the constant Color.RED class Math contains the constant Math.PI

17 Printing out results, part 1 In Java, “print” really means “display on the screen” Actually printing on paper is much harder! System is one of Java’s built-in classes System.out is a data object in the System class that knows how to “print” to your screen Because print is a method of the System.out object We can “talk to” (send messages to) this mysterious object without knowing very much about it

18 Printing out results, part 2 System.out is a object with useful methods that will let you print anything: print( x ) turns x into a String and displays it println( x ) (pronounced “print line”) turns x into a String and displays it, then goes to the next line Examples: System.out.print("The sum of x and y is "); System.out.println(x + y);

19 New vocabulary class: the type, or description, of an object object: an instance, or member, of a class message: something that you “say” to a class, either telling it something or asking it for information immutable: cannot be changed after it is created operand: one of the inputs to an operation

20 The End “If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.” --Anon.