Skip to content

Commit 8ace11c

Browse files
authored
Add files via upload
1 parent db238a3 commit 8ace11c

19 files changed

+3170
-0
lines changed

ArbitraryFloatingPointNumbers.java

Lines changed: 312 additions & 0 deletions
Large diffs are not rendered by default.

ArbitraryWholeNumbers.java

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/**
2+
* A class that represents Arbitrary Whole Numbers
3+
* @author David Nguyen
4+
* @since 11/14/2022
5+
*/
6+
public class ArbitraryWholeNumbers extends ArbitraryFloatingPointNumbers {
7+
8+
// A boolean field that indicates whether the number is less than 0 or not
9+
private boolean isNegative;
10+
// An array that stores the digits of the floating point number
11+
private int[] digits;
12+
13+
/**
14+
* Creates an Arbitrary Whole Number.
15+
* Assigns the class's fields the values that the constructor got when a Arbitrary Whole Number object is created.
16+
* Passes the fields to the superclass (Arbitrary Floating Point Number).
17+
* @param isNegative A boolean indicating whether the current number is negative or not
18+
* @param digits An array that represents the digits of an arbitrary floating point number
19+
*/
20+
public ArbitraryWholeNumbers(boolean isNegative, int[] digits) {
21+
super(isNegative, digits);
22+
this.isNegative = isNegative;
23+
this.digits = digits;
24+
}
25+
26+
/**
27+
* Determines whether the current arbitrary whole number is negative or not.
28+
* Returns true if the current number is negative.
29+
* Returns false if the current number is NOT negative.
30+
* @return A boolean that indicates whether the current number is negative.
31+
*/
32+
public boolean isNegativeNumber() {
33+
if (isNegative)
34+
return true;
35+
else
36+
return false;
37+
}
38+
39+
/**
40+
* An overridden toString method that creates the string representation of an arbitrary whole number given.
41+
* @return A string that is the proper string representation of an arbitrary whole number given
42+
*/
43+
public String toString() {
44+
StringBuilder result = new StringBuilder();
45+
// Append '-' sign to the beginning of the number if the number is negative
46+
if (this.isNegative == true) {
47+
result.append('-');
48+
}
49+
// A new array that is same as digits but has all leading zeros removed.
50+
int[] array = removeLeadingZero(digits);
51+
// Append every element of the array that has all leading zeros removed to the resulting string
52+
for (int i = (array.length - 1); i >= 0; i--) {
53+
result.append(array[i]);
54+
}
55+
return result.toString();
56+
}
57+
58+
/**
59+
* A helper method to remove leading zeros.
60+
* @param array Any given int array that needs its leading zeros removed
61+
* @return An array that has all its leading zeros removed
62+
*/
63+
public int[] removeLeadingZero(int[] array) {
64+
// A variable that stores the numbers of leading zeros
65+
int numLeadingZero = 0;
66+
// A variable that stores whether the current digit in the array is a non-zero
67+
boolean isNonZero = false;
68+
// A variable storing the index for the following 'while' loop
69+
int i = array.length - 1;
70+
/* A loop that determines the number of leading zeros.
71+
* The nonZero variable will also be set when the first non-zero is met to indicates the end of loop
72+
* Condition: The index must be more than or equal to 0 and the isNonZero variable must be FALSE
73+
*/
74+
while (i >= 0 && !isNonZero) {
75+
if (array[i] == 0) {
76+
numLeadingZero++;
77+
isNonZero = false;
78+
i--;
79+
}
80+
else
81+
isNonZero = true;
82+
}
83+
// A variable that stores the length of the new array
84+
int newLength = array.length - numLeadingZero;
85+
// An array of type int that stores the new array
86+
int[] newArray = new int[newLength];
87+
// A variable storing the index of the old (given) array
88+
int index1 = array.length - numLeadingZero - 1;
89+
// A variable storing the index of the new array
90+
int index2 = newArray.length - 1;
91+
/* A loop that copies every element from old, original array to the new array
92+
* Condition: index of both arrays must be more than or equal to 0
93+
*/
94+
while (index1 >= 0 && index2 >= 0) {
95+
newArray[index2] = array[index1];
96+
index1--;
97+
index2--;
98+
}
99+
return newArray;
100+
}
101+
102+
/**
103+
* A helper method that converts all arrays to strings.
104+
* @param array Any given arrays of type int
105+
* @return A String that is the string representation of the given array
106+
*/
107+
public String arrayToString(int[] array) {
108+
// A variable of type StringBuilder that stores the output string
109+
StringBuilder output = new StringBuilder();
110+
/* A loop that appends every element of the given array into the output string
111+
* Condition: index must be less than length of array */
112+
for (int i = 0; i < array.length; i++) {
113+
if (array[i] != 0)
114+
output.append(array[i]);
115+
}
116+
return output.toString();
117+
}
118+
119+
/**
120+
* An overridden equals method that determines if two whole numbers are equal to each other
121+
* This can be done by comparing the contents of the two string representations of the two numbers
122+
* @param o Any given object
123+
* @return Returning true or false depending on these numbers' equality
124+
*/
125+
public boolean equals(Object o) {
126+
if (o instanceof ArbitraryWholeNumbers) {
127+
ArbitraryWholeNumbers number = (ArbitraryWholeNumbers) o;
128+
// A variable storing this number's digits with leading zeros removed
129+
int[] newArray1 = removeLeadingZero(this.digits);
130+
// A variable storing input, given number's digits with leading zeros removed
131+
int[] newArray2 = removeLeadingZero(number.digits);
132+
// A variable storing the string representation of this ArbitraryWholeNumbers
133+
String s1 = arrayToString(newArray1);
134+
// A variable storing the string representation of the given ArbitraryWholeNumbers
135+
String s2 = arrayToString(newArray2);
136+
return (this.isNegativeNumber() == number.isNegativeNumber() && s1.equals(s2));
137+
}
138+
return false;
139+
}
140+
141+
/**
142+
* A method to add two arbitrary whole numbers and return the string representation of the result
143+
* @param value1 Any given value of type ArbitraryWholeNumbers
144+
* @param value2 Any given value of type ArbitraryWholeNumbers
145+
* @exception UnsupportedOperationException Throw this exception when given input numbers have different signs
146+
* @return A new object of type ArbitraryWholeNumbers that has all the numbers added up
147+
*/
148+
public ArbitraryWholeNumbers add (ArbitraryWholeNumbers value1, ArbitraryWholeNumbers value2) {
149+
// A variable to keep track of the carry
150+
int carry = 0;
151+
// A variable to set the length of the array
152+
int length = 0;
153+
// Remove Leading Zeros of both input values
154+
// Array1 is a new array of type int that is the digits of value1 that has been removed all leading zeros
155+
int[] newArray1 = removeLeadingZero(value1.digits);
156+
// Array2 is a new array of type int that is the digits of value2 that has been removed all leading zeros
157+
int[] newArray2 = removeLeadingZero(value2.digits);
158+
/* Setting length of resulting array to the maximum length between the two newArrays.
159+
*/
160+
if (newArray1.length > newArray2.length)
161+
length = newArray1.length + 1;
162+
else
163+
length = newArray2.length + 1;
164+
// An array of type int storing a new array with the same length as the result array for input value2's digits
165+
int[] array1 = new int[length];
166+
// Copying all digits from value1's original digits array (with leading 0's removed) to this array (array1)
167+
for (int i = 0; i < newArray1.length; i++) {
168+
array1[i] = newArray1[i];
169+
}
170+
// An array of type int storing a new array with the same length as the result array for input value2's digits
171+
int[] array2 = new int[length];
172+
// Copying all digits from value1's original digits array (with leading 0's removed) to this array (array1)
173+
for (int i = 0; i < newArray2.length; i++) {
174+
array2[i] = newArray2[i];
175+
}
176+
// An array storing the resulting array from adding
177+
int[] result = new int[length];
178+
// A variable storing index of result's digits array
179+
int i = 0;
180+
// A temporary variable storing the sum of the addition of each corresponding pair of digits
181+
int s = 0;
182+
// If the values for adding have different signs, throw an UnsupportedOperationException
183+
if (value1.isNegativeNumber() == value2.isNegativeNumber()) {
184+
/* A loop that adds every corresponding pair of digits from the two arrays and stores them in the result array
185+
* Condition: index must be less than or equal to the result array's length */
186+
while (i < result.length) {
187+
s = array1[i] + array2[i] + carry;
188+
if (s >= 10) {
189+
carry = 1;
190+
result[i] = s - 10;
191+
}
192+
else {
193+
carry = 0;
194+
result[i] = s;
195+
}
196+
i++;
197+
}
198+
}
199+
else
200+
throw new UnsupportedOperationException("Invalid inputs");
201+
// A whole number object storing the output of the addition, which will also be returned
202+
ArbitraryWholeNumbers output = new ArbitraryWholeNumbers(false, result);
203+
// If the inputs are negative, the result must also have isNegative set to true
204+
if (value1.isNegativeNumber())
205+
output.isNegative = true;
206+
return output;
207+
}
208+
}

Bid.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//David Nguyen - The class Bid represents a bid that a contractor makes for a contract//
2+
public class Bid{
3+
//The contract field represents the contract name for the bid
4+
Contract contract;
5+
//The contractor field represents the contractor's name
6+
Contractor contractor;
7+
//The value field represents the value of the bid
8+
Double value;
9+
10+
//A constructor that creates a bid//
11+
public Bid (Contract contract, Contractor contractor, double value) {
12+
this.contract = contract;
13+
this.contractor = contractor;
14+
this.value = value;
15+
}
16+
17+
//A method that returns the contract for the bid
18+
public Contract contract() {
19+
return contract;
20+
}
21+
22+
//A method that returns the contractor for the bid
23+
public Contractor contractor() {
24+
return this.contractor;
25+
}
26+
27+
//A method that returns the value of the bid//
28+
public double value() {
29+
return this.value;
30+
}
31+
}

ComplexNumber.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* A class that represents Complex Numbers
3+
* @author David Nguyen
4+
* @since 11/14/2022
5+
*/
6+
public class ComplexNumber implements ComplexNumberInterface{
7+
8+
// A field representing the real part
9+
private ArbitraryFloatingPointNumbers real;
10+
// A field representing the imaginary part
11+
private ArbitraryFloatingPointNumbers imaginary;
12+
13+
/**
14+
* Creates a Complex Number.
15+
* Assigns the class's fields the values that the constructor got when a ComplexNumber object is created.
16+
* @param real An ArbitraryFloatingPointNumbers that represents a real part
17+
* @param imaginary An ArbitraryFloatingPointNumbers that represents an imaginary part
18+
*/
19+
public ComplexNumber (ArbitraryFloatingPointNumbers real, ArbitraryFloatingPointNumbers imaginary) {
20+
this.real = real;
21+
this.imaginary = imaginary;
22+
}
23+
24+
/**
25+
* A method that retrieves the real part of the complex number.
26+
* @return A floating point number that represents the real part of a complex number
27+
*/
28+
public ArbitraryFloatingPointNumbers getRealPart() {
29+
return real;
30+
}
31+
32+
/**
33+
* A method that retrieves the imaginary part of the complex number.
34+
* @return ArbitraryFloatingPointNumbers A floating point number that represents the imaginary part of a complex number
35+
*/
36+
public ArbitraryFloatingPointNumbers getImaginaryPart() {
37+
return imaginary;
38+
}
39+
40+
/**
41+
* A method that retrieves the imaginary part of the complex number.
42+
* @param number Any ComplexNumber object that is given as input
43+
* @return A Complex Number that is the result of the addition between this ComplexNumber and the input number
44+
*/
45+
public ComplexNumber add(ComplexNumberInterface number) {
46+
ArbitraryFloatingPointNumbers newReal = this.getRealPart().add(this.getRealPart(), number.getRealPart());
47+
ArbitraryFloatingPointNumbers newImaginary = this.getImaginaryPart().add(this.getImaginaryPart(), number.getImaginaryPart());
48+
ComplexNumber result = new ComplexNumber (newReal, newImaginary);
49+
return result;
50+
}
51+
52+
/**
53+
* A method that compares any two complex numbers.
54+
* @param value1 Any ComplexNumber object that is given as input
55+
* @param value2 Any ComplexNumber object that is given as input
56+
* @return A boolean (true or false) depending on equality of these two numbers
57+
*/
58+
public boolean equals(ComplexNumber value1, ComplexNumber value2) {
59+
return (value1.getRealPart().equals(value2.getRealPart())) && (value1.getImaginaryPart().equals(getImaginaryPart()));
60+
}
61+
62+
/**
63+
* A method that returns the string representation of this complex number.
64+
* @return A String that is the proper string representation of the complex number
65+
*/
66+
public String toString() {
67+
String output = new String();
68+
if (!getImaginaryPart().isNegativeNumber()) {
69+
output = (getRealPart() + " + " + getImaginaryPart() + "i");
70+
}
71+
else
72+
output = (getRealPart() + " " + getImaginaryPart() + "i");
73+
return output;
74+
}
75+
}

ComplexNumberInterface.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* An interface that represents Complex Numbers
3+
* @author David Nguyen
4+
* @since 11/14/2022
5+
*/
6+
public interface ComplexNumberInterface {
7+
8+
/**
9+
* An abstract method that retrieves the real part of the complex number.
10+
* @return A floating point number that represents the real part of the complex number
11+
*/
12+
ArbitraryFloatingPointNumbers getRealPart();
13+
14+
/**
15+
* An abstract method that retrieves the imaginary part of the complex number
16+
* @return A floating point number that represents the imaginary part of the complex number
17+
*/
18+
ArbitraryFloatingPointNumbers getImaginaryPart();
19+
}

0 commit comments

Comments
 (0)