Using Objects 21-Nov-18.

Slides:



Advertisements
Similar presentations
13-Jun-14 OOP features of Jeroo. Overview In this presentation we will discuss these topics: OOP terminology Jeroo syntax constructors methods.
Advertisements

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 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
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.
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.
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,
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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.
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.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
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.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
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.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Just Enough Java 29-Sep-17.
Week 2 - Wednesday CS 121.
Chapter 2 Basic Computation
Chapter 3 Syntax, Errors, and Debugging
COMP 170 – Introduction to Object Oriented Programming
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Introduction to Computer Science / Procedural – 67130
Classes and Objects in Java
Variables and Primative Types
Variables, Expressions, and IO
Variables, Printing and if-statements
Introduction to Objects
OOP features of Jeroo 8-Nov-18.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
String Objects & its Methods
Lesson 2: Building Blocks of Programming
Class Structure 16-Nov-18.
Type Conversion, Constants, and the String Object
INPUT & OUTPUT scanf & printf.
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Introduction to Primitive Data types
Class Structure 2-Jan-19.
Classes and Objects in Java
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Class Structure 25-Feb-19.
CLASSES, AND OBJECTS A FIRST LOOK
Chapter 2 Programming Basics.
Overview of Java 6-Apr-19.
Introduction to Primitives
Introduction to Primitives
Unit 3: Variables in Java
Output Manipulation.
IAT 800 Foundations of Computational Art and Design
Just Enough Java 17-May-19.
Classes and Objects in Java
OOP features of Jeroo 3-Jul-19.
Introduction to Primitive Data types
String Objects & its Methods
Introduction to Objects
Presentation transcript:

Using Objects 21-Nov-18

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

Classes and objects A class is the type of an object Just as a variable classSize 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

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;

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

Combining declaration and assignment Declaration and assignment can be combined into a single statement: int classSize = 57; 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 This rule is “true enough” for now Exceptions to this rule are complicated and left for later

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

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

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

Messages and methods Messages can be used to: Tell an object some information Ask an object for information (usually about itself) Tell 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

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

Messages to a Color Once you make a Color, you cannot change it; you can only ask it for information // 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(); The last method doesn’t change the color; it makes a new color

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, Strings are just objects

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

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, that thing is first turned into a String 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

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

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 We can “talk to” (send messages to) this mysterious object without knowing very much about it

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

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

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.