1 / 46

COMP 110: Introduction to Programming

Learn about classes and objects in Java programming, including attributes, methods, and instance variables. Explore how to create and use objects of different classes.

bfree
Télécharger la présentation

COMP 110: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110:Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014

  2. Announcements • Program 2 due Wednesday by midnight • Lab 4 • Part 2 is now extra credit

  3. Lab 2

  4. Questions?

  5. Today in COMP 110 • Classes & Methods • Read Section 5.1 in text

  6. Classes • Java programs consists of objects of various class types interacting with one another • You have already been using classes • System, String, Scanner, Math

  7. Classes • A class is a definition of a kind of object • It describes a general template or blueprint for creating objects of that class • A class “Car” is a definition of what a “Car” is and what it can do

  8. Classes & Objects • Objects of the class Car might have the following attributes • Make • Model • Year • Owner • Location • All objects of the class Car have these attributes, but with potentially different values

  9. Class Car Make Model Year Owner Location focus object Make = Ford Model = Focus Year = 2001 Owner = Samantha Smart Location = School Classes & Objects • camry object • Make = Toyota • Model = Camry • Year = 2000 • Owner = John Doe • Location = Garage

  10. Classes & Objects Important: classes do not have data; individual objects have data Classes specify what kind of data objects have • camry object • Make = Toyota • Model = Camry • Year = 2000 • Owner = John Doe • Location = Garage • Class Car • Make • Model • Year • Owner • Location 10

  11. Classes & Objects • Both the “camry” and “ford” objects satisfy the definition of the class “Car” • They are said to be instances of the “Car” class • When we create an object of a certain class, we are said to instantiate it

  12. Methods • Classes also define the actions that objects of its type can perform • These are called methods • All objects of the same class have the same methods • The class “Car” might have the following methods • accelerate() • brake() • sell() • start()

  13. String Methods • string.length() • string.charAt(index) • string.substring(index1, index2) • string.toLowerCase() • string.equals(A_String)

  14. UML • A UML class diagram can be useful in describing classes • UML stands for Universal Modeling Language

  15. UML • A UML class diagram for the class “Car” Class name Attributes Methods (actions)

  16. Defining Classes • You have already been defining your own classes! • Every program you have written defines a class //define the class TipCalculator publicclass TipCalculator { … }

  17. Class Files & Separate Compilation Each Java class definition goes in its own, SEPARATE .java file ClassName  ClassName.java The class “Car” must be saved in a file called “Car.java”

  18. Class Files & Separate Compilation What happens when you compile a .java file? .java file gets compiled into a .class file .class file contains Java bytecode Car.java is compiled into Car.class You can compile a Java class before you have a program that uses it

  19. The Student Class

  20. Defining the Class Student public classStudent { publicString name; public intyear; public doubleGPA; publicStringmajor; // ... publicStringgetMajor() { return major; } public voidincreaseYear() { year++; } // … } Class name Data (instance variables) Methods

  21. Instance Variables The data members of a class are called instance variables publicString name; public intyear; public doublegpa; publicStringmajor; public No restrictions on how these instance variables are used (more details later)

  22. Creating Objects • Declaring/Initializing Primitives int i = 0; double area = 10.5; • Declaring/Creating Objects Scanner keyboard = new Scanner(System.in);

  23. Creating Objects • Create an object called “jack” of class “Student” Student jack = new Student(); Assign memory address of object to variable Create an object

  24. Using Instance Variables: Inside the Class Definition public classStudent { publicString name; public intyear; public doubleGPA; publicStringmajor; // ... publicStringgetMajor() { return major; } public voidincreaseYear() { year++; } // … }

  25. Using public Instance Variables Outside a Class public static voidmain(String[] args) { Student jack = new Student(); jack.name = "Jack Smith"; jack.major = "Computer Science"; System.out.println(jack.name + " is majoring in " + jack.major); Student sam = new Student(); sam.name = "Samantha Smart"; sam.major = "Biology"; System.out.println(sam.name + " is majoring in " + sam.major); } “jack.name”and “sam.name” are two different variables because they belong to different objects

  26. Methods Two kinds of methods Methods that return a value Examples string.substring() string.charAt() Methods that return nothing Example System.out.println()

  27. Methods public StringgetMajor() { return major; } public void increaseYear() { year++; } returns a String return type returns nothing

  28. Defining Methods that Return Nothing Method heading: keywords public: no restriction on how to use the method (more details later) void: the method returns nothing Method body: statements executed when the method is called (invoked) Must be inside a pair of braces public voidincreaseYear() { year++; }

  29. Method printData As usual, inside a block (defined by braces), you can have multiple statements public voidprintData() { System.out.println("Name: " + name); System.out.println("Major: " + major); System.out.println("GPA: " + gpa); }

  30. Methods • All method definitions must appear inside the definition of the class to which they belong! public classStudent { // … publicStringgetMajor() { return major; } public voidincreaseYear() { year++; } // … }

  31. Methods • When you use a method such as keyboard.nextInt() you are said to call or invoke the method

  32. Calling Methods that Return Nothing Syntax object.method(); Use them like Java statements Student jack = new Student(); jack.year = 1; jack.increaseYear(); //year = 2 jack.increaseYear(); //year = 3 System.out.println("Jack’s class year is " + jack.year); Output Jack’s class year is 3

  33. Defining Methods that Return a Value Method heading: keywords public: no restriction on how to use the method (more details later) Type: the type of value the method returns Method body: statements executed Must be inside a pair of braces Must have a returnstatement publicStringgetMajor() { return major; }

  34. The Return Statement A method that returns a value must have at least onereturn statement Terminates the method, and returns a value to the caller Syntax: returnExpression; Expression can be any expression that produces a value of the return type 34

  35. Methods that Return a Value publicString getClassYear() { if(year == 1) return"Freshman"; else if(year == 2) return"Sophomore"; else if ... }

  36. Calling Methods that Return a Value Syntax object.method(); Use as a variable of the method’s return type Student jack = new Student(); jack.name = "Jack Smith"; jack.major = "Computer Science"; String m = jack.getMajor(); System.out.println("Jack’s full name is " + jack.getName()); System.out.println("Jack’s major is " + m); 36

  37. The Return Statement Can also be used in methods that return nothing Terminates the method Syntax: return; public void increaseYear() { if(year >= 4) return; //exit the method year++; } 37

  38. The Return Statement • Typically, you want to write your methods to contain only one return statement • It will make your programs easier to read

  39. The Main Method • The main method is a method like any other publicstatic void main(String[] args) { // … } • The main method is not invoked by you, but invoked automatically by the system when you run a program

  40. The Main Method • Classes that have a main method can be run as a program • Not all classes have a main method • Some are used inside the main method of other classes

  41. Local Variables • Variables declared inside a method are called local variables • As opposed to instance variables, which are declared in the class definition public class Student { public double gpa; //an instance variable publicstatic void main(String[] args) { int size; //a local variable // … } }

  42. Local/Instance variables Instance variables Declared in a class Can be used anywhere in the class that declares the variable, including inside the class’ methods Local variables Declared in a method Can only be used inside the method that declares the variable

  43. Programming Demo • Counter class • A class used to count things • Define a class that records a count as a non-negative whole number • Include methods for the following • Reset counter to 0 • Increment the count • Decrement the count • Get the current count • Display the current count

  44. Counter Class • No methods should allow the count to become negative • Write a program to test the class

  45. Programming Demo • Programming

  46. Wednesday • More about Classes

More Related