logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    sfolax6776
    Aug 21, 2018
      ·  Edited: Dec 06, 2019

    5.13 Project P1.7 - How will robots count windows?

    in Introduction to Java

    A. Introduction

    At the beginning of this course, we discussed about the importance of developing an algorithm in pseudo code before writing code. We reiterate the same point when discussing multiple phases of programming. Now it is a good time to review this process and keep in mind: programming is a multi-step process.

    Have you worked on the pseudo code of this robot problem before? Can you turn the pseudo code into Java now?


    B. Some Assumptions:

    Let's assume the room size is 20x20 units, surrounded by four walls labeled #1-4. Use a random number to generate 1, 2 or 3 windows to install on the walls. All windows will have a width of 2 units, and at most 1 window can be installed on each wall. No window will wrap around a corner.

    Write a program to count the number of windows. To start off, your program will first set up the windows, then ask the user " Where is the robot?". Instruct the user to enter 2 integers between 1 and 20 to tell the robot's location.


    C. Your output should print:

    1. The numbers of windows the robot found in the room.

    2. The location of those windows.

    1 comment
    0
    mr.ricklu
    Aug 26, 2018

    import java.util.Scanner;

    class beepboop {

    public static void main(String[] args) {

    int WindowCount = 0;

    int Windows = (int) Math.ceil(Math.random()*3);

    int[] WindowWalls = new int[4]; //wall 1 is bottom, wall 2 is right, wall 3 is top, and wall 4 is left

    int count = 0;

    while(Windows != count){

    WindowWalls[(int) Math.floor(Math.random()*4)]++;

    count = 0;

    for(int i=0; i<4; i++){

    if(WindowWalls[i]>0){

    count++;

    }

    }

    }

    int[] WindowPosition = new int[4];

    for(int i=0; i<4; i++){

    if(WindowWalls[i]>0){

    WindowPosition[i] = (int) Math.ceil(Math.random()*19); //indicates the bottom of the window or the left of the window

    }

    }

    Scanner Andrew = new Scanner(System.in);

    System.out.println("What is the robot's x coordinate? (must be between 1 and 20 (inclusive))");

    int x = Andrew.nextInt();

    if(x>20 || x<1){

    System.out.println("WHAT HAVE YOU DONE?? YOU PUT THE ROBOT OUTSIDE ITS CAGE. NOW IT HAS ESCAPED. WE'RE DOOMED.");

    System.exit(0);

    }

    System.out.println("What is the robot's y coordinate? (must be between 1 and 20 (inclusive))");

    int y = Andrew.nextInt();

    if(y>20 || y<1){

    System.out.println("WHAT HAVE YOU DONE?? YOU PUT THE ROBOT OUTSIDE ITS CAGE. NOW IT HAS ESCAPED. WE'RE DOOMED.");

    System.exit(0);

    }

    int[] Position = {x,y};

    for(; Position[0]>1; Position[0]--){}

    for(; Position[1]>1; Position[1]--){}

    for(; Position[0]<20; Position[0]++){

    if(WindowPosition[0] == Position[0]){

    WindowCount++;

    System.out.println("There is a window from (" + Position[0] + ",1) to (" + (Position[0]+1) + ",1)");

    }

    }

    for(; Position[1]<20; Position[1]++){

    if(WindowPosition[1] == Position[1]){

    WindowCount++;

    0
    1 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners