forked from sensei-thundercleese/closet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.java
More file actions
130 lines (106 loc) · 4.09 KB
/
Copy pathBinary.java
File metadata and controls
130 lines (106 loc) · 4.09 KB
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
//skeleton file for class Binary
public class Binary implements Comparable {
private int _decNum;
private String _binNum;
/*=====================================
default constructor
pre: n/a
post: initializes _decNum to 0, _binNum to "0"
=====================================*/
public Binary() {
// *** YOUR IMPLEMENTATION HERE ***
}
/*=====================================
overloaded constructor
pre: n >= 0
post: sets _decNum to n, _binNum to equiv string of bits
=====================================*/
public Binary( int n ) {
// *** YOUR IMPLEMENTATION HERE ***
}
/*=====================================
String toString() -- returns String representation of this Object
pre: n/a
post: returns String of 1's and 0's representing value of this Object
=====================================*/
public String toString() {
// *** YOUR IMPLEMENTATION HERE ***
return ""; //placeholder to get past compiler
}
/*=====================================
String decToBin(int) -- converts base-10 input to binary
pre: n >= 0
post: returns String of bits
eg decToBin(0) -> "0"
decToBin(1) -> "1"
decToBin(2) -> "10"
decToBin(3) -> "11"
decToBin(14) -> "1110"
=====================================*/
public static String decToBin( int n ) {
// *** YOUR IMPLEMENTATION HERE ***
return ""; //placeholder to get past compiler
}
/*=====================================
String decToBinR(int) -- converts base-10 input to binary, recursively
pre: n >= 0
post: returns String of bits
eg decToBinR(0) -> "0"
decToBinR(1) -> "1"
decToBinR(2) -> "10"
decToBinR(3) -> "11"
decToBinR(14) -> "1110"
=====================================*/
public static String decToBinR( int n ) {
// *** YOUR IMPLEMENTATION HERE ***
return ""; //placeholder to get past compiler
}
/*=============================================
boolean equals(Object) -- tells whether 2 Objs are equivalent
pre: other is an instance of class Binary
post: Returns true if this and other are aliases (pointers to same
Object), or if this and other represent equal binary values
=============================================*/
public boolean equals( Object other ) {
// *** YOUR IMPLEMENTATION HERE ***
return false; //placeholder to get past compiler
}
/*=============================================
int compareTo(Object) -- tells which of two Binary objects is greater
pre: other is instance of class Binary
post: Returns 0 if this Object is equal to the input Object,
negative integer if this<input, positive integer otherwise
=============================================*/
public int compareTo( Object other ) {
// *** YOUR IMPLEMENTATION HERE ***
return -1; //placeholder to get past compiler
}
//main method for testing
public static void main( String[] args ) {
System.out.println();
System.out.println( "Testing ..." );
Binary b1 = new Binary(5);
Binary b2 = new Binary(5);
Binary b3 = b1;
Binary b4 = new Binary(7);
System.out.println( b1 );
System.out.println( b2 );
System.out.println( b3 );
System.out.println( "\n==..." );
System.out.println( b1 == b2 ); //should be false
System.out.println( b1 == b3 ); //should be true
System.out.println( "\n.equals()..." );
System.out.println( b1.equals(b2) ); //should be true
System.out.println( b1.equals(b3) ); //should be true
System.out.println( b3.equals(b1) ); //should be true
System.out.println( b4.equals(b2) ); //should be false
System.out.println( b1.equals(b4) ); //should be false
System.out.println( "\n.compareTo..." );
System.out.println( b1.compareTo(b2) ); //should be 0
System.out.println( b1.compareTo(b3) ); //should be 0
System.out.println( b1.compareTo(b4) ); //should be neg
System.out.println( b4.compareTo(b1) ); //should be pos
/*=========================================
=========================================*/
}//end main()
} //end class