ulrail.gif

Lab Quiz 2

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Pledged Lab Quiz

This lab quiz is pledged.  You may use the course textbook, and NOTHING ELSE.  This includes any files in your home directory, previous assignments, slides, etc.  However, you should still save your file into your home directory.

You will need to submit a single file called LabQuiz2.java.  You may download the skeleton code here: LabQuiz2.java (right click on the link and select "save as" (or "save target as")).

The TAs cannot help you with Java-related issues on this quiz.  If you are having problems getting JCreator to work, can't log in to the machines, etc., then you are welcome to ask the TAs for assistance.  If you do not understand what the lab is asking, the TAs may be able to help you (meaning if it's a problem with our explanation, then they can help you -- if you've been skipping the lectures so far and have no idea what any of this means, you are on your own).

Advice

  • This quiz is split into two parts.  It is recommended that you do part 1 before you go part 2 -- you will be able to get a lot of partial credit that way.  But read through the entire quiz first.
     
  • To maximize your points, make sure your program compiles successfully.
     
  • If towards the end things are not going as well as you would want (i.e., the program does not compile), it might be best to try commenting out errant statements (i.e., put a // in front of them)
     
  • If you run out of time, submit what you have -- there will be partial credit for this quiz.  Also remember that you can submit a file as many times as you want -- only the most recent submission is graded.  So once you have it working, submit what you have.

Lab Quiz Part 1: Triangle Counting

The code for this part should all be within the main() method.  You will need to read in an int value from the keyboard (we'll call it n).  Only read one int value for this whole program, or else it will not run correctly for the graders!

You will then need to examine all triangles from (1,1,1) to (n,n,n).  Thus, if n is 2, you need to consider 8 triangles: (1,1,1), (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2), (2,2,1), (2,2,2).  This will probably require nested loops.

First, check if the triangle is not valid.  There are two reasons to not consider a potential triangle

  • If a triangle has sides (1,1,5), then it can't be a valid triangle -- the short sides will never be able to meet, due to the length of the long side.  We have provided a method in the skeleton code, called isTriangle(), to help you determine this.  If your triangle sides are in int variables a, b, and c, then calling isTriangle(a,b,c) will return true or false, depending on whether (a,b,c) forms a valid triangle.  Note that the method assumes that a is less than or equal to b, and that b is less than or equal to c (see next bullet point).
     
  • If the sides aren't it sorted order, then we won't need to consider it.  Otherwise, we would consider (3,4,5), (5,4,3), and the other variations of the same exact triangle.

For each triangle, you will need to print out its properties.

     
Right triangle   Equilateral triangle   Isosceles triangle   Scalene triangle
  • If the triangle is a right triangle, then print out such.  A right triangle means that it fulfils the Pythagorean theorem: a2 + b2 = c2
  • If the triangle is equilateral, then print out such.  An equilateral triangle means all three sides are equal to each other.
  • If the triangle is isosceles, then print out such.  An isosceles triangle has two sides equal to each other, and the third side different.  An equilateral triangle should not indicate that it is also an isosceles triangle.
  • If the triangle is scalene, then print out such.  A scalene triangle has no sides equal to each other

If you get stuck on some of them, keep on going -- you can always come back and try to fix it later.

Sample Output

The text in red is what was input by the user.  The format of your program does not have to look the same, but it should print out the same information.  Note that for some triangles, such as (3,4,5) have multiple properties (right and scalene) -- they can be printed out on separate lines.

Triangle manipulation

Enter max n:
5
(1,1,1) is an equilateral triangle
(1,2,2) is an isosceles triangle
(1,3,3) is an isosceles triangle
(1,4,4) is an isosceles triangle
(1,5,5) is an isosceles triangle
(2,2,2) is an equilateral triangle
(2,2,3) is an isosceles triangle
(2,3,3) is an isosceles triangle
(2,3,4) is a scalene triangle
(2,4,4) is an isosceles triangle
(2,4,5) is a scalene triangle
(2,5,5) is an isosceles triangle
(3,3,3) is an equilateral triangle
(3,3,4) is an isosceles triangle
(3,3,5) is an isosceles triangle
(3,4,4) is an isosceles triangle
(3,4,5) is a right triangle
(3,4,5) is a scalene triangle
(3,5,5) is an isosceles triangle
(4,4,4) is an equilateral triangle
(4,4,5) is an isosceles triangle
(4,5,5) is an isosceles triangle
(5,5,5) is an equilateral triangle

Lab Quiz Part 2: Method Creation

For this part, you should move the code that tests whether (a,b,c) is a triangle of various kinds into separate methods.  For example, the isRightTriangle(a,b,c) method should return true if (a,b,c) is a right triangle, and false otherwise.  The isTriangle() method does something similar.  Thus, you should implement four methods: isRightTriangle(), isScaleneTriangle(), isIsoscelesTriangle(), and isEquilateralTriange(), each of which take in 3 int parameters.

Your methods must be public and static, or else your code will not work properly for yourself or for the graders!

These methods will be MUCH easier if you ensure that the values passed into the method are already in sorted order (i.e. prior to calling these methods, you ensure that the first parameter is less than or equal to the second parameter, and that the second parameter is less than or equal to the third parameter).

Make sure the spelling of your methods is correct!  Especially isosceles.  And equilateral.  If your methods are spelled incorrectly, the grading system will not realize they are there, and you will lose points.

Your code in the main() method should then be modified to use your new methods.  The program output will be the same.

How to Proceed

Get the steps working in order!  Make sure part 1 works before you move onto part 2.  If you don't follow this, you will end up with a mess of code that doesn't work, and you will not be able to fix it within the lab quiz time.  There will be partial credit given for this lab quiz -- so if you are unable to handle one triangle type, you can still receive a lot of points for the quiz.

Other Requirements

You are only required to follow a few of the good programming practices discussed in HW J1 (yes, you may follow that link during the lab quiz).  If you have time, feel free to do the others -- but due to the time constraints of the quiz, you don't have to do them all.  The ones you must do are listed below.

  • Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.
     
  • Proper variable names (although, in this case, you are welcome to use n, a, b, and c, as that is what the provided formulas use)
     
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.

Submission

When you are finished, submit your LabQuiz2.java file.  You will have to "sign" a pledge on the submission.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif