# Creating a data algorithum method that calculates the height difference

• October 7th, 2013, 10:36 AM
ss1w
Creating a data algorithum method that calculates the height difference
I am working on a new project known as "construction building". I am pretty weak in java coding thus i am sincerely asking all of you to hold my hand and guide me step by step with the implementation. I know it is ridiculous to ask you guys for help without putting in effort myself thus i have spent some thought on the implementation process and these is what i have came up with.

A construction company would like to calculate the height difference between the planks set.
(Height different between 1 and 2 is (1)) && (Height different between 1 and 4 is (2)) && (Height different between 1 and 5 is (3))

Attachment 2362

so to start off with it, i thought i would set a criteria. The focus point will be the top most plank.
since the insert order might be random, <start, end , height>

<4,5,1> <2,6,4>
<5,7,3> <1,3,3>
<2,4,2> into <5,7,3>
<2,6,4> <2,4,2>
<1,3,3> <4,5,1>

then i will grab the plank with the highest height value and set it as my "focus"
using the focus <start, end , height>, i will start to do a compare value as per what you have showed me previously to eliminate those planks that is not anywhere below the "focus" plank.

plank.start > focus end
plank.end < focus.start
plank.height > focus.height

after eliminating those planks that are not anywhere below the "focus" plank, we take the height of the "focus" plank and subtract the height of the "other" planks below.

however there is a part which i have yet thought of the implementation process and that is how do i get the
plank 1)<2,6,4> - plank5)<4,5,1> = <4,5,3> (where <4,5,x> is the place where the height difference is calculated , and x is the height difference)

I am thinking of implementing my code in such format which i have adopted from on of the forum member. I know it is not much and it is more of a sudo code than a working code.

Code :

``` public static Map<Position, List<Position>> getHeightDifference(List<Position> planks) {   final float start; final float end; final float height;   public Position(float start){ this.start = start; } public Position(float end){ this.end = end; } public Position(float height){ this.height = height; }   public float getStart(){ return this.start; } public float getEnd(){ return this.end; } public float getHeight(){ return this.height; }   @Override public int compareTo(Position focus) { if(this.start > focus.getEnd()) { return -1; } else if (this.end<focus.getstart) { return -1; } else if (this.height > focus.height) { return -1; } else { return 0; } }```
• October 7th, 2013, 03:26 PM
GregBrannon
Re: Creating a data algorithum method that calculates the height difference
I honestly don't understand the plank concepts you've presented, but even without complete understanding, it seems you could create a Plank class with the attributes start, end, and height. From there, the Plank class can have all of the methods necessary to do what you need with Plank instances: compare them to determine equality, or which of two is the larger; subtract them; add them, etc. Using Plank methods, a collection of planks can be ordered or whatever else needs to be done.
• October 7th, 2013, 04:20 PM
ss1w
Re: Creating a data algorithum method that calculates the height difference
Hi @GregBrannon , just wondering how would the plank class look like ? what is confusing me is how can i extract a single value from the <start,end,height> list to compare and return the whole list if it matched.
• October 7th, 2013, 04:58 PM
GregBrannon
Re: Creating a data algorithum method that calculates the height difference
You're focused on the visual representation of a Plank and seeing the problem from that limited (or limiting) perspective. The Plank can "look" however you want it to. That's what toString() methods are for. But a Plank can be represented in the program completely differently than how it "looks".

Here's bare bones Plank class based on my limited understanding of what you intend to do with them. It's not much different than what you posted, but it's purpose and use would be different:
Code java:

```// class Plank defines the attributes of a Plank object and contains the // methods necessary to get and set those attributes and a compareTo() // method to compare two Plank objects public class Plank { // instance variables that define a Plank object's attributes double start; double end; double height;   // a 3-parameter constructor that sets the attributes on creation public Plank( double start, double end, double height ) { this.start = start; this.end = end; this.height = height;   } // end constructor   // attribute getters (accessors) and setters (mutators) as needed     // method compareTo compares the current object (this) to the Plank // object parameter and returns -1 if this is < the parameter, 0 if // this and the parameter are equal, and 1 if this is > the parameter // // note: this is a very basic method that could be dressed up a bit public int compareTo( Plank plank ) { // i chose height as the important variable to compare, but the // actual comparison may be length (start - end) or some other // formula/combination of the attributes i don't understand if ( this.height < plank.height ) { return -1; } else if ( this.height > plank.height ) { return 1; }   // the object's are equal return 0;   } // end method compareTo()   } // end class Plank```
I'm not sure what you mean by "return the whole list." You'll have to explain that better.
• October 7th, 2013, 07:40 PM
ss1w
Re: Creating a data algorithum method that calculates the height difference
Thanks GregBrannon. That is good to get me started.

Cheers
• October 8th, 2013, 07:20 AM
ss1w
Re: Creating a data algorithum method that calculates the height difference
Hi @GregBrannon, i notice that your constructor is set as(this.start = start). I am thinking you are assuming that my values will be literally <start,end,height>. However my input might be just random number such as <3,2,5> <4,2,1> <3,2,1> does it still work ?
Code :

```public class HeightCalculator {   double start; double end; double height;   public HeightCalculator(List<Position> platforms) { this.start = start; this.end = end; this.height = height;   }}```
• October 8th, 2013, 07:48 AM
GregBrannon
Re: Creating a data algorithum method that calculates the height difference
I'm sure there's lots I'm not getting, but again you're focused on the format and and/or appearance of the data, considering it fixed/immutable/inflexible and driving the whole rest of the program. Consider the possible ways the data can be manipulated to achieve the desired results rather than as the main driver of the program's requirements.

How is the plank data read? From the keyboard? From a file? If from a file, can you give a short example of the file (no more than 10 lines or so).

Be careful how you use the word 'random,' because it means a specific thing in programming. I'm not sure how you meant it.
• October 8th, 2013, 08:34 AM
ss1w
Re: Creating a data algorithum method that calculates the height difference
the plank data is read from a junit test file which is in such format

Code :

```public class HeightCalculatorTest {   @Test public void basicTest() { List<Position> plank = new ArrayList<Position>(); Position p1 = new Position(1, 3, 2); Position p2 = new Position(2, 5, 4); Position p3 = new Position(2, 4, 1); Position p4 = new Position(4, 7, 3); plank.add(p1); plank.add(p2); plank.add(p3); plank.add(p4); } }```

To summaries the whole idea of what i am trying to do.
I am to calculate the height difference between the top plank and the bottom plank below. The bottom plank below have to be in the same start - end range as the top plank, with different height. Both planks cannot be overlapping each other.

My chain of thoughts is to sort all the planks according to height, find the place where both the top plank and bottom plank have the same (start-stop) and find the height difference.
• October 8th, 2013, 09:03 AM
GregBrannon
Re: Creating a data algorithum method that calculates the height difference
I'm starting to get it, and I don't see any complexity. I'm not sure which sort makes sense yet. You may want to group them by the same start/end values. In the four you've shown above, none qualify as being a top/bottom pair, right? Have you begun working the comparison algorithm?
• October 8th, 2013, 09:36 AM
ss1w
Re: Creating a data algorithum method that calculates the height difference
Actually there is. There is an top/bottom region between (2, 5, 4),(4, 7, 3)

*assuming that the numbers are the actual planks
----2,3,4,5
--------4,5,6,7
---12,3
----2,3,4

(start,end,height)
(2, 5, 4) is the top most plank.
(4, 7, 3)
(1, 3, 2)
(2, 4, 1) last plank

I have came up with a way to sort out those that disqualify the conditions. I will be assuming all of them fulfill the condition and filter them through a forloop or if else

plank.start > focus.end
plank.end < focus.start
plank.height > focus.height

*any plank that matches this will be ignored
• October 8th, 2013, 04:34 PM
GregBrannon
Re: Creating a data algorithum method that calculates the height difference
Ahhh. I didn't understand what you meant by "Both planks cannot be overlapping each other." I thought that meant the end points have to be the same, but instead two planks just have to include common ranges entirely within their end points. So the logic for that would be something like:

if ( plank1.start >= plank4.start && plank1.end >= plank4.end )

Can it be reversed? Could it be:

*assuming that the numbers are the actual planks
----2,3,4
--------4,5,6,7
---12,3
----2,3,4,5

If so, then the if statement I've outlined above would have to be ORd with plank1 and plank4 changing places. That works for the correct answer you've shown. I haven't checked to make sure it fails appropriately for the remaining plank comparisons.

I'm still a bit confused, but you should be able to take what I've started with and polish it a bit or unconfuse it.
• October 8th, 2013, 04:57 PM
ss1w
Re: Creating a data algorithum method that calculates the height difference
hahaa. i see the confusing. Sorry i didnt explain it properly. What i meant by "Both planks cannot be overlapping each other." is that planks at the same level cannot be in the same start - end range.

I am polishing up what you have shared with me, will put it up here when i am done :)