Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

Desenvolva uma classe chamada Octagon para representar um octógono, ou seja, um polígono regular com oito lados de mesmo comprimento. A classe possui um único atributo denominado side, do tipo double, que representa o lado do octógono e cujo valor deve ser maior ou igual a zero e menor ou igual a vinte. O lado do octógono pode ser obtido e alterado pelo usuário por meio dos métodos getSide() e setSide(), respectivamente. A classe também apresenta os métodos area() e perimeter(), que retornam a área e o perímetro do octógono, respectivamente. A área de um octógono de lado l é obtida pela fórmula 2 * (√2 + 1) * l2. O perímetro de um octógono de lado l é obtido pela fórmula 8 * l.

 

Implementação na Linguagem de Programação Java Implementação na Linguagem de Programação C++
Diagrama de Classes na Linguagem de Programação Java RegularPolygon.java Octagon.java Application.java Saída da Implementação na Linguagem de Programação Java
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação Java

Arquivo RegularPolygon.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial05.exercicio05;

/**
 * Classe responsavel pela representacao de um poligono regular
 */
public abstract class RegularPolygon
{
  /**
   * Lado do poligono regular
   */
  private double side;

  /**
   * Construtor padrao
   *
   * @throws IllegalArgumentException valor do lado do poligono regular invalido
   */
  public RegularPolygon() throws IllegalArgumentException
  {
    this(0.0);
  }

  /**
   * Construtor para inicializar o lado do poligono regular
   *
   * @param side lado do poligono regular
   * @throws IllegalArgumentException valor do lado do poligono regular invalido
   */
  public RegularPolygon(double side) throws IllegalArgumentException
  {
    setSide(side);
  }

  /**
   * Retornar o lado do poligono regular
   *
   * @return lado do poligono regular
   */
  public double getSide()
  {
    return side;
  }

  /**
   * Configurar o lado do poligono regular
   *
   * @param side lado do poligono regular
   * @throws IllegalArgumentException valor do lado do poligono regular invalido
   */
  public void setSide(double side) throws IllegalArgumentException
  {
    if((side >= 0.0) && (side <= 20.0))
    {
      this.side = side;
    }
    else
    {
      throw new IllegalArgumentException("O lado do polígono regular deve estar contido entre 0.0 e 20.0: '" + side + "'");
    }
  }

  /**
   * Retornar a area do poligono regular
   *
   * @return area do poligono regular
   */
  public abstract double area();

  /**
   * Retornar o perimetro do poligono regular
   *
   * @return perimetro do poligono regular
   */
  public abstract double perimeter();
}

Arquivo Octagon.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial05.exercicio05;

/**
 * Classe responsavel pela representacao de um octogono
 */
public class Octagon extends RegularPolygon
{
  /**
   * Construtor padrao
   *
   * @throws IllegalArgumentException valor do lado do octogono invalido
   */
  public Octagon() throws IllegalArgumentException
  {
    super();
  }
  
  /**
   * Construtor para inicializar o lado do octogono
   *
   * @param side lado do octogono
   * @throws IllegalArgumentException valor do lado do octogono invalido
   */
  public Octagon(double side) throws IllegalArgumentException
  {
    super(side);
  }

  /**
   * Retornar a area do octogono
   *
   * @return area do octogono
   */
  public double area()
  {
    return 2.0 * (Math.sqrt(2.0) + 1.0) * Math.pow(getSide(), 2.0);
  }

  /**
   * Retornar o perimetro do octogono
   *
   * @return perimetro do octogono
   */
  public double perimeter()
  {
    return 8.0 * getSide();
  }
}

Arquivo Application.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial05.exercicio05;

/**
 * Classe responsavel pela execucao da classe Octagon
 */
public class Application
{
  /**
   * Construtor padrao
   */
  private Application()
  {

  }

  /**
   * Metodo principal da linguagem de programacao Java
   *
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    try
    {
      Octagon octagon = new Octagon(5.0);

      System.out.print("O lado do octógono é: ");
      System.out.println(octagon.getSide());

      System.out.print("A área do octógono é: ");
      System.out.println(octagon.area());

      System.out.print("O perímetro do octógono é: ");
      System.out.println(octagon.perimeter());
    }
    catch(IllegalArgumentException exception)
    {
      exception.printStackTrace();
    }
    catch(Exception exception)
    {
      exception.printStackTrace();
    }
  }
}

Saída da Implementação na Linguagem de Programação Java

O lado do octógono é: 5.0
A área do octógono é: 120.71067811865474
O perímetro do octógono é: 40.0
Diagrama de Classes na Linguagem de Programação C++ Exception.hpp Exception.cpp IllegalArgumentException.hpp IllegalArgumentException.cpp RegularPolygon.hpp RegularPolygon.cpp Octagon.hpp Octagon.cpp Application.cpp makefile Saída da Implementação na Linguagem de Programação C++
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação C++

Arquivo Exception.hpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#ifndef EXCEPTION_HPP
#define EXCEPTION_HPP

#include <string>

/**
 * Excecao padrao
 */
class Exception
{
  public:

