Calling a Void

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

In deze les zitten 26 slides, met interactieve quizzen en tekstslides.

time-iconLesduur is: 15 min

Onderdelen in deze les

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

Slide 1 - Tekstslide

Deze slide heeft geen instructies

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 - Sleepvraag

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 - Sleepvraag

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 - Sleepvraag

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 - Sleepvraag

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 - Tekstslide

Deze slide heeft geen instructies

Slide 7 - Tekstslide

Deze slide heeft geen instructies

How to write a Method
public void printArea( )

Slide 8 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 - Tekstslide

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 vraag

Deze slide heeft geen instructies

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



Slide 17 - Tekstslide

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 - Tekstslide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 19 - Tekstslide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 20 - Tekstslide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 21 - Tekstslide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 22 - Tekstslide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 23 - Tekstslide

Non static method doesn't include the keyword static

How does Java flow between Class and Program?

Slide 24 - Tekstslide

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 - Quizvraag

Deze slide heeft geen instructies

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 - Quizvraag

Deze slide heeft geen instructies