In Worked Example 10.1, add a default method default int [] values(int n) that yields an array of the first n values of the sequence.
Please use only the code provided below from the worked example and if possible provide screenshots using all the code provided and the test class provided:
Sequence.java
public interface Sequence
{
int next();
}
LastDigitDistribution.java
public class LastDigitDistribution
{
public void process(Sequence seq, int valuesToProcess) {
for (int i = 1; i <= valuesToProcess; i++) {
int value = seq.next();
int lastDigit = value % 10; counters[lastDigit]++;
}
}
}
SquareSequence.java
public class SquareSequence implements Sequence
{
private int n;
public int next()
{
n++;
return n * n;
}
}
RandomSequence.java
public class RandomSequence implements Sequence {
public int next() {
return (int) (Integer.MAX_VALUE * Math.random());
}
}
SequenceDemo.java
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();
}
}
Modified code is given below. Only the code in the interface will change. The question asks for a default method, it has been implemented in the interface shown below
public interface Sequence
{
int next();
default int [] values(int n){
int[] v = new int[n];
for(int i = 0; i < n; i++)
v[i] =
next();
return v;
}
}
In Worked Example 10.1, add a default method default int [] values(int n) that yields an...
need source code for NetBeans
here is the file
Write a class PrimeSequence that implements the Sequence interface of Worked Example 10.1* and produces a sequence of prime numbers. * See Files section bj6_ch10_we.pdf Hint: Pseudocode to produce next prime: class Prime Sequence implements Sequence instance variable int p function next ()I isPrime-true While (isPrime) increment p; for (d-2; until d*d > p divides p) isPrime-false; d+) if (d if isPrime) return p; isPrime-true Use your PrimeSequence class to generate...
Consider the following class: public class Sequence { private int[] values; public Sequence(int size) { values = new int[size]; } public void set(int i, int n) { values[i] = n; } public int get(int i) { return values[i]; } public int size() { return values.length; } } Add a method public boolean sameValues(Sequence other) to the Sequence class that checks whether two sequences have the same values in some order, ignoring duplicates. For example, the two sequences 1 4 9...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...
Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator<Integer> f //complete this You are required to create an inner class in the FibSequence class. The inner class should be called FibIterator. It implements the Iterator<Integer> interface and implements the hasNext() and the next() methods. The iterator iterates through the Fibonacci sequence. The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers....
In class we wrote a method closestPairFast that on an input array of numbers, finds the distance of the closest pair by first sorting the input array and then finding the closest adjacent pair. (See the file ClosestPair1D.java in the Code folder on D2L.) In this problem, you are asked to modify the method so that it returns an integer array consisting of the indices of the closest pair in the original array. If there is a tie, just return...
Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...
Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...
Add a method to the DoubleLinkedList class built in class to reverse every set of values For example: 1, 2, 3, 4, 5, 6 Reverse 3: 3,2,1,6,5,4 Reverse 2: 2,1,4,3,6,5 Reverse 6: 6,5,4,3,2,1 Method header: public void reverseSegments(int setSize) outcome should be like this: Input: 3 1 2 3 4 5 6 output: 3 2 1 6 5 4 Input: 2 1 2 3 4 5 6 output: 2 1 6 5 4 3 ============================================code====================================================================== public class MyDoubleLinkedList<E> { private...
(JAVA NetBeans)
Write programs in java
Example 10.21-24
//Animal.java
/** Animal class
* Anderson. Franceschi
*/
import java.awt.Graphics;
public abstract class Animal
{
private int x; // x position
private int y; // y position
private String ID; // animal ID
/** default constructor
* Sets ID to empty String
*/
public Animal( )
{
ID = "";
}
/** Constructor
* @param rID Animal ID
* @param rX x position
* @param rY y position
*/
public Animal( String...
Add another method public static void displayObject(ArrayList list, int n) that will display then information on the nth object of list. If n is not a valid index, the method should generate and throw and exception that the main can then process. You get to decide what exception (one built into Java or a custom exception) and how you would like to “handle” the exception (terminate the program, prompt for more input, etc). Here is the program so far: import...