Write a fix method that takes in a double and returns a doub
Solution
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


