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

Desenvolva uma classe chamada Cylinder para representar um cilindro. A classe possui dois atributos denominados height e radius, que representam a altura e o raio do cilindro, respectivamente, ambos do tipo double e cujos valores devem ser maiores ou igual a zero e menores ou igual a dez. A altura do cilindro pode ser obtida e alterada pelo usuário por meio dos métodos getHeight() e setHeight(), respectivamente. O raio do cilindro pode ser obtido e alterado pelo usuário por meio dos métodos getRadius() e setRadius(), respectivamente. A classe também apresenta os métodos baseArea(), lateralArea(), surfaceArea() e volume(), que retornam a área da base, a área lateral, a área total e o volume do cilindro, respectivamente. A área da base de um cilindro de raio r é obtida pela fórmula π * r2. A área lateral de um cilindro de altura h e de raio r é obtida pela fórmula 2 * π * h * r. A área total de um cilindro de altura h e de raio r é obtida pela fórmula 2 * π * (h + r) * r. O volume de um cilindro de altura h e de raio r é obtido pela fórmula π * h * r2.

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 Cylinder.java Application.java
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação Java

Arquivo Cylinder.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.exercicio25;

/**
 * Classe responsavel pela representacao de um cilindro
 */
public class Cylinder
{
  /**
   * Altura do cilindro
   */
  private double height;

  /**
   * Raio do cilindro
   */
  private double radius;

  /**
   * Retornar a altura do cilindro
   *
   * @return altura do cilindro
   */
  public double getHeight()
  {
    return height;
  }

  /**
   * Configurar a altura do cilindro
   *
   * @param height altura do cilindro
   */
  public void setHeight(double height)
  {
    if((height >= 0.0) && (height <= 10.0))
    {
      this.height = height;
    }
    else
    {
      throw new IllegalArgumentException("A altura do cilindro deve estar "
        + "contido entre 0.0 e 10.0: '" + height + "'");
    }
  }

  /**
   * Retornar o raio do cilindro
   *
   * @return raio do cilindro
   */
  public double getRadius()
  {
    return radius;
  }

  /**
   * Configurar o raio do cilindro
   *
   * @param radius raio do cilindro
   */
  public void setRadius(double radius)
  {
    if((radius >= 0.0) && (radius <= 10.0))
    {
      this.radius = radius;
    }
    else
    {
      throw new IllegalArgumentException("O raio do cilindro deve estar "
        + "contido entre 0.0 e 10.0: '" + radius + "'");
    }
  }

  /**
   * Retornar a area da base do cilindro
   *
   * @return area da base do cilindro
   */
  public double baseArea()
  {
    return Math.PI * Math.pow(getRadius(), 2.0);
  }

  /**
   * Retornar a area lateral do cilindro
   *
   * @return area lateral do cilindro
   */
  public double lateralArea()
  {
    return 2.0 * Math.PI * getHeight() * getRadius();
  }

  /**
   * Retornar a area total do cilindro
   *
   * @return area total do cilindro
   */
  public double surfaceArea()
  {
    return 2.0 * Math.PI * (getHeight() + getRadius()) * getRadius();
  }

  /**
   * Retornar o volume do cilindro
   *
   * @return volume do cilindro
   */
  public double volume()
  {
    return Math.PI * getHeight() * Math.pow(getRadius(), 2.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.tutorial05.exercicio25;

import java.util.Scanner;

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

    double height = scanner.nextDouble();

    System.out.print("Forneça o valor do raio do cilindro: ");

    double radius = scanner.nextDouble();

    scanner.close();

    Cylinder cylinder = new Cylinder();

    try
    {
      cylinder.setHeight(height);

      cylinder.setRadius(radius);
    }
    catch (IllegalArgumentException exception)
    {
      System.out.println(exception.getMessage());

      System.exit(0);
    }

    System.out.print("A altura do cilindro é: ");
    System.out.println(cylinder.getHeight());

    System.out.print("O raio do cilindro é: ");
    System.out.println(cylinder.getRadius());

    System.out.print("A área da base do cilindro é: ");
    System.out.println(cylinder.baseArea());

    System.out.print("A área lateral do cilindro é: ");
    System.out.println(cylinder.lateralArea());

    System.out.print("A área total do cilindro é: ");
    System.out.println(cylinder.surfaceArea());

    System.out.print("O volume do cilindro é: ");
    System.out.println(cylinder.volume());
  }
}
Diagrama de Classes na Linguagem de Programação C++ Exception.hpp Exception.cpp IllegalArgumentException.hpp IllegalArgumentException.cpp Cylinder.hpp Cylinder.cpp Application.cpp makefile
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 Cylinder.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 CYLINDER_HPP
#define CYLINDER_HPP

#include "IllegalArgumentException.hpp"

/**
 * Classe responsavel pela representacao de um cilindro
 */
class Cylinder
{
  public:

  /**
   * Retornar a altura do cilindro
   *
   * @return altura do cilindro
   */
  double getHeight() const;

  /**
   * Configurar a altura do cilindro
   *
   * @param height altura do cilindro
   * @throws IllegalArgumentException valor da altura do cilindro invalido
   */
  void setHeight(const double height) throw (IllegalArgumentException);

  /**
   * Retornar o raio do cilindro
   *
   * @return raio do cilindro
   */
  double getRadius() const;

  /**
   * Configurar o raio do cilindro
   *
   * @param radius raio do cilindro
   * @throws IllegalArgumentException valor do raio do cilindro invalido
   */
  void setRadius(const double radius) throw (IllegalArgumentException);

  /**
   * Retornar a area da base do cilindro
   *
   * @return area da base do cilindro
   */
  double baseArea() const;

  /**
   * Retornar a area lateral do cilindro
   *
   * @return area lateral do cilindro
   */
  double lateralArea() const;

  /**
   * Retornar a area total do cilindro
   *
   * @return area total do cilindro
   */
  double surfaceArea() const;

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

  private:

  /**
   * Altura do cilindro
   */
  double height;

  /**
   * Raio do cilindro
   */
  double radius;
};

#endif

Arquivo Cylinder.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                           *
 *************************************************************************/

#define _USE_MATH_DEFINES

#include <cmath>
#include <sstream>

#include "Cylinder.hpp"

/**
 * Retornar a altura do cilindro
 *
 * @return altura do cilindro
 */
double Cylinder::getHeight() const
{
  return height;
}

/**
 * Configurar a altura do cilindro
 *
 * @param height altura do cilindro
 * @throws IllegalArgumentException valor da altura do cilindro invalido
 */
void Cylinder::setHeight(const double height) throw (IllegalArgumentException)
{
  if((height >= 0.0) && (height <= 10.0))
  {
    Cylinder::height = height;
  }
  else
  {
    std::stringstream buffer;

    buffer << "A altura do cilindro deve estar contido entre 0.0 e 10.0: '"
           << height << "'";

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

/**
 * Retornar o raio do cilindro
 *
 * @return raio do cilindro
 */
double Cylinder::getRadius() const
{
  return radius;
}

/**
 * Configurar o raio do cilindro
 *
 * @param radius raio do cilindro
 * @throws IllegalArgumentException valor do raio do cilindro invalido
 */
void Cylinder::setRadius(double radius) throw (IllegalArgumentException)
{
  if((radius >= 0.0) && (radius <= 10.0))
  {
    Cylinder::radius = radius;
  }
  else
  {
    std::stringstream buffer;

    buffer << "O raio do cilindro deve estar contido entre 0.0 e 10.0: '"
           << radius << "'";

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

/**
 * Retornar a area da base do cilindro
 *
 * @return area da base do cilindro
 */
double Cylinder::baseArea() const
{
  return M_PI * pow(getRadius(), 2.0);
}

/**
 * Retornar a area lateral do cilindro
 *
 * @return area lateral do cilindro
 */
double Cylinder::lateralArea() const
{
  return 2.0 * M_PI * getHeight() * getRadius();
}

/**
 * Retornar a area total do cilindro
 *
 * @return area total do cilindro
 */
double Cylinder::surfaceArea() const
{
  return 2.0 * M_PI * (getHeight() + getRadius()) * getRadius();
}

/**
 * Retornar o volume do cilindro
 *
 * @return volume do cilindro
 */
double Cylinder::volume() const
{
  return M_PI * getHeight() * pow(getRadius(), 2.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 "Cylinder.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 height;

  double radius;

  cout << "Forneça o valor da altura da cilindro: ";
  cin  >> height;

  cout << "Forneça o valor do raio da cilindro: ";
  cin  >> radius;

  Cylinder* cylinder = new Cylinder();

  try
  {
    cylinder->setHeight(height);

    cylinder->setRadius(radius);
  }
  catch(IllegalArgumentException exception)
  {
    cerr << exception.getMessage() << endl;

    delete cylinder;

    return 0;
  }
  catch(...)
  {
    cerr << "Exceção desconhecida" << endl;

    delete cylinder;

    return 0;
  }

  cout << "A altura do cilindro é: "
       << cylinder->getHeight() << endl;

  cout << "O raio do cilindro é: "
       << cylinder->getRadius() << endl;

  cout << "A área da base do cilindro é: "
       << cylinder->baseArea() << endl;

  cout << "A área lateral do cilindro é: "
       << cylinder->lateralArea() << endl;

  cout << "A área total do cilindro é: "
       << cylinder->surfaceArea() << endl;

  cout << "O volume do cilindro é: "
       << cylinder->volume() << endl;

  delete cylinder;

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

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

g++ -o application Exception.o IllegalArgumentException.o Cylinder.o Application.o