Difference between revisions of "CISC181 F2017 Lab1"

From class_wiki
Jump to: navigation, search
Line 23: Line 23:
 
Consider the following pattern:
 
Consider the following pattern:
  
$$$$$$$$$$
+
$$$$$$$$$$
¢¢¢¢¢¢¢¢¢
+
  ¢¢¢¢¢¢¢¢¢
  $$$$$$$$
+
  $$$$$$$$
  ¢¢¢¢¢¢¢
+
    ¢¢¢¢¢¢¢
    $$$$$$
+
    $$$$$$
    ¢¢¢¢¢
+
      ¢¢¢¢¢
      $$$$
+
      $$$$
      ¢¢¢
+
        ¢¢¢
        $$
+
        $$
        ¢
+
          ¢
  
 
It consists of 10 alternating rows of $ and ¢ characters.  The first row has 10 columns of characters, and each successive row has one less.
 
It consists of 10 alternating rows of $ and ¢ characters.  The first row has 10 columns of characters, and each successive row has one less.
 
<tt>drawLayeredPyramid()</tt> should make a similar pattern with <tt>numRows</tt> rows, alternating characters <tt>firstChar</tt> and <tt>secondChar</tt>.
 
<tt>drawLayeredPyramid()</tt> should make a similar pattern with <tt>numRows</tt> rows, alternating characters <tt>firstChar</tt> and <tt>secondChar</tt>.

Revision as of 21:45, 11 September 2017

  • Make a new project (but now n = 1) following these instructions
  • Name your class "Lab1". This happens when you are creating a new module, in the Java class name field
    • Add your name and section number in a comment before the class declaration
    • As explained in the subsections below, modify static void main() and create two other methods: static void countOccurrences() { ... } and static void printLayeredPyramid() { ... }
  • Submit your Lab1.java on Sakai by Monday, September 18

static void main(String[] args)

Your main() should do the following:

  • Ask the user (using println() or printf()) to input a single character and a string, each on separate lines. Read these with a Scanner object and pass them to countOccurrences().
  • Whatever value is returned by countOccurrences(),
  • If a valid choice is made, call the corresponding function immediately. Otherwise print an error message
  • Let the program end (no loop -- just print prompt, read response, and execute one time)

static int countOccurrences(char c, String S)

Iterate through every character in the String S and count how many times char c occurs. Return this value.

static void drawLayeredPyramid(int numLayers, char firstChar, char secondChar)

Consider the following pattern:

$$$$$$$$$$
 ¢¢¢¢¢¢¢¢¢
  $$$$$$$$
   ¢¢¢¢¢¢¢
    $$$$$$
     ¢¢¢¢¢
      $$$$
       ¢¢¢
        $$
         ¢

It consists of 10 alternating rows of $ and ¢ characters. The first row has 10 columns of characters, and each successive row has one less. drawLayeredPyramid() should make a similar pattern with numRows rows, alternating characters firstChar and secondChar.