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

Desenvolva uma classe chamada Dodecahedron para representar um dodecaedro, ou seja, um poliedro regular de doze faces, considerado um dos cinco Sólidos Platônicos. A classe possui um único atributo denominado edge, do tipo double, que representa a aresta do dodecaedro e cujo valor deve ser maior ou igual a zero e menor ou igual a trinta. A classe possui dois construtores: o primeiro configura a aresta do dodecaedro com o valor padrão 1.0, e o segundo recebe como parâmetro a aresta do dodecaedro. A aresta do dodecaedro pode ser obtida e alterada pelo usuário por meio dos métodos getEdge() e setEdge(), respectivamente. A classe também apresenta os métodos area() e volume(), que retornam a área e o volume do dodecaedro, respectivamente. A área de um dodecaedro de aresta a é obtida pela fórmula 3 * √(25 + 10 * √5) * a2. O volume de um dodecaedro de aresta a é obtido pela fórmula 1 / 4 * (15 + 7 * √5) * a3.

 

Terminal

ybadoo@server:~$ ./application
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 Dodecahedron.java Application.java
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação Java

Arquivo Dodecahedron.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.tutorial02.exercicio13;

/**
 * Classe responsavel pela representacao de um dodecaedro
 */
public class Dodecahedron
{
  /**
   * Aresta do dodecaedro
   */
  private double edge;

  /**
   * Construtor para inicializar o dodecaedro
   */
  public Dodecahedron()
  {
    setEdge(1.0);
  }

  /**
   * Construtor para inicializar a aresta do dodecaedro
   *
   * @param edge aresta do dodecaedro
   */
  public Dodecahedron(double edge)
  {
    setEdge(edge);
  }

  /**
   * Retornar a aresta do dodecaedro
   *
   * @return aresta do dodecaedro
   */
  public double getEdge()
  {
    return edge;
  }

  /**
   * Configurar a aresta do dodecaedro
   *
   * @param edge aresta do dodecaedro
   */
  public void setEdge(double edge)
  {
    if((edge >= 0.0) && (edge <= 30.0))
    {
      this.edge = edge;
    }
    else
    {
      if(edge > 30.0)
      {
        this.edge = 30.0;
      }
      else
      {
        this.edge = 0.0;
      }
    }
  }

  /**
   * Retornar a area do dodecaedro
   *
   * @return area do dodecaedro
   */
  public double area()
  {
    return 3.0 * Math.sqrt(25.0 + 10.0 * Math.sqrt(5.0))
      * Math.pow(edge, 2.0);
  }

  /**
   * Retornar o volume do dodecaedro
   *
   * @return volume do dodecaedro
   */
  public double volume()
  {
    return 1.0 / 4.0 * (15.0 + 7.0 * Math.sqrt(5.0)) * Math.pow(edge, 3.0);
  }
}

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.tutorial02.exercicio13;

import java.util.Scanner;

/**
 * Classe responsavel pela execucao da classe Dodecahedron
 */
public class Application
{
  /**
   * Construtor para inicializar a execucao da classe Dodecahedron
   */
  private Application()
  {

  }

  /**
   * Metodo principal da linguagem de programacao Java
   *
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Forneça o valor da aresta do dodecaedro: ");

    double edge = scanner.nextDouble();

    scanner.close();

    Dodecahedron dodecahedron = new Dodecahedron(edge);

    System.out.print("A aresta do dodecaedro é: ");
    System.out.println(dodecahedron.getEdge());

    System.out.print("A área do dodecaedro é: ");
    System.out.println(dodecahedron.area());

    System.out.print("O volume do dodecaedro é: ");
    System.out.println(dodecahedron.volume());
  }
}
Diagrama de Classes na Linguagem de Programação C++ Dodecahedron.hpp Dodecahedron.cpp Application.cpp makefile
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação C++

Arquivo Dodecahedron.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 DODECAHEDRON_HPP
#define DODECAHEDRON_HPP

/**
 * Classe responsavel pela representacao de um dodecaedro
 */
class Dodecahedron
{
  public:

  /**
   * Construtor para inicializar o dodecaedro
   */
  Dodecahedron();

  /**
   * Construtor para inicializar a aresta do dodecaedro
   *
   * @param edge aresta do dodecaedro
   */
  Dodecahedron(const double edge);

  /**
   * Retornar a aresta do dodecaedro
   *
   * @return aresta do dodecaedro
   */
  double getEdge() const;

  /**
   * Configurar a aresta do dodecaedro
   *
   * @param edge aresta do dodecaedro
   */
  void setEdge(const double edge);

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

  /**
   * Retornar o volume do dodecaedro
   *
   * @return volume do dodecaedro
   */
  double volume() const;

  private:

  /**
   * Aresta do dodecaedro
   */
  double edge;
};

#endif

Arquivo Dodecahedron.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 "Dodecahedron.hpp"

/**
 * Construtor para inicializar o dodecaedro
 */
Dodecahedron::Dodecahedron()
{
  setEdge(1.0);
}

/**
 * Construtor para inicializar a aresta do dodecaedro
 *
 * @param edge aresta do dodecaedro
 */
Dodecahedron::Dodecahedron(const double edge)
{
  setEdge(edge);
}

/**
 * Retornar a aresta do dodecaedro
 *
 * @return aresta do dodecaedro
 */
double Dodecahedron::getEdge() const
{
  return edge;
}

/**
 * Configurar a aresta do dodecaedro
 *
 * @param edge aresta do dodecaedro
 */
void Dodecahedron::setEdge(const double edge)
{
  if((edge >= 0.0) && (edge <= 30.0))
  {
    Dodecahedron::edge = edge;
  }
  else
  {
    if(edge > 30.0)
    {
      Dodecahedron::edge = 30.0;
    }
    else
    {
      Dodecahedron::edge = 0.0;
    }
  }
}

/**
 * Retornar a area do dodecaedro
 *
 * @return area do dodecaedro
 */
double Dodecahedron::area() const
{
  return 3.0 * sqrt(25.0 + 10.0 * sqrt(5.0)) * pow(edge, 2.0);
}

/**
 * Retornar o volume do dodecaedro
 *
 * @return volume do dodecaedro
 */
double Dodecahedron::volume() const
{
  return 1.0 / 4.0 * (15.0 + 7.0 * sqrt(5.0)) * pow(edge, 3.0);
}

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 "Dodecahedron.hpp"

/**
 * 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)
{
  using namespace std;

  double edge;

  cout << "Forneça o valor da aresta do dodecaedro: ";
  cin  >> edge;

  Dodecahedron* dodecahedron = new Dodecahedron(edge);

  cout << "A aresta do dodecaedro é: "
       << dodecahedron->getEdge() << endl;

  cout << "A área do dodecaedro é: "
       << dodecahedron->area() << endl;

  cout << "O volume do dodecaedro é: "
       << dodecahedron->volume() << endl;

  delete dodecahedron;

  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 Dodecahedron.o -c Dodecahedron.cpp

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

g++ -o application Dodecahedron.o Application.o