7 Replies - 1689 Views - Last Post: 11 October 2012 - 02:55 PM Rate Topic: -----

#1 PSCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 12

JAVA/BLUEJ TURTLE PROBLEM

Posted 10 October 2012 - 09:02 AM

Ive been playing around with the turtle in blueJ/java
i want it to make a semicircle so i created a method with the following code
public void drawSemiCircle(int radius)
    {
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
        this.turn(20);
        this.forward(5);
    }




its too long and repetative, is there anyway to make it shorter?
new at this

Is This A Good Question/Topic? 0
  • +

Replies To: JAVA/BLUEJ TURTLE PROBLEM

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JAVA/BLUEJ TURTLE PROBLEM

Posted 10 October 2012 - 09:13 AM

Look into looping

while

for
Was This Post Helpful? 0
  • +
  • -

#3 PSCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 12

Re: JAVA/BLUEJ TURTLE PROBLEM

Posted 10 October 2012 - 01:32 PM

View PostRyano121, on 10 October 2012 - 09:13 AM, said:

Look into looping

while

for


So i would do this instead?

    public static void main(String[] args)
{
int count = 1;         
while(count<12);
}




}
}
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JAVA/BLUEJ TURTLE PROBLEM

Posted 10 October 2012 - 01:39 PM

public void drawSemiCircle(int radius) {
 
    for(int i = 0; i < 12; i++) {
        this.forward(5);
        this.turn(20);
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 PSCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 12

Re: JAVA/BLUEJ TURTLE PROBLEM

Posted 10 October 2012 - 01:45 PM

View Postpbl, on 10 October 2012 - 01:39 PM, said:

public void drawSemiCircle(int radius) {
 
    for(int i = 0; i < 12; i++) {
        this.forward(5);
        this.turn(20);
    }
}



So i could do this with any repetitive code?
I have some that it repeats twice then it changes then repeats the first two twice again, i wouldnt be able to use looping for that would i ?

thank you for the help on my original question:)!
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JAVA/BLUEJ TURTLE PROBLEM

Posted 10 October 2012 - 01:49 PM

There is surely a way.... depends on your exact pattern
you can have nested loops too so it depends
Was This Post Helpful? 0
  • +
  • -

#7 PSCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 12

Re: JAVA/BLUEJ TURTLE PROBLEM

Posted 10 October 2012 - 01:55 PM

View Postpbl, on 10 October 2012 - 01:49 PM, said:

There is surely a way.... depends on your exact pattern
you can have nested loops too so it depends


[quote]
public void drawAStar(int Length)
{
this.turn(90);
this.forward(Length);
this.turn(155);
this.forward(Length);
this.turn(270);
this.forward(Length);
this.turn(155);
this.forward(Length);
this.turn(280);
this.forward(Length);
this.turn(155);
this.forward(Length);
this.turn(270);
this.forward(Length);
this.turn(155);
this.forward(Length);
this.turn(286);
this.forward(Length);
this.turn(150);
this.forward(Length);



}
[/code]

id assume this one would be difficult to do because nothing is competative other than the sideLength portion
Was This Post Helpful? 0
  • +
  • -

#8 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: JAVA/BLUEJ TURTLE PROBLEM

Posted 11 October 2012 - 02:55 PM

This looks like the solution to a homework problem, but here it goes anyway...

Put the turns in an array: int[] turns = { 90, 155, ...}

Then create a loop that picks up the angle from the turns array and then moves forward by length.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1