Practice 1 (Year 3 Sem 1)

Practice 1 is the first practice in semester 1 of year 3, and the first practice in the total of year 3. It is a practice for classes, and is considered to be easy.

Description
No description was provided for this practice.

Unlocked after completion

 * OCD (Score at least 60% for Extra Practice 1: Classes and Objects)
 * OOP or OPPS? (Score at least 80% for Extra Practice 1: Classes and Objects)

Question
Design a class named LinearEquation for a 2 * 2 system of linear equations as shown below: The class contains:   Private data fields a, b, c, d, e, and f.  A constructor with the arguments for a, b, c, d, e, and f.  Six getter methods for a, b, c, d, e, and f.  A method named isSolvable that returns true if ad - bc is not 0.  Methods getX and getY that return the solution for the equation.  Design your own program to test LinearEquation class. For example, Sample Output: Enter a, b, c, d, e, f: 1 2 3 4 5 6 x is -4.0 and y is 4.5  Enter a, b, c, d, e, f: 2 4 1 2 1 1  The equation has no solution.

Solution
todo

Question
Design a class named StopWatch. The class contains:   Private data fields long startTime and long endTime with getter methods.</li>  A no-arg constructor that initializes startTime with the current time.</li>  A method named start that resets the startTime to the current time.</li>  A method named stop that sets the endTime to the current time.</li>  A method named getElapsedTime that returns the elapsed time for the stopwatch in milliseconds. Use System.currentTimeMillis to get time your time in milliseconds.</li> </ul> A test driver StopWatchTester.java is provided for you to test your class. You are expected to fill in some code before the test driver can work. Sample Output: <font face="Courier New">The execution time of sorting 100,000 numbers using selection sort: 1624 milliseconds

Solution
todo

Question
Design a class named Fan to represent a fan. The class contains three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed.  A private int data field named speed that specifies the speed of the fan (the default is SLOW).</li> A private boolean data field named on that specifies whether the fan is on (the default is false).</li> A private double data field named radius that specifies the radius of the fan (the default is 5).</li> A string data field named color that specifies the color of the fan (the default is blue).</li> The accessor methods for all four data fields (namely getSpeed, getOn, getRadius, getColor).</li> The mutator methods for all four data fields (namely changeSpeed, turnOn, changeRadius, changeColor). All mutators are to take in values.</li> A no-arg constructor that creates a default fan.</li> A method named toString that returns a string description for the fan. If the fan is on, the method returns the fan speed, color, and radius in one combined string. If the fan is not on, the method returns the fan color and radius along with the string “fan is off” in one combined string.</li> </ul> Complete the code required in FanTester to test your Fan class. Sample Output: <span style="font-family: &quot;Courier New&quot;;">Fan speed: FAST, color: yellow, radius: 10.0 <span style="font-family: &quot;Courier New&quot;;">Fan color: blue, radius: 5.0  <span style="font-family: &quot;Courier New&quot;;">fan is off

Solution
todo

Question
We discussed about the HandPhone class in the notes (Chapter 9 Designing Classes II). We would like to make the following updates to the implementation: Constructors:  <li>Update the constructor with 5 parameters such that the input parameter is of the same name as the instance variables, and you have to use this. syntax to initialize the variables.</li> <li>Update the no-arg constructor such that it will use this to call the constructor that has 5 parameters.</li> <li>Add a third constructor which takes in 3 inputs (brand, model and price only). Set the default talkTime to 180, and weight 120. Once again you should use this to call the constructor that has 5 parameters to initialize the variables.</li> </ul> Adding product id for each HandPhone object: <ul> <li>Add a new class attribute named <font face="Courier New">numHandPhone which helps to keep track of how many HandPhone objects have been created.</li> <li>Add a new data attribute named <font face="Courier New">productID of String datatype, which stores the product id of each HandPhone object in the format &lt;Number&gt;-&lt;Model&gt; (e.g. 1-S7, 2-iPhoneX). Number denotes the number of HandPhone object created, while Model is the model of the handphone object.</li> <li>Add relevant code in the constructors such that the attributes will be updated correctly. Note that if your constructors are coded correctly, you only need to add code in one of the constructor to this update. </li> </ul> Adding a toString method: <ul><li>Add a toString method to print relevant information of your HandPhone object. See sample output below for format of string to return</li></ul> You are to test your HandPhone class using HandPhoneTester.java provided. Sample Output: <font face="Courier New">Samsung S7 <font face="Courier New">Product ID: 1-S7 <font face="Courier New">Talk time: 120.0 <font face="Courier New">Weight: 145 <font face="Courier New">Price: $599.0 <font face="Courier New"> <font face="Courier New">Apple iPhoneX <font face="Courier New">Product ID: 2-iPhoneX <font face="Courier New">Talk time: 180.0 <font face="Courier New">Weight: 120 <font face="Courier New">Price: $999.9 <font face="Courier New"> <font face="Courier New">Hua Wei Pro10 <font face="Courier New">Product ID: 3-Pro10 <font face="Courier New">Talk time: 180.0 <font face="Courier New">Weight: 120 <font face="Courier New">Price: $699.0 <font face="Courier New"> <font face="Courier New">Apple iPhoneX <font face="Courier New">Product ID: 2-iPhoneX <font face="Courier New">Talk time: 1999.0 <font face="Courier New">Weight: 120 <font face="Courier New">Price: $999.9 <font face="Courier New"> <font face="Courier New">Xiao Mi Red <font face="Courier New">Product ID: 4-Red <font face="Courier New">Talk time: 180.0 <font face="Courier New">Weight: 120 <font face="Courier New">Price: $299.0

Solution
todo