380 likes | 397 Views
Objects, Types, Methods The whole 9 yards. Prof. Ankur Teredesai “amt@cs.rit.edu”. Defining a Class. Let’s look at our car class
E N D
Objects, Types, MethodsThe whole 9 yards Prof. Ankur Teredesai “amt@cs.rit.edu”
Defining a Class • Let’s look at our car class • Properties are variables which describe the essential characteristics of an object. For a car they are: color, model, make, how many doors, transmission type, direction of movement, etc. • Behaviours are methods that describe how the object behaves and how the behaviours may be modified. For a car they are: braking, changing gears, opening doors, painting the car, moving forwards or backwards, etc.
Properties or Instance Variables Defining a Class Behaviour or Method 2 Behaviour or Method 1 Behaviour or Method 3 Behaviour or Method 4
Parameter Values • The class definition will include parameter definitions (properties) that represent data about a particular object. • For instance the fact that Jessica's car has 4 gallons of gas in it while Clint's car has 10 gallons of gas. • The amount of gas in each car will change without affecting the amount of gas in the other cars. • These are called instance variables. • All instances (objects) of a class will have a set of instance variables that are specific to the individual object.
Parameter Instance Values Car GasLevel John’s Lamborghini Austin’s Porsche Joe’s BMW Car Car Car GasLevel = 9 GasLevel = 4 GasLevel = 10
Parameter Values • The class definitions may also include parameter definitions that represent data that is shared by all class instances (objects). • In the case of a the car class we will define a maximum gas level that represents the amount of gas the gas tanks can hold. This will be the same for each individual car. • These types of parameters are called class variables.
Class Parameter Values Car GasLevel MaxGasLevel = 18 John's Lamborghini Clint's Porsche Jessica's BMW Car Car Car GasLevel = 9 GasLevel = 4 GasLevel = 10
Parameter Values • Class variables may also be used to keep track of things like how many instances of a class exist. • For our car example, lets create a counter the records how many cars we have built.
Class Parameter Values Car GasLevel MaxGasLevel = 18 NumCars = 3 John's Lamborghini Clint's Porsche Jessica's BMW Car Car Car GasLevel = 9 GasLevel = 4 GasLevel = 10
Constant Parameters • It is possible that you will have a parameter whose value should not change while your program is running. Such a parameter is called a constant. • The MaxGasLevel parameter that we defined for the Car class is a constant. The amount of gas the gas tank can hold will not change once the car has been built.
Messages • Objects communicate much like humans do, they pass messages back and forth to one another. • Each message has three components: • The object to whom the message is addressed. • The name of the method to perform in that object. • Any parameters (variables) needed by that method.
Messages • When we "send a message" directly to another person our message will include: • An indication of who the message is intended for. • What task we want the person to do. • What the information is that they need to complete the task.
Messages • In object-oriented programming we must explicitly state to what object we are sending a message (the object name), what task (method) we want the object to complete, and any required information (parameters). • For instance, we will define a task to turn on the car's hazards in the Car class. If we want to tell Jessica to turn on her BMW's hazards we would say something like. • Jessica's BMW.turnOnHazards()
Messages Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() John's Lamborghini Clint's Porsche Jessica's BMW Car Car Car GasLevel = 9 turnOnHazards() GasLevel = 4 turnOnHazards() GasLevel = 10 turnOnHazards()
Messages and Methods • In order to process a message an object needs to have a method defined for the requested task. • For instance, we have to define a method to turn on the car's hazards. • A method is a small well defined piece of code that completes a specific task.
Instance Methods • Each class can have methods that are specific to each object. • This means that when the method is executed, it only affects the instance variables of the object. • If Jessica's BMW has 4 gallons of gas and she puts 6 more gallons of gas in her car, she now has 10 gallons. The amount of gas in Clint's Porsche and John's Lamborghini does not change because Jessica put gas in her BMW. • Methods that only affect the object's parameters are called instance methods.
Methods Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) John's Lamborghini Clint's Porsche Jessica's BMW Car Car Car GasLevel = 9 turnOnHazards() addGas(amount) GasLevel = 10 turnOnHazards() addGas(amount) GasLevel = 10 turnOnHazards() addGas(amount)
Methods • It is also possible that you want information about an object, in this case you would define a method that sends (returns) a message back containing the information. • We need to know how much gas is in our cars, so we will create a new method that returns the value of GasLevel for our car.
Methods Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel John's Lamborghini Clint's Porsche Jessica's BMW Car Car Car GasLevel = 9 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 4 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 10 turnOnHazards() addGas(amount) getGasLevel():GasLevel
Class Methods • Class methods are used to get or manipulate information about all objects created from the class. • Typically, class methods are changing class variables. • Every time we build a new car we need to add one more to the number of cars we have built. • Also, we may want to know how many cars total we have built.
Methods Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() John's Lamborghini Clint's Porsche Jessica's BMW Car Car Car GasLevel = 9 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 4 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 10 turnOnHazards() addGas(amount) getGasLevel():GasLevel
Object-Oriented Programming • When writing object-oriented programs, first one must define the classes (like car). • Then while the program in running the instances of the classes (or objects) (like Jessica's BMW, Clint's Porsche, and John's Lamborghini) are created.
Object-Oriented Programming • Object-oriented programming allows the programmer to hide the implementation details from the other objects and the users. • In other words the implementation is transparent to the other objects or the user. • Examples: Word, Excel, Web pages.
Object-Oriented Programming • Benefits of Object-oriented programming: • An object can be written and maintained separately from the rest of the program, modularity. • An object has a public face that it uses to communicate with other objects but the other objects can not directly access the instance variables, Information Hiding.
Inheritance • All classes in Java are organized into a class hierarchy. • The highest level classes are very general and the lower level classes are more specific. • The lower level classes are based upon the higher level classes and inherit the higher level class instance variables and methods. They also contain their own instance variables and methods beyond the higher level class definition.
Inheritance • The higher level classes are called Superclasses and the new or lower level classes are called the subclasses. • A subclass can always become a superclass • Inheritance allows you to define certain behaviors once and then to reuse those behaviors over and over again in the subclasses. This is called reusability.
Inheritance • Our Car class is very general. • Let's define a new class called BMW that contains the parameters: model, color, engine size.
Inheritance Car BMW Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars()
Inheritance • Now let's define two new classes. One for the Z3 and another for the 3 Series Sedan. • What would be some of the differences between the two classes?
Inheritance Car BMW GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel Z3 3 Series Sedan Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel Model Color EngineSize NumDoors GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel
Java has four basic data types: integer numbers 10, 6785943, -8 real numbers 5.564, 8976.5687 boolean true, false characters (Unicode) a, &, z, ? Primitive data types are basic data such as numbers, characters, and Boolean values. Primitive Data Types
Primitive Data Types • The Java primitive data types each have a set size (bits) and value range. • This size and range are the same on every type of computer (PC, Mac, Sun workstation) that runs Java programs. • This is not true with other programming languages
Integer data types: long uses 64 bits and represents the numbers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 When typing long integer constants into a program the number must have an L at the end of the number. 99999999999L Integer data types: byte uses 8 bits and represents the numbers -128 to 127 short uses 16 bits and represents the numbers -32,768 to 32,768 int uses 32 bits and represents the numbers -2,147,483,648 to 2,147,483,647 usually the type you will use Integer
Real or floating point numbers are represented in scientific notation inside a computer. The number is divided up into two parts the mantissa and the exponent. The mantissa contains a number between -1.0 and 1.0 The exponent contains the power of 2 required to scale the number to its actual value. Value = mantissa * 2exponent Real numbers are characterized by their precision and range. Precision represents the number of significant digits that is represented in the number. Precision depends upon the number of bits in the mantissa. Range represents the difference between the largest and smallest numbers that can be represented. Range depends upon the number of bits in the exponent Real
Real data types: float uses 32 bits and represents the numbers -3.40292347E+38 to 3.40292347E+38 The mantissa is 24 bits and the exponent is 8 bits. The float type permits 6 to 7 decimal digits of precision. Real data types: double uses 64 bits and represents the numbers -1.79769313486231570E+308 to 1.79769313486231570E+308. The mantissa is 53 bits and the exponent is 11 bits The double type permits 15 to 16 decimal digits of precision. The double data type is more precise than float Real
Real Values • Round off Error • When a double is stored to a float, part of the number is lost because a double is much larger than a float. The float can not represent the same exact number. In this situation the seven most significant digits are preserved and the rest of the information is lost. • It is important to know which real data type is required for your calculation.
Character Values: may represent both printable and non-printable characters some printable characters are: a, % some non-printable characters are: newline - \n, tab - \t, carriage return - \r What is unique about these characters? Character data types: uses 16 bits and represents the characters \u0000 to \uFFFF Unicode character set which represents almost every modern language Char
Boolean • Boolean data types • uses 1 bit • represents the values true and false.