02 SBQ SmartPhones HashMap Exceptions
02 SBQ SmartPhones HashMap Exceptions
productid – int
brandname – String
rom – String
price – double
In a seperate Solution class with the main method, implement the following
static methods:
findTheSmartPhoneWithMaxPriceInGivenPriceRange: This
method should take collection of smartphone objects and two
double parameters (startRange and endRange) and returns a
Smartphone object, which had the maximum price from the give
price range. If no Smartphone object found within the price range
then return null.
The above static methods should be called from the main method.
Before calling the static methods in the main method, use the scanner
object to read value for no.of Smartphone objects, then for each
Smartphone object read the attributes as per above mentioned sequence.
Next, read one String value and two double values using the scanner object
for capturing the brand, startRange and endRange.
Constraints:
For more clarity, refer to the sample input and output testcases which are
provided below.
Sample Testcase 1:
Input:
739
Samsung
4GB
12000
382
Redmi
8GB
14000
839
Samsung
16GB
29000
738
IPhone
16GB
43000
Samsung
25000
50000
Output:
739
Samsung
4GB
12000.0
839
Samsung
16GB
29000.0
IPhone
16GB
43000.0
Sample Testcase 2:
Input:
739
Redmi
4GB
13000
109
Nokia
2GB
4500
291
Samsung
16GB
32000
IPhone
35000
50000
Output:
Testcase 3:
Input:
8392
IPhone
32GB
112000
3829
IPhone
16GB
71000
2901
IPhone
8GB
40000
1092
Oneplus
32GB
28000
iPhone
10000
25000
Output:
8392
IPhone
32GB
112000.0
3829
IPhone
16GB
71000.0
2901
IPhone
8GB
40000.0
No mobile found in the given price range
Testcase 4:
Input:
1900
Samsung
4GB
13500
1900
Oneplus
4GB
16700
2392
Oneplus
12GB
23000
IPhone
13000
17000
Output:
Oneplus
4GB
16700.0
---------------------------------------------------------
Sometimes scanner object does not read the new line character by invoking
methods like nextInt(), nextDouble() etc.
Usually, this is not an issue, but this may be visible by calling nextLine()
method.
Savings
int x = sc.nextInt();
String str = sc.nextLine(); --> Here we expect str to have value Savings.
Instead, it may be like “”.
If the above issue is observed, then it is suggested to add one more explicit
call to nextLine() after reading numeric value.
------------------------------------------------------------------------------------------------------------
------
Create a custom exception class named ‘NotNeonException’.
In this main method, read the Integer values and store in the collection of
Integers. From the Integer collection, for each integer value, print the
integer value only if the integer value is neon number. If the integer is
not a neon number, then throw the exception.
First read the integer value for size of collection. Next, read the sequence of
Integer values using Scanner object and store in the collection of type
Integer.
Neon Number:
A Neon Number is a number where the sum of digits of square of the
number is equal to the number.
0, 1 and 9 are examples of Neon Numbers.
Refer the test cases for better understanding.
Constraint:
Duplicate Student objects are not allowed.
Insertion order should not be followed.
During exception handling, catch block should throw the exception in the
specified format: “exception_class_name:<space>message_of_exception”.
Methods Used:
(exception_variable_name).getClass().getSimpleName()
(exception_variable_name).getMessage()
For more clarity, refer to the sample input and output test cases.
Output:
0
1
NotNeonException: 888 is not a neon number
NotNeonException: 8 is not a neon number
NotNeonException: 29 is not a neon number
Description:
0 = 0 * 0 = 0 (0 == 0)
1 = 1 * 1 = 1 (1 == 1)
888 = 888 * 888 = 7,88,544 = 36 (sum of digits of square) (888 !=
36)
8 = 8 * 8 = 64 = 10 (sum of digits of square) (8 != 10)
29 = 29 * 29 = 841 = 13 (sum of digits of square) (29 != 13)
Description:
38 = 38 * 38 = 1444 = 13 (sum of digits of square) (38 != 13)
9 = 9 * 9 = 81 = 9 (sum of digits of square) (9 == 9)
45 = 45 * 45 = 2025 = 9 (sum of digits of square) (45 != 9)
Test Case 3
Input:
0
9
1
Output:
0
1
9
Output:
1
NotNeonException: 85 is not a neon number
NotNeonException: 12 is not a neon number
NotNeonException: 93 is not a neon number