Class Objects data types and Data declaration Sahar

Class, Objects, data types and Data declaration Sahar Mosleh California State University San Marcos Page 1

What is a Class? • It is easier to explain a class using examples. Suppose, we define a Class called "Car". We would start by grouping attributes and operations that are common to all Cars, thus: • A Car has: attributes and operations • Attributes are: • color, year, model, height, weight, length, etc. • Operations are: change gear, increase or decrease velocity, change direction (Left/Right, Fwd/Rev), etc… Sahar Mosleh California State University San Marcos Page 2

• We expect that all cars have a color, a height and weight, etc, and that they will all be able to change gears, speed, and direction, (at least if everything is working) • In Object Oriented model, we create a class by collecting the attributes and operations that are common to our desired class. • In the case of the "Car" class, we have defined our class by it's attributes and operations. • The class is the generic term for the object - the vehicle that I drive is a "Car, " but it is only one instance of the class "Car. " Sahar Mosleh California State University San Marcos Page 3

What is an Object? • An object is a concrete example of, or instance of, a class. Take cars for example. Suppose that I wanted to discuss a specific car, my car, and not "Cars" in general. My car has values for the attributes • It is a white 2005 Toyota Camry. • My Camry is thus a specific instance of the class "Car. “ • Your Buick is another instance of the class "Car. " • They are both "Cars, " but they are different instances of the class "Car" or alternately you could say that they are different "objects". Sahar Mosleh California State University San Marcos Page 4

Sahar Mosleh California State University San Marcos Page 5

Attribute • To understand what an attribute is, you must first understand what a variable is. • Imagine a safety deposit box in a bank. You can store items in it, and remove them, or even change what is in the box at your leisure. • In a programming language a variable exhibits similar behavior to this type of box. • Let's say that we wanted to put a coin collection into the bank. • The bank tells us that it has different boxes for different coin denominations. • The smallest box is the "penny. Box", that takes only pennies. • Next is the "nickel. Box, " that takes only Nickels. And so on Sahar Mosleh California State University San Marcos Page 6

• So we separate our coins and deposit them in the appropriate boxes - one for the pennies, the nickels, the dimes etc. • Each box now contains a number of coins. The value of each box can change by simply adding or removing more coins to or from the box. • You might even say that each box stores a variable amount of coins. In a programming language, data is stored in "variables" (just as coins were stored in safety deposit boxes). • There are usually many types of data ("data types"), just as there were many types of coins. Sahar Mosleh California State University San Marcos Page 7

Variables vs Attributes • Recall that a class is made up of attributes and operations. An attribute is a variable that exists for the life of the object. • At first that previous statements may sound confusing - at least the part about existing for the life of the object. • That means, as long as the "object" that contains the attribute is in memory, the attribute will also be in memory. Sahar Mosleh California State University San Marcos Page 8

Variable Names • The name that you choose for a variable (or anything in Java) is called an Identifier. An identifier can be any length • Can be made up of letters, digits, and the underscore (_) character • Cannot start with a digit • Cannot use other symbols such as ? or % • Spaces are not permitted inside identifiers • You cannot use reserved words • They are case sensitive Sahar Mosleh California State University San Marcos Page 9

• The following are NOT valid Java Identifiers: Two*Three. Is= &My. Special. Char 3 Digits Jim Hart Void • The following are all valid Java Identifiers: _The. Under. Score My. Var 1 A_Long_Variable_Name Sahar Mosleh California State University San Marcos Page 10

Variable Types • Variables store data. The type of data stored is determined by the data type. For example, in the following declaration the data type is an integer: int number. Of. Cars; • “int” is the data type in this case • “number. Of. Cars is variable name, and • “; ” marks the end of the statement. • This particular variable can only store values that are of the “int” data type. Sahar Mosleh California State University San Marcos Page 11

Primitive Data Types • There are Three categories of primitive data types: • Numeric values • Unicode characters, and • Logical variables Sahar Mosleh California State University San Marcos Page 12

Numeric Variables • There are Two categories of Numeric variables: • Integer Data Types • Floating Point variables Sahar Mosleh California State University San Marcos Page 13

Integer Data Types • There are four types of variables that you can use to store integer data: • byte: byte variables can have values from -128 to +127 and occupy 1 byte (8 bits) in memory • short: short variables can have values from -32768 to +32767 and occupy 2 bytes (16 bits) in memory • int: int variables can have values from -2147483648 to +2147483647 and occupy 4 bytes (32 bits) in memory • long: long variables can have values from -9223372036854775808 to +9223372036854775807 and occupy 8 bytes (64 bits) in memory Sahar Mosleh California State University San Marcos Page 14

Integer Values • Example of integers are: byte short int long hockey. Score; age; world. Population; inches. To. The. Moon; • Java assumes all integer data type literal are of type int. • When you declare a variable if you do not specify what type of literal you will be using, Java will assume the default. Sahar Mosleh California State University San Marcos Page 15

Literals or constant value • Literals are explicit data that can (and almost always do) appear in your program: my. Variable = 15; your. Variable = 21; our. String = "This is a string literal" • In the above example 15, 21, and "This is a string literal", are all literals. • All non-decimal numeric literals default to the int data type. Thus 15, and 21, are assumed to be int values. You cannot normally specify literals of byte and short data types Sahar Mosleh California State University San Marcos Page 16

• Integer literals can also be specified as hexadecimal integers by prefixing the literal with '0 x': My. Hex = 0 x 100; • You can specify a "long" literal by appending an uppercase "L" to the literal: mylong = 12 L; Sahar Mosleh California State University San Marcos Page 17

