Difference between revisions of "CISC181 S2017 Lab1"

From class_wiki
Jump to: navigation, search
 
(24 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
<p style="font-size:40px">[http://nameless.cis.udel.edu/class_data/181_s2017/Lab1_TestCases.pdf Lab #1 tests]</p>
 +
 
* [http://developer.android.com/sdk/index.html Download Android Studio] and install it on your machine
 
* [http://developer.android.com/sdk/index.html Download Android Studio] and install it on your machine
 
<!--
 
<!--
Line 6: Line 8:
 
* Either on your machine or on an eCalc machine in lab:
 
* Either on your machine or on an eCalc machine in lab:
 
** Make a new project following [[CISC181_S2017_NewAndroidStudioProject | these ]] instructions
 
** Make a new project following [[CISC181_S2017_NewAndroidStudioProject | these ]] instructions
** If you haven't already, paste the following into <tt>MyClass</tt>: <tt>public static void main(String[] args) { System.out.println("Hello, world!"); }</tt>
 
 
** Confirm that you can build and run it (by pressing the green triangle button or choosing "Run" from the Run menu)
 
** Confirm that you can build and run it (by pressing the green triangle button or choosing "Run" from the Run menu)
* Modify <tt>MyClass.java</tt> as follows:
+
** Add your name and section number in a comment before the class declaration
** Add your name and section number as a ''multi-line comment''
+
** As explained in the subsections below, modify <tt>static void main()</tt>  and create '''two''' other methods: <tt>static void heronsFormula() { ... }</tt> and <tt>static void convertSeconds() { ... }</tt>  
** Using a ''single-line comment'', disable the "Hello, world" print statement in <tt>main()</tt>
+
* Submit your <tt>MyClass.java</tt> on Sakai by '''Friday, February 17'''
** You will use <tt>main()</tt> to compute several geometric identities involving a general triangle with sidelengths ''a'', ''b'', and ''c'' as shown below.  Declare these variables as <tt>double</tt>, prompt the user to enter them, and read each in using the [http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html <tt>Scanner</tt>] class.<br>[[Image:Triangle_with_notations_2.svg.png|center|300px]]<br>
+
 
*** [http://en.wikipedia.org/wiki/Heron%27s_formula Heron's Formula] gives a method to compute the area ''A'' of the triangle. Follow the [http://en.wikipedia.org/wiki/Heron%27s_formula link] and use the first formula to derive the ''semi-perimeter'' ''s'' and ''A'' from ''a'', ''b'', and ''c''; implement it using [http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html Java math] expressions and/or functions; and report both ''s'' and ''A'' with <tt>System.out.println()</tt>.
+
Use proper [https://google.github.io/styleguide/javaguide.html naming and formatting style] throughout your code.
*** 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 ''γ''.
+
===<tt>static void main(String[] args)</tt>===
*** Use proper [https://google.github.io/styleguide/javaguide.html naming and formatting style] throughout your code.
+
 
 +
Your <tt>main()</tt> should do the following:
 +
 
 +
* Tell the user (using <tt>println()</tt>) that they can choose either of the two functions above
 +
* Prompt the user to enter a number to choose one of the options (1 for Heron's, 2 for convert seconds) and use the <tt>Scanner</tt> class to read it
 +
* 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)
 +
 
 +
===<tt>static void heronsFormula()</tt>===
 +
 
 +
You will compute several geometric identities involving a general triangle with sidelengths ''a'', ''b'', and ''c'' as shown below.   
 +
 
 +
<!--<br>[[Image:Triangle_with_notations_2.svg.png|center|300px]]<br>-->
 +
[[Image:Triangle_with_notations_2.svg.png|300px]]
 +
 
 +
Your function should:
 +
* Declare these sidelength variables as <tt>double</tt>, prompt the user to enter them, 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 ''γ''.
 +
 
 +
===<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
  
Submit your <tt>MyClass.java</tt> on Sakai by '''Friday, February 17'''
+
For this function you may assume that ''t'' is positive, and you do not have to use a <tt>long</tt>

Latest revision as of 21:42, 26 February 2017

Lab #1 tests

  • Download Android Studio and install it on your machine
  • Either on your machine or on an eCalc machine in lab:
    • Make a new project following these instructions
    • Confirm that you can build and run it (by pressing the green triangle button or choosing "Run" from the Run menu)
    • 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 heronsFormula() { ... } and static void convertSeconds() { ... }
  • Submit your MyClass.java on Sakai by Friday, February 17

Use proper naming and formatting style throughout your code.

static void main(String[] args)

Your main() should do the following:

  • Tell the user (using println()) that they can choose either of the two functions above
  • Prompt the user to enter a number to choose one of the options (1 for Heron's, 2 for convert seconds) and use the Scanner class to read it
  • 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 void heronsFormula()

You will compute several geometric identities involving a general triangle with sidelengths a, b, and c as shown below.

Triangle with notations 2.svg.png

Your function should:

  • Declare these sidelength variables as double, prompt the user to enter them, and read each in using the Scanner class.
  • 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 link and use the first formula to:
    • Derive the semi-perimeter s and area A from a, b, and c using Java math expressions and/or functions
    • Report both s and A with System.out.println().
  • The Law of Cosines can be used to calculate the angle γ (gamma) between a and b (second formula in Applications section of link). Math.acos() will give the angle in radians; please convert it to degrees and report it with System.out.println()
  • You might notice a lot of decimal places printed in your answers. Change your println() to format as necessary to only print 2 digits after the decimal for s, A, and γ.

static void convertSeconds()

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 long