The Artima Developer Community
Sponsored Link

Java Answers Forum
Making small program w/ filter... filters dont work. Help?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Pyriel Baranos

Posts: 3
Nickname: pyriel
Registered: Jul, 2002

Making small program w/ filter... filters dont work. Help? Posted: Jul 22, 2002 4:45 PM
Reply to this message Reply
Advertisement
Ok, I will post the code here. My filters don't seem to work and I can't find out what I am doing wrong for the life of me... other than that I beleive it works. If someone sees it before midnight tonight (its due then) plz reply. Thx.

import java.util.Random;

// this interface permits us to accept or
// reject an object
interface Filter
{
boolean accept(Object o);
}

// this interface permits us to process an
// array of objects based on a filter criteria
interface Processor
{
Object process(Object[] arr, Filter f);
}

// this implementation of the filter accepts everything
class AllFilter implements Filter
{
public boolean accept(Object o)
{
return true;
}
}

// this implementation of the filter should only accept
// even numbers
class EvenFilter implements Filter
{
public boolean accept(Object o)
{
int number = ((Integer)o).intValue();
int numDivByTwo = number % 2;

if(numDivByTwo == 0)
return true;
else
return false;
}
}

// this implementation of the filter should only accept
// odd numbers
class OddFilter implements Filter
{
public boolean accept(Object o)
{
int number = ((Integer)o).intValue();
int numDivByTwo = number % 2;

if(numDivByTwo != 0)
return true;
else
return false;
}
}

// this implementation f the filter should only accept
// every other number that is input to it.
class AlternatingFilter implements Filter
{
public boolean accept(Object o)
{
boolean everyOther = true;

if (everyOther == true)
{
everyOther = false;
return true;
}
else
{
everyOther = true;
return false;
}
}
}

// this implementation of a processor should take all the
// aceptable objects out of the array, and concatenate them
// space separated into a string, then return that string.
class ToStringProcessor implements Processor
{

StringBuffer s1 = new StringBuffer();

public Object process(Object[] arr, Filter f)
{
for (int i = 0; i < arr.length; i++)
{
if (f.accept(arr))
{
s1.append(arr.toString());
s1.append(" ");
}

}
return s1;
}
}

// this implementation of a processor should compute the
// average of all the acceptable objects in the array, and
// return that average.
class AverageProcessor implements Processor
{
public Object process(Object[] arr, Filter f)
{
double average;
int total = 0;
int number;

for (int i = 0; i < arr.length; i++)
{
if (f.accept(arr))
{
number = ((Integer)arr).intValue();
total = total + number;
}
}
average = (double)total / arr.length;
return new Double(average);
}
}

public class Week01
{
public static final int MAXSIZE = 100;

public static void main(String[] args)
{
Random random = new Random(3141592655L);

// create an array of random Integer objects
Integer randomInts[] = new Integer[MAXSIZE];
for (int i=0; i<randomInts.length; ++i)
randomInts = new Integer(random.nextInt(1000));

Filter all = new AllFilter(); // all elements
Filter evens = new EvenFilter(); // only even elements
Filter odds = new OddFilter(); // only odd elements
Filter alternating = new AlternatingFilter(); // alternating elments

Processor print = new ToStringProcessor();
Processor avg = new AverageProcessor();

// print all the matching objects
System.out.println("All: " + print.process(randomInts, all));
System.out.println("\nEvens: " + print.process(randomInts, evens));
System.out.println("\nOdds: " + print.process(randomInts, odds));
System.out.println("\nAlternating: " + print.process(randomInts, alternating));

// print the average of the matching objects.
System.out.println("\nAll: " + avg.process(randomInts, all));
System.out.println("\nEvens: " + avg.process(randomInts, evens));
System.out.println("\nOdds: " + avg.process(randomInts, odds));
System.out.println("\nAlternating: " + avg.process(randomInts, alternating));

}
}

Topic: sound Previous Topic   Next Topic Topic: Problem with program - string array & tokenizers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use