CS305j Introduction to Computing Using Objects, Interactive Programs, Loop Techniques
1
Topic 9 Using Objects, Interactive Programs and Loop Techniques
"There are only two kinds of programming
languages: those people always [complain] 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 Computing Using Objects, Interactive Programs, Loop Techniques
2
Objects and Classes
CS305j Introduction to Computing Using Objects, Interactive Programs, Loop Techniques
3
Objects
So far, we have seen:
– methods, which represent behavior – variables, which represent data – types, which represent categories of data
In Java and other "object-oriented" programming languages, it is possible to create new types that 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.
- There are methods inside the object, representing its behavior.
Today, we will learn how to communicate with certain objects that exist in Java.
CS305j Introduction to Computing Using Objects, Interactive Programs, Loop Techniques
4
Constructing objects
construct: To create a new object.
– Objects are constructed with the new keyword. – Most objects other than Strings must be constructed before they can be used.
Constructing objects, general syntax:
<type> <name> = new <type> ( <parameters> );
– Examples:
BigInteger rhs = new BigInteger("123456123456"); Color orange = new Color(255, 128, 0); Point origin = new Point(0, 0); Polygon poly = new Polygon();