forked from DSkilton/UdemyTimBurchalka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberToWords.java
119 lines (106 loc) · 4.61 KB
/
NumberToWords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.TimBuchalka;
//Write a method called numberToWords with one int parameter named number. The method should
//print out the passed number using words for the digits. If the number is negative, print
//"Invalid Value". To print the number as words, follow these steps: 1. Extract the last
//digit of the given number using the remainder operator. 2. Convert the value of the digit
//found in Step 1 into a word. There are 10 possible values for that digit, those being 0,
//1, 2, 3, 4, 5, 6, 7, 8, 9. Print the corresponding word for each digit, e.g. print "Zero"
//if the digit is 0, "One" if the digit is 1, and so on. 3. Remove the last digit from the
//number. 4. Repeat Steps 2 through 4 until the number is 0.
//
//The logic above is correct, but in its current state, the words will be printed in reverse
//order. For example, if the number is 234, the logic above will produce the output
//"Four Three Two" instead of "Two Three Four". To overcome this problem, write a second
//method called reverse. The method reverse should have one int parameter and return the
//reversed number (int). For example, if the number passed is 234, then the reversed number
//would be 432. The method reverse should also reverse negative numbers.
//
//Use the method reverse within the method numberToWords in order to print the words in the
//correct order. Another thing to keep in mind is any reversed number with leading zeroes
//(e.g. the reversed number for 100 is 001). The logic above for the method numberToWords
//will print "One", but that is incorrect. It should print "One Zero Zero". To solve this
//problem, write a third method called getDigitCount. The method getDigitCount should have
//one int parameter called number and return the count of the digits in that number. If the
//number is negative, return -1 to indicate an invalid value. For example, if the number has
//a value of 100, the method getDigitCount should return 3 since the number 100 has 3 digits
//(1, 0, 0).
//
//Example Input/Output - getDigitCount method
//* getDigitCount(0); should return 1 since there is only 1 digit
//* getDigitCount(123); should return 3
//* getDigitCount(-12); should return -1 since the parameter is negative
//* getDigitCount(5200); should return 4 since there are 4 digits in the number
//Example Input/Output - reverse method
//* reverse(-121); should return -121
//* reverse(1212); should return 2121
//* reverse(1234); should return 4321
//* reverse(100); should return 1
//
//Example Input/Output - numberToWords method
//* numberToWords(123); should print "One Two Three".
//* numberToWords(1010); should print "One Zero One Zero".
//* numberToWords(1000); should print "One Zero Zero Zero".
//* numberToWords(-12); should print "Invalid Value" since the parameter is negative.
//
public class NumberToWords {
public static int numberToWords(int number){
while(number != 0) {
int temp = number % 10;
switch(temp) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
case 4:
System.out.println("four");
break;
case 5:
System.out.println("five");
break;
case 6:
System.out.println("six");
break;
case 7:
System.out.println("seven");
break;
case 8:
System.out.println("eight");
break;
case 9:
System.out.println("nine");
break;
case 0:
System.out.println("zero");
break;
}
number /= 10;
}
return -1;
}
public static int reverse(int num){
int remainder;
int newNum = 0;
while(num != 0) {
remainder = num % 10;
newNum = newNum+remainder;
newNum *= 10;
num /= 10;
}
newNum /= 10;
System.out.println(newNum);
return newNum;
}
public static int getDigitCount(int number){
int count = 0;
for(int i = number; i > 0; i /= 10){
count++;
}
System.out.println(count);
return count;
}
}