computational expression
play

Computational Expression Classes Janyl Jumadinova 21 October, 2019 - PowerPoint PPT Presentation

Computational Expression Classes Janyl Jumadinova 21 October, 2019 Janyl Jumadinova Computational Expression 21 October, 2019 1 / 13 Classes Most of our previous programs all just had a main() method in one file. Janyl Jumadinova


  1. Computational Expression Classes Janyl Jumadinova 21 October, 2019 Janyl Jumadinova Computational Expression 21 October, 2019 1 / 13

  2. Classes Most of our previous programs all just had a main() method in one file. Janyl Jumadinova Computational Expression 21 October, 2019 2 / 13

  3. Classes Most of our previous programs all just had a main() method in one file. Most programs are composed of the main() method for the primary class and one or more additional, supporting classes. Janyl Jumadinova Computational Expression 21 October, 2019 2 / 13

  4. Classes Most of our previous programs all just had a main() method in one file. Most programs are composed of the main() method for the primary class and one or more additional, supporting classes. These additional classes do not have main() methods, but have other methods to implement behaviors. Janyl Jumadinova Computational Expression 21 October, 2019 2 / 13

  5. Classes Most of our previous programs all just had a main() method in one file. Most programs are composed of the main() method for the primary class and one or more additional, supporting classes. These additional classes do not have main() methods, but have other methods to implement behaviors. Remember: Classes are templates for objects Objects are composed of data members (attributes) and member methods (behaviors) Objects respond to messages Janyl Jumadinova Computational Expression 21 October, 2019 2 / 13

  6. Defining a Method Think about creating methods in terms of three things: 1 Parameters - Parameters/arguments/values to send to a method? - “What does the method need so that it can do its job?” Janyl Jumadinova Computational Expression 21 October, 2019 3 / 13

  7. Defining a Method Think about creating methods in terms of three things: 1 Parameters - Parameters/arguments/values to send to a method? - “What does the method need so that it can do its job?” 2 Task - What does the method do? Janyl Jumadinova Computational Expression 21 October, 2019 3 / 13

  8. Defining a Method Think about creating methods in terms of three things: 1 Parameters - Parameters/arguments/values to send to a method? - “What does the method need so that it can do its job?” 2 Task - What does the method do? 3 Return/Results - What does the method produce as a result? - What is it’s final answer? - Does it even need to return anything? Janyl Jumadinova Computational Expression 21 October, 2019 3 / 13

  9. Janyl Jumadinova Computational Expression 21 October, 2019 4 / 13

  10. Gradebook class // Define class GradeBook with a method displayMessage public class GradeBook { // method to display a welcome message // to the GradeBook user public void displayMessage () { System.out.println("Welcome to the Grade Book!"); } } Janyl Jumadinova Computational Expression 21 October, 2019 5 / 13

  11. Gradebook class This class by itself is not useful in that you can compile it to a .class file, but you cannot execute it with the JVM since there is no main() method. We will need to create a separate class that will use or instantiate an object of this class. Janyl Jumadinova Computational Expression 21 October, 2019 6 / 13

  12. GradebookMain class /** Now we have a .java file that uses the GradeBook class, * like creating an int variable. This creates or * instantiates a new variable or object * called myGradeBook of the GradeBook class. */ public class GradeBookMain { public static void main ( String args[] ) { GradeBook myGradeBook = new GradeBook (); myGradeBook.displayMessage(); } } Janyl Jumadinova Computational Expression 21 October, 2019 7 / 13

  13. GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered Janyl Jumadinova Computational Expression 21 October, 2019 8 / 13

  14. GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered Middle compartment: - Attributes (data members) Janyl Jumadinova Computational Expression 21 October, 2019 8 / 13

  15. GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered Middle compartment: - Attributes (data members) Bottom compartment: - Behaviors (member methods), method name, followed by parentheses - Plus (+) sign indicates public member method GradeBook + displayMessage() Janyl Jumadinova Computational Expression 21 October, 2019 8 / 13

  16. GradeBook class with argument Bottom compartment: - Behaviors (member methods) - method name, followed by parentheses - Plus (+) sign indicates public member method - Parameters are listed in the parentheses, name first followed by a colon and the data type - Data type is language-independent since the UML is used by multiple languages GradeBook + displayMessage(String courseName) Janyl Jumadinova Computational Expression 21 October, 2019 9 / 13

  17. GradeBook class with instance variables Middle Compartment: - Attributes (data members) - Minus (-) sign indicates private member method - Data member name followed by colon and data type Janyl Jumadinova Computational Expression 21 October, 2019 10 / 13

  18. GradeBook class with instance variables Middle Compartment: - Attributes (data members) - Minus (-) sign indicates private member method - Data member name followed by colon and data type Bottom compartment: - Parentheses followed by colon and return data type GradeBook - courseName : String + setCourseName(String name) + getCourseName() : String + displayMessage() Janyl Jumadinova Computational Expression 21 October, 2019 10 / 13

  19. Constructor A special method that is used to initialize a newly created object. All Java objects have a constructor, even if you don’t specifically code it. Janyl Jumadinova Computational Expression 21 October, 2019 11 / 13

  20. Constructor A special method that is used to initialize a newly created object. All Java objects have a constructor, even if you don’t specifically code it. Default constructor created by the compiler: public GradeBook () { } Constructor is called just after the memory is allocated for the object. Constructors may contain code that is run when the object is created (initializes the object. A constructor must have the same name as the class its in. Janyl Jumadinova Computational Expression 21 October, 2019 11 / 13

  21. GradeBook class with a constructor Indicate a constructor using constructor before name Constructor usually listed first GradeBook - courseName : String <<constructor>> GradeBook (String name) + setCourseName(String name) + getCourseName() : String + displayMessage() Janyl Jumadinova Computational Expression 21 October, 2019 12 / 13

  22. Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook . Janyl Jumadinova Computational Expression 21 October, 2019 13 / 13

  23. Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook . Reference variables contain a reference to an object of the specified class. Janyl Jumadinova Computational Expression 21 October, 2019 13 / 13

  24. Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook . Reference variables contain a reference to an object of the specified class. Reference variables allow you send messages to objects, such as myGradeBook.displayMessage() . Janyl Jumadinova Computational Expression 21 October, 2019 13 / 13

  25. Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook . Reference variables contain a reference to an object of the specified class. Reference variables allow you send messages to objects, such as myGradeBook.displayMessage() . Primitive-type variables are not objects, so cannot receive messages. Janyl Jumadinova Computational Expression 21 October, 2019 13 / 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend