The Artima Developer Community
Sponsored Link

Java Answers Forum
for loop

65 replies on 5 pages. Most recent reply: Mar 8, 2007 3:14 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 65 replies on 5 pages [ 1 2 3 4 5 | » ]
Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

for loop Posted: Jan 15, 2007 5:01 AM
Reply to this message Reply
Advertisement
Hi all I am having a slight problem with my looping i have only been doing the whole java thing for about 3 month and have designed a picture and need my sun to rise from the bottom of the page to the top each time with a greater diameter. so basicly all i need to know is where to put the code and what is the right code.

Any help would be great cheers

import element.*;
import java.awt.Color;
public class MyAssignment
{
public static void main(String[] args)

{
DrawingWindow dWin=new DrawingWindow(300,300);

Rect myRect0 = new Rect(0,0,300,150);
dWin.setForeground(Color.cyan);
dWin.fill(myRect0);
{

loop body sun1;
Circle sun = new Circle(150,150,35);
dWin.setForeground (Color.yellow);
//dWin.fill(aCircle);
sun Drawsun1 = new sun();
Drawsun1.drawImage(dWin,150,150,37);
Drawsun1.drawImage(dWin,150,140,38);
Drawsun1.drawImage(dWin,150,130,39);

Drawsun1.drawImage(dWin,150,120,40);
}
//DrawLights.drawImage (dWin,50,70);


Rect myRect3 = new Rect(0,150,300,150);
dWin.setForeground(Color.blue);
dWin.fill(myRect3);


Lights DrawLights= new Lights();

Rect myRect1 = new Rect(40,60,60,120);
dWin.setForeground(Color.black); //1st building set n balck/ fill
dWin.fill(myRect1);
dWin.drawLine(40,60,70,30);
dWin.drawLine(100,60,70,30);
Rect myRect2 = new Rect(200,60,60,120);
dWin.fill(myRect2);
dWin.setForeground(Color.black);
dWin.drawLine(200,60,230,30);
dWin.drawLine(260,60,230,30);
dWin.drawLine(100,140,200,140);

DrawLights.drawImage (dWin,50,70);

DrawLights.drawImage (dWin,50,90);
DrawLights.drawImage (dWin,50,110);
DrawLights.drawImage (dWin,50,130);
DrawLights.drawImage (dWin,50,150);


DrawLights.drawImage (dWin,80,70);

DrawLights.drawImage (dWin,80,90);
DrawLights.drawImage (dWin,80,110);
DrawLights.drawImage (dWin,80,130);
DrawLights.drawImage (dWin,80,150);

DrawLights.drawImage (dWin,210,70);

DrawLights.drawImage (dWin,210,90);
DrawLights.drawImage (dWin,210,110);
DrawLights.drawImage (dWin,210,130);
DrawLights.drawImage (dWin,210,150);

DrawLights.drawImage (dWin,240,70);

DrawLights.drawImage (dWin,240,90);
DrawLights.drawImage (dWin,240,110);
DrawLights.drawImage (dWin,240,130);
DrawLights.drawImage (dWin,240,150);


}


}


Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

Re: for loop Posted: Jan 15, 2007 7:52 AM
Reply to this message Reply
If nobody can help me with this maybe someone can help me with just the code for the (for loop command)

would really appreciate it

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: for loop Posted: Jan 15, 2007 8:19 AM
Reply to this message Reply
Homework, eh?

This is how the for statement works:

for (intialisation; repeat-condition; post-command) {loop code)}

in the first are you can declare and set your counter, the post-command gets executed everytime before after the loop, the repeat condition is checked everytime before the loop.

So you can do this for example:
    for (int i = startValue; i < maxValue; i += incrementValue) {
        //do something useful
    }

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: for loop Posted: Jan 15, 2007 8:20 AM
Reply to this message Reply
> public class MyAssignment

It looks like homework. You'll need to be specific about what it is you don't know in order to get help with it.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: for loop Posted: Jan 15, 2007 8:22 AM
Reply to this message Reply
So many errors ...

Correct:
in the intialisation part you can declare and set your counter, the post-command gets executed everytime after the loop, the repeat condition is checked everytime before the loop.
The variable declared in the initialisation part is visible only inside this statement.

Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

Re: for loop Posted: Jan 15, 2007 8:31 AM
Reply to this message Reply
Thanx for your help here peeps.

I do actually have 2 other sets of code for the lights and the sun stating the variables which i have to compile before the MyAssignment code. i have just used the loop command and it does not recognise the integers i have declared.

Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

Re: for loop Posted: Jan 15, 2007 8:33 AM
Reply to this message Reply
import element.*;
import java.awt.Color;
public class sun
{

public void drawImage(DrawingWindow dW,int x, int y,int dia)
{Circle sun1 = new Circle(x,y,dia);
dW.setForeground(Color.yellow);

dW.fill(sun1);
}

}
the other part i have to compile

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: for loop Posted: Jan 15, 2007 8:55 AM
Reply to this message Reply
Use the java tags if you post code.
Reading unformatted code just plain sucks.


How did you use the for statement?

Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

Re: for loop Posted: Jan 15, 2007 9:10 AM
Reply to this message Reply
Sorry i was not aware i didnt use the java tags!
I tried to use the for code in the Myassignment before the text of the sun and it threw up loads of errors not recognising( Start Value, Max and increment values.

Diving me mad

Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

Re: for loop Posted: Jan 15, 2007 9:11 AM
Reply to this message Reply
have you got it working????

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: for loop Posted: Jan 15, 2007 10:08 PM
Reply to this message Reply
Which values did you insert for start, maximum and increment?

In the example I used variables which a) have to be replaced with real values or b) have to be defined and set before the loop.

Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

Re: for loop Posted: Jan 16, 2007 1:09 AM
Reply to this message Reply
it is the 1st time i have used the loops so trying to get it to work without errors is a bit of a mission. I am going to try to put the valuse in now. where did you put the values and the code.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: for loop Posted: Jan 16, 2007 1:35 AM
Reply to this message Reply
I won't do your homework for you.

Show me what you got and I tell you why it doesn't work.

And use the java tags when posting it.

Rob wheeler

Posts: 39
Nickname: rob1978
Registered: Jan, 2007

Re: for loop Posted: Jan 16, 2007 5:20 AM
Reply to this message Reply
import element.*;
import java.awt.Color;
public class MyAssignment
{
public static void main(String[] args)

{
DrawingWindow dWin=new DrawingWindow(300,300);

Rect myRect0 = new Rect(0,0,300,150);
dWin.setForeground(Color.cyan);
dWin.fill(myRect0);
for (int x = startValue (5); y < maxValue (5); dia += incrementValue(5)) {
//do something useful
}


Circle sun = new Circle(150,150,35);
dWin.setForeground (Color.yellow);
//dWin.fill(aCircle);
sun Drawsun1 = new sun();

Drawsun1.drawImage(dWin,150,150,37);
Drawsun1.drawImage(dWin,150,140,38);
Drawsun1.drawImage(dWin,150,130,39);

Drawsun1.drawImage(dWin,150,120,40);



//DrawLights.drawImage (dWin,50,70);


Rect myRect3 = new Rect(0,150,300,150);
dWin.setForeground(Color.blue);
dWin.fill(myRect3);


Lights DrawLights= new Lights();

Rect myRect1 = new Rect(40,60,60,120);
dWin.setForeground(Color.black); //1st building set n balck/ fill
dWin.fill(myRect1);
dWin.drawLine(40,60,70,30);
dWin.drawLine(100,60,70,30);
Rect myRect2 = new Rect(200,60,60,120);
dWin.fill(myRect2);
dWin.setForeground(Color.black);
dWin.drawLine(200,60,230,30);
dWin.drawLine(260,60,230,30);
dWin.drawLine(100,140,200,140);

DrawLights.drawImage (dWin,50,70);

DrawLights.drawImage (dWin,50,90);
DrawLights.drawImage (dWin,50,110);
DrawLights.drawImage (dWin,50,130);
DrawLights.drawImage (dWin,50,150);


DrawLights.drawImage (dWin,80,70);

DrawLights.drawImage (dWin,80,90);
DrawLights.drawImage (dWin,80,110);
DrawLights.drawImage (dWin,80,130);
DrawLights.drawImage (dWin,80,150);

DrawLights.drawImage (dWin,210,70);

DrawLights.drawImage (dWin,210,90);
DrawLights.drawImage (dWin,210,110);
DrawLights.drawImage (dWin,210,130);
DrawLights.drawImage (dWin,210,150);

DrawLights.drawImage (dWin,240,70);

DrawLights.drawImage (dWin,240,90);
DrawLights.drawImage (dWin,240,110);
DrawLights.drawImage (dWin,240,130);
DrawLights.drawImage (dWin,240,150);


}


}

This is how i set it out and it just shows me errors, was going quite well till we had to make things move

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: for loop Posted: Jan 16, 2007 8:40 AM
Reply to this message Reply
1. You didn't use Java tags. If you don't know what these are: Look at the right side of the text field, when you post a message. There you see "[ java ]" and "[ /java]" (without the empty spaces in the brackets). Put the first tag before your code, the second tag after your code.

2. If I say "replace" then I don't mean "adding the value in brackets".

3. x, y, dia?
You didn't define y and dia, so they can't be used.

I think you wanted to do this:
    for (int dia = 5; dia < 35; dia+=10) {
        //put here the code "inside the module brackets"
    }


That menas to the Java compiler:
1. create a integer variable called dia and set it to 5
2. repeat what's inside the '{' and '}' brackets (I'll call them module brackets, don't know what the right english term is) as long as dia is smaller than 35.
3. everytime after the execution add 10 to dia's value

What will this loop do? Actually nothing but wasting time, because there is nothing inside the module brackets.
The only thing it will do will be the creation of the variable dia, then set dia to 5, then to 15, then 25, then 35. After that it will destroy the variable dia.

Now instead of calling Drawlights.drawimage 20 times call it once inside the module brakcets of the for loop. Instead of using a constant diameter, use the dia variable created in the loop's header.

Flat View: This topic has 65 replies on 5 pages [ 1  2  3  4  5 | » ]
Topic: for loop Previous Topic   Next Topic Topic: Parametrized types

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use