+ 3

Java task (Solving problem)

Can somebody help me with this tastk? Im not sure how to start Task description: Pipes are objects, which can be attached to each other, thus forming a cascade of pipes. Data can be fed on one side of the pipe cascade. While the data flows down the pipes, it is processed by the individual pipe objects and finally can be gathered at the draining end of the cascade. The data flow through the pipe cascade can be controlled either from the feeding end (front end) or from the draining end (back end). In the first case, the object at the front end provides methods to feed one or more data elements to the pipe cascade. A call to such a method at the front starts a cascade of further method calls downwards the pipe cascade and finally ends in a last method call at the back end of the cascade. This last method is then responsible for the final handling of the processed data, e.g. writing it to some device. In the second case, the object at the back end of the cascade provides methods to fetch one or several processed data elements from the pipe cascade. A call to such a method at the back end starts a cascade of further method calls upwards the pipe cascade, finally ending in a last method call at the front end of the pipe cascade. This last method call fetches a new data element e.g. from a pool, where the not yet processed elements are stored (e.g. an array). It is also possible, that the front end is endlessly producing new data elements. The second approach is often combined with a technique called lazy evaluation, which means that data is only processed when it is required and methods are only called when necessary. In this task you will implement such a pipe processing system for integer values, using the second approach. By doing this you will get an idea how java streams work Class Descriptions Abstract Class Pipe involves getFeedingPipe, hasNextInteger() Returns true if there are more integer numbers ready to be fetched, nextInteger() Returns either the next integer number, or null Class Feeder method nextIn

8th Nov 2022, 9:09 AM
Stefi
11 Antworten
+ 3
This is one possible way to implement. In the abstract class, you can think what is the common functionality that applies to all pipes. In the subclasses we must call the constructor of the abstract class, and in Feeder I think we can do super(null) because this is the beginning of the pipeline. I added /* comments */ I hope might be helpful for you. Only did the multiplication class but the rest should be easy. Try combining multiple pipes in the test, when you complete the other cases. I enjoyed this task quite a bit, found it interesting. https://code.sololearn.com/c5T7Pchd37kL/?ref=app
8th Nov 2022, 10:47 PM
Tibor Santa
Tibor Santa - avatar
+ 3
BurningHeart Java is an Object Oriented Programming language (OOP) as much as it tries to represent the reality as interactions between 'objects'. A class is like a template or blueprint of what an object contains and what it can do. An class is composed from data (fields) and behaviour (methods). An instance, or more commonly referred to as 'object', is a concrete representation of the class, with specific values assigned to the fields. Instances are created with the 'new' keyword.
10th Nov 2022, 5:39 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Attempts?
8th Nov 2022, 9:27 AM
A͢J
A͢J - avatar
+ 2
Start by writing a class. In Java, this approach usually works ;) The task already gives you plenty of instruction how to structure the code. And most of those instructions you did not even copy.
8th Nov 2022, 9:41 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Stefi can you put the code onto the code playground and attach it here using the plus ➕️ button? There are smart people here (not me) that can definitely help, but they can't read your mind (as far as I know, although some are that smart, I wouldn't be surprised).
8th Nov 2022, 8:01 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
package integerpipes; public class Pipe { public Pipe(Pipe feedingPipe){ } public boolean hasNextInteger() { } public Integer nextInteger() { } protected Pipe getFeedingPipe() { } } class Feeder extends Pipe{ private int[] integers; public Feeder(int[] integers) { this.integers = integers; } public boolean hasNextInteger() { } public Integer nextInteger() { } } class MultiplyingPipe extends Pipe{ private int factor; public MultiplyingPipe(Pipe feedingPipe, int factor) { super(feedingPipe); this.factor = factor; } public boolean hasNextInteger() { } public Integer nextInteger() { } } class ModuloFilterPipe extends Pipe{ private int divisor; public ModuloFilterPipe(Pipe feedingPipe, int divisor){ super(feedingPipe); this.divisor = divisor; public boolean hasNextInteger() { } public Integer nextInteger() { } } } class SummationPipe extends Pipe{ public SummationPipe(Pipe feedingPipe) { super(feedingPipe); } public boolean hasNextInteger() { } public Integer nextInteger() { } }
8th Nov 2022, 9:20 PM
Stefi
+ 2
Abstract Class Pipe This is the abstract base class for all pipes. It knows its feeding Pipe object. This must be provided to the constructor. The final protected method getFeedingPipe returns a reference to the feeding Pipe object. The class declares the following abstract methods: hasNextInteger() Returns true if there are more integer numbers ready to be fetched from this pipe. It returns false, if there are no more integer numbers available. Implementations typically will consult the feeding pipe object to response calls to this method. nextInteger(). Returns either the next integer number, or null if there is no more number available. Implementations typically will consult the feeding pipe object to response calls to this method. Class Feeder An object of the final class Feeder is the front end of a pipe cascade. It can be load with an array of integers upon construction. It feeds the integers to the connected pipe (one per call of the method nextInteger()). The instance variable integers shall be final. Class MultiplyingPipe A final pipe class, which multiplies the transfered integer numbers by a fixed factor. The factor must be provided to the constructor and shall be final. Class ModuloFilterPipe A final pipe class, which applies a modulo operation to each transfered integer number and only lets pass numbers, where the result of the modulo operation is 0. The divisor for the modulo operation must be provided to the constructor and shall be final. Class SummationPipe A final pipe class, which sums up all integer numbers fetched from the feeding pipe object. The sum is calculated and returned upon the first call to the method nextInteger(), given there are any integer numbers available from the feeding pipe. Otherwise null is returned. In any case further calls to nextInteger() will result in a null return value. Can someone please help me ?
8th Nov 2022, 9:21 PM
Stefi
+ 2
https://www.sololearn.com/compiler-playground/cNyzsHipKl9t Oki i added some code, but i tested MultiplyingPipe and i get an Error atClassDiagrammConformace: @Test void testClassDiagramConformance() throws NoSuchFieldException, SecurityException { // base class and modifiers assertEquals(Pipe.class, MultiplyingPipe.class.getSuperclass()); assertEquals(Modifier.PUBLIC | Modifier.FINAL, Feeder.class.getModifiers()); // fields Field field = MultiplyingPipe.class.getDeclaredField("factor"); assertEquals(Modifier.PRIVATE | Modifier.FINAL, field.getModifiers()); assertEquals("int", field.getType().getName()); }
9th Nov 2022, 7:10 AM
Stefi
+ 2
Well the hints are also in the task description, it seems the test requires you to mark all non-abstract classes with the 'final' keyword so they cannot be modified any more by subclassing them. Also the factor variable should be final. Minor details that can be easily fixed, and since you have all that testing fixture available to you, if you just scan through that code you copied last, you can intuitively understand which modifier keywords are validated by the test on every class and field.
9th Nov 2022, 8:08 AM
Tibor Santa
Tibor Santa - avatar
+ 1
I did, i filled up the classes with all the constructors, methods and some variable i thought would work But its not doing much im just stuck with the half empty coded
8th Nov 2022, 10:14 AM
Stefi
8th Nov 2022, 10:10 PM
Ausgrindtube
Ausgrindtube - avatar