Desenvolva um programa que calcule a média aritmética simples, a média geométrica e a média harmônica de uma sequência de valores inteiros fornecidos pelo usuário.
/*************************************************************************
* Copyright (C) 2009/2025 - 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.tutorial03.exercicio10;
/**
* Calcular a média da série numérica
*/
public abstract class AbstractMean
{
/**
* Valores da série numérica
*/
private int[] values;
/**
* Inicializar os valores da série numérica
*
* @param values valores da série numérica
*/
protected AbstractMean(int[] values)
{
this.values = values;
}
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
public abstract double average();
/**
* Retornar a quantidade de elementos da série numérica
*
* @return quantidade de elementos da série numérica
*/
public int getAmount()
{
return values.length;
}
/**
* Retornar o nome da média
*
* @return nome da média
*/
protected abstract String getNameMean();
/**
* Retornar os valores da série numérica
*
* @return valores da série numérica
*/
public int[] getValues()
{
return values.clone();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
StringBuilder out = new StringBuilder();
out.append("A média ")
.append(getNameMean())
.append(" de {");
boolean nextValue = false;
for (int value : values)
{
if (nextValue)
{
out.append(", ");
}
else
{
nextValue = true;
}
out.append(value);
}
out.append("} é ")
.append(String.format("%1$,.2f", average()));
return out.toString();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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.tutorial03.exercicio10;
/**
* Calcular a média aritmética da série numérica
*/
public class ArithmeticMean extends AbstractMean
{
/**
* Inicializar os valores da série numérica
*
* @param values valores da série numérica
*/
public ArithmeticMean(int[] values)
{
super(values);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial03.exercicio10.AbstractMean#average()
*/
@Override
public double average()
{
double sum = 0.0;
for (int value : getValues())
{
sum = sum + value;
}
return sum / getAmount();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial03.exercicio10.AbstractMean#getNameMean()
*/
@Override
protected String getNameMean()
{
return "aritmética";
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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.tutorial03.exercicio10;
/**
* Calcular a média geométrica da série numérica
*/
public class GeometricMean extends AbstractMean
{
/**
* Inicializar os valores da série numérica
*
* @param values valores da série numérica
*/
public GeometricMean(int[] values)
{
super(values);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial03.exercicio10.AbstractMean#average()
*/
@Override
public double average()
{
double product = 1.0;
for (int value : getValues())
{
product = product * value;
}
return Math.pow(product, 1.0 / getAmount());
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial03.exercicio10.AbstractMean#getNameMean()
*/
@Override
protected String getNameMean()
{
return "geométrica";
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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.tutorial03.exercicio10;
/**
* Calcular a média harmônica da série numérica
*/
public class HarmonicMean extends AbstractMean
{
/**
* Inicializar os valores da série numérica
*
* @param values valores da série numérica
*/
public HarmonicMean(int[] values)
{
super(values);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial03.exercicio10.AbstractMean#average()
*/
@Override
public double average()
{
double sum = 0.0;
for (int value : getValues())
{
sum = sum + 1.0 / value;
}
return getAmount() / sum;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial03.exercicio10.AbstractMean#getNameMean()
*/
@Override
protected String getNameMean()
{
return "harmônica ";
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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.tutorial03.exercicio10;
/**
* Classe responsável pela execução da aplicação
*/
public class Application
{
/**
* Constructor padrão
*/
private Application()
{
}
/**
* Método principal da linguagem de programação Java
*
* @param args argumentos da linha de comando (não utilizado)
*/
public static void main(String[] args)
{
int[] values = {1, 2, 3, 4, 5};
ArithmeticMean arithmeticMean = new ArithmeticMean(values);
System.out.println(arithmeticMean);
GeometricMean geometricMean = new GeometricMean(values);
System.out.println(geometricMean);
HarmonicMean harmonicMean = new HarmonicMean(values);
System.out.println(harmonicMean);
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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 <string>
#ifndef ABSTRACTMEAN_HPP
#define ABSTRACTMEAN_HPP
/**
* Calcular a média da série numérica
*/
class AbstractMean
{
public:
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
virtual double average() = 0;
/**
* Retornar a quantidade de elementos da série numérica
*
* @return quantidade de elementos da série numérica
*/
int getAmount() const;
/**
* Retornar os valores da série numérica
*
* @return valores da série numérica
*/
int* getValues();
/*
* Retornar informacoes sobre a classe
*
* @return informacoes sobre a classe
*/
std::string toString();
protected:
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
AbstractMean(const int amount, int* values);
/**
* Retornar o nome da média
*
* @return nome da média
*/
virtual std::string getNameMean() = 0;
private:
/**
* Valores da série numérica
*/
int* values;
/**
* Quantidade de valores da série numérica
*/
int amount;
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - 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 <iomanip>
#include <sstream>
#include "AbstractMean.hpp"
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
AbstractMean::AbstractMean(const int amount, int* values)
{
AbstractMean::amount = amount;
AbstractMean::values = values;
}
/**
* Retornar a quantidade de elementos da série numérica
*
* @return quantidade de elementos da série numérica
*/
int AbstractMean::getAmount() const
{
return amount;
}
/**
* Retornar os valores da série numérica
*
* @return valores da série numérica
*/
int* AbstractMean::getValues()
{
return values;
}
/*
* Retornar a média da série numérica em forma textual
*
* @return média da série numérica em forma textual
*/
std::string AbstractMean::toString()
{
using namespace std;
stringstream buffer;
buffer << "A média " << getNameMean() << " de {";
int nextValue = 0;
for (int value = 0; value < amount; value++)
{
if (nextValue)
{
buffer << ", ";
}
else
{
nextValue = 1;
}
buffer << values[value];
}
buffer << "} é " << fixed << setprecision(2) << average() << "\n";
return buffer.str();
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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 "AbstractMean.hpp"
#ifndef ARITHMETICMEAN_HPP
#define ARITHMETICMEAN_HPP
/**
* Calcular a média aritmética da série numérica
*/
class ArithmeticMean : public AbstractMean
{
public:
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
ArithmeticMean(const int amount, int* values);
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
double average();
protected:
/**
* Retornar o nome da média
*
* @return nome da média
*/
std::string getNameMean();
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - 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 "ArithmeticMean.hpp"
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
ArithmeticMean::ArithmeticMean(const int amount, int* values) : AbstractMean(amount, values)
{
}
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
double ArithmeticMean::average()
{
int amount = getAmount();
int* values = getValues();
double sum = 0.0;
for (int value = 0; value < amount; value++)
{
sum = sum + values[value];
}
return sum / amount;
}
/**
* Retornar o nome da média
*
* @return nome da média
*/
std::string ArithmeticMean::getNameMean()
{
return "aritmética";
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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 "AbstractMean.hpp"
#ifndef GEOMETRICMEAN_HPP
#define GEOMETRICMEAN_HPP
/**
* Calcular a média geométrica da série numérica
*/
class GeometricMean : public AbstractMean
{
public:
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
GeometricMean(const int amount, int* values);
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
double average();
protected:
/**
* Retornar o nome da média
*
* @return nome da média
*/
std::string getNameMean();
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - 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 "GeometricMean.hpp"
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
GeometricMean::GeometricMean(const int amount, int* values) : AbstractMean(amount, values)
{
}
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
double GeometricMean::average()
{
int amount = getAmount();
int* values = getValues();
double product = 1.0;
for (int value = 0; value < amount; value++)
{
product = product * values[value];
}
return pow(product, 1.0 / amount);
}
/**
* Retornar o nome da média
*
* @return nome da média
*/
std::string GeometricMean::getNameMean()
{
return "geométrica";
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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 "AbstractMean.hpp"
#ifndef HARMONICMEAN_HPP
#define HARMONICMEAN_HPP
/**
* Calcular a média harmônica da série numérica
*/
class HarmonicMean : public AbstractMean
{
public:
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
HarmonicMean(const int amount, int* values);
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
double average();
protected:
/**
* Retornar o nome da média
*
* @return nome da média
*/
std::string getNameMean();
};
#endif
/*************************************************************************
* Copyright (C) 2009/2025 - 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 "HarmonicMean.hpp"
/**
* Inicializar os valores da série numérica
*
* @param amount quantidade de elementos da série numérica
* @param values valores da série numérica
*/
HarmonicMean::HarmonicMean(const int amount, int* values) : AbstractMean(amount, values)
{
}
/**
* Retornar a média da série numérica
*
* @return média da série numérica
*/
double HarmonicMean::average()
{
int amount = getAmount();
int* values = getValues();
double sum = 0.0;
for (int value = 0; value < amount; value++)
{
sum = sum + 1.0 / values[value];
}
return amount / sum;
}
/**
* Retornar o nome da média
*
* @return nome da média
*/
std::string HarmonicMean::getNameMean()
{
return "harmônica ";
}
/*************************************************************************
* Copyright (C) 2009/2025 - 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 "AbstractMean.hpp"
#include "ArithmeticMean.hpp"
#include "GeometricMean.hpp"
#include "HarmonicMean.hpp"
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)
{
int values[5] = {1, 2, 3, 4, 5};
ArithmeticMean* arithmeticMean = new ArithmeticMean(5, values);
cout << arithmeticMean->toString();
delete arithmeticMean;
GeometricMean* geometricMean = new GeometricMean(5, values);
cout << geometricMean->toString();
delete geometricMean;
HarmonicMean* harmonicMean = new HarmonicMean(5, values);
cout << harmonicMean->toString();
delete harmonicMean;
return 0;
}
##########################################################################
# Copyright (C) 2009/2025 - 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 AbstractMean.o -c AbstractMean.cpp
g++ -o ArithmeticMean.o -c ArithmeticMean.cpp
g++ -o GeometricMean.o -c GeometricMean.cpp
g++ -o HarmonicMean.o -c HarmonicMean.cpp
g++ -o Application.o -c Application.cpp
g++ -o application AbstractMean.o ArithmeticMean.o GeometricMean.o HarmonicMean.o Application.o