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

Desenvolva uma classe chamada Triangle para representar um triângulo equilátero, ou seja, um trilátero regular com três lados de mesmo comprimento. A classe possui um único atributo denominado side, do tipo double, que representa o lado do triângulo equilátero e cujo valor deve ser maior ou igual a zero e menor ou igual a vinte. O lado do triângulo equilátero 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 triângulo equilátero, respectivamente. A área de um triângulo equilátero de lado l é obtida pela fórmula √3 / 4 * l2. O perímetro de um triângulo equilátero de lado l é obtido pela fórmula 3 * l.

 

Diagrama de Classes - Linguagem de Programação Java
Diagrama de Classes - Linguagem de Programação Java

Arquivo Triangle.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Classe responsavel pela representacao de um triangulo
 */
public class Triangle
{
  /**
   * Lado do triangulo (atributo)
   */
  private double side;

  /**
   * Construtor padrao
   */
  public Triangle()
  {
    side = -1.0;
  }

  /**
   * Retornar o lado do triangulo
   *
   * @return lado do triangulo
   * @throws NullPointerException lado do triangulo nao foi especificado
   */
  public double getSide() throws NullPointerException
  {
    if(side != -1.0)
    {
      return side;
    }
    else
    {
      throw new NullPointerException("O lado do triangulo nao foi "
        + "especificado");
    }
  }

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

  /**
   * Retornar a area do triangulo
   *
   * @return area do triangulo
   * @throws NullPointerException lado do triangulo nao foi especificado
   */
  public double area() throws NullPointerException
  {
    return Math.sqrt(3.0) / 4.0 * Math.pow(getSide(), 2.0);
  }

  /**
   * Retornar o perimetro do triangulo
   *
   * @return perimetro do triangulo
   * @throws NullPointerException lado do triangulo nao foi especificado
   */
  public double perimeter() throws NullPointerException
  {
    return 3.0 * getSide();
  }
}

Arquivo Application.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Classe responsavel pela execucao da classe Triangle
 */
public class Application
{
  /**
   * Metodo principal da linguagem de programacao Java
   *
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    Triangle triangle = new Triangle();

    try
    {
      triangle.setSide(5.0);

      System.out.println("O lado do triangulo eh: "
        + triangle.getSide());

      System.out.println("A area do triangulo eh: "
        + triangle.area());

      System.out.println("O perimetro do triangulo eh: "
        + triangle.perimeter());
    }
    catch(IllegalArgumentException exception)
    {
      System.out.println(exception.getMessage());
    }
    catch(NullPointerException exception)
    {
      System.out.println(exception.getMessage());
    }
    catch(Exception exception)
    {
      System.out.println(exception.getMessage());
    }
  }
}

Saída

O lado do triangulo eh: 5.0
A area do triangulo eh: 10.825317547305483
O perimetro do triangulo eh: 15.0
Diagrama de Classes - Linguagem de Programação C++
Diagrama de Classes - Linguagem de Programação C++

Arquivo IllegalArgumentException.h

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

#include <string>

using namespace std;

#ifndef ILLEGALARGUMENTEXCEPTION_H
#define ILLEGALARGUMENTEXCEPTION_H

/**
 * Classe responsavel pela excecao lado do triangulo fora do intervalo
 */
class IllegalArgumentException
{
  private:

  /**
   * Mensagem (atributo)
   */
  string message;

  public:

  /**
   * Construtor para configurara mensagem da excecao
   *
   * @param message mensagem da excecao
   */
  IllegalArgumentException(string message);

  /**
   * Retornar a mensagem da excecao
   *
   * @return mensagem da excecao
   */
  string getMessage();
};

#endif

Arquivo IllegalArgumentException.cpp

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

#include "IllegalArgumentException.h"

/**
 * Construtor para configurar a mensagem da excecao
 *
 * @param message mensagem da excecao
 */
IllegalArgumentException::IllegalArgumentException(string message)
{
  IllegalArgumentException::message = message;
}

/**
 * Retornar a mensagem da excecao
 *
 * @return mensagem da excecao
 */
string IllegalArgumentException::getMessage()
{
  return message;
}

Arquivo NullPointerException.h

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

#include <string>

using namespace std;

#ifndef NULLPOINTEREXCEPTION_H
#define NULLPOINTEREXCEPTION_H

/**
 * Classe responsavel pela excecao lado do triangulo nao especificado
 */
class NullPointerException
{
  private:

  /**
   * Mensagem (atributo)
   */
  string message;

  public:

  /**
   * Construtor para configurara mensagem da excecao
   *
   * @param message mensagem da excecao
   */
  NullPointerException(string message);

  /**
   * Retornar a mensagem da excecao
   *
   * @return mensagem da excecao
   */
  string getMessage();
};

#endif

Arquivo NullPointerException.cpp

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

#include "NullPointerException.h"

/**
 * Construtor para configurar a mensagem da excecao
 *
 * @param message mensagem da excecao
 */
NullPointerException::NullPointerException(string message)
{
  NullPointerException::message = message;
}

/**
 * Retornar a mensagem da excecao
 *
 * @return mensagem da excecao
 */
string NullPointerException::getMessage()
{
  return message;
}

Arquivo Triangle.h

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

#include "IllegalArgumentException.h"
#include "NullPointerException.h"

#ifndef TRIANGLE_H
#define TRIANGLE_H

/**
 * Classe responsavel pela representacao de um triangulo
 */
class Triangle
{
  private:

  /**
   * Lado do triangulo (atributo)
   */
  double side;

  public:

  /**
   * Construtor padrao
   */
  Triangle();

  /**
   * Retornar o lado do triangulo
   *
   * @return lado do triangulo
   * @throws NullPointerException lado do triangulo nao foi especificado
   */
  double getSide() throw (NullPointerException);

  /**
   * Configurar o lado do triangulo
   *
   * @param side lado do triangulo
   * @throws IllegalArgumentException lado do triangulo fora do intervalo
   */
  void setSide(double side) throw (IllegalArgumentException);

  /**
   * Retornar a area do triangulo
   *
   * @return area do triangulo
   * @throws NullPointerException lado do triangulo nao foi especificado
   */
  double area() throw (NullPointerException);

  /**
   * Retornar o perimetro do triangulo
   *
   * @return perimetro do triangulo
   * @throws NullPointerException lado do triangulo nao foi especificado
   */
  double perimeter() throw (NullPointerException);
};

#endif

Arquivo Triangle.cpp

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

#include <cmath>
#include <sstream>
#include "Triangle.h"

/**
 * Construtor padrao
 */
Triangle::Triangle()
{
  side = -1.0;
}

/**
 * Retornar o lado do triangulo
 *
 * @return lado do triangulo
 * @throws NullPointerException lado do triangulo nao foi especificado
 */
double Triangle::getSide() throw (NullPointerException)
{
  if(side != -1.0)
  {
    return side;
  }
  else
  {
    throw NullPointerException("O lado do triangulo nao foi especificado");
  }
}

/**
 * Configurar o lado do triangulo
 *
 * @param side lado do triangulo
 * @throws IllegalArgumentException lado do triangulo fora do intervalo
 */
void Triangle::setSide(double side) throw (IllegalArgumentException)
{
  if((side >= 0.0) && (side <= 20.0))
  {
    Triangle::side = side;
  }
  else
  {
    stringstream buffer;

    buffer << "O lado do triangulo deve estar contido entre 0.0 e 20.0: '"
           << side << "'";

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

/**
 * Retornar a area do triangulo
 *
 * @return area do triangulo
 * @throws NullPointerException lado do triangulo nao foi especificado
 */
double Triangle::area() throw (NullPointerException)
{
  return sqrt(3.0) / 4.0 * pow(getSide(), 2.0);
}

/**
 * Retornar o perimetro do triangulo
 *
 * @return perimetro do triangulo
 * @throws NullPointerException lado do triangulo nao foi especificado
 */
double Triangle::perimeter() throw (NullPointerException)
{
  return 3.0 * getSide();
}

Arquivo Application.cpp

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

#include <iostream>
#include "Triangle.h"

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)
{
  Triangle* triangle = new Triangle();

  try
  {
    triangle->setSide(-5.0);

    cout << "O lado do triangulo eh: "
         << triangle->getSide() << endl;

    cout << "A area do triangulo eh: "
         << triangle->area() << endl;

    cout << "O perimetro do triangulo eh: "
         << triangle->perimeter() << endl;
  }
  catch(IllegalArgumentException exception)
  {
    cout << exception.getMessage() << endl;
  }
  catch(NullPointerException exception)
  {
    cout << exception.getMessage() << endl;
  }
  catch(...)
  {
    cout << "Excecao desconhecida" << endl;
  }

  delete triangle;

  return 0;
}

Saída

O lado do triangulo eh: 5
A area do triangulo eh: 10.8253
O perimetro do triangulo eh: 15