Computer Applications
A shopping website offers a special discount if the order ID has the sequence 555 anywhere in it. For example, 158545553031, 198555267140, …. .
Fill in the blanks (a) and (b) in the given Java Method to convert the order ID (a long integer) into a string and check if the sequence 555 is present in it.
void checkOrder(long oid)
{
String str = _______(a)_________;
if(______(b)_______)
{
System.out.println("Special Discount Eligible: " + oid);
}
}
Java String Handling
2 Likes
Answer
(a) String str = String.valueOf(oid);
(b) if (str.contains("555"))
Reason
To check if "555" appears in the order ID, we need to convert the long
number into a String
. String.valueOf(oid)
correctly converts a long
to a String
. contains("555")
checks if the substring "555" is present in str.
Answered By
3 Likes
Related Questions
A university student's registration number follows the format:
<CourseCode><Year><CollegeCode><RollNumber>
where
Component Description CourseCode A 3-letter code representing the course (e.g., CSE for Computer Science, ECE for Electronics & Communication) Year The last two digits of the admission year. CollegeCode A 3-digit code representing the college RollNumber A 4-digit unique student roll number. Examples
Registration Number Course Code Admission Year College Code Roll Number CSE240011023 CSE 24 001 1023 ECE252104297 ECE 25 210 4297 ASE230277259 ASE 23 027 7259 Define a class that accepts a student's registration number as input, extracts the relevant details, and displays them in the specified format.
import java.util.Scanner; public class KboatStuRegNum { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter reg. no.: "); _______(1)_________ int l = n.length(); if (l != 12) { System.out.println("Invalid reg. no."); System.exit(0); } _______(2)_________ _______(3)_________ _______(4)_________ _______(5)_________ System.out.println("Course Code : " + cc); System.out.println("Admission Year : " + yr); System.out.println("College Code : " + cl); System.out.println("Roll Number : " + rNo); } }
The output of a program which extracts a part of the string "SUBMISSION" is as follows:
(a) "MISS"
(b) "MISSION"If
String str = "SUBMISSION";
write appropriate Java statements to get the above outputs.Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }