E10.7 In Worked Example 10.1, make the process method a default method of the Se
ID: 3853175 • Letter: E
Question
E10.7 In Worked Example 10.1, make the process method a default method of the Sequence
interface.
Worked Example 10.1 (four classes and an interface)
1)
/**
This class analyzes the distribution of the last digit of values
from a sequence.
*/
public class LastDigitDistribution
{
private int[] counters;
/**
Constructs a distribution whose counters are set to zero.
*/
public LastDigitDistribution()
{
counters = new int[10];
}
/**
Processes values from this sequence.
@param seq the sequence from which to obtain the values
@param valuesToProcess the number of values to process
*/
public void process(Sequence seq, int valuesToProcess)
{
for (int i = 1; i <= valuesToProcess; i++)
{
int value = seq.next();
int lastDigit = value % 10;
counters[lastDigit]++;
}
}
/**
Displays the counter values of this distribution.
*/
public void display()
{
for (int i = 0; i < counters.length; i++)
{
System.out.println(i + ": " + counters[i]);
}
}
}
2)
public class RandomSequence implements Sequence
{
public int next()
{
return (int) (Integer.MAX_VALUE * Math.random());
}
}
3)
public interface Sequence
{
int next();
public void process(Sequence seq, int valuesToProcess);
}
4)
public class SequenceDemo
{
public static void main(String[] args)
{
LastDigitDistribution dist1 = new LastDigitDistribution();
dist1.process(new SquareSequence(), 1000);
dist1.display();
System.out.println();
LastDigitDistribution dist2 = new LastDigitDistribution();
dist2.process(new RandomSequence(), 1000);
dist2.display();
}
}
5)
public class SquareSequence implements Sequence
{
private int n;
public int next()
{
n++;
return n * n;
}
}
Explanation / Answer
Note:- in order to make a process as default process we need to define the method in the interface with default keyword.
USE OF DEFAULT METHOD :- default method will be executed,if the class which interfaces won't define the method.Then,in that case default method will be executed
Changes:- I have added the given method itself as default method for process and the changes made are in BOLD format
Code:-
1)
/**
This class analyzes the distribution of the last digit of values
from a sequence.
*/
public class LastDigitDistribution
{
private int[] counters;
/**
Constructs a distribution whose counters are set to zero.
*/
public LastDigitDistribution()
{
counters = new int[10];
}
/**
Displays the counter values of this distribution.
*/
public void display()
{
for (int i = 0; i < counters.length; i++)
{
System.out.println(i + ": " + counters[i]);
}
}
}
2)
public class RandomSequence implements Sequence
{
public int next()
{
return (int) (Integer.MAX_VALUE * Math.random());
}
}
3)
public interface Sequence
{
int next();
default public void process(Sequence seq, int valuesToProcess){
/**
Processes values from this sequence.
@param seq the sequence from which to obtain the values
@param valuesToProcess the number of values to process
*/
for (int i = 1; i <= valuesToProcess; i++)
{
int value = seq.next();
int lastDigit = value % 10;
counters[lastDigit]++;
}
}
4)
public class SequenceDemo
{
public static void main(String[] args)
{
LastDigitDistribution dist1 = new LastDigitDistribution();
dist1.process(new SquareSequence(), 1000);
dist1.display();
System.out.println();
LastDigitDistribution dist2 = new LastDigitDistribution();
dist2.process(new RandomSequence(), 1000);
dist2.display();
}
}
5)
public class SquareSequence implements Sequence
{
private int n;
public int next()
{
n++;
return n * n;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.