Java Programming Object Oriented Programming
Total Page:16
File Type:pdf, Size:1020Kb
Object Java Oriented Programming Programming From Flowcharts To Java CSC10 – Spring 2016 Object Oriented Programming Classes . Many objects are related to . This means that your each other – having the same program consists of a series abilities and attributes of objects that will interact with each other . These objects belong to the same class – which is a . An object is very abstract can classification of related be anything objects 5/1/2016 Sacramento State - CSc 10A 3 5/1/2016 Sacramento State - CSc 10A 4 Classes Example Properties . "Cat" Class properties can include: . A class describes what an • name object will store, how it behaves, etc… • fur color • breed . Properties contain data about the object . "Student" Class properties can include: • name . Methods describe how the • major treats data (its and others) • academic level – freshman, junior, … 5/1/2016 Sacramento State - CSc 10A 5 5/1/2016 Sacramento State - CSc 10A 6 1 Example Methods Class Inheritance . "Cat" Class methods: • scratch . Classes can also "inherit" from classes • purr . When a class inherits another: • sleep • gets all the features of the original class . "Student" Class methods: • but can extend its functionality • study • allows work, created previously to become the • play on smart phone foundation of a more advanced class • sleep 5/1/2016 Sacramento State - CSc 10A 7 5/1/2016 Sacramento State - CSc 10A 8 Instances / Objects Instances / Objects . Classes just describe the . Instances will have all the behavior of some "object" features of its class . They don't do anything . So, different instances of the . In object-oriented same class share the same programming, you will create features instances of these classes – . But, each instance is a i.e. objects different and unique 5/1/2016 Sacramento State - CSc 10A 9 5/1/2016 Sacramento State - CSc 10A 10 Example Classes & Instances . "Game" Class can have instances of: • Pac-Man • Call of Duty Introduction to • World of Warcraft Java . "Food" Class can have instances of: • ice cream • pizza • top ramen Start the coffee maker – seriously... 5/1/2016 Sacramento State - CSc 10A 11 2 What is Java? History of Java . The Java Programming Language was created by . Java followed a long Sun Microsystems evolutionary chain that started with the C . Currently, it is one of the programming language most popular languages . C was designed by Dennis . Although Sun collapsed Ritchie at Bell Laboratories in (purchased by Oracle), Java the1970's survived 5/1/2016 Sacramento State - CSc 10A 13 5/1/2016 Sacramento State - CSc 10A 14 C-riously Popular And Along Comes Java . C became extremely popular . When Java was developed, • minimalistic C/C++ had been in use for • made efficient programs on early machines over 20 years . C++ extended the concepts of C . So, to aid programmers... • added object oriented programming • Java uses a syntax very similar • was backwards compatible... C++ could run C to C++ programs • Java as most of the same • still used today semantics as C++ 5/1/2016 Sacramento State - CSc 10A 15 5/1/2016 Sacramento State - CSc 10A 16 However, it is different The Result... Java contains many advanced features . However, Java is not . But, has a very symbolic syntax compatible with C++ • contains very few "words" - not English-like . It removed the low-level • so, programs are not easy to read at first features of C++ . It is not a beginners language . But, it will still work on • syntax it can be intimidating snippets of code • you must type of bunch of "weird" stuff you won't understand at first 5/1/2016 Sacramento State - CSc 10A 17 5/1/2016 Sacramento State - CSc 10A 18 3 Structure of Java Programs . Java programs consist of Structure of series of class definitions Java . Each class contains local variables (properties) and Programs functions . Each function contains its own local variables as well as What the heck am I looking at? statements 5/1/2016 Sacramento State - CSc 10A 20 What are Statements? What are Statements? . Statements can be grouped together into a . A statement will carry out a specific task block . Statements are executed in order from the . Some types of statements… first listed to the last • calls to other functions . In Java, you can create your own and use • control – looping, etc… ones created for you • create variables 5/1/2016 Sacramento State - CSc 10A 21 5/1/2016 Sacramento State - CSc 10A 22 Structure of a Java Program Data about class Java Data Types Used by Method What information Java can hold 5/1/2016 Sacramento State - CSc 10A 23 4 Data in Java Integers . Used to store whole numbers . Java classes are made of . Java has three data types other classes or some that store integers primitive types . Why three? . Primitive types are not really • more bytes you use to store a classes, but data that the value, the larger can be processor understands • however, it will take more memory 5/1/2016 Sacramento State - CSc 10A 25 5/1/2016 Sacramento State - CSc 10A 26 Integer Examples Integer Data Types Data Type Range of values Bytes . 1 byte -128 .. 127 1 . 5 . -100 short -32,768 .. 32,767 2 . 1846 -2,147,483,648 .. int 4 . 1947 2,147,483,647 . -12345 -9,223,372,036,854,775,808 .. long 8 9,223,372,036,854,775,807 5/1/2016 Sacramento State - CSc 10A 27 5/1/2016 Sacramento State - CSc 10A 28 Real Numbers Real Numbers . Real numbers in Java are . Java has two data types for called "floating-point" storing real numbers . Why call it a float? . Why? • name is based on how it is • again, you might need to use actually stored more bytes to store larger • the decimal place is "floats values around" like it does in scientific • but, it will cost more memory notation 5/1/2016 Sacramento State - CSc 10A 29 5/1/2016 Sacramento State - CSc 10A 30 5 Floating-Point Examples Floating Point Data Types Data Type Range of values Bytes . -6.78 . 3.1415 10-38 to 10+38 float Both positive and negative 4 . 1.618 About 6 digits precision . 2.71828 . -355.1234 10-308 to 10+308 double Both positive and negative 8 . 1234.0 Note the zero! About 15 digits precision 5/1/2016 Sacramento State - CSc 10A 31 5/1/2016 Sacramento State - CSc 10A 32 Character Data Type Character Examples . Used to store letter individual . 'A' letters, digits, symbols, etc… . '4' Space . These are the keys you have . ' ' on your keyboard . '$' . In Java, chars are delimited . '&' by single quotes (also called apostrophes) . '^' 5/1/2016 Sacramento State - CSc 10A 33 5/1/2016 Sacramento State - CSc 10A 34 Boolean Data Type Primitive Data Type Summary Data Type Range of values byte -128 .. 127 . Used to store either a true or false value short -32,768 .. 32,767 . These are used with Boolean-Expressions int -2,147,483,648 .. 2,147,483,647 to store flags long -9,223,372,036,854,775,808 .. -9,223,372,036,854,775,807 . This is just how you did it in pseudocode float 10-38 to 10+38, positive or negative, about 6 digits precision and Flowgorithm double 10-308 to 10+308 , positive or negative, about 15 digits precision char Unicode characters (generally 16 bits per char) boolean True or false 5/1/2016 Sacramento State - CSc 10A 35 5/1/2016 Sacramento State - CSc 10A 36 6 How do you store words? Examples of Strings . What if you want to store a word? . "Sac State" • text is really just a long series of characters • so, Java implements these in memory using . "Computer Science" multiple chars called a string . "Joe Gunchy" . Java denotes a string literal with double . "Hornet" quotes . "1947" . These are stored using a class – so a . "Pac-Man" String is not a primitive data type 5/1/2016 Sacramento State - CSc 10A 37 5/1/2016 Sacramento State - CSc 10A 38 Characters . Characters are actually More on integers . Each has a unique value Characters • characters and their matching values are a "character set" • there have been many characters sets developed over How Text is Stored time 5/1/2016 Sacramento State - CSc 10A 40 Java Escape Sequences Java Escape Sequences . Java has help escape . Often you want to add a sequences start with a control character do your backslash program . This is followed by another . … but you can't type them character that represents the control character 5/1/2016 Sacramento State - CSc 10A 41 5/1/2016 Sacramento State - CSc 10A 42 7 Important Control Characters Adding characters you can't type NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI Code Value Description \a DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 7 Alert (Bell) sp ! " # $ % & ' ( ) * + , - . / \b 8 Backspace 0 1 2 3 4 5 6 7 8 9 : ; < = > ? \t 9 Tab @ A B C D E F G H I J K L M N O \n 10 New Line (Line Feed) P Q R S T U V W X Y Z [ \ ] ^ _ \v 11 Vertical tab ` a b c d e f g h i j k l m n o \f 12 Form feed (new printer page) p q r s t u v w x y z { | } ~ DEL \c 13 Carriage return 5/1/2016 Sacramento State - CSc 10A 43 5/1/2016 Sacramento State - CSc 10A 44 Some Convenient Codes Code Description \" Double quote. Allows double quotes inside string literals.