Question no 01:
Using just the ahead and turnRight/turnLeft methods create a robot that travels in a
complete square once, beginning from its starting position. Make the robot travel 150 units
for each side of the square.
public class Rebort {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<=150;i++)
{
for(int j=0;j<=150;j++)
{
System.out.print("Rebort");
}
System.out.print("\n");
}
Question no 2:
Using a for loop create a robot that travels in a complete square 10 times.
import java.io.*;
public class Rebort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number_of_times=10;
for(int k=0;k<=number_of_times;k++)
for(int i=0;i<=150;i++)
{
for(int j=0;j<=150;j++)
{
System.out.print("Rebort");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
Question no 03:
1. Adapt the code so that it uses a loop that repeats four times to control the forward
movement and turns.
import java.io.*;
public class Rebort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number_of_times=4;
for(int k=0;k<=number_of_times;k++)
for(int i=0;i<=150;i++)
{
for(int j=0;j<=150;j++)
{
System.out.print("Rebort");
}
System.out.print("\n");
}
System.out.print("\n");
}
Question no 04:
Using a while loop create a robot that travels in a square forever (or until the round ends
anyway).
import java.util.Scanner;
public class Rebort2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int side,i=1,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any side of a square :");
side=sc.nextInt();
while(i<=side)
{
j=1;
while(j<=side)
{
System.out.print("Rebort");
j++;
}
i++;
System.out.print("\n");
}
Question no 05:
Add an integer variable, called lengthOfSquare and initialise it to the value 150. Adapt the
code so that it uses this variable rather than a hard-coded value of 150.
import java.io.*;
public class Rebort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int length_of_Square=150;
for(int i=0;i<=length_of_Square;i++)
{
for(int j=0;j<=length_of_Square;j++)
{
System.out.print("Rebort");
}
System.out.print("\n");
}
}
}
Question no 06:
Alter your robot so that it counts the number of squares it has travelled and writes this to the
console.
import java.io.*;
public class Rebort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number_of_times=4;
int count=0;
while(number_of_times>0) {
number_of_times=number_of_times/10;
count++;
}
System.out.print(+count);
for(int k=0;k<=number_of_times;k++)
for(int i=0;i<=150;i++)
{
for(int j=0;j<=150;j++)
{
System.out.print("Rebort");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
Question no 07:
Alter the above robot so that it incorporates an if statement such that it travels first clockwise
and then anti-clockwise (hint: x % 2 == 0 for even numbers of x, and turnRight and
turnLeft can accept negative degrees). Also have the robot print out whether it’s currently
travelling clockwise or anti-clockwise
import java.io.*;
import java.util.Scanner;
public class Rebort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x;
System.out.print("Enter the value of number of square move ");
Scanner sc=new Scanner(System.in);
x=sc.nextInt();
if(x%2==0)
{for(int i=0;i<=150;i++)
{
for(int j=0;j<=150;j++)
{
System.out.print("Rebort");
}
System.out.print("\n");
}
}
else
{
for(int i=150;i>=0;i--)
{
for(int j=150;i>=0;i--)
{
System.out.print("Rebort");
}
System.out.print("\n");
}
}
}
}