Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 41 Defining Classes and Methods Chapter 4.

Similar presentations


Presentation on theme: "Chapter 41 Defining Classes and Methods Chapter 4."— Presentation transcript:

1 Chapter 41 Defining Classes and Methods Chapter 4

2 2 Reminders Project 2 was due last night Project 3 released: due Sept 29 @ 10:30 pm - No Late Submissions - 2 weeks b/c of exam Follow the newsgroup for project information and questions – newsgroup postings from GTAs are official. Even if you don’t follow the newsgroup you are responsible for all updates/changes to any projects mentioned there.

3 Chapter 43 Reminders 2 Make sure to match given output exactly in your projects. You will lose points if your output does not match exactly. We give you sample inputs and outputs for this reason. Project submissions use your lab section, not your recitation section (this is clearly written on all project outlines) turnin –c cs180secXXXX –p project1 …

4 Chapter 44 Exam 1 Reminder Tuesday, September 20 7:00 pm – 8:00 pm Physics 112 Covers chapters 1 – 4 More details towards end of recitation

5 Chapter 45 Using Methods two kinds of methods: –methods that return a single value (e.g. nextInt ) –methods that perform some action other than returning a single value (e.g println ), called void methods

6 Chapter 46 Defining Methods That Return a Value example public int fiveFactorial(); { int factorial = 5*4*3*2*1; return factorial; } As before, the method definition consists of the method heading and the method body. –The return type replaces void.

7 Chapter 47 Methods That Return a Value, cont. The body of the method definition must contain return Expression; –This is called a return statement. –The Expression must produce a value of the type specified in the heading. The body can contain multiple return statements, but a single return statement makes for better code. void methods can have return statements, but must not return anything. return;

8 Chapter 48 Naming Methods Use a verb to name a void method. –void methods typically perform some action(s). (e.g. getAge ) Use a noun to name a method that returns a value. –Methods that return a value are used like a value. (e.g. fiveFactorial ) Observe the convention of starting the method name with a lowercase letter.

9 Chapter 49 Variables Variables of a class type name objects, which is different from how primitive variables store values. All variables are implemented as memory locations. –The value of a variable of a primitive type is stored in the assigned location. –The value of a variable of a class type is the address where a named object of that class is stored.

10 Chapter 410 Variables, cont. A value of any particular primitive type always requires the same amount of storage. –example: a variable of type int always requires 4 bytes. An object of a class type might be arbitrarily large. –An object of type String might be empty, or might contain 1, 120, 5280, or more characters.

11 Chapter 411 Variables, cont. However, there is always a limit on the size of an address. The memory address where an object is stored is called a reference to the object. Variables of a class type behave differently from variables of a primitive type.

12 Chapter 412

13 Chapter 413 Allocating Memory for a Reference and an Object A declaration such as SpeciesFourthTry s; creates a variable s that can hold a memory address. A statement such as s = new SpeciesFourthTry(); allocates memory for an object of type SpeciesFourthTry.

14 Chapter 414 == with Variables of a Class Type

15 Chapter 415 == with Variables of a Class Type, cont. When used with variables of a class type, == tests if the variables are aliases of each other, not if they reference objects with identical data. To test for equality of objects in the intuitive sense, define and use an appropriate equals method.

16 Chapter 416 == with Variables of a Class Type class Species

17 Chapter 417 == with Variables of a Class Type class SpeciesEqualsDemo

18 Chapter 418 Method equals The definition of method equals depends on the circumstances. –In some cases, two objects may be “equal” when the values of only one particular instance variable match. –In other cases, two objects may be “equal” only when the values of all instance variables match. Always name the method equals.

19 Chapter 419 Boolean-Valued Methods A method that returns a value of type boolean is called a boolean-valued method. Method equals produces and returns a value of type boolean. The invocation of a boolean-valued method can be used as the condition of an if-else statement, a while statement, etc.

20 Chapter 420 Boolean-Valued Methods, cont. The value returned by a boolean-valued method can be stored in a variable boolean areEqual = s1.equals(s2); Any method that returns a boolean value can be used in the same way.

21 Chapter 421 Class Parameters Recall –When the assignment operator is used with objects of a class type, a memory address is copied, creating an alias. –When the assignment operator is used with a primitive type, a copy of the primitive type is created.

