πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ“±

2.3 Calling a Void Method

7 min readβ€’june 18, 2024

Avanish Gupta

Avanish Gupta

user_sophia9212

user_sophia9212


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Introduction to Methods

Now, we will learn about methods. A method is a piece of code that is called in order to achieve a task. These methods may require input parameters, but some of them may not. In different programs, methods may change data, print text to the screen, move an object in a graphic, and so much more! In AP CSA, we will only scratch the surface of what methods can do. Different classes contain different methods, and the methods that objects of that class can use is called the object's behavior.

Void Methods vs. Non-Void Methods

One of the classifications that we can do between methods is if they are void methods or non-void methods. We will discuss non-void methods in Topic 2.5. Void methods do not return a value but instead change other things. Applications of void methods include changing characteristics of an object or printing text to the console. In a class, the header for a void method is of the following format, where the void keyword signifies that this method is a void method:
public void methodName(parameterList)
Like constructors, methods have method signatures, which contain the methodName and the parameter list. Some methods do not have a parameter list, so their signature is just this:
methodName()
Notice how method names also follow the naming conventions used by variable and object names and also use camelcase in terms of capitalization as seen above.

Static vs. Non-Static Methods

Another classification we use to distinguish how methods work is whether they are static or non-static methods. Static methods are general to the class and not tied to any particular object, such as changing the minimum wage in a city... it is the same for all workers in an area. Here is its implementation:
public static void incrementMinimumWage() { minimumWage++; }
The use of the static keyword is to signify that a method is a static method. To call a static method, we use dot notation, with the class name coming before the method separated by a dot as follows:
ClassName.incrementMinimumWage();
When calling a method, the normal flow of code is interrupted and the program runs the code in that is written in the method, then continuing with the program.
On the other hand, non-static methods act on a particular object. For example, printing a person's name is a non-static method, since each person has a different name. Here is its implementation:
public void printName() { System.out.println(name); }
When calling a non-static method, we also use dot notation. But, instead of using the class name, we use the object name so we know what object the method acts on. Also, we don't need to do dot notation of ClassName.objectName.methodName() because an object already acts on a certain class, so using the class name is just redundant. The proper way to call the printName() method is as follows:
objectName.printName();

Methods Practice Problems

Consider the following class definition.
public Dog()
{
Β Β Β Β name = "";
Β Β Β Β age = 0;
Β Β Β Β isTrained = false;
}
public void setName(String newName)
{
Β Β Β Β name = newName;
}
public void train()
{
Β Β Β Β isTrained = true;
}
Assume that a Dog object called myDog has been properly declared and initialized in a class other than Dog. Which of the following statements are valid?
A. myDog.setName("Buddy");
B. myDog.train(true);
C. myDog.age(5);
D. System.out.println( myDog.isTrained() );
E. myDog.setAge(3);

Answer: A. myDog.setName("Buddy");


Consider the following class definition.
public Car()
{
Β Β Β Β make = "";
Β Β Β Β model = "";
Β Β Β Β year = 0;
Β Β Β Β isNew = true;
}
public void setMake(String newMake)
{
Β Β Β Β make = newMake;
}
public void setModel(String newModel)
{
Β Β Β Β model = newModel;
}
public void setYear(int newYear)
{
Β Β Β Β year = newYear;
Β Β Β Β if (year < 2020)
Β Β Β Β {
Β Β Β Β Β Β Β Β isNew = false;
Β Β Β Β }
}
Assume that a Car object called myCar has been properly declared and initialized in a class other than Car. Which of the following statements are not valid?
A. myCar.setMake("Ford");
B. myCar.setModel(Mustang);
C. myCar.setYear(2022);
D. myCar.isNew();
E. myCar.setModel(β€œMustang”);

Answers:Β 
B. myCar.setModel(Mustang);
D. myCar.isNew();


Consider the following class definition.
public Student()
{
Β Β Β Β name = "";
Β Β Β Β age = 0;
Β Β Β Β grade = 0;
Β Β Β Β isEnrolled = false;
}
public void setName(String newName)
{
Β Β Β Β name = newName;
}
public void enroll()
{
Β Β Β Β isEnrolled = true;
}
Assume that a Student object called myStudent has been properly declared and initialized in a class other than Student. Which of the following statements are valid?
A. myStudent.setGrade(11);
B. myStudent.enroll(true);
C. myStudent.age(17);
D. System.out.println( myStudent.isEnrolled() );
E. myStudent.setName("Jane");

Answer: E. myStudent.setName("Jane");


Consider the following class definition.
public Book()
{
Β Β Β Β title = "";
Β Β Β Β author = "";
Β Β Β Β pages = 0;
Β Β Β Β isCheckedOut = false;
}
public void setTitle(String newTitle)
{
Β Β Β Β title = newTitle;
}
public void setAuthor(String newAuthor)
{
Β Β Β Β author = newAuthor;
}
public void checkOut()
{
Β Β Β Β isCheckedOut = true;
}
Assume that a Book object called myBook has been properly declared and initialized in a class other than Book. Which of the following statements are valid?
A. myBook.setTitle(To Kill a Mockingbird);
B. myBook.setAuthor("Harper Lee");
C. myBook.checkOut(true);
D. myBook.pages(250);
E. System.out.println( myBook.isCheckedOut() );

Answer: B. myBook.setAuthor("Harper Lee");


