Practice 2 (Year 3 Sem 1)

Practice 2 is the second practice in semester 1 of year 3, and the second practice in the total of year 3. It is a practice for revision of chapter 1 to 8, and is considered to be easy.

Description
No description was provided for this practice.

This revision lab consists of some extra coding exercises covering concepts in Chapters 1 to 8. This will be a good practice for Lab Test 1. You should just take 1h 30min to complete this revision as a "simulation" for lab test 1. Please note that the autograder only grade parts of the questions. You should do your own testing to verify the correctness of your code. Similarly, during lab test, live autograding is minimal.

Unlocked after completion

 * OCD (Score at least 60% for Extra Practice 2: Revision for C1 to C8)
 * All Set (Score at least 80% for Extra Practice 2: Revision for C1 to C8)

Question
The Fibonacci sequence, Fn, is defined such that each number is the sum of the two preceding ones, starting from 0 and 1. Mathematically: Hence, the first 6 terms of the sequence is: 1, 1, 2, 3, 5, 8 (we do not consider the 0 th term as part of the sequence for this question) You are to implement the following methods in the class Fibonacci. The main has already been coded for you and hence, your task is primarily to implement the following methods below.  Your Task:  (a) Implement a method get Fibonacci(int n)  which takes in an integer n, where n is at least 1, and returns Fn, the n th integer in the Fibonacci sequence. You may assume that n is always at least 1. ''' You are only allowed to use a loop and NOT allowed to use arrays / arraylist for this question.  (b) Implement a method disp Fibonacci(int n) ''' which takes in an integer n, where n is at least 1, and displays the first n terms of the Fibonacci sequence. You can only display at most 8 terms in a line and the numbers must be left-aligned, with a character width of 5 between each value. You may assume that n is always at least 1. ''' You may use the method implemented in (a) and you are only allowed to use a loop for this question. You are NOT allowed to use arrays / arraylist for this question.  (c) Implement a method goldenRatio(int n)''' which takes in an integer n, where n is at least 1, and returns an approximation to the Golden Ratio, which is of the double data type. An approximation to the Golden Ratio can be obtained by dividing any term of the sequence (lets say the n th term) from its successive term (the (n + 1)th term), meaning to say: Note that the larger the value of n, the more accurate the approximation of the golden ratio. You may assume that n is always at least 1. ''' You are NOT allowed to use arrays / arraylist for this question.  (d) Implement a method menu''' which will display a menu of options, as seen in the sample output below, ask the user for an input, and will continuously run until the user enters any integer other than 1 or 2 or 3. ''' Your method should be able to catch any non-integer input at any point and display an error message. When that erroneous situation happens, the program stops running completely. ''' In all options, the user will be prompted for an integer n. You can assume that at this stage, the user will enter an integer n which is at least 1. If the user chooses  option 1 , the method from (a) will be called. If the user chooses  option 2 , the method from (b) will be called. If the user chooses  option 3 , the method from (c) will be called. If the user enters any other integer, you will have to print "Goodbye! Have a nice day.".  You are only allowed to use a switch statement and loops for this question       Sample Output 1:  Select one of the options below: Option 1: Find nth Fibonacci number Option 2: Display first n Fibonacci number Option 3: Find nth order approximation to Golden Ratio Select an option (1, 2, 3): a  Error! Enter only an integer!  Sample Output 2:  Select one of the options below: Option 1: Find nth Fibonacci number Option 2: Display first n Fibonacci number <span style="font-family: &quot;Courier New&quot;;">Option 3: Find nth order approximation to Golden Ratio <span style="font-family: &quot;Courier New&quot;;">Select an option (1, 2, 3): 1 <span style="font-family: &quot;Courier New&quot;;">Enter n: 20 <span style="font-family: &quot;Courier New&quot;;">The nth number in the Fibonacci Sequence is 6765 <span style="font-family: &quot;Courier New&quot;;">Select one of the options below: <span style="font-family: &quot;Courier New&quot;;">Option 1: Find nth Fibonacci number <span style="font-family: &quot;Courier New&quot;;">Option 2: Display first n Fibonacci number <span style="font-family: &quot;Courier New&quot;;">Option 3: Find nth order approximation to Golden Ratio <span style="font-family: &quot;Courier New&quot;;">Select an option (1, 2, 3): 2 <span style="font-family: &quot;Courier New&quot;;">Enter n: 20 <span style="font-family: &quot;Courier New&quot;;">1   1    2    3    5    8    13   21 <span style="font-family: &quot;Courier New&quot;;">34  55   89   144  233  377  610  987 <span style="font-family: &quot;Courier New&quot;;">1597 2584 4181 6765 <span style="font-family: &quot;Courier New&quot;;">Select one of the options below: <span style="font-family: &quot;Courier New&quot;;">Option 1: Find nth Fibonacci number <span style="font-family: &quot;Courier New&quot;;">Option 2: Display first n Fibonacci number <span style="font-family: &quot;Courier New&quot;;">Option 3: Find nth order approximation to Golden Ratio <span style="font-family: &quot;Courier New&quot;;">Select an option (1, 2, 3): 3 <span style="font-family: &quot;Courier New&quot;;">Enter n: 20 <span style="font-family: &quot;Courier New&quot;;">The nth order approximation of the Golden Ratio is 1.6180339985218033 <span style="font-family: &quot;Courier New&quot;;">Select one of the options below: <span style="font-family: &quot;Courier New&quot;;">Option 1: Find nth Fibonacci number <span style="font-family: &quot;Courier New&quot;;">Option 2: Display first n Fibonacci number <span style="font-family: &quot;Courier New&quot;;">Option 3: Find nth order approximation to Golden Ratio <span style="font-family: &quot;Courier New&quot;;">Select an option (1, 2, 3): 0 <span style="font-family: &quot;Courier New&quot;;">Goodbye! Have a nice day.

Solution
todo

Question
Being a CS student, your Math teacher acknowledges your programming capabilities and has tasked you to help him/her code a program to help facilitate teaching coordinate geometry. As your Math teacher knows a little bit of programming, you are given the source code LineTester.java and you are now tasked to code and implement the relevant classes to support LineTester.java  Your Task:  (a) '''Implement a class Point. The class should have the following:''' Private data members to denote the x and y coordinate (Both coordinates should be able to contain decimal values) Three constructors where: <ul> <li>There is a no-arg constructor initializes the point (0, 0)</li> <li>There is a constructor which takes an x and y value</li> <li>A copy constructor which takes in a point object</li> </ul> All relevant accessor and mutator methods A method <span style="font-family: &quot;Courier New&quot;;">toString which returns the string representation of the point. E.g, (1, 5) '''(b) Implement a class Line. The class should have the following:''' Private data members gradient and y-intercept. (Both values should be able to contain decimal values) Two constructors where: <ul> <li>One initializes data members by taking in two Point objects ( from (a) )</li> <li>One initializes data members by taking in the gradient and y-intercept</li> <li>You may assume that the the line will never be a vertical line and the two Point objects will have distinct x and y coordinates</li> </ul> All relevant assessor and mutator methods A method <span style="font-family: &quot;Courier New&quot;;">getXintercept which returns the x-intercept of the given line object A method <span style="font-family: &quot;Courier New&quot;;">find <span style="font-family: &quot;Courier New&quot;;">Intersection(Line L1) which takes in a line object and returns the point of intersection of the two lines as a Point object A method <b style="font-family: &quot;Courier New&quot;;">isParallel(Line L1)</b>that takes in a line object and returns a boolean indicating if the lines are parallel A method <b style="font-family: &quot;Courier New&quot;;">isPerpendicular(Line L1)</b>that takes in a line object and returns a boolean indicating if the lines are perpendicular A method <b style="font-family: &quot;Courier New&quot;;">isOnLine(Point p)</b>that takes in a Point object and check if the point is on the line. It returns a boolean indicating if the point is indeed on the line A method <span style="font-family: &quot;Courier New&quot;;">toString which returns the string representation of the line in the form y = mx + c (c) Test your Point and Line class using LineTester.java provided.

Solution
todo

