Difference between revisions of "CISC181 F2017 Lab1"
From class_wiki
Line 3: | Line 3: | ||
* Name your class "Lab1". This happens when you are creating a new module, in the ''Java class name'' field | * 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 | ** Add your name and section number in a comment before the class declaration | ||
− | ** As explained in the subsections below, modify <tt>static void main()</tt> and create '''two''' other methods: <tt>static void | + | ** As explained in the subsections below, modify <tt>static void main()</tt> and create '''two''' other methods: <tt>static void countOccurrences() { ... }</tt> and <tt>static void printLayeredPyramid() { ... }</tt> |
− | * Submit your <tt> | + | * Submit your <tt>Lab1.java</tt> on Sakai by '''Monday, September 18''' |
Use proper [https://google.github.io/styleguide/javaguide.html naming and formatting style] throughout your code. | Use proper [https://google.github.io/styleguide/javaguide.html naming and formatting style] throughout your code. | ||
Line 12: | Line 12: | ||
Your <tt>main()</tt> should do the following: | Your <tt>main()</tt> should do the following: | ||
− | * Ask the user (using <tt>println()</tt> or <tt>printf()</tt>) to input | + | * Ask the user (using <tt>println()</tt> or <tt>printf()</tt>) to input a single character <tt>c</tt> and a String <tt>S</tt>, each on separate lines. Read these with a <tt>Scanner</tt> object and pass them to countOccurrences(). |
+ | * Whatever value is returned by leapYearsInRange() | ||
* If a valid choice is made, call the corresponding function immediately. Otherwise print an error message | * 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) | * Let the program end (no loop -- just print prompt, read response, and execute ''one'' time) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | ===<tt>static | + | ===<tt>static int countOccurrences(char c, String S)</tt>=== |
− | + | Iterate through every character in the String S and count how many time char c occurs. | |
− | + | ===<tt>static void drawLayeredPyramid(int numLayers)</tt>=== | |
− | |||
− | + | Draw the following pattern | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 22:35, 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
Use proper naming and formatting style throughout your code.
static void main(String[] args)
Your main() should do the following:
- Ask the user (using println() or printf()) to input a single character c and a String S, each on separate lines. Read these with a Scanner object and pass them to countOccurrences().
- Whatever value is returned by leapYearsInRange()
- 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 time char c occurs.
static void drawLayeredPyramid(int numLayers)
Draw the following pattern