89.3k views
3 votes
Write a class called SimpleSquare that has the following properties: - public int field called num - private int field called square - a constructor that takes a single parameter that sets the num, then sets square to num to the second power

1 Answer

7 votes

Answer:

public class SimpleSquare{

public int num;

private int square;

public SimpleSquare(int number){

num = number;

square = number * number;

}

public int getSquare(){

return square;

}

}

Step-by-step explanation:

*The code is in Java.

Create a class called SimpleSquare

Declare two fields, num and square

Create a constructor that takes an integer number as a parameter, sets the num and sets the square as number * number.

Since the square is a private field, I also added the getSquare() method which returns the value of the square.

User Kevswanberg
by
6.9k points