Introduction to Computer Science I Classes Janyl Jumadinova 5-7 March, 2018
Classes Most of our previous programs all just had a main() method in one file. 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. 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. 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 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? 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/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? 3/13
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!"); } } 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. 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(); } } 7/13
GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered 8/13
GradeBook class UML Diagram Top compartment: - Name of class, Bolded, Centered Middle compartment: - Attributes (data members) 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 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 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 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 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. 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 Dog () { } 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. 11/13
GradeBook class with a constructor Indicate a constructor using constructor before name Constructor usually listed first 12/13
Reference Data Types Reference data types, also known as non-primitive types, are user-defined classes such as Dog or GradeBook. 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. 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(). 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. 13/13