Difference between revisions of "CISC181 F2017 Lab7"

From class_wiki
Jump to: navigation, search
(Created page with "==Preliminaries== * Make a new project with ''n'' = 7 (following these instructions) * Name your main class "Lab7" (when creat...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
==Preliminaries==
+
===Preliminaries===
  
 +
* Make a new '''Android''' project with ''n'' = 7 following [[CISC181_F2017_NewAndroidStudioProject | these ]] instructions through step 6.  On step 7 "Select form factor...", switch over to step 4 on the Android developers page tutorial  [https://developer.android.com/training/basics/firstapp/creating-project.html here] and complete through step 6 which ends with "...click '''Finish'''".  Make the activity be called "MyActivity".
  
* Make a new project with ''n'' = 7 (following [[CISC181_F2017_NewAndroidStudioProject | these ]] instructions) 
+
===Instructions===
* Name your main class "Lab7" (when creating a new module in the instructions above, in the ''Java class name'' field)
 
  
==Instructions==
+
First, make the empty activity project as described above and confirm that you can build and run it -- either on the emulator or an actual Android device as explained on the next page [https://developer.android.com/training/basics/firstapp/running-app.html "Run Your App"]. 
  
In this lab you will develop a public '''Fraction''' class (in <tt>Fraction.java</tt>) with the arithmetic and output functionality listed below.  However, you are also required to use a [http://en.wikipedia.org/wiki/Test-driven_development#Test-driven_development_cycle "test-driven development"] approach based on unit testing.  Thus, as you start to implement the methods below, you must also create a public '''FractionTests''' class (in <tt>FractionTests.java</tt>) which contains a suite of unit tests (aka methods) to test <tt>Fraction</tt> thoroughly.
+
Second, take the empty activity project and modify it to become a very simple "rock paper scissors" generator as detailed below:
  
===Fraction===
+
# Change the layout in <tt>activity_my.xml</tt> to a vertical <tt>LinearLayout</tt>, similar to [http://developer.android.com/guide/topics/ui/layout/linear.html this example]
 +
# Change the old <tt>TextView</tt> that was printing "Hello, world" to say "Click the button below to get a new shape".
 +
# Add an <tt>ImageView</tt> underneath, centered laterally, that initially shows a rock, paper, OR scissors image.  Find your own for these, cropped and scaled to 250 x 250, and put them in <tt>res/drawable</tt> with the names "rock.png", "paper.png", and "scissors.png".  Here's some help for [http://developer.android.com/guide/topics/resources/drawable-resource.html drawable resources]
 +
# Add a <tt>TextView</tt> underneath the image which acts as a caption, saying "Rock" if the image is a rock, "Paper" if it's paper, and "Scissors" if it's scissors.  The caption should be centered laterally. 
 +
# Finally, add a centered <tt>Button</tt> under the caption.  This button should be labeled "New" and when pressed causes another random selection which (possibly) changes the image and caption above .  The caption text should be set not through resources but instead by using <tt>setText()</tt> in <tt>MyActivity.java</tt>, and something similar should be done with the image.
  
<tt>Fraction</tt> must have two private ints: <tt>numerator</tt> and <tt>denominator</tt>.  If any arguments to the functions below have denominators with 0 value, you must throw an <tt>IllegalArgumentException</tt>.  '''<tt>Fraction</tt> should also implement the [https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html<tt>Comparable</tt>] interface'''. Here are other the public methods that Fraction must implement:
+
''Note'': Do NOT make changes to any other XML resource file besides <tt>activity_my.xml</tt>.  
  
====Constructors====
+
Here is a screenshot of a sample program that satisfies the above requirements (except it should say "MyLab_7"):
* A two-parameter constructor that initializes the numerator and denominator
 
* A one-parameter constructor that takes the numerator and sets the denominator to 1
 
  
====Operators====
+
[[Image:300px-Lab10.png]]<br>
* <tt>void add(Fraction F)</tt>: Add another fraction F to this one
 
* <tt>void subtract(Fraction F)</tt>: Subtract another fraction F from this one
 
* <tt>void multiply(Fraction F)</tt>: Multiply this fraction by another fraction F
 
* <tt>void divide(Fraction F)</tt>: Divide this fraction by another fraction F
 
* <tt>void pow(int n)</tt>: Raise this fraction to the ''n''th power
 
  
====Special====
+
===Submission===
* <tt>void reduce()</tt>: Reduces numerator and denominator to [http://en.wikipedia.org/wiki/Irreducible_fraction lowest terms].  For example, "2 / 4" -> "1 / 2", or "10 / 15" -> "2 / 3".  You may look up pseudocode for how to reduce factors.
 
* <tt>String toString()</tt>: Returns the fraction as a string, written as "numerator / denominator" -- e.g. "1 / 4", or "3 / 5".  It should '''always''' be reduced first.  If the numerator is 0, just return "0".  If the denominator is 1, just return the numerator -- e.g. "5 / 1" -> "5".  Also, handle negatives properly!  E.g., -3 / -5 should be written as "3 / 5", and 1 / -2 should be written as "-1 / 2".
 
  
===FractionTests===
+
Submit your <tt>MyActivity.java</tt> and <tt>activity_my.xml</tt>. Make sure your name is in both.  FYI, comments in XML files look like this:
  
<tt>FractionTests</tt> must have tests that cover every method above, with multiple "normal" and "border" cases as input, including <tt>compareTo()</tt> as implied by <tt>Fraction's</tt> implementation of the <tt>Comparable</tt> interface.  You should also test that denominator = 0 exceptions are also thrown as specified. <!--If you are not using JUnit, these will cause your program to end, so you may comment them out in <tt>run()</tt> (see below) as your testing progresses.-->
+
<nowiki>
 
+
  <!-- here is my name -->
Each test method should have the following form: "<tt>void xNTest() { ... }</tt>", such as "<tt>add1Test()</tt>" or "<tt>division2Test()</tt>", etc.  Each test method should make at least one <tt>Fraction</tt> object, set some values, perform an operation or more, and test whether the correct output was obtained.  Write comments to explain *why* each test is useful!
+
</nowiki>
 
 
To declare that a test has been failed, define a new public class called <tt>FractionException</tt> derived from <tt>Exception</tt>.  It should have a constructor that takes a String.  Throw an instance of <tt>FractionException</tt> in test method <tt>xNTest()</tt> constructed with the message string "Failed xNTest()" IF AND ONLY IF A TEST IS FAILED.
 
 
 
You will be using JUnit, so simply annotate all of your test functions appropriately.  There should be ''at a minimum'' 20+ tests to get decent coverage of <tt>Fraction's</tt> functionality.
 
 
 
===main()===
 
 
 
This is what your <tt>main()</tt> should contain:
 
 
 
Result result = JUnitCore.runClasses(FractionTests.class);
 
for (Failure failure : result.getFailures())
 
  System.out.println(failure.toString());
 
 
 
==Submission==
 
 
 
Submit your <tt>Fraction.java</tt> and <tt>FractionTests.java</tt> on Sakai (be sure to add your name and section number to both).  Do NOT submit <tt>Lab7.java</tt>.
 

Latest revision as of 08:54, 7 November 2017

Preliminaries

  • Make a new Android project with n = 7 following these instructions through step 6. On step 7 "Select form factor...", switch over to step 4 on the Android developers page tutorial here and complete through step 6 which ends with "...click Finish". Make the activity be called "MyActivity".

Instructions

First, make the empty activity project as described above and confirm that you can build and run it -- either on the emulator or an actual Android device as explained on the next page "Run Your App".

Second, take the empty activity project and modify it to become a very simple "rock paper scissors" generator as detailed below:

  1. Change the layout in activity_my.xml to a vertical LinearLayout, similar to this example
  2. Change the old TextView that was printing "Hello, world" to say "Click the button below to get a new shape".
  3. Add an ImageView underneath, centered laterally, that initially shows a rock, paper, OR scissors image. Find your own for these, cropped and scaled to 250 x 250, and put them in res/drawable with the names "rock.png", "paper.png", and "scissors.png". Here's some help for drawable resources
  4. Add a TextView underneath the image which acts as a caption, saying "Rock" if the image is a rock, "Paper" if it's paper, and "Scissors" if it's scissors. The caption should be centered laterally.
  5. Finally, add a centered Button under the caption. This button should be labeled "New" and when pressed causes another random selection which (possibly) changes the image and caption above . The caption text should be set not through resources but instead by using setText() in MyActivity.java, and something similar should be done with the image.

Note: Do NOT make changes to any other XML resource file besides activity_my.xml.

Here is a screenshot of a sample program that satisfies the above requirements (except it should say "MyLab_7"):

300px-Lab10.png

Submission

Submit your MyActivity.java and activity_my.xml. Make sure your name is in both. FYI, comments in XML files look like this:

<!-- here is my name -->