Getting Mean of LinkedList<?>

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 457 Views - Last Post: 29 January 2019 - 05:39 PM Rate Topic: -----

#1 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Getting Mean of LinkedList<?>

Posted 28 January 2019 - 11:26 AM

Hi,

I am trying to make a statistics program but I don't know if I am correctly using the code:
This is the TableColumn.class
package ...Data.Table;

import java.util.LinkedList;

public abstract class TableColumn {
	
	private String name;
	
	public TableColumn(String name) {
		this.name=name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public abstract LinkedList<?> getDataList();
	
}



I and have a four classes like this one for each type of Data: Integer, Double, BigInteger, BigDecimal:
import java.util.LinkedList;

import ...Table.TableColumn;

public class IntegerColumn extends TableColumn {
	
	private LinkedList<Integer> dataList;
	
	public IntegerColumn(String name) {
		super(name);
		this.dataList=new LinkedList<>();
	}

	public LinkedList<Integer> getDataList() {
		return dataList;
	}
}



But to get the mean I use this and I don't know if it is correct or can be simplified:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.LinkedList;

public class NumericalSynthesis {

	public Number getMean(LinkedList<?> dataList) {
		
		if(dataList!=null && !dataList.isEmpty()){
		    Object obj = dataList.get(0);
		    if(obj instanceof BigInteger) {
		    	
				BigInteger summatory=new BigInteger("0");
		    	LinkedList<BigInteger> list = (LinkedList<BigInteger>)dataList;
				Iterator<BigInteger> iterator = list.iterator();
		    	while(iterator.hasNext()) {
					BigInteger b = iterator.next();
					summatory=summatory.add(B)/>/>/>;
				}
		    	return summatory.divide(new BigInteger(String.valueOf(list.size())));
		    	
		    } else if(obj instanceof BigDecimal) {
		    	
		    	BigDecimal summatory=new BigDecimal("0");
		    	LinkedList<BigDecimal> list = (LinkedList<BigDecimal>)dataList;
				Iterator<BigDecimal> iterator = list.iterator();
		    	while(iterator.hasNext()) {
		    		BigDecimal b = iterator.next();
					summatory=summatory.add(B)/>/>/>;
				}
		    	return summatory.divide(new BigDecimal(String.valueOf(list.size())));
		    	
		    } else if(obj instanceof Integer) {
		    	
		    	Integer summatory=0;
		    	LinkedList<Integer> list = (LinkedList<Integer>)dataList;
				Iterator<Integer> iterator = list.iterator();
		    	while(iterator.hasNext()) {
		    		Integer b = iterator.next();
					summatory=summatory+b;
				}
		    	return summatory/list.size();
		    	
		    } else if(obj instanceof Double) {
		    	
		    	Double summatory=0.0;
		    	LinkedList<Double> list = (LinkedList<Double>)dataList;
				Iterator<Double> iterator = list.iterator();
		    	while(iterator.hasNext()) {
		    		Double b = iterator.next();
					summatory=summatory+b;
				}
		    	return summatory/Double.valueOf(list.size());
		    	
		    }
		}
    	return null;
	}
	
}



I am still a begginer in Java, any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting Mean of LinkedList<?>

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 11:36 AM

Do you have any specific questions about the program?

If there are errors, copy the full text of the error messages and paste it here.

If there are problems with the output, copy and paste it here with comments describing what is wrong and show the desired output.
Was This Post Helpful? 0
  • +
  • -

#3 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 11:40 AM

View PostNormR, on 28 January 2019 - 11:36 AM, said:

Do you have any specific questions about the program?

If there are errors, copy the full text of the error messages and paste it here.

If there are problems with the output, copy and paste it here with comments describing what is wrong and show the desired output.


My problem is that I don't know if it is better to use the method getMean(LinkedList<?>) or make one different method for each type of LinkedList like:

public double getMeanIntegerList(LinkedList<Integer> dataList) {
		Integer summatory=0;
		Iterator<Integer> iterator = dataList.iterator();
    	while(iterator.hasNext()) {
    		Integer b = iterator.next();
			summatory+=b;
		}
    	return (double)summatory/dataList.size();
	}
	
	public double getMeanDoubleList(LinkedList<Double> dataList) {
		Double summatory=0.0;
		Iterator<Double> iterator = dataList.iterator();
    	while(iterator.hasNext()) {
    		Double b = iterator.next();
			summatory+=b;
		}
    	return (double)summatory/dataList.size();
	}





or what is better and optimal to do,
Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 11:43 AM

One method that excepts different types of lists is a better solution.

The method should not have to use instanceof.

This post has been edited by NormR: 28 January 2019 - 11:45 AM

Was This Post Helpful? 1
  • +
  • -

#5 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 11:53 AM

View PostNormR, on 28 January 2019 - 11:43 AM, said:

One method that excepts different types of lists is a better solution.

The method should not have to use instanceof.


But without instanceof I can not get the type that the list holds, therefore I can not get the mean (make the operations) :blink:/>
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 11:54 AM

The list has objects with numeric values. Is there a way to get that value without knowing the specific type of the object?
Was This Post Helpful? 1
  • +
  • -

#7 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:01 PM

View PostNormR, on 28 January 2019 - 11:54 AM, said:

The list has objects with numeric values. Is there a way to get that value without knowing the specific type of the object?


Maybe using reflection :sad2: but I don't think I could do it.

Thank you anyway, I Will continue searching and trying :/
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:07 PM

Have you looked at the API doc for the classes you are using to see what super classes are used and what methods are available to access the value that the object contains?

Nothing to do with reflection.
Was This Post Helpful? 0
  • +
  • -

#9 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:07 PM

View PostJackEsparrou, on 28 January 2019 - 12:01 PM, said:

View PostNormR, on 28 January 2019 - 11:54 AM, said:

The list has objects with numeric values. Is there a way to get that value without knowing the specific type of the object?


Maybe using reflection :sad2:/> but I don't think I could do it.

Thank you anyway, I Will continue searching and trying ://>


Maybe something like this?

@SuppressWarnings("unchecked")
	public static <N extends Number> N multiply(N num, int mult) {
	    Class<? extends Number> cls = number.getClass();
	    if (cls == Integer.class) {
	        return (N) Integer.valueOf(number.intValue() * mult);
	    }
	    if (cls == Long.class) {
	        return (N) Long.valueOf(number.longValue() * mult);
	    }
	    throw new UnsupportedOperationException("unknown class: " + cls);
	}


Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:09 PM

Please read my last post again.
Was This Post Helpful? 0
  • +
  • -

#11 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:15 PM

View PostNormR, on 28 January 2019 - 12:07 PM, said:

Have you looked at the API doc for the classes you are using to see what super classes are used and what methods are available to access the value that the object contains?

Nothing to do with reflection.


Sincerely I thought that I could do it that way, but maybe I should simple remove all children and change it to LinkedList<Number>, I learned it some days ago so now I've wached it again so I think I could simply change the method to getMean(LinkedList<Number>) and then do operations with this?:

@SuppressWarnings("unchecked")
	public static <N extends Number> N multiply(N number1, N number2) {
	    Class<? extends Number> cls1 = number1.getClass();
	    Class<? extends Number> cls2 = number2.getClass();
	    if (cls1 == Integer.class && cls2 == Integer.class) {
	        return (N) Integer.valueOf(number1.intValue() * number2.intValue());
	    }
	    throw new UnsupportedOperationException("unknown class: ");
	}


Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:17 PM

Still too complicated. Does the Number class have methods to access the value the object contains?

I just noticed the new method: multiply


Does the getMean() method work now?

It's better to finish with one problem before moving to the next one.
Work on one at a time.

This post has been edited by NormR: 28 January 2019 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#13 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:22 PM

View PostNormR, on 28 January 2019 - 12:17 PM, said:

Still too complicated. Does the Number class have methods to access the value the object contains?

I just noticed the new method: multiply


Does the getMean() method work now?

It's better to finish with one problem before moving to the next one.
Work on one at a time.


Then I guess I can do something like that:

if(true) {
			// INTEGERS
			Number a = 1;
			Number b = 2;
			Number sum;
			Class<? extends Number> cls = a.getClass();
			Class<? extends Number> cls2 = b.getClass();
			if (cls == Integer.class && cls2 == Integer.class) {
				sum=a.intValue()+b.intValue();
			}
		}
		
		if(true) {
			//BIGDECIMALS
			Number a = new BigDecimal("1");
			Number b = new BigDecimal("1");
			Number sum;
			Class<? extends Number> cls = a.getClass();
			Class<? extends Number> cls2 = b.getClass();
			if (cls == Integer.class && cls2 == Integer.class) {
				sum=((BigDecimal)a).add((BigDecimal)B)/>;
			}
		}



But the problem starts now when the number types are different for example if I want to sum and BigInteger with a double :'(
Was This Post Helpful? 0
  • +
  • -

#14 JackEsparrou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 19

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:37 PM

View PostNormR, on 28 January 2019 - 12:17 PM, said:

Still too complicated. Does the Number class have methods to access the value the object contains?

I just noticed the new method: multiply


Does the getMean() method work now?

It's better to finish with one problem before moving to the next one.
Work on one at a time.


I don't know which one you mean but this one Works:

public Number getMean(LinkedList<Number> dataList) {
		
		BigDecimal summatory =new BigDecimal("0");
		Iterator<Number> iterator = dataList.iterator();
		while(iterator.hasNext()) {
			Number listnum = iterator.next();
			summatory=summatory.add(new BigDecimal(listnum.toString()));
		}
		return summatory.divide(new BigDecimal(String.valueOf(dataList.size())));
		/*
		
		if(true) {
			// INTEGERS
			Number a = 1;
			Number b = 2;
			Number sum;
			Class<? extends Number> cls = a.getClass();
			Class<? extends Number> cls2 = b.getClass();
			if (cls == Integer.class && cls2 == Integer.class) {
				sum=a.intValue()+b.intValue();
			}
		}
		
		if(true) {
			//BIGDECIMALS
			Number a = new BigDecimal("1");
			Number b = new BigDecimal("1");
			Number sum;
			Class<? extends Number> cls = a.getClass();
			Class<? extends Number> cls2 = b.getClass();
			if (cls == Integer.class && cls2 == Integer.class) {
				sum=((BigDecimal)a).add((BigDecimal)B)/>/>;
			}
		}
		
		*/
		
	}


Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:44 PM

That is also ugly code. Did you look at the API doc for the Numbers class?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2