Difference between revisions of "CISC181 F2017 Lab1"

From class_wiki
Jump to: navigation, search
(static char mostCommonChar(String queryString))
(static void main(String[] args))
 
(14 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
Line 11: Line 11:
 
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 a string on its own line.  Read this with a <tt>Scanner</tt> object and pass it to <tt>mostCommonChar()</tt>.  Print the returned value.
+
* 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.
* Ask the user to enter two chars and a positive number.  Read these with your <tt>Scanner</tt> object and pass them to <tt>drawInvertedPyramid</tt>.
+
* Option 1: Ask the user to enter ''theta'' and ''v'', read them, pass them to <tt>computeDistanceToLanding()</tt>, and print the returned result.
* 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 / secondAsk the user to enter theta and ''v'', pass them to <tt>computeDistanceToLanding()</tt>, and print the returned result.
+
* Option 2: Ask the user to enter two chars and a positive number.  Read these and pass them to <tt>drawLayeredPyramid()</tt>.
* For all user inputs, check for legality here in <tt>main()</tt>.  If there is a problem, print an informative error message and let the program end.
+
* Option 3: Ask the user  to input a string on its own lineRead 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.
  
===<tt>static char mostCommonChar(String queryString)</tt>===
+
===<tt>static float computeDistanceToLanding(float thetaDegrees, float initialVelocityMetersPerSecond)</tt>===
 +
 
 +
[[Image:cannon.jpg|400px]]
  
Iterate through the characters in <tt>queryString</tt> and count how many times each character occursWhichever 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'.  You should do this ''without'' any external data structures such as arrays.
+
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 / secondAssume 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.
  
 
===<tt>static void drawLayeredPyramid(int numLayers, char firstChar, char secondChar)</tt>===
 
===<tt>static void drawLayeredPyramid(int numLayers, char firstChar, char secondChar)</tt>===
Line 37: Line 40:
 
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>numLayers</tt> rows, alternating characters <tt>firstChar</tt> and <tt>secondChar</tt>.
 
<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.
  
===<tt>static float computeDistanceToLanding(float thetaDegrees, float velocityMetersPerSecond)</tt>===
+
===<tt>static char mostCommonChar(String queryString)</tt>===
  
  [[Image:cannon.jpg|400px]]
+
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()].
  
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 hereOnce you have calculated the distance, return the answer in meters.
+
This is somewhat tricky, but you can (and must, for this assignment) do this ''without'' any external data structures such as arraysThe 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 07: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().