Question
<span style=" font-family: Arial, sans-serif;">The Luhn algorithm is typically used in credit card numbers to ensure the correctness of credit card numbers when used in transactions. In a typical sixteen-digit credit card number, the first fifteen digits are determined by the issuing bank, but the last digit, called the check digit, is mathematically determined based on all the other digits. An example is shown below.



'''<span style=" font-family: Arial, sans-serif;">To calculate the check digit, multiply every even-position digit (when counted from the right) in the number by two. If the result is a two-digit number, then add these digits together to make a single digit <span style=" font-family: Arial, sans-serif;">(this is called the digital root'''). '''To this total, we then add every odd-position digit. This final total obtained is known as the checksum'''. <span style="font-family: Arial, sans-serif; ">The check digit is then the number that needs to be added to this total to make the next multiple of 10. <span style=" font-family: Arial, sans-serif;">A visual example is shown in the table below: <span style=" font-family: Arial, sans-serif;"> <span style=" font-family: Arial, sans-serif;">Checksum = <span style=" font-family: Arial, sans-serif;">1 + 4 + 1 + 7+ 3 + 2 + 6 + 8 + 9 + 8 + 4 + 3 + 8 + 4 + 3 + 8 + 1 + 2 = 67 <span style=" font-family: Arial, sans-serif;">The nearest multiple of 10 to 67 is 70, where 70 - 67 = 3. Hence, the check digit for the above credit card number is 3.  Your Task:  In the public class <span style="font-family: &quot;Courier New&quot;;">CreditCard , <span style="color: rgb(0, 0, 0); font-family: Roboto, sans-serif;">

<span style=" font-family: Arial, sans-serif;">(a) Implement a static method <span style="font-family: &quot;Courier New&quot;;">getC <span style=" font-family: &quot;Courier New&quot;;">heckDigit(String creditCardno)  ﻿to obtain the check digit of a given credit card number <span style=" font-family: Arial, sans-serif;">. You may assume that only a 16-digit credit card number will be entered.

<span style=" font-family: Arial, sans-serif;">(b) Implement a static method <span style="font-family: &quot;Courier New&quot;;">getC <span style=" font-family: &quot;Courier New&quot;;">heckSum(String creditCardno)  ﻿to calculate and return the checksum of a given credit card number <span style=" font-family: Arial, sans-serif;">. You may assume that only a 16-digit credit card number will be entered.

<span style=" font-family: Arial, sans-serif;">(c) Implement a method <span style=" font-family: Arial, sans-serif;"> <span style=" font-family: &quot;Courier New&quot;;">checkCreditCard( '<span style=" font-family: &quot;Courier New&quot;;">String creditCardno '<span style=" font-family: &quot;Courier New&quot;;">) <span style=" font-family: Arial, sans-serif;">  which checks whether a credit card number is valid or not. The method should return only boolean values.

<span style=" font-family: Arial, sans-serif;">(d) The <span style="font-family: &quot;Courier New&quot;;">main method should obtain the credit card number as a String from the user, utilise the methods implemented in (a) and (b) to print the checksum and check digit, as well as print a statement to tell the user whether the credit card number is valid.

<p style=" text-align: justify;"><span style=" font-family: Arial, sans-serif;"> Sample Output  <p style=" text-align: justify;"><span style=" font-family: &quot;Courier New&quot;;">Enter credit card number: 5457623898234113 <p style=" text-align: justify;"><span style="font-family: &quot;Courier New&quot;; ">Check Digit: 3 <p style=" text-align: justify;"><span style=" font-family: &quot;Courier New&quot;;">Checksum: 67 <p style=" text-align: justify;"><span style=" font-family: &quot;Courier New&quot;;"> <p style=" text-align: justify;"><span style=" font-family: &quot;Courier New&quot;;">This is a valid card <span style="font-family: &quot;Courier New&quot;;"> <p style=" text-align: justify;"> <span style=" font-family: &quot;Courier New&quot;;">Enter credit card number: 5457633358234129 <span style="font-family: &quot;Courier New&quot;; ">Check Digit: 9 <span style="font-family: &quot;Courier New&quot;; ">Checksum: 57 <p style=" text-align: justify;"><span style=" font-family: &quot;Courier New&quot;;">This is not a valid card <span style=" font-family: &quot;Courier New&quot;;"> 

Solution
todo