topic 9 using objects interactive programs and loop
play

Topic 9 Using Objects, Interactive Programs and Loop Techniques - PowerPoint PPT Presentation

Topic 9 Using Objects, Interactive Programs and Loop Techniques " There are only two kinds of programming languages: those people always [complain] Objects and Classes about and those nobody uses." Bjarne Stroustroup, creator of


  1. Topic 9 Using Objects, Interactive Programs and Loop Techniques " There are only two kinds of programming languages: those people always [complain] Objects and Classes about and those nobody uses." — Bjarne Stroustroup, creator of C++ Based on slides for Building Java Programs by Reges/Stepp, found at http://faculty.washington.edu/stepp/book/ CS305j Introduction to Using Objects, Interactive Programs, Loop CS305j Introduction to Using Objects, Interactive Programs, Loop 1 2 Computing Techniques Computing Techniques Objects Constructing objects � So far, we have seen: � construct : To create a new object. – methods , which represent behavior – Objects are constructed with the new keyword. – variables , which represent data – Most objects other than Strings must be – types , which represent categories of data constructed before they can be used. � In Java and other "object-oriented" programming � Constructing objects, general syntax: languages, it is possible to create new types that <type> <name> = new <type> ( <parameters> are combinations of the existing primitive types. ); – Such types are called object types or reference types . – An object is an entity that contains data and behavior. • There are variables inside the object, storing its data. – Examples: • There are methods inside the object, representing its behavior. BigInteger rhs = new BigInteger("123456123456"); Color orange = new Color(255, 128, 0); � Today, we will learn how to communicate with Point origin = new Point(0, 0); Polygon poly = new Polygon(); certain objects that exist in Java. CS305j Introduction to Using Objects, Interactive Programs, Loop 3 CS305j Introduction to Using Objects, Interactive Programs, Loop 4 Computing Techniques Computing Techniques

  2. Reminder: primitive variables Reference variables � We now need to examine some important differences � However, objects behave differently than primitives. between the behavior of objects and primitive values. – When working with objects, we have to understand the distinction between an object, and the variable that stores it. � We saw with primitive variables that modifying the value of – Variables of object types are called reference variables . – Reference variables do not actually store an object; they store the one variable does not modify the value of another. address of an object's location in the computer memory. � When one variable is assigned to another, the value is – If two reference variables are assigned to refer to the same object, copied. the object is not copied; both variables literally share the same object. Calling a method on either variable will modify the same – Example: object. int x = 5; int y = x ; // x = 5, y = 5 y = 17; // x = 5, y = 17 – Example: x = 8; // x = 8, y = 17 Point p1 = new Point(10, 20); // x and y coords. Point p2 = p1; // does not create a new Point p2.move(5, 10)' // new x and y coordinates System.out.println( "x: " + p1.getX() + ", y: " + p1.getY() ); CS305j Introduction to Using Objects, Interactive Programs, Loop CS305j Introduction to Using Objects, Interactive Programs, Loop 5 6 Computing Techniques Computing Techniques Modifying parameters Objects as parameters � When an object is passed as a parameter, it is not copied. � When we call a method and pass primitive variables' values It is shared between the original variable and the method's as parameters, it is legal to assign new values to the parameter. parameters inside the method. – If a method is called on the parameter, it will affect the original object – But this does not affect the value of the variable that was passed, that was passed to the method. because its value was copied. – Example: public static void main(String[] args) { – Example: Point p1 = new Point(5, 10); System.out.println( p1.toString() ); public static void main(String[] args) { foo(p); int x = 1; System.out.println( p1.toString() ); foo( x ); } System.out.println(x); // output: 1 } value 1 is copied into parameter public static void foo(Point p) { System.out.println( p.toString() ); public static void foo(int x) { p.move(1, 2); x = 2; System.out.println( p.toString() ); } parameter's value is changed to 2 } (variable x in main is unaffected) CS305j Introduction to Using Objects, Interactive Programs, Loop 7 CS305j Introduction to Using Objects, Interactive Programs, Loop 8 Computing Techniques Computing Techniques

  3. Strings Indexes � One of the most common types of objects in Java is type � The characters in a String are each internally numbered with String. an index , starting with 0 for the first character: – String : A sequence of text characters. – Example: String name = "M. Scott"; – Object data types' names are usually uppercase (String), unlike primitives (int). 0 1 2 3 4 5 6 7 name --> � String variables can be declared and assigned, just like 'M' '.' ' ' 'S' 'c' 'o' 't' 't' primitive values: – String <name> = " <text> "; – String <name> = <expression that produces a String> ; � Individual text characters are represented by a primitive type – Examples: String name = "Tom Danielson"; called char . Literal char values are surrounded with apostrophe (single-quote) marks, such as 'a' or '4' . int x = 3, y = 5; – An escape sequence can be represented as a char , such as '\n' String point = "(" + x + ", " + y + ")"; (new-line character) or '\'' (apostrophe). CS305j Introduction to Using Objects, Interactive Programs, Loop CS305j Introduction to Using Objects, Interactive Programs, Loop 9 10 Computing Techniques Computing Techniques Calling methods of Strings String methods � Strings are objects that contain methods. � Here are several of the most useful String – A String contains code inside it that can manipulate or process the methods: String in several useful ways. – When we call a method of a String, we don't just write the method's Method name Description name. We also have to write which String we want to execute the method. The results will be different from one String to another. charAt( index ) character at a specific index � Calling a method of an object, general syntax: indexOf( String ) index where the start of the given String <name> . <methodName> ( <parameters> ) appears in this String (-1 if it is not there) length() number of characters in this String – Examples: String name = "Mike"; substring( index1 , index2 ) the characters from index1 to just before System.out.println( name.toUpperCase() ); // MIKE index2 String name2 = "Mike Scott"; toLowerCase() a new String with all lowercase letters System.out.println( name2.length() ); // 10 toUpperCase() a new String with all uppercase letters CS305j Introduction to Using Objects, Interactive Programs, Loop 11 CS305j Introduction to Using Objects, Interactive Programs, Loop 12 Computing Techniques Computing Techniques

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend