Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a fix method that takes in a double and returns a double. The return value

ID: 3937519 • Letter: W

Question

Write a fix method that takes in a double and returns a double. The return value is the decimal portion of the input. Eg 3.14159 would return .14159. Use math, not built-in Java methods Write a method that takes a string as an argument. If your string is more than 10 letters or less than 1 letter return "Bad number." Otherwise return the telephone number equivalent to the input. Your return can be of type string. Eg. A, B, or C are 2, D, E, F are 3 etc., "GOCOSC1046" would return "4626721046" (even better would be "462-673-1946" but not required). Write a brief main method that collects input and shows off your methods.

Explanation / Answer

import java.util.*;
class Phone
{
   //Returns the fractional patr
   double fix(double d)
   {
       int x = (int)d;       //Extracts the integral part
       double y = (x - d);   //Extracts the fractional part
       y = - y;       //Finds the absolute value
       return y;
   }
   //String formation of the number
   String Number(String s)
   {
       int len = s.length();   //Finds the length
       String res = "";   //Initializes to null
       if(len < 1)   //Checks the validity number should be greater than 0
       {
           System.out.println("Length is less than one " + len);
           return "Bad Number";
       }
       if(len > 10)   //Checks the validity number should be less than 10
       {
           System.out.println("Length is greater than 10 " + len);
           return "Bad Number";
       }
       //Loops till length
       for(int c = 0; c < len; c++)
       {
           char ch = s.charAt(c);   //Extracts a character
           switch(ch)
           {
               case 'a':
               case 'A':
               case 'b':
               case 'B':
               case 'c':
               case 'C':
                   res = res + "2";
                   break;      
               case 'd':
               case 'D':
               case 'e':
               case 'E':
               case 'f':
               case 'F':
                   res = res + "3";
                   break;  
               case 'g':
               case 'G':
               case 'h':
               case 'H':
               case 'i':
               case 'I':
               case 'j':
               case 'J':
                   res = res + "4";
                   break;  
               case 'k':
               case 'K':
               case 'l':
               case 'L':
               case 'm':
               case 'M':
               case 'n':
               case 'N':
                   res = res + "5";
                   break;  
               case 'o':
               case 'O':
               case 'p':
               case 'P':
               case 'q':
               case 'Q':
               case 'r':
               case 'R':
                   res = res + "6";
                   break;                  
               case 's':
               case 'S':
               case 't':
               case 'T':
               case 'u':
               case 'U':
               case 'v':
               case 'V':
                   res = res + "7";
                   break;  
               case 'w':
               case 'W':
               case 'x':
               case 'X':
               case 'y':
               case 'Y':              
               case 'z':
               case 'Z':              
                   res = res + "8";
                   break;
               default:
                   res = res + ch;
           }
           if(c == 2 || c == 5)
               res = res + "-";
       }
       return res;
   }
   public static void main(String ss[])
   {
       Phone ph = new Phone();
       Scanner sc = new Scanner(System.in);
       double d;
       String st;
       System.out.println("Enter a double Number: ");
       d = sc.nextDouble();
       System.out.println(String.format("Fractional part = %1.05f", ph.fix(d)));
       sc.nextLine();
       System.out.println("Enter a Phone number: ");
       st = sc.nextLine();
       System.out.println(ph.Number(st));
   }
}

Output:

Enter a double Number:
3.14159
Fractional part = 0.14159
Enter a Phone number:
GOCOSC1046
462-672-1046

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote