1 / 59

ITK 177 Spring 2011

mykelti
Download Presentation

ITK 177 Spring 2011

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. ITK 177 Spring 2011 Shirley White

    2. Java fundamentals 2-2

    3. How to read character data next() Reads next token and returns as String nextLine() Reads next line of tokens and returns as String nextChar() No such method!! charAt() Used to remove a single char from a String 2-3

    4. Eclipse issues The work folder Error message Selection does not have a main type Run As Java Application not a choice Moving code from local computer to H-drive Where did the drop-down hints go? 2-4

    5. Do not Attempt to add a new folder to your work space using copy and paste or drag and drop Delete a folder from your workspace outside of Eclipse Rearrange existing files in your workspace outside of Eclipse Attempt to import files using Open File 2-5

    6. You may Copy and paste files from one project to another inside Eclipse Drag and drop (to relocate) while inside Eclipse Delete files and folders inside Eclipse by selecting and using the delete key 2-6

    7. You may need to Refresh projects if added files do not appear Restart Eclipse if your project becomes corrupted Switch workspaces if you want to work locally (while off campus or if you do not have an Internet connection) You must have Java and Eclipse installed locally to do this 2-7

    8. Eclipse workspace issues Eclipse requires you to choose a workspace Can have multiple Only one will open at a time Java Projects Stored in a folder in the work folder Hold source code in the src folder 2-8

    9. No main type 2-9

    10. Cannot run as java application 2-10

    11. Keeping h-drive up-to-date Open both your local work space and H-drive workspace You must have an existing Java Project on your H-drive Open the src folder on both drives Copy the most up-to-date version of your source code to the src folder on your H-drive If an older version exists you will get a pop-up to confirm the move 2-11

    12. 2-12

    13. 2-13

    14. Refreshing your workspace If you move a file that was created on your local workspace – meaning there is nothing to write over – it will not appear in your H-drive project until you refresh it Note you must select the Java Project you want to refresh 2-14

    15. Restoring ctrl-space help 2-15

    16. Chapter Topics Today we will discuss the following main topics Classes vs Objects Class Design Class diagrams Class data Class methods private vs public Parameters Return types 2-16

    17. What is the difference between a class and an object? 2-17

    18. What is the difference between an instance and a primitive variable? 2-18

    19. What is a class attribute? 2-19

    20. Why is class data made private? 2-20

    21. Class diagrams Name the 3 parts in order top to bottom How do you indicate private How do you indicate public How do you describe data How do you describe methods 2-21

    22. What is a constructor? 2-22 How do you code one?

    23. What is a setter? 2-23 How do you code one?

    24. What is a getter? 2-24 How do you code one?

    25. Code it… 2-25

    26. Javadoc comments!! Class level comment One for every public method Brief but clear description What not How… @param with name and description for every parameter passed @return with description if non-void return type @author only at class level 2-26

    27. code it… Part class Data includes Part number Wholesale cost Quantity on hand Quantity on order Mark-up percentage as a constant 25% 2-27

    28. Part class constructors No default constructor allowed since every part needs a part number assigned Multiple constructors required so we can Create a part without a specific cost determined, none in stock, and none on order Create a part with a specific cost but none in stock or on order Create a part with a specific cost, none in stock, but some on order Note all constructors should assign all data 2-28

    29. Part class methods Setter for wholesale cost only Getters for all data Include getter for retail selling price Method to order inventory Method to receive inventory Method to sell inventory Method to calculate inventory wholesale value Method to calculate inventory retail value Method to print all data (labeled) to console 2-29

    30. Questions? 2-30

    31. Chapter review slides start here 2-31

    32. Classes From chapter 2, we learned that a reference variable contains the address of an object. String cityName = "Charleston" 3-32

    33. Classes The length() method of the String class returns and integer value that is equal to the length of the string. int stringLength = cityName.length(); The variable stringLength will contain 10 after this statement since the string "Charleston" has 10 characters. Primitive can make method calls whereas objects can. A class can be thought of as a blueprint that objects are created from. 3-33

    34. Classes and Instances Many objects can be created from a class. Each object is independent of the others. String person = "Jenny"; String pet = "Fido"; String favoriteColor = "Blue"; 3-34

    35. Classes and Instances 3-35

    36. Classes and Instances Each instance of the String class contains different data. The instances all share the same design. Each instance has all of the attributes and methods that were defined in the String class. Classes are defined to represent a single concept or service. 3-36

    37. Access Modifiers An access modifier is a Java keyword that indicates how a field or method can be accessed. There are three Java access modifiers: public private protected 3-37

    38. Access Modifiers public: This access modifier states that any other class can access the resource. private: This access modifier indicates that only data within this class can access the resource. protected: This modifier indicates that only classes in the current package or a class lower in the class hierarchy can access this resource. These will be explained in greater detail later. 3-38

    39. Access Modifiers Classes that need to be used by other classes are typically made public. If there are more than one class in a file, only one may be public and it must match the file name. Class headers have a format: AccessModifier class ClassName { Class Members } 3-39

    40. Encapsulation Classes should be as limited in scope as needed to accomplish the goal. Each class should contain all that is needed for it to operate. Enclosing the proper attributes and methods inside a single class is called encapsulation. Encapsulation ensures that the class is self-contained. 3-40

    41. Designing a Class When designing a class, decisions about the following must be made. what data must be accounted for, what actions need to be performed, what data can be modified, what data needs to be accessible, and any rules as to how data should be modified. Class design typically is done with the aid of a Unified Modeling Language (UML) diagram. 3-41

    42. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has three main sections. 3-42

    43. Attributes The data elements of a class defines the object to be instantiated from the class. The attributes must be specific to the class and define it completely. Example: A rectangle is defined by length width. The attributes are then accessed by methods within the class. 3-43

    44. Data Hiding Another aspect of encapsulation is the concept of data hiding. Classes should not only be self-contained but they should be self-governing as well. Classes use the private access modifier on fields to hide them from other classes. Classes need methods to allow access and modification of the class’ data. 3-44

    45. Methods The class’ methods define what actions an instance of the class can perform Methods headers have a format: AccessModifier ReturnType MethodName(Parameters) { //Method body. } Methods that need to be used by other classes should be made public. 3-45

    46. Methods The attributes of a class might need to be: changed, accessed, and calculated. The methods that change and access attributes are called accessors and mutators. 3-46

    47. Accessors and Mutators Because of the concept of data hiding, fields in a class are private. The methods that retrieve the data of fields are called accessors. The methods that modify the data of fields are called mutators. Each field that the programmer wishes to be viewed by other classes needs an accessor. Each field that the programmer wishes to be modified by other classes needs a mutator. 3-47

    48. Accessors and Mutators For the Rectangle example, the accessors and mutators are: setLength : Sets the value of the length field. public void setLength(double len) … setWidth : Sets the value of the width field. public void setLength(double w) … getLength : Returns the value of the length field. public double getLength() … getWidth : Returns the value of the width field. public double getWidth() … Other names for these methods are getters and setters. 3-48

    49. Stale Data Some data is the result of a calculation. Consider the area of a rectangle. length times width It would be impractical to use an area variable here. Data that requires the calculation of various factors has the potential to become stale. To avoid stale data, it is best to calculate the value of that data within a method rather than store it in a variable. 3-49

    50. Stale Data Rather than use an area variable in a rectangle class: public double getArea() { return length * width; } This dynamically calculates the value of the rectangle’s area when the method is called. Now, any change to the length or width variables will not leave the area of the rectangle stale. 3-50

    51. UML Data Type and Parameter Notation UML diagrams are language independent. UML diagrams use an independent notation to show return types, access modifiers, etc. 3-51

    52. UML Data Type and Parameter Notation UML diagrams are language independent. UML diagrams use an independent notation to show return types, access modifiers, etc. 3-52

    53. UML Data Type and Parameter Notation UML diagrams are language independent. UML diagrams use an independent notation to show return types, access modifiers, etc. 3-53

    54. UML Data Type and Parameter Notation UML diagrams are language independent. UML diagrams use an independent notation to show return types, access modifiers, etc. 3-54

    55. Converting the UML Diagram to Code Putting all of this information together, a Java class file can be built easily using the UML diagram. The UML diagram parts match the Java class file structure. 3-55

    56. Converting the UML Diagram to Code 3-56

    57. Converting the UML Diagram to Code 3-57

    58. Class Layout Conventions The layout of a source code file can vary by employer or instructor. Typically the layout is generally: Attributes are typically listed first Methods are typically listed second The main method is sometimes first, sometimes last. Accessors and mutators are typically grouped. 3-58

    59. Questions?

More Related