Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12.

Similar presentations


Presentation on theme: "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12."— Presentation transcript:

1 CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12

2 Classes and Objects - 1 Object-Oriented Programming – software development methodology – development is based on looking at the problem from the perspective of what objects are interacting in the system. – i.e. University Registration System Students Professors Classrooms Courses with multiple class sections

3 What have we been doing so far? Procedural programming – Thinking about the different sub-procedures of how to solve a problem. – Data and procedures are separate and unrelated except for the fact that procedures operate on the data – e.g. Average grades for students: Procedural: getGrades, averageGrades, dispalyAverage

4 Classes and Objects - 2 An Object is a programming construct that has state (contains data) – i.e. every student has an id and name, but values may be different for each student and has behavior (contains methods) – interface allows us to access and manipulate the data in a controlled environment. – i.e. Functionality to retrieve the students name from the object or update the students GPA.

5 Some Object Semantics We communicate with an object by sending messages to the object. In Java, we send messages by calling methods on the object. Scanner s = new Scanner (System.in); s.nextInt(); s.nextDouble(); Creating a scanner object Sending message to scanner obj to tell it to read the next integer from the keyboard. Sending message to scanner obj to tell it to read the next double from the keyboard.

6 Primitive vs. Reference Variables Primitive Variable – a variable with a primitive data type – primitive data types are part of the core Java language – short, int, long, char, byte, boolean, double, float. Reference Variable – a variable with a non- primitive data type – EVERYTHING ELSE! – String, Scanner, TouchSensor

7 Primitive Variables Can be declare with out “creating” using the keyword new. We access the variable directly – Doesn’t have an interface of methods through which to access functionality and data. – Programmer can use the relational operators to compare int a = 10; int b = 20; if (a < b) { //… }

8 Reference Variables Used to refer to objects Must use the new operator to create an object of a particular type – new Scanner(System.in); – new returns a reference (memory location) that is stored in the reference variable – Scanner s = new Scanner (System.in); returns a memory location that is then stored in s

9 The Java API API = Application Programming Interface Contains about 3000+ classes that are already implemented and tested You can use this so as to not re-invent the wheel. – Scanner is a perfect example – Scanner class contains the code to create a scanner object that can allow you to read from a data source. – String is another example We’ll explore this in much more depth throughout the semester.

10 Classes vs. Objects A class is a blueprint for an object. – It tells the JVM how to construct an object when the programmer asks for one Student Name:String id:String gpa:float void getName() void PrintGPA Student Name:String id:String gpa:float void getName() void PrintGPA Class Student Mark 12345678 4.0 Mark 12345678 4.0 Sam 11223344 3.8 Sam 11223344 3.8 Joe 22334455 3.7 Joe 22334455 3.7 Objects of Type Student

11 The String Class You can have multiple Strings in your program Can call any method from the String class on any of the String objects. String fName = “Mark”; String lName = “Fontenot; String school = “Southern Methodist University”; Each created based on same String blueprint int len = fName.length(); System.out.println(lName.charAt(2)); if (school.equals(“TCU”)) System.out.println(“Boo!”);

12 How do you know what methods are available?? Look at the API documentation… – http://java.sun.com/j2se/1.5.0/docs/api/index.html http://java.sun.com/j2se/1.5.0/docs/api/index.html – Lists all of the methods that you can call on a java String object – There is an API doc page for every class that is part of the Java API.

13 BREAKOUT 1

14 ?


Download ppt "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12."

Similar presentations


Ads by Google