Floating Point Variables • There are two types of variables that you can use to store floating point data: float and double. • float: • float variables can have values from +1038 and occupy 4 byte (32 bits) in memory. • Values are represented with approximately 7 digits accuracy • double: • double variables can have values from +10308 and occupy 8 byte (64 bits) in memory. • Values are represented with approximately 17 digits accuracy Sahar Mosleh California State University San Marcos Page 18

• Example: float double my. Wage; bosses. Wage; • Java assumes all floating Point data type literals are of type double. • When you declare a variable if you do not specify what type of literal you will be using, Java will assume the default. Sahar Mosleh California State University San Marcos Page 19

• Therefore if we would like to use a float literal instead of the default double we must append an "F" (upper or lowercase does not matter) to the literal: my. Float = 12. 5 F; my. Other. Float = 12. 5 f; my. Double = 12. 5; • In the above example my. Float and my. Other. Float are both assigned float literals, while my. Double is assigned the default double literal. Sahar Mosleh California State University San Marcos Page 20

Declaring Variables • Variables are declared by stating the data type, and then the identifier (variable name), followed by a semi-colon: For example: int this. Is. My. Integer; • You can declare more than one variable of the same type by delimiting a list with commas ( , ). For example, int first. Int, second. Int, third. Int; • You can also assign a value to a variable at the time that you declare it. For example: int first. Int = 3, second. Int = 5, third. Int = 7; • In the above examples we used the int data type, but the same rules apply for all primitive data types. Sahar Mosleh California State University San Marcos Page 21

Assignment Statements • You can store the result of a calculation in a variable. You do this with an assignment statement. • An assignment statement consists of a variable name followed by an assignment operator, followed by an arithmetic expression, followed by a semicolon. • For example: number. Of. Fords = 6; number. Of. Dodges = 6 + 13; number. Of. Cars = number. Of. Fords + number. Of. Dodges; Sahar Mosleh California State University San Marcos Page 22

• Here the assignment operator is the equals sign ( = ). The arithmetic expression to the right of the equals sign is evaluated first then assigned from right to left. Such that: x = y = z = 400 + 33; // 400 + 33 evaluates to 433 • Which means: x = y = z = 433 // z = 433 evaluates to 433 • Which means: x = y = 433 // y = 433 evaluates to 433 • Which means: x = 433 Sahar Mosleh California State University San Marcos Page 23

Integer Calculations • The basic operations are add (+), subtract (-), multiply (*) and divide (/). Each of these is a binary operator, that is, it requires two operands to produce a result. • The normal rules for arithmetic order of operations apply here as well (multiplication and division takes precedence over addition and subtraction. ) my. Var = 5; my. Answer = 6 + 3 * 10 / ( 5 + my. Var ); = 6 + 3 * 10 / ( 10 ) =6+3*1 =6+3=7 Sahar Mosleh California State University San Marcos Page 24

What is an Operation? • Operations are the work that an object does. Let’s take our Car example. • One operation that the car might perform is to change the direction of the car. Sahar Mosleh class Car { // attributes. . . //. private STRING direction. Of. Motion; // operations. . . public void set. Dir(int direction. Change ) { if (diretion. Change = = 1 ) direction. Of. Motion = "Forward"; else direction. Of. Motion = "Reverse"; } // rest of the code for this class goes here. } California State University San Marcos Page 25

Java Program Structure • The necessary elements of a Java program are: • At least one class but there may be many • Typically you put the code for each class in a separate file. • Each file is given the same name as the class that is defined within it • A Java source file should always have the. java extension. • The Java Class Library • The Java class Library contains many useful classes that we can use to build our own application. • These classes support the basic language features • They are available directly in your programs by default because the package is loaded automatically with your program. Sahar Mosleh California State University San Marcos Page 26

Some of the important classes are: • java. io: Classes for input and output operations • java. util: This package contains utility classes of various kinds, including classes for managing data. • javax. swing: These classes provide easy to use and flexible components for building Graphical User Interfaces (referred to as swing components) • java. awt: Classes in this package also provide GUI component classes. • java. awt. image: These classes support image handling • java. awt. event: The classes in this package are used in the implementation of windowed application to handle events in your program. Sahar Mosleh California State University San Marcos Page 27

Example • The following java program will demonstrate the data declaration, Outputting variables, calling a method of class within main routine. • The Example outputs the program header, which you need to use it for every java program in this course. It also outputs the number of fruits that are been declared in the program. Sahar Mosleh California State University San Marcos Page 28
![public class Fruit { public static void main (String args[ ]) { // Declare public class Fruit { public static void main (String args[ ]) { // Declare](http://slidetodoc.com/presentation_image_h2/638ac3ee1eecfeaf79b9f6d1d6bc6983/image-29.jpg)
public class Fruit { public static void main (String args[ ]) { // Declare and initialize three variables int num. Oranges = 5; // Counts oranges int num. Apples = 10; // Counts apples int num. Fruit = 0; // Counts fruit // Call header Function Fruit. header(); num. Fruit = num. Oranges + num. Apples; // Calculates the total Fruit // Display the result. . . System. out. println("nnt A totally fruity program"); System. out. println("nnt Total Fruit is " + num. Fruit); } // Function to Display Header public static void header() { System. out. println("Name: tttyour name "); System. out. println("Email: tttyour email"); System. out. println("Instructor Name: t. Sahar Mosleh"); System. out. println("Lab #: tttyour lab number"); } } Sahar Mosleh California State University San Marcos Page 29
- Slides: 29