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 Computational Expression 21 October, 2019 2 / 13
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
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
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
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
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
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
Janyl Jumadinova Computational Expression 21 October, 2019 4 / 13
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
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
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
GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered Janyl Jumadinova Computational Expression 21 October, 2019 8 / 13
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
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
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
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
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
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
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
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
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
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
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
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
Recommend
More recommend