Undoubled String
Added on 2019-09-20
7 Pages1486 Words129 Views
|
|
|
1. (a) Why does Java provide both ints and floats, if floats can encode any possible int? Ans:Both data types has their own ranges and the int data type has less range in comparison offloat data type. Int data type is used to store the whole number while the float is used to store the floating type value which have mantissa and exponent value. The int data type is used to store the whole number and float data type is used to store the decimal values. Int data type is a 32-bitsigned two’s complement integer. The float data type is a single-precision 32-bit IEEE 754 floating point.(b) Why does Java provide four types for storing integer data (byte, short, int, and long)? I.e., why would you use one over another?Ans:Each data type has their own ranges to store the values. It depends on what type of range value we have to store. The size of byte is 1 byte. Short is of 2 bytes. Int is of 4 bytes. Long is of 8 bytes. It is like you have an empty container A of 2 kg, and another one empty container B of 3kg and you have something of 1.5 kg weight. We can put this thing in any container but we prefer the most fitted container so that we can save the extra space for other things. Here is sequence of data type from low to high.Low------------------HighByte->short->int->longWe can store byte type value in any one of these ( short or int or long) data type.We can store short type value in any one of these ( int or long) data type.We can store int type value in any one of these (long) data type.We can store long type value cannot to be stored in any of these data types – short or int or byte.2. Given the following variable definitions, what are the types and values of the following expressions? (As an example, the expression x + 2 has type int and value 5.) Ans:Int x = 3;Double d = 2.0;
ExpressionData typeValue5 / xint15 / ddouble2.55 / 2 + 1int34 < xbooleanfalse5 % xint24 != x && 2.5 > dbooleantrue!(3 >= x || true)booleanflase3. You can submit the solution to parts (a) and (b) as a single response in an attached file, or two separate attached files. (a) Write a program (main method) that prompts the user for a “doubled” String, where every character is immediately duplicated. It prints the un-doubled version. Include a loop so that the user is repeatedly prompted. Belowis an example transcript. 1. public static void main(String args[]) {2. do {3. Scanner sc = new Scanner(System.in);4. System.out.println("Please enter a doubled string: ");5. String inputStr = sc.next();6. System.out.println("Undoubled is: " + getUndoubledString(inputStr));7. System.out.println("Do you want to play again? (y / n)");8. String choice = sc.next();9. if (!choice.equals("y")) {10. break;11. }12. } while (true);13. }14.15. public static String getUndoubledString(String str) {16. String undoubleStr = "";17. for (int i = 0; i < str.length(); i++) {18. if (!undoubleStr.contains(str.charAt(i) + "")) {19. undoubleStr += str.charAt(i) + "";20. }21. if (str.charAt(i) == 13) {22.23. undoubleStr += str.charAt(i) + "";
24. }25. }26. return undoubleStr;27. }(b) Extend the previous solution so that it verifies that the input is doubled. If so, it behaves as above. If not, it prints an appropriate error message.1. public static void main(String args[]) {2. do {3. Scanner sc = new Scanner(System.in);4. System.out.println("Please enter a doubled string: ");5. String inputStr = sc.next();6. if(!checkForDoubledInput(inputStr)){7.8. System.out.println("The input string is not doubled string!!");9. }else{10. System.out.println("Undoubled is: " + getUndoubledString(inputStr));11. System.out.println("Do you want to play again? (y / n)");12. String choice = sc.next();13. if (!choice.equals("y")) {14. break;15. }16. }17. } while (true);18. }19.20. public static String getUndoubledString(String str) {21. String undoubleStr = "";22. for (int i = 0; i < str.length(); i++) {23. if (!undoubleStr.contains(str.charAt(i) + "")) {24. undoubleStr += str.charAt(i) + "";25. }26. if (str.charAt(i) == 13) {27.28. undoubleStr += str.charAt(i) + "";29. }30. }31. return undoubleStr;32. }33.
End of preview
Want to access all the pages? Upload your documents or become a member.
Related Documents