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 ou reais, utilizando como base o padrão de projeto Visitor.
/**
* Copyright (C) 2009/2025 - 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;
/**
* Element
*/
public interface Numbers
{
/**
* Aceitar o visitante
*
* @param average visitante
*/
public void accept(Mean average);
}
/**
* Copyright (C) 2009/2025 - 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;
import java.util.ArrayList;
import java.util.List;
/**
* ConcreteElement
*/
public class IntegerNumbers implements Numbers
{
/**
* Conjunto de numeros inteiros
*/
private List<Integer> list;
/**
* Construtor padrao
*/
public IntegerNumbers()
{
this.list = new ArrayList<Integer>();
}
/**
* Construtor para inicializar o conjunto de numeros inteiros
*
* @param numbers numeros a serem adicionados ao conjunto de numeros inteiros
*/
public IntegerNumbers(Integer ... numbers)
{
this();
add(numbers);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Numbers#accept(com.ybadoo.tutoriais.poo.Average)
*/
public void accept(Mean average)
{
average.visit(this);
}
/**
* Adicionar o numero ao conjunto de numeros inteiros
*
* @param number numero a ser adicionado ao conjunto de numeros inteiros
*/
public void add(Integer number)
{
this.list.add(number);
}
/**
* Adicionar os numeros ao conjunto de numeros inteiros
*
* @param numbers numeros a serem adicionados ao conjunto de numeros inteiros
*/
public void add(Integer ... numbers)
{
for(Integer number: numbers)
{
this.list.add(number);
}
}
/**
* Retornar o conjunto de numeros inteiros como um array
*
* @return conjunto de numeros inteiros como um array
*/
public Integer[] toArray()
{
return this.list.toArray(new Integer[0]);
}
}
/**
* Copyright (C) 2009/2025 - 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;
import java.util.ArrayList;
import java.util.List;
/**
* ConcreteElement
*/
public class DoubleNumbers implements Numbers
{
/**
* Conjunto de numeros reais
*/
private List<Double> list;
/**
* Construtor padrao
*/
public DoubleNumbers()
{
this.list = new ArrayList<Double>();
}
/**
* Construtor para inicializar o conjunto de numeros reais
*
* @param numbers numeros a serem adicionados ao conjunto de numeros reais
*/
public DoubleNumbers(Double ... numbers)
{
this();
add(numbers);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Numbers#accept(com.ybadoo.tutoriais.poo.Average)
*/
public void accept(Mean average)
{
average.visit(this);
}
/**
* Adicionar o numero ao conjunto de numeros reais
*
* @param number numero a ser adicionado ao conjunto de numeros reais
*/
public void add(Double number)
{
this.list.add(number);
}
/**
* Adicionar os numeros ao conjunto de numeros reais
*
* @param numbers numeros a serem adicionados ao conjunto de numeros reais
*/
public void add(Double ... numbers)
{
for(Double number: numbers)
{
this.list.add(number);
}
}
/**
* Retornar o conjunto de numeros reais como um array
*
* @return conjunto de numeros reais como um array
*/
public Double[] toArray()
{
return this.list.toArray(new Double[0]);
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* Visitor
*/
public interface Mean
{
/**
* Calcular a media do conjunto de numeros reais
*
* @param element conjunto de numeros reais
*/
public void visit(DoubleNumbers element);
/**
* Calcular a media do conjunto de numeros inteiros
*
* @param element conjunto de numeros inteiros
*/
public void visit(IntegerNumbers element);
}
/**
* Copyright (C) 2009/2025 - 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;
import java.util.Locale;
/**
* ConcreteVisitor
*/
public class ArithmeticMean implements Mean
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Mean#visit(com.ybadoo.tutoriais.poo.DoubleNumbers)
*/
public void visit(DoubleNumbers element)
{
Double[] numbers = element.toArray();
int amount = 0;
double sum = 0.0;
for(Double number: numbers)
{
amount = amount + 1;
sum = sum + number;
}
double average = sum / amount;
System.out.format(new Locale("pt", "BR"),
"A média aritmética dos números reais é %-10.2f%n", average);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Mean#visit(com.ybadoo.tutoriais.poo.IntegerNumbers)
*/
public void visit(IntegerNumbers element)
{
Integer[] numbers = element.toArray();
int amount = 0;
int sum = 0;
for(Integer number: numbers)
{
amount = amount + 1;
sum = sum + number;
}
double average = sum * 1.0 / amount;
System.out.format(new Locale("pt", "BR"),
"A média aritmética dos números inteiros é %-10.2f%n", average);
}
}
/**
* Copyright (C) 2009/2025 - 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;
import java.util.Locale;
/**
* ConcreteVisitor
*/
public class GeometricMean implements Mean
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Mean#visit(com.ybadoo.tutoriais.poo.DoubleNumbers)
*/
public void visit(DoubleNumbers element)
{
Double[] numbers = element.toArray();
int amount = 0;
double product = 1.0;
for(Double number: numbers)
{
amount = amount + 1;
product = product * number;
}
double average = Math.pow(product, 1.0/amount);
System.out.format(new Locale("pt", "BR"),
"A média geométrica dos números reais é %-10.2f%n", average);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Mean#visit(com.ybadoo.tutoriais.poo.IntegerNumbers)
*/
public void visit(IntegerNumbers element)
{
Integer[] numbers = element.toArray();
int amount = 0;
int product = 1;
for(Integer number: numbers)
{
amount = amount + 1;
product = product * number;
}
double average = Math.pow(product, 1.0/amount);
System.out.format(new Locale("pt", "BR"),
"A média geométrica dos números inteiros é %-10.2f%n", average);
}
}
/**
* Copyright (C) 2009/2025 - 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;
import java.util.Locale;
/**
* ConcreteVisitor
*/
public class HarmonicMean implements Mean
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Mean#visit(com.ybadoo.tutoriais.poo.DoubleNumbers)
*/
public void visit(DoubleNumbers element)
{
Double[] numbers = element.toArray();
int amount = 0;
double sum = 0.0;
for(Double number: numbers)
{
amount = amount + 1;
sum = sum + 1.0/number;
}
double average = amount / sum;
System.out.format(new Locale("pt", "BR"),
"A média harmônica dos números reais é %-10.2f%n", average);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Mean#visit(com.ybadoo.tutoriais.poo.IntegerNumbers)
*/
public void visit(IntegerNumbers element)
{
Integer[] numbers = element.toArray();
int amount = 0;
double sum = 0.0;
for(Integer number: numbers)
{
amount = amount + 1;
sum = sum + 1.0/number;
}
double average = amount / sum;
System.out.format(new Locale("pt", "BR"),
"A média harmônica dos números inteiros é %-10.2f%n", average);
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* Client
*/
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)
{
IntegerNumbers integers = new IntegerNumbers(5, 10, 6);
DoubleNumbers doubles = new DoubleNumbers(2.0, 6.0, 8.0);
ArithmeticMean arithmetic = new ArithmeticMean();
GeometricMean geometric = new GeometricMean();
HarmonicMean harmonic = new HarmonicMean();
// A média aritmética dos números inteiros é 7,00
arithmetic.visit(integers);
// A média aritmética dos números reais é 5,33
arithmetic.visit(doubles);
// A média geométrica dos números inteiros é 6,69
geometric.visit(integers);
// A média geométrica dos números reais é 4,58
geometric.visit(doubles);
// A média harmônica dos números inteiros é 6,43
harmonic.visit(integers);
// A média harmônica dos números reais é 3,79
harmonic.visit(doubles);
}
}