Conditionals and Loops – 50 course points
The purpose of this assignment is to practice conditionals and loops.
Refer to our Programming Assignments FAQ for instructions on how to install VScode, how to use the command line and how to submit your assignments.
Implementation Notes
- YOU MAY only update the methods with the WRITE YOUR CODE HERE line.
- COMMENT all extra testing print statements you have written from Buses.java, DogWalker.java
- DO NOT add any instance variables to the Buses.java, DogWalker.java class.
- DO NOT add any public methods to the Buses.java, DogWalker.java class.
- DO NOT add/rename the project or package statements.
- DO NOT change the class name.
- DO NOT use System.exit()
Write 2 programs and submit on Autolab.
- WATCH this video: how to open the files on VSCode and how to submit into Autolab.
- We provide a zip file (find it under Buses on Autolab) containing Buses.java and DogWalker.java.
- For each problem UPDATE and SUBMIT the corresponding file.
- DO NOT print additional messages, follow the examples for each problem.
1. Buses (25 points)
Programming
Write a program Buses.java that takes a 4-digit integer as a command-line argument and displays ERROR, LX, or H.
You might have noticed that each Rutgers campus bus has its own four-digit number. Suppose that the sum:
- of an LX buses’ digits are always even
- of an H buses’ digits are always odd
Write a program that reads a singular integer input and displays LX or H depending on the sum of the four digits in the integer. The program displays ERROR if the input is negative and ends the program (does not ask for the input again).
Precondition:
- The input value used to test your program is a 4-digit integer
Note:
- You can use the modulus operator to extract the last digit of any number.
Executing and Debugging
From the folder containing Buses.java:
javac Buses.java
java Buses 7302
Expected output:
LX
javac Buses.java
java Buses 6432
Expected output:
H
java Buses -1432
Expected output:
ERROR

2. DogWalker (25 points)
Programming
Imagine that your dog’s collar has a tag that allows you to monitor how much exercise your dog is getting from their daily walk (safely with someone).
- This program will simulate the behavior of a dog walker, and your dog, moving randomly in the city.
Imagine that the city is a two-dimensional grid of points. At each step, the walker randomly moves 1 unit north (up), south (down), east (right), or west (left) with probability equal to 1/4 for each direction, independent of previous moves. You will use Math.random() to generate a random number between 0.0 (inclusive) and 1.0 (non-inclusive) to simulate the probability.
- Write a program DogWalker.java that takes an integer command-line argument n and simulates the motion of a walker randomly taking n steps.
- Print the location at each step (including the starting point), treating the starting point as the origin (0, 0). Each step should be on its own line. Output formatting will be as follows: the coordinates of each step taken by the dog within a pair of parenthesis where the x and y coordinate are separated by a comma and NO SPACES. For example: (2,0)
- Also, print the square of the final Euclidean distance from the origin as a double. The Euclidean distance lets you know how far from the starting point (home) your dog is after n steps. Output the words “Squared distance= ” followed by your distance.
where x_final, y_final are your last x- and y-coordinates, and x_initial, y_initial are your starting x- and y-coordinates.
Note:
- You do not need arrays for this problem; keep track of the x and y coordinates during the random walk.
- In the example below the walker takes 20 steps; there are 21 printed locations. The first location is the starting point (0,0). Because this is random, your numbers may not be the same.
Executing and Debugging
From the folder containing DogWalker.java:
javac DogWalker.java
java DogWalker 20
(0,0)
(0,1)
(-1,1)
(-1,2)
(0,2)
(1,2)
(1,3)
(0,3)
(-1,3)
(-2,3)
(-3,3)
(-3,2)
(-4,2)
(-4,1)
(-3,1)
(-3,0)
(-4,0)
(-4,-1)
(-3,-1)
(-3,-2)
(-3,-3)
Squared distance = 18.0


Buses created by: Ayden Lyubimov
Guidelines
Implementation Notes
- YOU MAY only update the methods with the WRITE YOUR CODE HERE line.
- COMMENT all extra testing print statements you have written from Buses.java, DogWalker.java
- DO NOT add any instance variables to the Buses.java, DogWalker.java class.
- DO NOT add any public methods to the Buses.java, DogWalker.java class.
- DO NOT add/rename the project or package statements.
- DO NOT change the class name.
- DO NOT use System.exit()
VSCode Extensions
You can install the VSCode extension pack for Java. Take a look at this tutorial.
We suggest:
Importing VSCode Project
- Download Assignment3.zip from Autolab Attachments.
- Unzip the file by double clicking or right clicking -> Unzip
- Open VSCode
- Import the folder to a workspace through File > Open Folder
Before submission
Collaboration policy. Read our collaboration policy here.
Submitting the assignment. Submit Buses.java and DogWalker.java separately via the web submission system called Autolab. To do this, click the Assignments link from the course website; click the Submit link for that assignment.
Getting help
If anything is unclear, don’t hesitate to drop by office hours or post a question on Piazza.
- Find instructors office hours here
- Find tutors office hours in Canvas -> Tutoring, RU CATS
- Find head TAs office hours here
- POST on Piazza in Canvas -> Piazza
- In addition to office hours we have the CSL (Coding and Social Lounge), a community space staffed with lab assistants which are undergraduate students further along the CS major to answer questions.
- Refer to our Programming Assignments FAQ for instructions regarding our assignments and policies.
Submitting
Write the program and submit on Autolab.
- We provide a zip file (find it under Welcome on Autolab) containing Buses.java, DogWalker.java
- For this assignment UPDATE and SUBMIT the corresponding file.