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.

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.
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 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.
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.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Classes and Objects in Java
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
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,
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
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.
Unit 3: Java Data Types Math class and String class.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Classes and Objects in Java
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Computer Programming
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Working With Objects Tonga Institute of Higher Education.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
SEEM Java – Basic Introduction, Classes and Objects.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
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.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
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.
Chapter 3 Syntax, Errors, and Debugging
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Classes and Objects in Java
Introduction to Objects
OOP features of Jeroo 8-Nov-18.
Class Structure 16-Nov-18.
Using Objects 21-Nov-18.
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Classes and Objects in Java
Class Structure 25-Feb-19.
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Introduction to Primitives
Introduction to Primitives
Java Programming Language
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.
Classes and Methods 15-Aug-19.
String Objects & its Methods
Introduction to Objects
Presentation transcript:

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 int If we say String name, the type of name is String If we say Color background, the type of background is Color A variable can only hold values of the appropriate type count = 5; name = "Dave"; background = Color.RED; A literal is when we write an actual value, not a variable 5 is a literal of type int "Dave" is a literal of type String There aren’t any Color literals There are exactly eight primitive types There are thousands of classes, and you can create more

3 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; In both cases, the syntax is: Type name ;

4 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"; Assignment to object variables is just like assignment to primitive variables

5 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

6 Methods Primitives have operations, classes have methods One exception: Strings have the + operation The syntax for operations is built into Java 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

7 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); In this example the keyword new says we are using a constructor, which is similar to a method 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();

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

9 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

10 Different objects, different methods When we send a message to an object, that object must have a method that “knows” how to respond to the message The method must have the correct name and the same number and corresponding types of parameters Different objects may have methods with the same name and same number and types of parameters We could send the same message to two different objects, and (if those objects are of different classes) they might execute different methods If we do this, the methods should do similar things

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.