Lab 3 (Year 3 Sem 1)

Lab 3 is the fourth lab in semester 1 of year 3, and the fourth lab in the total of year 3. It is a lab for conditionals, and is considered to be easy (but tedious).

Description
No description was provided for this lab.

Unlocked after completion

 * OCD (Score at least 60% for Lab 3: Conditionals)
 * If Only (Score at least 80% for Lab 3: Conditionals)

Question
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400.

Write a program named LeapYear to determine whether a year is a leap year.

Your program should:


 * prompt the user to enter a year.
 * have a method named isLeap which will take in parameter year, checks if the year is leap, and return a boolean true if it's a leap year, false otherwise.
 * Finally, print the result (refer to sample output for format).

Sample Output 1:

Enter a year: 2008 2008 is a leap year? true

Sample Output 2:

Enter a year: 1900 1900 is a leap year? false

Solution
use % (modulo) operator to check divisibility, also use boolean operators as described in the notes.

Question
The two roots of a quadratic equation  can be obtained using the following formula:

$$r_1 = \frac{-b+\sqrt{b^2-4ac}}{2a}$$ and $$r_2 = \frac{-b-\sqrt{b^2-4ac}}{2a}$$

b2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.

Write a program named Quadratic that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots”.

Your program should:


 * prompt the user to enter values for a, b, c.
 * have a method named discriminant which will take in parameter a,b,c, calculates the discriminant and return value of the discriminant.
 * have a method named r1 which will take in parameter a,b,c, calculates the r1 and return value of the r1.
 * have a method named r2 which will take in parameter a,b,c, calculates the r2 and return value of the r2.
 * Finally, print the result (refer to sample output for format). Note that r1 (and r2) should be formatted to 5 decimal places.

Sample Output 1:

Enter a, b, c: 1.0 3 1 The equation has two roots -0.38197 and -2.61803

Sample Output 2:

Enter a, b, c: 1 2.0 1 The equation has one root -1

Sample Output 3:

Enter a, b, c: 1 2 3 The equation has no real roots

Solution
basic understanding of conditionals is enough for this question

do not call the r1 and r2 methods if discriminant is negative

Question
Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0), and (0, 100).



Write a program named PointInTriangle that prompts the user to enter a point with x- and y-coordinates and determines whether the point is inside the triangle.

Write your code in the main method. Refer to sample output for format of printout.

Sample Output 1:

Enter x and y: 100.5 25.5 The point is in the triangle

Sample Output 2:

Enter x and y: 100.5 50.5 The point is not in the triangle

Solution
the line describing the hypotenuse of the triangle is given by $$-0.5x+100$$

check if the point is in the 200x100 box bounded by the (0, 100) and (200, 0) points.

if it is, check if the y coordinate is lower than $$-0.5x+100$$

Question
Write a program named Palindrome that prompts the user to enter a three-digit integer and determines whether it is a palindrome number. A number is palindrome if it reads the same from right to left and from left to right.

Your program should:


 * prompt the user to enter a 3-digit integer. Print out "incorrect input" if the input is not a 3 digit integer.
 * check if the integer is a palindrome number.
 * Finally, print the result (see sample output for format).

Sample output 1:

Enter a three-digit integer: 121 121 is a palindrome

Sample output 2:

Enter a three-digit integer: 123 123 is not a palindrome

Solution
this is literally y2s1p2q4 Practice 2 (Year 2 Sem 1)

but now you need to try catch input mismatch exceptions and make sure the integer is in range