-
Notifications
You must be signed in to change notification settings - Fork 3
/
OdometerOOPs.java
170 lines (130 loc) · 3.69 KB
/
OdometerOOPs.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package odometerproblem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Odometer{
public ArrayList<Digit> digits;
int numOfDigits;
Integer minValue;
Integer maxValue;
Odometer(int numOfDigits ){
this.numOfDigits = numOfDigits;
digits = getMinValue(numOfDigits);
minValue = odometerToNumber(this.getMinValue(this.numOfDigits));
maxValue = odometerToNumber(this.getMaxValue(this.numOfDigits));
}
public String toString(){
String s = "";
for(Digit d: digits)
{
s += "" + d.getValue();
}
return s;
}
void setDigits(ArrayList<Digit> d){
digits = d;
}
boolean isAscendingCondition(ArrayList<Digit > digits){
for(int i = 0; i < digits.size() - 1; i++ )
{
if(digits.get(i).getValue() >= digits.get(i + 1).getValue())
{
return false;
}
}
return true;
}
ArrayList<Digit> getMinValue(int numOfDigits){
ArrayList<Digit> dgt = new ArrayList<Digit>();
for ( int i = 0; i < numOfDigits ; i++){
Digit d = new Digit(i);
dgt.add(d);
}
return dgt;
}
ArrayList<Digit> getMaxValue(int numOfDigits){
ArrayList<Digit> dgt = new ArrayList<Digit>();
int j = 9;
for ( int i = 0; i < numOfDigits ; i++){
Digit d = new Digit(j--);
dgt.add(d);
}
Collections.reverse(dgt);
return dgt;
}
ArrayList<Digit> getNextValue(){
Integer number;
number = odometerToNumber(digits);
number++;
while(!isAscendingCondition(numberToOdometer(number)))
{
if(number >= maxValue)
{
number = minValue;
}
else{
number++;
}
}
digits = new ArrayList<Digit >(numberToOdometer(number));
return digits;
}
ArrayList<Digit> numberToOdometer(Integer number){
ArrayList<Digit> digit = new ArrayList<Digit>();
while(number > 0){
Digit d = new Digit(number % 10);
digit.add(d);
number = number / 10;
}
if(digit.size() + 1 == this.numOfDigits)
{
digit.add(new Digit(0));
}
Collections.reverse(digit);
return digit;
}
Integer odometerToNumber(ArrayList<Digit> digit){
Integer number = new Integer(0);
for(Digit dgt: digit)
{
number = number * 10 + dgt.getValue();
}
return number;
}
}
class Digit{
int value;
Digit(int value){
this.value = value;
}
int getValue(){
return value;
}
void setValue(int value){
this.value = value;
}
}
public class OdometerProblem {
public static void main(String[] args) {
System.out.println("Number of digits : ");
Scanner sc = new Scanner(System.in);
int numOfDigits = sc.nextInt();
if(numOfDigits > 10 || numOfDigits < 1){
System.out.println("Invalid number of digits");
return;
}
System.out.println("Odometer start value : ");
Odometer odometer = new Odometer(numOfDigits);
int odometerVal = sc.nextInt();
odometer.setDigits(odometer.numberToOdometer(odometerVal));
System.out.println("Number of odometer values : ");
Scanner s = new Scanner(System.in);
int printList = s.nextInt();
while(printList != 0)
{
odometer.getNextValue();
System.out.println(odometer);
printList--;
}
}
}