Calling a Void

Monday Entry Task
Complete the Entry Task in Canvas when you arrive
1 / 26
next
Slide 1: Slide
AP Computer Science12th Grade

This lesson contains 26 slides, with interactive quizzes and text slides.

time-iconLesson duration is: 15 min

Items in this lesson

Monday Entry Task
Complete the Entry Task in Canvas when you arrive

Slide 1 - Slide

This item has no instructions

public class Student
{
    private String firstName;
    private String lastName;
    private int gradeLevel;
  
  public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
    }

    public String toString()
    {
        return firstName + " " + lastName + " is in grade: " + gradeLevel;
    }
}
Where is the Constructor is in the code?

Slide 2 - Drag question

Where is the constructor?
What part defines the behavior of an object? - the methods here the toString method returns and prints out.
Why is it important? methods allow you to DO something with objects' values.

public class Student
{
    private String firstName;
    private String lastName;
    private int gradeLevel;
  
  public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
    }

    public String toString()
    {
        return firstName + " " + lastName + " is in grade: " + gradeLevel;
    }
}
Where is the Constructor is in the code?

Slide 3 - Drag question

Where is the constructor?
What part defines the behavior of an object? - the methods here the toString method returns and prints out.
Why is it important? methods allow you to DO something with objects' values.

public class Student
{
    private String firstName;
    private String lastName;
    private int gradeLevel;
  
  public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
    }

    public String toString()
    {
        return firstName + " " + lastName + " is in grade: " + gradeLevel;
    }
}
What part of the class defines the behavior of an object?

Slide 4 - Drag question

Where is the constructor?
What part defines the behavior of an object? - the methods here the toString method returns and prints out.
Why is it important? methods allow you to DO something with objects' values.

public class Student
{
    private String firstName;
    private String lastName;
    private int gradeLevel;
  
  public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
    }

    public String toString()
    {
        return firstName + " " + lastName + " is in grade: " + gradeLevel;
    }
}
What part of the class defines the behavior of an object?
Methods!

Slide 5 - Drag question

Where is the constructor?
What part defines the behavior of an object? - the methods here the toString method returns and prints out.
Why is it important? methods allow you to DO something with objects' values.

Rectangle Class
public class Rectangle
{
    private int width;
    private int height;
    public Rectangle(int rectWidth, int rectHeight)
    {
        width = rectWidth;
        height = rectHeight;
    }
    public int getArea()
    {
        return width * height;
    }
    public int getHeight()
    {
        return height;
    }
    public int getWidth()
    {
        return width;
    }
    public String toString()
    {
        return "Rectangle with width: " + width + " and height: " + height;
    }
}
An implementation of Rectangle class!

Rectangle Class

public class Rectangle
{
    private int width;
    private int height;
    public Rectangle(int rectWidth, int rectHeight)
    {
        width = rectWidth;
        height = rectHeight;
    }
    public int getArea()
    {
        return width * height;
    }
    public int getHeight()
    {
        return height;
    }
    public int getWidth()
    {
        return width;
    }
    public String toString()
    {
        return "Rectangle with width: " + width + " and height: " + height;
    }
}
An implementation of Rectangle class!









Slide 6 - Slide

This item has no instructions

Slide 7 - Slide

This item has no instructions

How to write a Method
public void printArea( )

Slide 8 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

How to write a Method
public void printArea( )

public is an access specifier which tells us who has access to use the method when writing classes and objects.

Slide 9 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

How to write a Method
public void printArea( )

void is the return type which tells us what type value is being returned from the method.
Void return type means the method is not returning any value!

Slide 10 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

How to write a Method
public void printArea( )

printArea is the method name that identifies the method's purpose. 
Remember must be in lowerCamelCase!

Slide 11 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

How to write a Method
public void printArea( )

( ) is the parameter list just like a Constructor might have a parameter list, so do Methods.  In this case, parameters are not needed.

Slide 12 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

How to write a Method
                                public void printArea( )
                              {
                                   //step one - get area of rectangle
                                   //step two - print area
                              }



Slide 13 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

How to write a Method
                                public void printArea( )
                              {
                                   int area = width * height
                                   //step two - print area
                              }



Slide 14 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

How to write a Method
           public void printArea( )
           {
               int area = width * height
               //step two - print area
           }



Slide 15 - Slide

Constructors are the signature of the class.

Methods have signatures as well that provide information about how it will function.

What can I add to print the area?

Slide 16 - Open question

This item has no instructions

How to write a Method
                                public void printArea( )
                              {
                                   int area = width * height
                                   System.out.println(area);
                              }



Slide 17 - Slide

Because method is printing area and not returning to program, this is a void method.

How can we use a Method?
We have to call a Method to use it...

To call a Method in our program, we would write:
objectName.method( );

Slide 18 - Slide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 19 - Slide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 20 - Slide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 21 - Slide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 22 - Slide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 23 - Slide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 24 - Slide

Non static method doesn't include the keyword static

How do you write a Method?
A
public void printArea( )
B
private void printArea( )
C
printArea( )
D
objectName.method( )

Slide 25 - Quiz

This item has no instructions

How can we use a Method in our program?
A
System.out.print(area)
B
method.ObjectName( );
C
objectName.method( );
D
int area = width * height

Slide 26 - Quiz

This item has no instructions