Amil Mehmedagic

Greenhorn
+ Follow
since Feb 27, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Amil Mehmedagic

Hi!

I have a set of data, such as this:
Anne Other 15 19 15 12 18
Roger the Dodger 11 13 9 15
Eric the Red 19 19 20 15 20
Java Janet 20 20 20
What I need to do is to create another file and print the sum of this data, so it looks like this:
Anne Other 79
Roger the Dodger 48
Eric the Red 93
Java Janet 60
My window however shows the following output:
79
48
93
60
How do I get Java to print out the names from the other file? Do I need to store all of the data in an array?
Here is my code:
public class ProcessingMarks {

public static void main (String [] args) throws IOException {
/*BufferedReader br =
new BufferedReader(new FileReader("marks.txt"));
FileWriter fw = new FileWriter(fileName, true);
PrintWriter pw = new PrintWriter(fw);*/
FileReader fr = new FileReader("marks.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("results.txt");
PrintWriter pw = new PrintWriter(fw);


String line = br.readLine();

int sum;
while (line != null) {
StringTokenizer st = new StringTokenizer(line);
sum = 0;
while (st.hasMoreTokens()) {
try {
//sum = sum + Integer.valueOf(st.nextToken());
//sum = sum + Integer.valueOf(st.nextToken()).intValue();
sum = sum + Integer.parseInt(st.nextToken());
} catch (NumberFormatException e) {}
}
pw.println("\t"+sum);
//pw.println(line+"\t"+sum);
line = br.readLine();

}

br.close();
//pw.println(line+"\t"+sum);

pw.close();
}
}
Looking forward to your response!
20 years ago
Thanks Stefan!

This is precisely what I wanted to accomplish! And thankx for the debug code too!
20 years ago
Hello!

I am trying to get the rat to stop when its centre falls within 5 pixels of the head of the snake. The head of the snake is the first element in an array of 25 snake objects. There's five classes to this application altogether and I am providing the Snake, Rat and SnakeRatPanel classes to help you on this.

I think the problem is in the moveRat method of the SnakeRatPanel class, as it is here where I am controlling the movement of the rat. The condition
to stop the rat when its centre is within 5 pixels of the snake's head
doesn't do anything.

I appreciate your help on this one!
Below are the three classes: Rat, Snake and SnakeRatPanel:
20 years ago
I am getting the error message non-static variable clockwise cannot be referenced from a static context, when running the program code below. How do I counter this problem?
The program below asks a user to input a probability and number of circles.
The program then animates the input number of circles around a square of side 400 pixels. At each move there is a probability that each circle moves anticlockwise. If it does so the variable count counts that circle.
When all circles are moving anticlockwise, the animation needs to stop.
Below is the code of the two classes. The error occurs in the main method of the Main class. This is where I check whether a circle moves anticlockwise to assign it to the count variable.
Please help in solving the above problem! [ edited to break long lines and remove the evil tab character -ds ]
[ April 27, 2004: Message edited by: Dirk Schreckmann ]
20 years ago
My dilemma with this problem is that the instance variable clockwise, which checks whether the circle is moving clockwise or anticlockwise is an instance variable that can only be used in the Circle class, while the loop that controls the movements of circles in array is in the main class.
I think that that particular code, which checks whether the circle is moving anticlockwise, should be in the main method of the Main class.
But how do I check whether the circles are moving anticlockwise with no access to the clockwise variable?
What I want to do is check whether the circle is moving anticlockwise. If it is count it. Loop until all circles are moving anticlockwise, then stop.
But how do do this please, because I cannot access the clockwise variable from the Main method.
20 years ago
I need to stop the animation when all circles are moving anticlockwise
20 years ago
Hi!
I've coded two classes that create an array of the circle object and then move this array of circles by means of redrawing its position in the window.
The circles move clockwise at first and at each move there is a probability, as input by the user, that they are going to change direction. When they change direction the circles cannot again move clockwise. This is what the code below does.
What I need to do to complete the program is to stop the animation when all circles are moving anticlockwise. I've written the following steps, but I am having difficulties coding them in Java.
1. Check the position of each circle (i.e. whether it's moving clockwise or anticlockwise)
2. If it's moving anticlockwise then count the circle.
3. Repeat for all other circles.
4. When all circles are moving anticlockwise stop the animation.
I've figured out that an animation is stopped by the construct if (...) { }.
Can anybody help me code the above four steps into Java?
Below is the code of the two classes.
20 years ago
After working on this for hours, I've finally figured out how to move the array of circles around and change the direction according to the given probability. However what I still haven't figured out is how to make the animation stop when the circles are moving anti-clockwise. To be more precise each time the circles move a random number is generated and if it's less than the given probability, the circle changes direction and starts moving anticlockwise. Now when the circles are moving anti-clockwise the random number needs to be generated again and if it's less than the given probability, the animation (of that particular circle) needs to stop.
Below are the two classes. Note that only the move method of the Circle class needs to be changed slightly. I have tried it myself without success. Please if there is someone out there who could assist me with this problem, I 'd greatly appreciate it.
Circle Class

Main Class
20 years ago
Hi Tobias and thanks for posting!


Now your problem is that nothing is been drawn.


For this task I was provided with classes that create the initial circles.
CirclesFigure.create( ); and CirclesFigure.draw(circle); draws the initial circles. The program requests the user to Input the number of circles at the beginning and that number of circles is displayed in the window.
Ok here is another unsuccessful attempt of mine at the move method. The first circle appears at initial position 10,10. I found this out using Transcript.println(getX());Transcript.println(getY()); This is why I used radius as the variable from where the array of circles moves!
Yet, there there seems to be something missing from the code, because the circles don't move. Am I using the wrong logic here?
20 years ago
Hi everybody!
I can't seem to figure out how to make an array of circles move!
The problem requires me to animate a number of circles input by the user clockwise around a square of side 400 pixels with a 20 Milliseconds delay. At each move the circles must be capable of changing direction with probability input by the user at run-time. When all circles are in a state where they move anticlockwise, the animation stops.
Now there are two classes, a Circle class that encapsulates the circle's state and a Main class, which I used to draw the array of circles.
In the main class the code below calls a move method, which is in the Circle class to move each circle. I am having difficulties with coding the actual move method.
for (int j = 0; j < circles; j++) {
circle[j].move();
}
Here is the code of the two classes. So far the circles have been drawn and the probabilites and number of circles have been input, but there is no animation happening. Could anyone please explain how to make the circles (which form the shape of a snake) move, as my logic doesn't seem to be working. Below is what I've done. I'd appreciate any comments as to why, although the code in the move method itself is fine, nothing happens as a result.
Circle Class:
20 years ago
I got it to work exactly as I wanted it to!
Thank you very much Tom for your help and support with this task. I wasted countless hours trying to make the program work using faulty logic. I probably wouldn't have finished this task so quickly without your help. Of course I thank Brian for his contribution as well.
This is now only the third small program I wrote. In your earlier message, you said that there is a more elegant way to do it. What exactly did u have in mind?
I'm thinking of trying to write the same program using switch-statements.
20 years ago
Here is my final shot at it.
It however still doesn't do what it's supposed to do.
What have I done wrong?
public static void main ( String [ ] args) {

CircleFigure.create();
CircleFigure.setRadius(30);
CircleFigure.moveTo(30,30);
int x = CircleFigure.getXCentre();
int y = CircleFigure.getYCentre();

boolean isClockwise = true;

while (true) {
Delay.milliseconds(20);
if (Math.random() < 0.01)
isClockwise = false;
if (isClockwise = true) {
while (x!=430) {
Delay.milliseconds(20);
CircleFigure.moveRight(5);
x = x + 5;
if (Math.random() < 0.01)
isClockwise = false;

}
while (y!=430) {
Delay.milliseconds(20);
CircleFigure.moveDown(5);
y = y + 5;
if (Math.random() < 0.01)
isClockwise = false;
}
while (x!=30) {
Delay.milliseconds(20);
CircleFigure.moveLeft(5);
x = x - 5;
if (Math.random() < 0.01)
isClockwise = false;
}
while (y!=30) {
Delay.milliseconds(20);
CircleFigure.moveUp(5);
y = y - 5;
if (Math.random() < 0.01)
isClockwise = false;
}


}else {
while (y!=430) {
Delay.milliseconds(20);
CircleFigure.moveDown(5);
y = y + 5;
if (Math.random() < 0.01)
isClockwise = true;}
while (x!=430) {
Delay.milliseconds(20);
CircleFigure.moveRight(5);
x = x + 5;
if (Math.random() < 0.01)
isClockwise = true;}
while (y!=30){
Delay.milliseconds(20);
CircleFigure.moveUp(5);
y = y - 5;
if (Math.random() < 0.01)
isClockwise = true;}
while (x!=30) {
Delay.milliseconds(20);
CircleFigure.moveLeft(5);
x = x - 5;
if (Math.random() < 0.01)
isClockwise = true;}
}
}
}
}
20 years ago
I've followed your advice and written this code. However the circle still
only moves around the perimeter. It doesn't change the direction.
What have I done wrong?
Here is the code:
[code]
public class MovingCirclenew {

public static void main ( String [ ] args) {

CircleFigure.create();
CircleFigure.setRadius(30);
CircleFigure.moveTo(30,30);
int x = CircleFigure.getXCentre();
int y = CircleFigure.getYCentre();

boolean isClockwise = true;

while (true) {
Delay.milliseconds(20);
if (Math.random() < 0.01)
isClockwise = !isClockwise;
if (isClockwise) {
while (x!=430) {
Delay.milliseconds(20);
CircleFigure.moveRight(5);
x = x + 5; }
while (y!=430) {
Delay.milliseconds(20);
CircleFigure.moveDown(5);
y = y + 5; }
while (x!=30) {
Delay.milliseconds(20);
CircleFigure.moveLeft(5);
x = x - 5;}
while (y!=30) {
Delay.milliseconds(20);
CircleFigure.moveUp(5);
y = y - 5;}


}else {
while (y!=430) {
Delay.milliseconds(20);
CircleFigure.moveDown(5);
y = y + 5;}
while (x!=430) {
Delay.milliseconds(20);
CircleFigure.moveRight(5);
x = x + 5; }
while (y!=30){
CircleFigure.moveUp(5);
y = y - 5; }
while (x!=30) {
Delay.milliseconds(20);
CircleFigure.moveLeft(5);
x = x - 5;}
}
}
}
}
The problem I believe lies in that the parameters or path that the circle is supposed to move in needs to be declared in a while loop. This is the only way I know to tell the object where to stop travelling along one direction. This means that the one loop is active until the circle moves to x == 430 and then the next loop is activated, which brings the circle down. During all of this time however the randomNum is not generated. It is only generated once the circle has travelled once around the perimeter.
How can I then program, so that the circle has a chance of 0.01 to change its direction, each time it moves either right,down,left or up? Because I think the code above only generates the number after the circle has already travelled around, instead doing it every time the object moves.
20 years ago
Thanks for your response!
I haven't been taught about the Timer class.
I am using the CircleFigure class, which has the following methods: moveRight(x),moveLeft(x),moveUp(x),moveDown(x),moveTo(x,y),setRadius(x),
setColour(c),drawHollow(),drawFilled(),getXCentre() and getYCentre().
Ultimately, I cannot use the Timer class for this problem.
Here is my code, which makes the circle move around a perimeter of 400 pixels. I know that I have to use the random method from the Math class to generate a number between 0 and 1, which will decide on the direction.
Note that this is missing from the code:
[code]
public class MovingCircle3 {

public static void main ( String [ ] args) {

/*Create a circle of radius 30 pixels at position(30,30).
Circle is to move at a rate of 5 pixels every 20 milliseconds to follow the perimeter of the square (400 pixels).
Initially the circle is to move in a clockwise direction.
However at each move the circle must have a probability of 0.01 to change direction.
*/
[code]
public class MovingCircle1{

public static void main ( String [ ] args){

CircleFigure.create();
CircleFigure.setRadius(30);
CircleFigure.moveTo(30,30);
int x = CircleFigure.getXCentre();
int y = CircleFigure.getYCentre();
double randomNum;
double probDirectionChange = 0.01;
while (true) {
do {
Delay.milliseconds(20);
x = x + 5;
CircleFigure.moveRight(5);
}
while ( x != 430);

do {
Delay.milliseconds(20);
y = y + 5;
CircleFigure.moveDown(5);
}
while ( y != 430);

do {
Delay.milliseconds(20);
x = x - 5;
CircleFigure.moveLeft(5);
}
while ( x != 30);

do {
Delay.milliseconds(20);
y = y - 5;
CircleFigure.moveUp(5);
}
while ( y != 30);

}
}
}
I have also written other slight variations of the code above. I also tried placing if-statements within each of the do-loops. The circle however just changes its direction for a second and immediately continues on its clockwise path. The correct way is that when the circle changes its direction with prob. 0.01 and thus starts moving anti-clockwise, then the probability of changing back to clockwise is 0.01 and vice versa. At the start the probability of changing the direction from clockwise to anti-clockwise is 0.01 and this is a constant.
However the random number (Math.random()) needs to be generated each time, the circle moves 5 pixels, so there is always a chance of direction change.
So far i've just been unable to utilize the Math.class correctly for this purpose, or is there something else I overlooked?
A thousand thanks for any assistance!
20 years ago
Hi there! I'm new to programming and have following problem.
A circle, with radius 30, needs to move around a perimeter of 400 pixels from position (30,30) in a clock-wise direction at first.
It moves each time by 5pixels delayed by 20 milliseconds.
With each move, it needs to be capable to change its direction with a probability of 0.01. Although I'm able to have the circle moving around the perimeter at the desired speed, I just can't get it to randomly change
its direction.
I have tried if-statements: (if randomNum > 0.01 then change direction,) switch-statements and boolean values. I however haven't been able to get it to work for me.
I know how to change the direction once or twice, but this needs to be done indefinitely, during each move of the circle and everything needs to be enclosed by a while (true) loop, thus making sure the circle moves until program is quit.
Clearly, this is repetition, so a loop is required, but where should the random class: Math.random be placed, as a number between 0 and 1 needs to be generated on each move. If num > 0.01 then move clockwise, if num < 0.01 then move anti-clockwise. During the next move it needs to be: if num >0.01 then move anti-clockwise, if num < 0.01 then move clockwise and so on.
Please help!!
20 years ago