简体   繁体   中英

Issue with using array in a class Java

I am trying to build a Java program based on this UML: UML of Polygon Class

But I ran into a few hiccups along the way. This is my basic code:

import java.util.Scanner;

public class Polygon {
        private int[] side;
        private double perimeter;
        
        public double addSide(double length[]) {
                int i = 0;
                double perimeter = 0;
                while(length[i] > 0){
                    perimeter += (double)length[i];
                    i++;
                }
                return perimeter;          
        }
        public int[] getSides() {return side;}
        public double getPerimeter() {return perimeter;}
        
        public static void main(String[] args) {
                Polygon polygon=new Polygon();
                polygon.side = new int[99];
                int i=0;
                do{
                    System.out.print("Side length(0 when done): ");
                    Scanner in = new Scanner(System.in);
                    polygon.side[i] = in.nextInt();
                    i++;
                }while(polygon.side[i]>0);
                //polygon.perimeter = addSide((double)polygon.side);
                
                System.out.println("Perimeter of " + i + "-sided polygon: " + polygon.getPerimeter());
        }
        

}

There's a couple of issues.

  1. I got it to compile but when it accepts the first side[0], it immediately stops and gives me the perimeter. Exiting the loop eventhough the conditions haven't been met for it to so. So there's an issue with my while-loop. I want it to keep accepting values into the side[] array until a non-positive value is entered.

  2. Also the UML requires I use double parameter-type for the addSide method. I tried to cast it in the argument and tried a couple of other different things with no success. How would one transition an int-array into a double-array for the perimeter calucalation which has to be double as per the requirements.

  3. I wouldn't surprised if I made other issues since I'm new to Java so feel free to point them out to me or if you have a better way to go about this, I would love to learn your thinking.

Any advice is appreciated!

There are a number of issues with your code.

First, differences from the UML specification:

  1. You haven't used the given signature for addSide . The UML says that it takes a single double parameter, and returns nothing, ie void in Java. You are passing an array of double and returning a double .
  2. You are directly accessing sides in your main method. Java allows you to do this, because your main method is part of the Polygon class, but the UML shows that the field is private. What does direct manipulation of sides do to the validity of the value in perimeter ?
  3. The UML shows the class having a field sides of type int . Your field sides is of type int[] .
  4. Similarly you haven't used the given signature for getSides , which should probably have been named getNumberOfSides .

Your code has quite a few other issues, but I think you should fix the issues above first.

A futher hint: The only things that the Polygon class can do is to tell you how many sides it has and what its total perimeter is. It does not care about the details of individual sides.

(Off topic, it is strange to include main in the UML description of Polygon )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM