Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Man, Woman, Dog: A Task from the Past

· Java Zone
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

## Introduction

This article is a personal reflection more than some kind of tutorial or how-to article--which I have mostly written in the past. It describes solutions for an exercise that was given to students by  economics faculty who visited the computer science department. The task is relatively easy but somehow we couldn't manage it at that time.

## Motivation

Why am I writing about it? The task has followed me for years without making a big headache but somehow it vanished and came back to me over and over again. Two weeks ago I decided to finish this problem so I sat in front of my PC and implemented two possible solutions for it.

## Exercise

More then ten years ago, when I was studying computer science, a friend of mine was studying economics in Germany. He was tasked to write a simple program for a simple problem common to physics lessons, where a man and a woman are far from each other and moving to each other meanwhile their dog is moving to and from them. The main task was to calculate how many times the dog moved to and from the man and the woman before they met each other. The program had to be written in C, which we didn't know well and was one of the reasons for the failure.

## Programming

### The past

Not knowing the programming syntax was the main reason for the problem, but the solution on an abstract level could be done without it. For the calculations about relations between velocity, time and distance, a physics formula was needed. I had only guessed it could be done by knowing the time of the meeting between the man and woman as a global case and the time of meeting between the dog and man and dog and woman as secondary case. That was the first projection for the problem which was used at that time. But without knowing the syntax well, the task couldn't be completed and no further answers or details of the solution could be solved. That was really frustrating.

### Solution

The solutions for this problem were made in several steps and was implemented in Java:

• Implementing the physics formula
• Adding the three different velocities
• Main Process - Iteration over time
• for every second
• by calculation of the meeting-time

### Iteration over time for every second

```package name.stojanok.dzone.manwomandog;

public class ManWomanDog1 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();

int initDistance = 1000;

int manVelocity = 15;
int womanVelocity = 10;
int dogVelocity = 30;

int time = 0;
int dogTime = 0;
int dogDistance = initDistance;

boolean directionToMan = true;

while (true) {
double dW = calculateDistance(womanVelocity, time);
double dM = calculateDistance(manVelocity, time);
if (dW + dM >= initDistance) {
System.out.println("man and woman met!");
break;
}

double dD = calculateDistance(dogVelocity, dogTime);
double dWd = calculateDistance(womanVelocity, dogTime);
double dMd = calculateDistance(manVelocity, dogTime);

if (directionToMan && dMd + dD >= dogDistance || !directionToMan
&& dWd + dD >= dogDistance) {
if (directionToMan && dMd + dD >= dogDistance) {
System.out.println("met man! woof!");
} else if (!directionToMan && dWd + dD >= dogDistance) {
System.out.println("met woman! woof!");
}
directionToMan = !directionToMan;
System.out.println(dD + "m");
System.out.println(dogTime + "s");
dogDistance = (int) Math.abs(initDistance - dW - dM);
dogTime = 0;
}

dogTime++;
time++;
}
System.out.println(System.currentTimeMillis() - startTime + "ms");
}

private static double calculateDistance(double velocity, double time) {
return velocity * time;
}
}

```

### Iteration over time by calculation of the meeting-time

```package name.stojanok.dzone.manwomandog;

public class ManWomanDog2 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();

int initDistance = 1000;

int manVelocity = 15;
int womanVelocity = 10;
int dogVelocity = 30;

int totalTime = 0;

boolean directionToMan = true;

double meetingDtime = 0;

double innerDistance = initDistance;

double meetingTtime = calculateTime(initDistance, manVelocity
+ womanVelocity, false);
while (true) {
double dogV = dogVelocity;
if (directionToMan) {
dogV += manVelocity;
meetingDtime = calculateTime(innerDistance, dogV, true);
innerDistance = calculateDistance(dogVelocity, meetingDtime)
- calculateDistance(womanVelocity, meetingDtime);
} else {
dogV += womanVelocity;
meetingDtime = calculateTime(innerDistance, dogV, true);
innerDistance = calculateDistance(dogVelocity, meetingDtime)
- calculateDistance(manVelocity, meetingDtime);
}
totalTime += meetingDtime;
if (totalTime >= meetingTtime) {
System.out.println("man and woman met!");
break;
} else {
if (directionToMan) {
System.out.println("met man! woof!");
} else {
System.out.println("met woman! woof!");
}
double dD = calculateDistance(dogVelocity, meetingDtime);
System.out.println(dD + "mm");
}
System.out.println(meetingDtime + "s");
directionToMan = !directionToMan;
}
System.out.println(System.currentTimeMillis() - startTime + "ms");
}

private static double calculateDistance(double velocity, double time) {
return velocity * time;
}

private static double lostTime = 0;

private static int calculateTime(double distance, double velocity,
boolean useOptimization) {
double rawResult = distance / velocity;
if (useOptimization && lostTime != 0) {
rawResult -= lostTime;
}
int returnValue = (int) Math.ceil(rawResult);
lostTime = useOptimization ? returnValue - rawResult : 0;
return returnValue;
}
}
```

### Problems

Because I wanted the debug outputs to be the same as possible it was necessary for the second program (by calculation of the meeting-time method) to use an extra time difference parameter as workaround for calculating the time. This is a general problem if you want to create more solutions (calculation methods) for the same problem when another calculation dimension is used as reference or the precision of every result has to be very detailed.

## Analysis

Why this task for an economics student? Maybe to see and learn other aspects of programming which are not familiar in economics. Another fact about the task is that it was estimated to take two hours to find a solution but if you are fit enough for this kind of work you can handle it in a half hour or less if you only want to achieve the end result as fast as possible.

## Inspiration

Another inspiration to me for writing this article was a song from the newest Daft Punk album "Random Access Memories", the third one, where the Italian music producer Giovanni Giorgio Moroder talks about his first days of writing music. The words there sounded quite nostalgic which had inspired me to think about my past too.

## Résumé

It is very interesting how something like this exercise can anguish you all the time, binding you to the problems of the past, until you solve them and maybe write about them. Why are those tiny problems demanding so much attention? However, solving them later in a very short time makes you wonder how this task became so easy and simple and what changed since the last time you encountered the problem. You see how the experience you gained over time is helping you to solve other similar problems.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.