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

computational expression
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Computational Expression

Classes Janyl Jumadinova 21 October, 2019

Janyl Jumadinova Computational Expression 21 October, 2019 1 / 13

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 9

Janyl Jumadinova Computational Expression 21 October, 2019 4 / 13

slide-10
SLIDE 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

slide-11
SLIDE 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

  • bject of this class.

Janyl Jumadinova Computational Expression 21 October, 2019 6 / 13

slide-12
SLIDE 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

slide-13
SLIDE 13

GradeBook class UML Diagram

Top compartment:

  • Name of class, Bolded, Centered

Janyl Jumadinova Computational Expression 21 October, 2019 8 / 13

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 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