22 Chapter 422 Class Parameters, cont. –When a parameter in a method invocation is a primitive type, the corresponding formal parameter is a copy of the primitive type.

23 Chapter 423 Class Parameters, cont. When a parameter in a method invocation is a reference to a class type (i.e. a named object), the corresponding formal parameter is a copy of that reference (i.e. an identically valued reference to the same memory location).

24 Chapter 424 Class Parameters, cont. Example if (s1.equals(s2)) … public boolean equals (Species otherObject) causes otherObject to become an alias of s2, referring to the same memory location, which is equivalent to otherObject = s2;

25 Chapter 425 Class Parameters, cont. Any changes made to the object named otherObject will be done to the object named s2, and vice versa, because they are the same object. –If otherObject is a formal parameter of a method, the otherObject name exists only as long as the method is active.

26 Chapter 426 Comparing Class Parameters and Primitive-Type Parameters A method cannot change the value of a variable of a primitive type passed into the method. A method can change the value(s) of the instance variable(s) of a class type passed into the method.

27 Chapter 427 The Graphics Class An object of the class Graphics represents an area of the screen. The class Graphics also has methods that allow it do draw figures and text in the area of the screen it represents.

28 Chapter 428

29 Chapter 429 The Graphics Class, cont. A Graphics object has instance variables that specify an area of the screen In examples seen previously, the Graphics object represented the area corresponding to the inside of an applet.

30 Chapter 430 The Graphics Class, cont. When an applet is run, a suitable Graphics object is created automatically and is used as an argument to the applet’s paint method when the paint method is (automatically) invoked. –The applet library code does all this for us. –To add this library code to an applet definition, use extends JApplet

31 Chapter 431

32 Chapter 432

33 Chapter 433 Programming Example, cont.

34 Chapter 434 The init Method An init method can be defined when an applet is written. The method init (like the method paint ) is called automatically when the applet is run. –The paint method is used only for things like drawing. –All other actions in an applet (adding labels, buttons, etc.) either occur or start in the init method.

35 Chapter 435 Adding Labels to an Applet A label is another way to add text to an applet.

36 Chapter 436 Adding Labels to an Applet,

37 Chapter 437 The Content Pane Think of the content pane as inside of the applet. Container contentPane = getContentPane(); When components are added to an applet, they are added to its content pane. The content pane is an object of type Container.

38 Chapter 438 The Content Pane, cont. A named content pane can do things such as setting color contentPane.setBackground(Color.WHITE); or specifying how the components are arranged contentPane.setLayout (new FlowLayout());

39 Chapter 439 The Content Pane, cont. or adding labels JLabel label1 = new JLabel(“Hello”); contentPane.add(label1);

40 Chapter 440 Summary You have become familiar with the concept of a class and an object that instantiates the class. You have learned how to define classes in Java. You have learned how to define and use methods in Java. You have learned how to create objects in Java

41 Chapter 441 Summary, cont. You have learned how parameters work in Java. You have learned about information hiding and encapsulation. You have become familiar with the notion of a reference (to understand class variables and class parameters). (optional) You have learned more about applets.

42 Chapter 442 Exam 1 Information Time/Location –Tuesday, Sept 20 7-8pm, Physics 112 Material Covered –Chapters 1 - 4 Format: –20 MC questions (2 pts each) –5 short programming questions (3 x 10pts, 2 x 15pts) Old exams on course website

43 Chapter 443 Exam 1 Information Topics –Encapsulation, polymorphism, information hiding –Accessor and mutator methods –Objects, classes –Public and private modifiers –Java naming conventions –Primitive types vs. class types

44 Chapter 444 Exam 1 Information Topics, cont –Defining classes and methods (including the main method) –Void methods vs. methods that return a value –Looping structures (while, do-while, for) –If-else, switch –Primitive types vs. class types (and memory representation) –Scanner class for input, System.out for output

45 Chapter 445 Exam 1 Information Topics, cont –Arithmetic expressions –Boolean variables –String methods –== vs. equals methods –Basic graphics methods


Download ppt "Chapter 41 Defining Classes and Methods Chapter 4."

Similar presentations


Ads by Google