<< Hide Menu
Avanish Gupta
user_sophia9212
Avanish Gupta
user_sophia9212
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.
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.
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();
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!)
© 2024 Fiveable Inc. All rights reserved.