Difference between revisions of "CISC181 F2017 Lab1"

From class_wiki
Jump to: navigation, search
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 leapYearsInRange() { ... }</tt> and <tt>static void xxx() { ... }</tt>  
+
** 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>MyClass.java</tt> on Sakai by '''Monday, September 11'''
+
* 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 the first year and last year of a range to check.  Read the values with a <tt>Scanner</tt> object. Pass these values to leapYearsInRange().
+
* 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)
  
<!--
 
You may find that it becomes tedious to do all of this in the console window inside AS.  You can automate this process (as the TA will for grading) by running the program from a terminal or console window as follows:
 
* After your code successfully compiles, you can find the "executable" created in ...
 
* From a terminal window of your operating system, run the program ...
 
* Instead of actually typing your inputs, you can have the program read them from a file by "piping": ...
 
-->
 
  
===<tt>static void heronsFormula()</tt>===
+
===<tt>static int countOccurrences(char c, String S)</tt>===
  
You will compute several geometric identities involving a general triangle with sidelengths ''a'', ''b'', and ''c'' as shown below.
+
Iterate through every character in the String S and count how many time char c occurs.
  
<!--<br>[[Image:Triangle_with_notations_2.svg.png|center|300px]]<br>-->
+
===<tt>static void drawLayeredPyramid(int numLayers)</tt>===
[[Image:Triangle_with_notations_2.svg.png|300px]]
 
  
Your function should:
+
Draw the following pattern
* Declare these sidelength variables as <tt>double</tt>, prompt the user to enter them ''on a single line, separated by spaces'', and read each in using the [http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html <tt>Scanner</tt>] class.
 
* [http://en.wikipedia.org/wiki/Heron%27s_formula Heron's Formula] gives a method to compute the area ''A'' of the triangle (ignore the fact that A is also the name of one of the triangle vertices). Follow the [http://en.wikipedia.org/wiki/Heron%27s_formula link] and use the first formula to:
 
** Derive the ''semi-perimeter'' ''s'' and area ''A'' from ''a'', ''b'', and ''c'' using [http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html Java math] expressions and/or functions
 
** Report both ''s'' and ''A'' with <tt>System.out.println()</tt>.
 
* The [http://en.wikipedia.org/wiki/Law_of_cosines Law of Cosines] can be used to calculate the angle ''γ'' (gamma) between ''a'' and ''b'' (second formula in Applications section of [http://en.wikipedia.org/wiki/Law_of_cosines#Applications link]).  <tt>Math.acos()</tt> will give the angle in radians; please convert it to degrees and report it with <tt>System.out.println()</tt>
 
* You might notice ''a lot'' of decimal places printed in your answers.  Change your <tt>println()</tt> to [http://docs.oracle.com/javase/tutorial/java/data/numberformat.html <tt>format</tt>] as necessary to only print '''2''' digits after the decimal for ''s'', ''A'', and ''γ''.
 
* To summarize: take in 3 space-separated numbers on a single line, do calculations, and print out 3 numbers on their own lines ''with nothing else''.  If illegal values are given (negative sidelengths, or three sidelengths that cannot possibly form a triangle), then print out a one-line error message without doing any calculations.
 
 
 
===<tt>static void convertSeconds()</tt>===
 
 
 
This method should ask the user to enter an integer which represents a length of time ''t'' in seconds, and then compute and print out the number of ''d'' days, ''h'' hours, ''m'' minutes, and ''s'' seconds corresponding to ''t'' on separate lines. 
 
 
 
When you are satisfied that you can calculate ''d'', ''h'', ''m'', and ''s'' correctly, use branching to modify your printing so that you:
 
* Start with the first non-zero time unit (i.e. do not print leading zero values)
 
* Print "1 day" instead of "1 days", "1 hour" instead of "1 hours", etc. where appropriate (i.e., do not use plural for a value of 1).  Some example outputs are below: 
 
 
 
If the user inputs "67", the output should be
 
 
 
1 minute
 
7 seconds
 
 
 
and if they input "8880" the output should be
 
 
 
2 hours
 
28 minutes
 
0 seconds
 
 
 
For this function you may assume that ''t'' is positive, and you do not have to use a <tt>long</tt>
 

Revision as of 21: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