All pastes #1626627 Raw Edit

lol

public text v1 · immutable
#1626627 ·published 2009-10-18 05:33 UTC
rendered paste body
/* 
 * - receba o numero de vertices;
 * - receba N pares de coordenadas(x, y) dos vertices do poligono em arrays adequados;
 * - calcule e mostre a area seguindo a formula do enunciado;
 *
 *   Falta:
 * - Dar a resposta correta.
 *
 */
import java.util.Scanner;

class Vertice
{
    private int x, y;

    public void setCoordenadas(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public int getX() 
    {
        return x;
    }
    public int getY() 
    {
        return y;
    }
}

public class exerc20 
{
    public static void main(String[] args)
    {
        Scanner s;
        int n,
            i,
            x, y,
            a, b, c,
            soma = 0;
        Vertice[] v;

        s = new Scanner(System.in);

        System.out.println("Digite o numero de vertices: ");
        n = s.nextInt();
        v = new Vertice[n];

        for (i = 0; i < n; i++) {
            v[i] = new Vertice();
            System.out.println("Digite as coordenadas do vertice " +
                    (i + 1) + "(separados por espaco ou quebra-de-linha):");
            x = s.nextInt();
            y = s.nextInt();
            v[i].setCoordenadas(x, y);
        }
        for (i = 0; i < n; i++) {
            if (i == 0) {
                soma += (v[i].getX() * v[i + 1].getY() -
                        v[i].getX() * v[i].getY());
            } else if (i == (n - 1)) {
                soma += (v[i].getX() * v[1].getY() -
                        v[i].getX() * v[i - 1].getY());
            } else {
                soma += (v[i].getX() * v[i + 1].getY() -
                        v[i].getX() * v[i - 1].getY());
            }
        }
        System.out.println(soma / 2);
    }
}