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

Desenvolva uma classe chamada Sphere para representar uma esfera. A classe possui um único atributo denominado radius, do tipo double, que representa o raio da esfera. O construtor da classe recebe como parâmetro o raio da esfera, cujo valor deve ser maior ou igual a zero. O raio da esfera pode ser obtido pelo usuário por meio do método getRadius(). A classe também apresenta os métodos area() e volume(), que retornam a área e o volume da esfera, respectivamente. A área de uma esfera de raio r é obtida pela fórmula 4 * π * r2. O volume de uma esfera de raio r é obtido pela fórmula 4 / 3 * π * r3.

 

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

Arquivo Sphere.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.exercicio28;

/**
 * Classe responsavel pela representacao de uma esfera
 */
public class Sphere
{
  /**
   * Raio da esfera
   */
  private double radius;

  /**
   * Construtor para inicializar o raio da esfera
   *
   * @param radius raio da esfera
   */
  public Sphere(double radius)
  {
    if(radius >= 0.0)
    {
      this.radius = radius;
    }
    else
    {
      this.radius = 0.0;
    }
  }

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

  /**
   * Retornar a area da esfera
   *
   * @return area da esfera
   */
  public double area()
  {
    return 4.0 * Math.PI * Math.pow(radius, 2.0);
  }

  /**
   * Retornar o volume da esfera
   *
   * @return volume da esfera
   */
  public double volume()
  {
    return 4.0 / 3.0 * Math.PI * Math.pow(radius, 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.exercicio28;

import java.util.Scanner;

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

    double radius = scanner.nextDouble();

    scanner.close();

    Sphere sphere = new Sphere(radius);

    System.out.print("O raio da esfera é: ");
    System.out.println(sphere.getRadius());

    System.out.print("A área da esfera é: ");
    System.out.println(sphere.area());

    System.out.print("O volume da esfera é: ");
    System.out.println(sphere.volume());
  }
}
Diagrama de Classes na Linguagem de Programação C++ Sphere.hpp Sphere.cpp Application.cpp makefile
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação C++

Arquivo Sphere.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 SPHERE_HPP
#define SPHERE_HPP

/**
 * Classe responsavel pela representacao de uma esfera
 */
class Sphere
{
  public:

  /**
   * Construtor para inicializar o raio da esfera
   *
   * @param radius raio da esfera
   */
  Sphere(const double radius);

  /**
   * Retornar o raio da esfera
   *
   * @return raio da esfera
   */
  double getRadius() const;

  /**
   * Retornar a area da esfera
   *
   * @return area da esfera
   */
  double area() const;

  /**
   * Retornar o volume da esfera
   *
   * @return volume da esfera
   */
  double volume() const;

  private:

  /**
   * Raio da esfera
   */
  double radius;
};

#endif

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

/**
 * Construtor para inicializar o raio da esfera
 *
 * @param radius raio da esfera
 */
Sphere::Sphere(const double radius)
{
  if(radius >= 0.0)
  {
    Sphere::radius = radius;
  }
  else
  {
    Sphere::radius = 0.0;
  }
}

/**
 * Retornar o raio da esfera
 *
 * @return raio da esfera
 */
double Sphere::getRadius() const
{
  return radius;
}

/**
 * Retornar a area da esfera
 *
 * @return area da esfera
 */
double Sphere::area() const
{
  return 4.0 * M_PI * pow(radius, 2.0);
}

/**
 * Retornar o volume da esfera
 *
 * @return volume da esfera
 */
double Sphere::volume() const
{
  return 4.0 / 3.0 * M_PI * pow(radius, 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 "Sphere.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 radius;

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

  Sphere* sphere = new Sphere(radius);

  cout << "O raio da esfera é: " << sphere->getRadius() << endl;
  cout << "A área da esfera é: " << sphere->area() << endl;
  cout << "O volume da esfera é: " << sphere->volume() << endl;

  delete sphere;

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

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

g++ -o application Sphere.o Application.o