7.7k views
5 votes
Write a void method named updateNumSides which takes a RegularPolygon and an integer, and that sets the number of sides of the polygon to the integer

You can call your method in the program's main method so you can test whether it works, but you must remove or comment out the main method before checking your code for a score.

in java

User Andrew Fan
by
6.8k points

1 Answer

3 votes

Answer:

Here is an example of a void method named updateNumSides that takes a RegularPolygon object and an integer as parameters and sets the number of sides of the polygon to the integer:

public class RegularPolygon {

private int numSides;

// other fields and methods

public void updateNumSides(RegularPolygon polygon, int num) {

polygon.numSides = num;

}

}

You can test the method by calling it in the main method and passing a RegularPolygon object and an integer as arguments:

public static void main(String[] args) {

RegularPolygon polygon = new RegularPolygon();

updateNumSides(polygon, 6);

System.out.println(polygon.getNumSides()); // should print 6

}

Note that the RegularPolygon class should have a getter method for the numSides variable.

It's important to comment or remove the main method before checking your code for a score.

User Netrolite
by
7.0k points