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

Desenvolva uma classe chamada Icosahedron para representar um icosaedro, ou seja, um poliedro regular de vinte 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 icosaedro. O construtor da classe recebe como parâmetro a aresta do icosaedro, cujo valor deve ser maior ou igual a zero. A aresta do icosaedro pode ser obtida pelo usuário por meio do método getEdge(). A classe também apresenta os métodos area() e volume(), que retornam a área e o volume do icosaedro, respectivamente. A área de um icosaedro de aresta a é obtida pela fórmula 5 * √3 * a2. O volume de um icosaedro de aresta a é obtido pela fórmula 5 / 12 * (3 + √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 Icosahedron.java Application.java
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação Java

Arquivo Icosahedron.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.tutorial01.exercicio14;

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

  /**
   * Construtor para inicializar a aresta do icosaedro
   *
   * @param edge aresta do icosaedro
   */
  public Icosahedron(double edge)
  {
    if(edge >= 0.0)
    {
      this.edge = edge;
    }
    else
    {
      this.edge = 0.0;
    }
  }

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

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

  /**
   * Retornar o volume do icosaedro
   *
   * @return volume do icosaedro
   */
  public double volume()
  {
    return 5.0 / 12.0 * (3.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.tutorial01.exercicio14;

import java.util.Scanner;

/**
 * Classe responsavel pela execucao da classe Icosahedron
 */
public class Application
{
  /**
   * Construtor para inicializar a execucao da classe Icosahedron
   */
  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 icosaedro: ");

    double edge = scanner.nextDouble();

    scanner.close();

    Icosahedron icosahedron = new Icosahedron(edge);

    System.out.print("A aresta do icosaedro é: ");
    System.out.println(icosahedron.getEdge());

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

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

Arquivo Icosahedron.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 ICOSAHEDRON_HPP
#define ICOSAHEDRON_HPP

/**
 * Classe responsavel pela representacao de um icosaedro
 */
class Icosahedron
{
  public:

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

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

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

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

  private:

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

#endif

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

/**
 * Construtor para inicializar a aresta do icosaedro
 *
 * @param edge aresta do icosaedro
 */
Icosahedron::Icosahedron(const double edge)
{
  if(edge >= 0.0)
  {
    Icosahedron::edge = edge;
  }
  else
  {
    Icosahedron::edge = 0.0;
  }
}

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

/**
 * Retornar a area do icosaedro
 *
 * @return area do icosaedro
 */
double Icosahedron::area() const
{
  return 5.0 * sqrt(3.0) * pow(edge, 2.0);
}

/**
 * Retornar o volume do icosaedro
 *
 * @return volume do icosaedro
 */
double Icosahedron::volume() const
{
  return 5.0 / 12.0 * (3.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 "Icosahedron.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 icosaedro: ";
  cin  >> edge;

  Icosahedron* icosahedron = new Icosahedron(edge);

  cout << "A aresta do icosaedro é: "
       << icosahedron->getEdge() << endl;

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

  cout << "O volume do icosaedro é: "
       << icosahedron->volume() << endl;

  delete icosahedron;

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

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

g++ -o application Icosahedron.o Application.o