Consider the following class definition.
public Flower()
{
Β Β Β Β species = "";
Β Β Β Β color = "";
Β Β Β Β isBlooming = false;
Β Β Β Β isPoisonous = false;
}
public void setSpecies(String newSpecies)
{
Β Β Β Β species = newSpecies;
}
public void setColor(String newColor)
{
Β Β Β Β color = newColor;
}
public void startBlooming()
{
Β Β Β Β isBlooming = true;
}
Assume that a Flower object called myFlower has been properly declared and initialized in a class other than Flower. Which of the following statements are valid?
A. myFlower.setSpecies("Rose");
B. myFlower.setcolor("Red");
C. myFlower.startBlooming(true);
D. myFlower.isPoisonous(true);
E. System.out.println( myFlower.isBlooming() );

Answer: A. myFlower.setSpecies("Rose");


Consider the following class definition.
public House()
{
Β Β Β Β address = "";
Β Β Β Β numBedrooms = 0;
Β Β Β Β numBathrooms = 0;
Β Β Β Β isForSale = false;
}
public void setAddress(String newAddress)
{
Β Β Β Β address = newAddress;
}
public void setNumBedrooms(int newNumBedrooms)
{
Β Β Β Β numBedrooms = newNumBedrooms;
}
public void putOnMarket()
{
Β Β Β Β isForSale = true;
}
Assume that a House object called myHouse has been properly declared and initialized in a class other than House. Which of the following statements are valid?
A. myHouse.setAddress(123 Main Street);
B. myHouse.setNumBedrooms(β€œ3”);
C. myHouse.putOnMarket();
D. myHouse.numBathrooms(2);
E. System.out.println( myHouse.isForSale() );

Answer: C. myHouse.putOnMarket();


Consider the following class definition.
public Game()
{
Β Β Β Β title = "";
Β Β Β Β developer = "";
Β Β Β Β releaseYear = 0;
Β Β Β Β isMultiplayer = false;
}
public void setTitle(String newTitle)
{
Β Β Β Β title = newTitle;
}
public void setDeveloper(String newDeveloper)
{
Β Β Β Β developer = newDeveloper;
}
public void setMultiplayer(boolean newIsMultiplayer)
{
Β Β Β Β isMultiplayer = newIsMultiplayer;
}
Assume that a Game object called myGame has been properly declared and initialized in a class other than Game. Which of the following statements are valid?
A. myGame.setTitle("Fortnite");
B. myGame.setDeveloper("Epic Games");
C. myGame.setMultiplayer(true);
D. myGame.releaseYear(2017);
E. System.out.println( myGame.isMultiplayer() );

Answer:Β 
A. myGame.setTitle("Fortnite");
B. myGame.setDeveloper("Epic Games");


Are you getting more comfortable with doing these types of problems? Let’s practice a few more that are a little bit harder!

Consider the following class definition.
public class Dog
{
public void bark()
{
System.out.print("Bark ");
}
public void wagTail()
{
Β Β Β Β System.out.print("wag");
}
public void greetOwner()
{
Β Β Β Β bark();
Β Β Β Β wagTail();
}
/* Constructors not shown */
}
Which of the following code segments, if located in a method in a class other than Dog, will cause the message β€œBark wag” to be printed?
A.
Dog a = new Dog().greetOwner();
B.
Dog a = new Dog();
a.bark();
a.wagTail();
C.
Dog a = new Dog();
a.greetOwner();
D.
Dog a = new Dog();
Dog.bark();
Dog.wagTail();
E.
Dog a = new Dog();
a.bark();

Answers:
B.
Dog a = new Dog();
a.bark();
a.wagTail();
C.
Dog a = new Dog();
a.greetOwner();


Consider the following class definition.
public class Robot
{
public void moveForward()
{
System.out.print("Move forward ");
}
public void turnRight()
{
Β Β Β Β System.out.print("turn right");
}
public void navigateMaze()
{
Β Β Β Β turnRight();
Β Β Β Β moveForward();
}
/* Constructors not shown */
}
Which of the following code segments, if located in a method in a class other than Robot, will cause the message β€œMove forward turn right” to be printed?
A.
Robot a = new Robot();
a.moveForward();
B.
Robot a = new Robot().navigateMaze();
C.
Robot a = new Robot();
a.moveForward();
a.turnRight();
D.
Robot a = new Robot();
Robot.moveForward();
Robot.turnRight();
E.
Robot a = new Robot();
a.navigateMaze();

Answer:
D.
Robot a = new Robot();
Robot.moveForward();
Robot.turnRight();


Consider the following class definition.
public class Car
{
public void accelerate()
{
System.out.print("Accelerate ");
}
public void brake()
{
Β Β Β Β System.out.print("brake");
}
public void drive()
{
Β Β Β Β accelerate();
Β Β Β Β brake();
}
/* Constructors not shown */
}
Which of the following code segments, if located in a method in a class other than Car, will cause the message β€œAccelerate brake” to be printed?
A.
Car a = new Car();
a.accelerate();
B.
Car a = new Car().drive();
C.
Car a = new Car();
a.brake();
a.accelerate();
D.
Car a = new Car();
Car.accelerate();
Car.brake();
E.
Car a = new Car();
a.drive();

Answer:
E.
Car a = new Car();
a.drive();


What does the following code print out?
public class Song
{
public void play()
{
System.out.print("We are ");
never();
ever();
ever();
together();
}
public void together()
{
Β Β Β Β System.out.println("getting back together!");
}
public void never()
{
Β Β Β Β System.out.print("never ");
}
public void ever()
{
Β Β Β Β System.out.print("ever ");
}
public static void main(String[] args)
{
Β Β Β Β Song s = new Song();
Β Β Β Β s.play();
}
}
A. We are never ever ever getting back together!
B. We are never ever ever.
C. We are getting back together! never ever ever.
D. We are never ever ever together.
E. Nothing, it does not compile.

Answer: A. We are never ever ever getting back together! (bonus points for guessing the song!)
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.