Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.

Similar presentations


Presentation on theme: "© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class."— Presentation transcript:

1 © 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class. You may get help from the TA’s and from the instructor. Any other help on any part of the assignment is forbidden. Be sure to protect your files, your password, your log on, and your stray papers. Do not submit an assignment to my physical mailbox or to the pocket outside of my door. Sliding a stapled PT under my door is acceptable.

2 © 2004 Pearson Addison-Wesley. All rights reserved3-2 Additional If you get help from a TA, that help must be cited in the acknowledgement section with the TA name and what they helped you with. If you do background research, the source of your information must be cited in the document. Background research on coding is not acceptable. For example, if you wanted to find a conversion factor for rods to miles directly, you could find that on the web, but then must state where you found that information.

3 © 2004 Pearson Addison-Wesley. All rights reserved3-3 Collaboration Policy In the Resources section of Blackboard, you will find a link to the collaboration policy for this course. Please read the collab_intro document which describes programming and programming tasks and the types of assistance that you might get. Then read the document for our class which specifies exactly what is permitted and not for programming assignments. Also, read the JMU honor code. If you have not done so, read also the course policies.

4 © 2004 Pearson Addison-Wesley. All rights reserved3-4 Finally There is an assignment on Blackboard for you to signify that you have read and understood these documents. When you have finished with the reading and have had any questions you have answered, write the declaration statement and submit it with the assignment. Note: you will not be attaching a document, but will be making the declaration in the Student Comment section of the assignment.

5 Chapter 3 Using Classes and Objects

6 © 2004 Pearson Addison-Wesley. All rights reserved3-6 Outline Creating Objects The String Class Packages Formatting Output Wrapper Classes

7 © 2004 Pearson Addison-Wesley. All rights reserved3-7 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable DecimalFormat fmt; No object is created with this declaration An object reference variable holds the address of an object (hence the term reference…it refers to the object itself). The object itself must be created separately

8 © 2004 Pearson Addison-Wesley. All rights reserved3-8 Creating Objects Generally, we use the new operator to create an object fmt = new DecimalFormat(“0.###"); This calls the DecimalFormat constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class

9 © 2004 Pearson Addison-Wesley. All rights reserved3-9 Note With Strings, you do not need to make the “new” object in general, since by assigning any kind of String value, you are causing the String variable to reference the String value: Examples String myName; myName = “John Smith”; String message; message = Keyboard.readString();

10 © 2004 Pearson Addison-Wesley. All rights reserved3-10 Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods String msg msg = fmt.format(12.6548); A method may return a value, which can be used in an assignment or expression A method invocation can be thought of as asking an object to perform a service

11 © 2004 Pearson Addison-Wesley. All rights reserved3-11 References Note that a primitive variable contains the value itself, but an object variable contains the address of the object An object reference can be thought of as a pointer to the location of the object Rather than dealing with arbitrary addresses, we often depict a reference graphically "Steve Jobs" name1 num1 38

12 © 2004 Pearson Addison-Wesley. All rights reserved3-12 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After:

13 © 2004 Pearson Addison-Wesley. All rights reserved3-13 Reference Assignment For object references, assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs"

14 © 2004 Pearson Addison-Wesley. All rights reserved3-14 For now Just be aware that reference typed variables contain a pointer or address of where the object is really stored. Be aware that they will behave differently than primitive types. Variables of primitive types store data directly in the variable. Variables of reference types store data in some other location; the variable stores the address directly in the variable. There are some important ramifications of this property that we will explore later in the semester.

15 © 2004 Pearson Addison-Wesley. All rights reserved3-15 Outline Creating Objects The String Class Packages Formatting Output Wrapper Classes

16 © 2004 Pearson Addison-Wesley. All rights reserved3-16 The String Class Because strings are so common, we don't have to use the new operator to create a String object String title; title = "Java Software Solutions"; This is special syntax that works only for strings Each string literal (enclosed in double quotes) represents a String object

17 © 2004 Pearson Addison-Wesley. All rights reserved3-17 String Methods Once a String object has been created, neither its value nor its length can be changed Thus we say that an object of the String class is immutable However, several methods of the String class return new String objects that are modified versions of the original See the list of String methods on page 119 and in Appendix M

18 © 2004 Pearson Addison-Wesley. All rights reserved3-18 String Indexes It is occasionally helpful to refer to a particular character within a string This can be done by specifying the character's numeric index The indexes begin at zero in each string In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4 See StringMutation.java (page 120)StringMutation.java

19 © 2004 Pearson Addison-Wesley. All rights reserved3-19 Outline Creating Objects The String Class Packages Formatting Output Wrapper Classes

20 © 2004 Pearson Addison-Wesley. All rights reserved3-20 Class Libraries A class library is a collection of classes that we can use when developing programs The Java standard class library is part of any Java development environment Its classes are not part of the Java language per se, but we rely on them heavily Various classes we've already used ( System, String ) are part of the Java standard class library Other class libraries can be obtained through third party vendors, or you can create them yourself

21 © 2004 Pearson Addison-Wesley. All rights reserved3-21 Packages The classes of the Java standard class library are organized into packages Some of the packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing

22 © 2004 Pearson Addison-Wesley. All rights reserved3-22 The import Declaration When you want to use a class from a package, you could use its fully qualified name java.text.DecimalFormat Or you can import the class, and then use just the class name import java.text.DecimalFormat; To import all classes in a particular package, you can use the * wildcard character import java.text.*;

23 © 2004 Pearson Addison-Wesley. All rights reserved3-23 The import Declaration All classes of the java.lang package are imported automatically into all programs It's as if all programs contain the following line: import java.lang.*; That's why we didn't have to import the System or String classes explicitly in earlier programs The DecimalFormat class, on the other hand, is part of the java.text package, and therefore must be imported

24 © 2004 Pearson Addison-Wesley. All rights reserved3-24 Keyboard Currently, you are downloading the Keyboard class into your working directory. Keyboard is a part of the cs1 package that was provided by your authors. This package is available for you to download onto your computers, so that you can access it using the import statement and don’t have to have multiple copies of the Keyboard class. The instructions tell you where to store the cs1 package to make it available to your compiler and interpreter.

25 © 2004 Pearson Addison-Wesley. All rights reserved3-25 Lab machines cs1 is not yet installed on the lab machines. When it is, you will begin to use the import statement to import the Keyboard class into your programs. Import will work on the Linux machines. They will also recognize the Keyboard methods without import.

26 © 2004 Pearson Addison-Wesley. All rights reserved3-26 Outline Creating Objects The String Class Packages Formatting Output Wrapper Classes

27 © 2004 Pearson Addison-Wesley. All rights reserved3-27 Formatting Output It is often necessary to format values in certain ways so that they can be presented properly The Java standard class library contains classes that provide formatting capabilities The NumberFormat class allows you to format values as currency or percentages The DecimalFormat class allows you to format values based on a pattern Both are part of the java.text package

28 © 2004 Pearson Addison-Wesley. All rights reserved3-28 Formatting Output The DecimalFormat class can be used to format a floating point value in various ways For example, you can specify that the number should be truncated to three decimal places The constructor of the DecimalFormat class takes a string that represents a pattern for the formatted number See CircleStats.java (page 134) - examplesCircleStats.java


Download ppt "© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class."

Similar presentations


Ads by Google