Difference between revisions of "CISC181 F2017 Lab1"

From class_wiki
Jump to: navigation, search
(Created page with " ** Make a new project (but now ''n'' = 1) following these instructions * Name your class "Lab1". This happens when you are creat...")
 
(static void main(String[] args))
 
(35 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
+
__NOTOC__
** Make a new project (but now ''n'' = 1) following [[CISC181_F2017_NewAndroidStudioProject | these ]] instructions
+
* Make a new project (but now ''n'' = 1) following [[CISC181_F2017_NewAndroidStudioProject | these ]] instructions
 
* 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 the '''three''' other methods described below.
* Submit your <tt>MyClass.java</tt> on Sakai by '''Monday, September 11'''
 
  
Use proper [https://google.github.io/styleguide/javaguide.html naming and formatting style] throughout your code.
+
* Submit your <tt>Lab1.java</tt> on Sakai by '''Monday, September 18'''
  
 
===<tt>static void main(String[] args)</tt>===
 
===<tt>static void main(String[] args)</tt>===
Line 12: Line 11:
 
Your <tt>main()</tt> should do the following:
 
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 by entering a single number: 1 for Heron's, 2 for convert seconds
+
* As with Lab #0 last week, give your user the choice of several options (using <tt>println()</tt> or <tt>printf()</tt>), to be chosen by inputting a single number (which is read with a <tt>Scanner</tt> object)This time, the options are: 1 to solve a basic physics problem, 2 to draw a particular pattern with print commands, and 3 to find the most common character in a string.  Whichever choice is given (after error-checking), take the steps specified below and call the corresponding function.  
* Use the <tt>Scanner</tt> class to read that numberCheck that it really is either 1 or 2
+
* Option 1: Ask the user to enter ''theta'' and ''v'', read them, pass them to <tt>computeDistanceToLanding()</tt>, and print the returned result.
* If a valid choice is made, call the corresponding function immediately. Otherwise print an error message
+
* Option 2: Ask the user to enter two chars and a positive numberRead these and pass them to <tt>drawLayeredPyramid()</tt>.
* Let the program end (no loop -- just print prompt, read response, and execute ''one'' time)
+
* Option 3: Ask the user  to input a string on its own line. Read this and pass it to <tt>mostCommonChar()</tt>. Print the returned value.
 
+
* For all user inputs, check for legality here in <tt>main()</tt>.  If there is a problem, print an informative message with the word "error" somewhere in it, and let the program end.
<!--
 
You may find that it becomes tedious to do all of this in the console window inside ASYou 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>===
 
 
 
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>-->
+
===<tt>static float computeDistanceToLanding(float thetaDegrees, float initialVelocityMetersPerSecond)</tt>===
[[Image:Triangle_with_notations_2.svg.png|300px]]
 
  
Your function should:
+
[[Image:cannon.jpg|400px]]
* 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>===
+
Suppose you want to calculate how far away a cannonball will land on Earth if fired at an inclination angle of ''theta'' degrees and an initial velocity of ''v'' meters / second.  Assume that the ground is perfectly planar, the cannon is at a height of 0, and neglect drag from atmospheric resistance. I am not going to provide the relevant physics formula -- finding it and turning it into Java is your assignment here.  Once you have calculated the distance, return the answer in meters.
  
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. 
+
===<tt>static void drawLayeredPyramid(int numLayers, char firstChar, char secondChar)</tt>===
  
When you are satisfied that you can calculate ''d'', ''h'', ''m'', and ''s'' correctly, use branching to modify your printing so that you:
+
Consider the following pattern:
* 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
+
It consists of 10 alternating rows of $ and ¢ characters. The first row has 10 columns of characters, and each successive row has one less.
7 seconds
+
<tt>drawLayeredPyramid()</tt> should make a similar pattern with <tt>numLayers</tt> rows, alternating characters <tt>firstChar</tt> and <tt>secondChar</tt>.
 +
This will require at least one ''loop''; see the [https://docs.google.com/presentation/d/1cXLfYiU7YJaXANzIi9ZBHAW2-CA5Ai6JxJbotKgYVuA/edit?usp=sharing slides for the Sep. 13 lecture] for material about doing loops in Java.
  
and if they input "8880" the output should be
+
===<tt>static char mostCommonChar(String queryString)</tt>===
  
  2 hours
+
Iterate (aka loop) through the characters in <tt>queryString</tt> and count how many times each character occurs. Whichever character occurs the most times, return it. Ties should be broken in alphabetical order--that is, if 'a' occurs 7 times and 'b' occurs 7 times, return 'a'. To make a comparison, use [https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#compare(char,%20char) Character.compare()].
  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>
+
This is somewhat tricky, but you can (and must, for this assignment) do this ''without'' any external data structures such as arrays.  The only <tt>String</tt> functions that you can use here are [https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int) charAt()] and [https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length() length()].

Latest revision as of 06:59, 12 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 the three other methods described below.
  • Submit your Lab1.java on Sakai by Monday, September 18

static void main(String[] args)

Your main() should do the following:

  • As with Lab #0 last week, give your user the choice of several options (using println() or printf()), to be chosen by inputting a single number (which is read with a Scanner object). This time, the options are: 1 to solve a basic physics problem, 2 to draw a particular pattern with print commands, and 3 to find the most common character in a string. Whichever choice is given (after error-checking), take the steps specified below and call the corresponding function.
  • Option 1: Ask the user to enter theta and v, read them, pass them to computeDistanceToLanding(), and print the returned result.
  • Option 2: Ask the user to enter two chars and a positive number. Read these and pass them to drawLayeredPyramid().
  • Option 3: Ask the user to input a string on its own line. Read this and pass it to mostCommonChar(). Print the returned value.
  • For all user inputs, check for legality here in main(). If there is a problem, print an informative message with the word "error" somewhere in it, and let the program end.

static float computeDistanceToLanding(float thetaDegrees, float initialVelocityMetersPerSecond)

Cannon.jpg

Suppose you want to calculate how far away a cannonball will land on Earth if fired at an inclination angle of theta degrees and an initial velocity of v meters / second. Assume that the ground is perfectly planar, the cannon is at a height of 0, and neglect drag from atmospheric resistance. I am not going to provide the relevant physics formula -- finding it and turning it into Java is your assignment here. Once you have calculated the distance, return the answer in meters.

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 numLayers rows, alternating characters firstChar and secondChar. This will require at least one loop; see the slides for the Sep. 13 lecture for material about doing loops in Java.

static char mostCommonChar(String queryString)

Iterate (aka loop) through the characters in queryString and count how many times each character occurs. Whichever character occurs the most times, return it. Ties should be broken in alphabetical order--that is, if 'a' occurs 7 times and 'b' occurs 7 times, return 'a'. To make a comparison, use Character.compare().

This is somewhat tricky, but you can (and must, for this assignment) do this without any external data structures such as arrays. The only String functions that you can use here are charAt() and length().