Reputation: 47
I have gotten this code to work out so far, but I am trying to learn how to complete this method so that it will compile the total amount of sides used and print that value on in the last print line. The problem method is getTotalSides, I currently have it set to return 0, but I want it to return the total sides instead, meaning: mp1.getSides() + mp2.getSides() + mp3.getSides() + mp4.getSides().
public class TestParts {
public static void main(String[] args) {
MyPolygon mp1 = new MyPolygon();
MyPolygon mp2 = new MyPolygon(4);
MyPolygon mp3 = new MyPolygon(5);
MyPolygon mp4 = new MyPolygon(6);
System.out.println("Polygon 1 has " + mp1.getSides()
+ " sides, angles total " + mp1.getSumOfAngles());
System.out.println("Polygon 2 has " + mp2.getSides()
+ " sides, angles total " + mp2.getSumOfAngles());
System.out.println("Polygon 3 has " + mp3.getSides()
+ " sides, angles total " + mp3.getSumOfAngles());
System.out.println("Polygon 4 has " + mp4.getSides()
+ " sides, angles total " + mp4.getSumOfAngles());
System.out.println("There are " + MyPolygon.getTotalSides()
+ " total sides");
}
}
class MyPolygon {
double getSides;
MyPolygon() {
getSides = 3;
}
static double getTotalSides() {
return 0;
}
double getSides() {
// TODO Auto-generated method stub
return getSides;
}
MyPolygon(double newGetSides) {
getSides = newGetSides;
}
double getSumOfAngles() {
return ((getSides - 2) * 180);
}
void setGetSides(double newGetSides) {
getSides = newGetSides;
}
Upvotes: 0
Views: 375
Reputation: 4330
If you want global information on the number of all sides ever created, try a static variable:
class MyPolygon {
private static int totalSides;
private int sides;
MyPolygon() {
this(3); // constructor chaining: really useful
}
MyPolygon(int numSides) {
this.sides = numSides;
MyPolygon.totalSides += numSides;
}
// ... the rest, incl. getter for sides and static getter for totalSides
}
However, when it may happen that you do not need one polygon anymore, you have to decrease the number explicitly again somehow...
Upvotes: 0
Reputation: 61138
You need look at the scope
of your objects, maybe you could use some sort of Collection
public class TestParts {
public static void main(String[] args) {
final MyPolygon mp1 = MyPolygon.newInstance(3);
final MyPolygon mp2 = MyPolygon.newInstance(4);
final MyPolygon mp3 = MyPolygon.newInstance(5);
final MyPolygon mp4 = MyPolygon.newInstance(6);
System.out.println("Polygon 1 has " + mp1.getSides()
+ " sides, angles total " + mp1.getSumOfAngles());
System.out.println("Polygon 2 has " + mp2.getSides()
+ " sides, angles total " + mp2.getSumOfAngles());
System.out.println("Polygon 3 has " + mp3.getSides()
+ " sides, angles total " + mp3.getSumOfAngles());
System.out.println("Polygon 4 has " + mp4.getSides()
+ " sides, angles total " + mp4.getSumOfAngles());
System.out.println("There are " + MyPolygon.getTotalSides()
+ " total sides");
}
}
class MyPolygon {
private static final Collection<MyPolygon> POLYGONS = new LinkedList<MyPolygon>();
private final double sides;
private MyPolygon(final double sides) {
this.sides = sides;
}
public static MyPolygon newInstance(final double sides) {
final MyPolygon polygon = new MyPolygon(sides);
POLYGONS.add(polygon);
return polygon;
}
public static double getTotalSides() {
double sides = 0d;
for (final MyPolygon polygon : POLYGONS) {
sides += polygon.getSides();
}
return sides;
}
public double getSides() {
return sides;
}
public double getSumOfAngles() {
return ((sides - 2) * 180);
}
}
I have tidied your code so it conforms to naming conventions (no getSides
variable).
Upvotes: 0
Reputation: 36449
If you really need a method, you can use varargs (variable arity arguments) for this purpose:
static double getTotalSides(MyPolygon... polygons) {
double x = 0;
for (MyPolygon p: polygons)
{
x+= p.getSides();
}
return x;
}
Then call like this:
System.out.println("There are " + MyPolygon.getTotalSides(mp1,mp2,mp3,mp4)
+ " total sides");
or make an array
MyPolygon myPolygons = new MyPolygon [4];
myPolygons [0] = mp1;
myPolygons [1] = mp2;
myPolygons [2] = mp3;
myPolygons [3] = mp4;
System.out.println("There are " + MyPolygon.getTotalSides(myPolygons)
+ " total sides");
However, the better solution is to store your Polygons in an array/List from the beginning then pass that whole array/List them off to the method, do the loop, and return the result. Be aware though, that Lists and arrays are different, and so, you will neeed to modify the method signature accordingly.
Upvotes: 1
Reputation: 143
Why don't you just put the expression that you wrote out in:
System.out.println("There are " + (mp1.getSides() + mp2.getSides() + mp3.getSides() + mp4.getSides()) + " total sides");
Since you are trying to write getTotalSides as part of the class, there is not a simple way to check for all other instantiated objects and call their method getSides to add up all the total sides. You could define a function that takes all the polygon objects and adds up the sides, but it can't be under the class definition.
Upvotes: 0