Skip to Main Content

Introduction to Computer Science

Computer Science Department

Arrays – 60 course points

The purpose of this assignment is to practice array manipulation using loops and other methods.

General Guidelines

Write 2 programs and submit on Autolab.

  • We provide a zip file (find it under Assignment4 on Autolab) containing BusStop.java and Pyramid.java.
  • For each problem UPDATE and SUBMIT the corresponding file only writing where it indicates.

Observe the following rules:

  1. DO NOT add any import statements
  2. DO NOT add the project statement
  3. DO NOT change the class name
  4. DO NOT change the headers of ANY of the given functions
  5. DO NOT add any new class fields
  6. DO NOT use System.exit()
  7. ONLY print the result as specified by each problem. Observe the examples’ output, display only what the problem is asking for.
  8. DO NOT print other messages, follow the examples for each problem.
  9.  

1. RU BusStop (30 points)

The purpose of this assignment is to teach student how to compare chars from input.

Task

  • UPDATE and SUBMIT BusStop.java in AutoLab.

Programming

Serena, a freshmen at Rutgers just finished her Intro to Logic class on College Ave and needs to return to Busch for her Intro to Computer Science class. She is currently standing at the Yard Bus Stop, waiting patiently for the H bus to arrive. Your job is to help Serena find in which order the H bus will arrive!

  • Because we only covered char at this point in the semester all busses routes’ names are 1 character in length. We cannot have B/L or REXB routes.
  • See Rutgers bus routes below, where some are fictitious.
    • A Route
    • B Route
    • C Route
    • E Route
    • F Route
    • H Route
    • L Route
    • R Route
    • X Route
  • Write the program BusStop.java that reads n char arguments from the command line.
    • The first n-1 characters refer to the order in which the buses will arrive at the bus stop.
    • The last char refers to the bus Serena is waiting for.
  • The program displays the order in which the bus Serena is waiting for will arrive. If the bus Serena is waiting for does not stop at the bus stop then the program displays -1.
  •  
  • To read the first character from the command line use:
    • char c = args[0].charAt(0);
  • To read the nth character from the command line use:
    • char c = args[n-1].charAt(0);
  • You will have to read an undetermined number of characters, so, you will need a loop to read all characters from the command line.
  •  
  • Hint: Read the first n-1 values and store them into a char 1D array. Then, read the last char and search for that value in the array.

 

Executing and Debugging

      • to compile: javac BusStop.java
      • to execute: java BusStop X X X X
      • Output should be: #

        First navigate to the Assignment4 directory/folder

  •  
    • Example 1

    • javac BusStop.java
    • java BusStop C L H H
    • 3
    • The first bus to arrive is C, followed by L, then H. The program displays 3 because H is the third bus to arrive.

    • Example 2

    • javac BusStop.java
    • java BusStop B R X B
    • 1
    • The first bus to arrive is B, followed by R, then X. The program displays 1 because B is the first bus to arrive.

    • Example 3
    •  
    • javac BusStop.java
    • java BusStop B C E A B H L
    • -1
    • The L bus does not stop at the bus stop, the program displays -1.

    • Assume: all the inputs are char values.
    •  
  •  

Author Credit Manya Sood

2. Egyptian Pyramid (30 points)

Long ago, some advanced civilizations such as the Egyptians modeled their architecture to be of pyramid form: 3-Dimensional polyhedra with some polygon base and triangle-shaped flat sides meeting to a point at the top. For the sake of this assignment, we will be considering 2-D pyramids, which take the form of triangles. Students will be tasked with understanding and implementing 2D array(s) to create a 2-Dimensional pyramid of your own.

Overview

Oh no! During Professor Centeno’s 2D Arrays lecture, a mysterious portal opened up right into the projected picture of the Egyptian pyramids, sucking all the CS111 students into another time dimension. It looks like the students are trapped in 2580 B.C. After some conversing (let’s assume due to advanced language translation technology that students happened to bring), they discover that the Egyptians need assistance in building their pyramids. Since Professor Centeno did not time travel with the CS111 students, it’s now up to them to help these Egyptians out.

The students need to code a program-taking in two tentative parameters from the terminal representing 1) the overall size of their figure and 2) the number of bricks provided-to build a pyramid.

Task

  • UPDATE and SUBMIT Pyramid.java in AutoLab.

Programming

Students should use a 2D array to construct this method. Bricks, denoted by the symbol ‘X’, should be placed beginning at the bottom left and continue until either

1) we run out of bricks or
2) the pyramid is complete.
The surrounding area should be filled with the symbol ‘=’. Be sure to include a statement detailing the number of bricks remaining, in the format listed below. Keep in mind that the output should be PRINTED into the terminal. No value is returned.

HINT: Make sure to include # Bricks Remaining at after printing out the pyramid (like it shows in the screenshots)

Executing and Debugging

  • First navigate to the Assignment4 directory/folder

    • to compile: javac EgyptianPyramid.java
    • to execute: java EgyptianPyramid # #
      • Example 1: Executing the command
      •           javac EgyptianPyramid.java
      •           java EgyptianPyramid 3 1
        should produce a 3×3 2D grid with 1 initial brick to construct the pyramid. 
      • The output should look like this (Note the placement of the brick is on the left side of the last row):


        Example 2:
         Executing the command

                  javac EgyptianPyramid.java
              java EgyptianPyramid 4 5

        should produce a 4×4 2D grid with 5 initial bricks to construct the pyramid.

        The output should look like this:

      • Example 3: Executing the command
                  javac EgyptianPyramid.java
              java EgyptianPyramid 5 16

        should produce a 5×5 2D grid with 16 initial bricks to construct the pyramid.

      • The output should look like this:
      • Example 4:
    •  

Author Credit Kushi Sharma and Ayla Muminovic

Submitting Guidelines

VSCode Extensions

You can install the VSCode extension pack for Java. Take a look at this tutorial.

We suggest:

Importing VSCode Project

  1. Download Assignment4.zip from Autolab Attachments.
  2. Unzip the file by double clicking.
  3. 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 BusStop.java and Pyramid.java separately via 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 2 programs and submit on Autolab.

  • We provide a zip file (find it under Assignment4 on Autolab) containing BusStop.java and Pyramid.java.
  • For each problem UPDATE and SUBMIT the corresponding file.