1.. (a) Why does Java provide both ints and floats, if floats

Added on - 20 Sep 2019

  • 7

    Pages

  • 1486

    Words

  • 165

    Views

  • 0

    Downloads

Trusted by +2 million users,
1000+ happy students everyday
Showing pages 1 to 3 of 7 pages
1.(a) Why does Java provide both ints and floats, if floats can encode anypossible 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 thefloating type value which have mantissa and exponent value. The int data type is used to storethe 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 754floating 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 rangevalue we have to store. The size of byte is 1 byte. Short is of 2 bytes. Int is of 4 bytes. Long is of8 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 weprefer the most fitted container so that we can save the extra space for other things. Here issequence 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 ofthe following expressions? (As an example, the expression x + 2 has type intand 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 anattached 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. Ifso, 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.
desklib-logo
You’re reading a preview
Preview Documents

To View Complete Document

Click the button to download
Subscribe to our plans

Download This Document