  /**
   * Construtor padrao
   */
  Exception();

  /**
   * Construtor para inicializar a mensagem de erro
   *
   * @param message mensagem de erro
   */
  Exception(std::string message);

  /**
   * Retornar a mensagem de erro
   *
   * @return mensagem de erro
   */
  std::string getMessage();

  private:

  /**
   * Mensagem de erro
   */
  std::string message;
};

#endif

Arquivo Exception.cpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#include "Exception.hpp"

/**
 * Construtor padrao
 */
Exception::Exception()
{
  message = '\0';
}

/**
 * Construtor para inicializar a mensagem de erro
 *
 * @param message mensagem de erro
 */
Exception::Exception(std::string message)
{
  Exception::message = message;
}

/**
 * Retornar a mensagem de erro
 *
 * @return mensagem de erro
 */
std::string Exception::getMessage()
{
  return message;
}

Arquivo IllegalArgumentException.hpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#ifndef ILLEGALARGUMENTEXCEPTION_HPP
#define ILLEGALARGUMENTEXCEPTION_HPP

#include "Exception.hpp"

/**
 * Excecao lancada caso o metodo receba um parametro invalido
 */
class IllegalArgumentException : public Exception
{
  public:

  /**
   * Construtor padrao
   */
  IllegalArgumentException();

  /**
   * Construtor para inicializar a mensagem de erro
   *
   * @param message mensagem de erro
   */
  IllegalArgumentException(std::string message);
};

#endif

Arquivo IllegalArgumentException.cpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#include "IllegalArgumentException.hpp"

/**
 * Construtor padrao
 */
IllegalArgumentException::IllegalArgumentException()
                         :Exception()
{

}

/**
 * Construtor para inicializar a mensagem de erro
 *
 * @param message mensagem de erro
 */
IllegalArgumentException::IllegalArgumentException(std::string message)
                         :Exception(message)
{

}

Arquivo RegularPolygon.hpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#ifndef REGULARPOLYGON_HPP
#define REGULARPOLYGON_HPP

#include "IllegalArgumentException.hpp"

/**
 * Classe responsavel pela representacao de um poligono regular
 */
class RegularPolygon
{
  public:

  /**
   * Construtor padrao
   *
   * @throws IllegalArgumentException valor do lado do poligono regular
   *   invalido
   */
  RegularPolygon() throw (IllegalArgumentException);

  /**
   * Construtor para inicializar o lado do poligono regular
   *
   * @param side lado do poligono regular
   * @throws IllegalArgumentException valor do lado do poligono regular
   *   invalido
   */
  RegularPolygon(double side) throw (IllegalArgumentException);

  /**
   * Destructor
   */
  ~RegularPolygon();

  /**
   * Retornar o lado do poligono regular
   *
   * @return lado do poligono regular
   */
  double getSide();

  /**
   * Configurar o lado do poligono regular
   *
   * @param side lado do poligono regular
   * @throws IllegalArgumentException valor do lado do poligono regular
   *   invalido
   */
  void setSide(double side) throw (IllegalArgumentException);

  /**
   * Retornar a area do poligono regular
   *
   * @return area do poligono regular
   */
  virtual double area() = 0;

  /**
   * Retornar o perimetro do poligono regular
   *
   * @return perimetro do poligono regular
   */
  virtual double perimeter() = 0;

  private:

  /**
   * Lado do poligono regular
   */
  double side;
};

#endif

Arquivo RegularPolygon.cpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#include <sstream>

#include "RegularPolygon.hpp"

/**
 * Construtor padrao
 *
 * @throws IllegalArgumentException valor do lado do poligono regular
 *   invalido
 */
RegularPolygon::RegularPolygon() throw (IllegalArgumentException)
{
  setSide(0.0);
}

/**
 * Construtor para inicializar o lado do poligono regular
 *
 * @param side lado do poligono regular
 * @throws IllegalArgumentException valor do lado do poligono regular
 *   invalido
 */
RegularPolygon::RegularPolygon(double side) throw (IllegalArgumentException)
{
  setSide(side);
}

/**
 * Destructor
 */
RegularPolygon::~RegularPolygon()
{

}

/**
 * Retornar o lado do poligono regular
 *
 * @return lado do poligono regular
 */
double RegularPolygon::getSide()
{
  return side;
}

/**
 * Configurar o lado do poligono regular
 *
 * @param side lado do poligono regular
 * @throws IllegalArgumentException valor do lado do poligono regular
 *   invalido
 */
void RegularPolygon::setSide(double side) throw (IllegalArgumentException)
{
  if((side >= 0.0) && (side <= 20.0))
  {
    RegularPolygon::side = side;
  }
  else
  {
    std::stringstream buffer;

    buffer << "O lado do polígono regular deve estar contido "
           << "entre 0.0 e 20.0: '" << side << "'";

    throw IllegalArgumentException(buffer.str());
  }
}

Arquivo Octagon.hpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#ifndef OCTAGON_HPP
#define OCTAGON_HPP

#include "RegularPolygon.hpp"

/**
 * Classe responsavel pela representacao de um octogono
 */
class Octagon : public RegularPolygon
{
  public:

  /**
   * Construtor padrao
   *
   * @throws IllegalArgumentException valor do lado do octogono invalido
   */
  Octagon() throw (IllegalArgumentException);

  /**
   * Construtor para inicializar o lado do octogono
   *
   * @param side lado do octogono
   * @throws IllegalArgumentException valor do lado do octogono invalido
   */
  Octagon(double side) throw (IllegalArgumentException);

  /**
   * Destructor
   */
  ~Octagon();

  /**
   * Retornar a area do octogono
   *
   * @return area do octogono
   */
  double area();

  /**
   * Retornar o perimetro do octogono
   *
   * @return perimetro do octogono
   */
  double perimeter();
};

#endif

Arquivo Octagon.cpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#include <cmath>

#include "Octagon.hpp"

/**
 * Construtor padrao
 *
 * @throws IllegalArgumentException valor do lado do octogono invalido
 */
Octagon::Octagon() throw (IllegalArgumentException)
       : RegularPolygon() 
{

}

/**
 * Construtor para inicializar o lado do octogono
 *
 * @param side lado do octogono
 * @throws IllegalArgumentException valor do lado do octogono invalido
 */
Octagon::Octagon(double side) throw (IllegalArgumentException)
       : RegularPolygon(side)
{

}

/**
 * Destructor
 */
Octagon::~Octagon()
{

}

/**
 * Retornar a area do octogono
 *
 * @return area do octogono
 */
double Octagon::area()
{
  return 2.0 * (sqrt(2.0) + 1.0) * pow(getSide(), 2.0);
}

/**
 * Retornar o perimetro do octogono
 *
 * @return perimetro do octogono
 */
double Octagon::perimeter()
{
  return 8.0 * getSide();
}

Arquivo Application.cpp

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                           *
 *************************************************************************/

#include <iostream>

#include "Octagon.hpp"

using namespace std;

/**
 * Metodo principal da linguagem de programacao C++
 *
 * @param argc quantidade de argumentos na linha de comando (nao utilizado)
 * @param argv argumentos da linha de comando (nao utilizado)
 */
int main(int argc, char** argv)
{
  try
  {
    Octagon* octagon = new Octagon(5.0);

    cout << "O lado do octógono é: "
         << octagon->getSide() << endl;

    cout << "A área do octógono é: "
         << octagon->area() << endl;

    cout << "O perímetro do octógono é: "
         << octagon->perimeter() << endl;

    delete octagon;
  }
  catch(IllegalArgumentException exception)
  {
    cerr << exception.getMessage() << endl;
  }
  catch(...)
  {
    cerr << "Exceção desconhecida" << endl;
  }

  return 0;
}

Arquivo makefile

##########################################################################
 # Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  #
 #                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  #
 #                                                                       #
 # Permission is granted to copy, distribute and/or modify this document #
 # under the terms of the GNU Free Documentation License, Version 1.3 or #
 # any later version published by the  Free Software Foundation; with no #
 # Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A #
 # A copy of the  license is included in  the section entitled "GNU Free #
 # Documentation License".                                               #
 #                                                                       #
 # Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             #
 # gcc/g++ (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005                       #
 ##########################################################################

g++ -o Exception.o -c Exception.cpp

g++ -o IllegalArgumentException.o -c IllegalArgumentException.cpp

g++ -o RegularPolygon.o -c RegularPolygon.cpp

g++ -o Octagon.o -c Octagon.cpp

g++ -o Application.o -c Application.cpp

g++ -o application Exception.o IllegalArgumentException.o RegularPolygon.o Octagon.o Application.o

Saída da Implementação na Linguagem de Programação C++

O lado do octógono é: 5
A área do octógono é: 120.711
O perímetro do octógono é: 40