Assignment 6 as a reference
public class ArrExample {
private int[] arr;
public ArrExample(int length) {
this.arr = new int[length];
}
public ArrExample(int[] arr) {
this.arr = arr;
}
public void arrDisplay() {
if (arr.length == 0) {
System.out.println("{}");
} else {
System.out.print("{");
System.out.print(arr[0]);
for (int i = 1; i < arr.length; i++) {
System.out.print("," + arr[i]);
}
System.out.println("}");
}
}
public int arrTot() {
int total = 0;
for (int i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
public int arrMax() {
int maxSeen = arr.length == 0 ? 0 : arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxSeen) {
maxSeen = arr[i];
}
}
return maxSeen;
}
public int arrMin() {
int minSeen = arr.length == 0 ? 0 : arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < minSeen) {
minSeen = arr[i];
}
}
return minSeen;
}
public int arrRange() {
return arrMax() - arrMin();
}
public double arrAverage() {
return (double) arrTot() / arr.length;
}
public void arrClip(int clipValue) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > clipValue) {
arr[i] = clipValue;
}
}
}
public int arrfindCount(int value) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
count++;
}
}
return count;
}
public static void testData() {
int[] arr0 = { -2, -1 };
int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] arr2 = { 2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22 };
ArrExample A1 = new ArrExample(arr0);
ArrExample A2 = new ArrExample(arr1);
ArrExample A3 = new ArrExample(arr2);
A1.arrDisplay();
A2.arrDisplay();
A3.arrDisplay();
System.out.println("Total of A1: " + A1.arrTot());
System.out.println("Total of A2: " + A2.arrTot());
}
1)a) This assignment builds on Assignment 6. In Assignment 6, your arrInt class always used a
completely
filled array. But what if you add values only one at a time, and dont know how many items you will
have
when you are done? In this assignment, you will create an array bffr without filling it right away.
That
means that you have an array but most of the values are meaningless zeroes. At the beginning, all
of the
values are meaningless zeroes. Only as you begin putting values into the array do some of the
elements
become meaningful. The important idea to capture, is that the size or capacity of the array, and
the
number of values in the array (or count), are two separate numbers. Start a project, as always.
Create
your main class with the same name as the project. Then create a new class called
BffrIntFirstname
(Use your first name ). The BffrIntFirstname class will have two private variables: an array of ints
(private int bffr[];), and a count of the number of values that have been added, called count. The
class
should have two constructors. The constructor that takes no arguments should create an array of
8 ints
(using a constant called BFFR_SZ) for initializing the array: bffr = new int[BFFR_SZ];. The other
constructor takes an in.
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
Assignment 6 as a reference public class ArrExample pr.pdf
1. Assignment 6 as a reference
public class ArrExample {
private int[] arr;
public ArrExample(int length) {
this.arr = new int[length];
}
public ArrExample(int[] arr) {
this.arr = arr;
}
public void arrDisplay() {
if (arr.length == 0) {
System.out.println("{}");
} else {
System.out.print("{");
System.out.print(arr[0]);
for (int i = 1; i < arr.length; i++) {
System.out.print("," + arr[i]);
}
System.out.println("}");
}
}
public int arrTot() {
int total = 0;
for (int i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
public int arrMax() {
int maxSeen = arr.length == 0 ? 0 : arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxSeen) {
maxSeen = arr[i];
}
}
return maxSeen;
}
public int arrMin() {
int minSeen = arr.length == 0 ? 0 : arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < minSeen) {
minSeen = arr[i];
2. }
}
return minSeen;
}
public int arrRange() {
return arrMax() - arrMin();
}
public double arrAverage() {
return (double) arrTot() / arr.length;
}
public void arrClip(int clipValue) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > clipValue) {
arr[i] = clipValue;
}
}
}
public int arrfindCount(int value) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
count++;
}
}
return count;
}
public static void testData() {
int[] arr0 = { -2, -1 };
int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] arr2 = { 2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22 };
ArrExample A1 = new ArrExample(arr0);
ArrExample A2 = new ArrExample(arr1);
ArrExample A3 = new ArrExample(arr2);
A1.arrDisplay();
A2.arrDisplay();
A3.arrDisplay();
System.out.println("Total of A1: " + A1.arrTot());
System.out.println("Total of A2: " + A2.arrTot());
}
1)a) This assignment builds on Assignment 6. In Assignment 6, your arrInt class always used a
completely
filled array. But what if you add values only one at a time, and dont know how many items you will
3. have
when you are done? In this assignment, you will create an array bffr without filling it right away.
That
means that you have an array but most of the values are meaningless zeroes. At the beginning, all
of the
values are meaningless zeroes. Only as you begin putting values into the array do some of the
elements
become meaningful. The important idea to capture, is that the size or capacity of the array, and
the
number of values in the array (or count), are two separate numbers. Start a project, as always.
Create
your main class with the same name as the project. Then create a new class called
BffrIntFirstname
(Use your first name ). The BffrIntFirstname class will have two private variables: an array of ints
(private int bffr[];), and a count of the number of values that have been added, called count. The
class
should have two constructors. The constructor that takes no arguments should create an array of
8 ints
(using a constant called BFFR_SZ) for initializing the array: bffr = new int[BFFR_SZ];. The other
constructor takes an integer argument and uses that to create the initial buffer array of that size.
Both
constructors should set the count to 0.
The BffrIntFirstname class should have the following methods,
addRand( which adds a random two digit integer to the array using the java.util.Random and not
Math.random),
getCount (which is a getter to return the current count), and
printToScreen (which works like the printLiteral method in assignment 6, except that it only
prints as many values as have been added).
Hint: The key to making addRand()method work is that when the array is empty (nothing yet
added), it
puts the first value in location 0, which would be bffr[0] = value;. After getting its first value, the
count
should be incremented so it becomes 1. Then next time addRand()is called to add a value, that
value
should be put in location 1: bffr[1] = value; and the count is incremented as before, and becomes
2. We
cannot write the actual array index in the assignment, because each time we add a new value,
that
index must be one bigger than the array index we used before. But notice that the array index that
we
want to use is the same value as the count before the new value is added. In other words, when
the
4. count is 0, the next value goes in location 0. When the count is 1, the next value goes in location
1.
When the count is 7, the next value will go in location 7. So the addRand() method should use the
count
variable as the array index for the new value, and then increment it after the new value has been
added
to the buffer array:
bffr[count] = value;
count++;
As a last step, make the BffrIntFirstname more robust and safe from buffer overflow. Make the
addRand method compare the count to the buffer length, first. If count is greater than or equal
to the length, no value is added. The return type should be changed to Boolean (boolean) so
that it returns true when the value is added, and false when it is not, due to having reached the
end of the buffer.
1)b)
Temperature as a Class Assignment
The goal of this assignment is to better acquaint you with using an object in the design of a
solution.
Your task is to create an application that asks the user to enter a temperature and the type of units
of
that temperature. It then reports the temperature in 3 different units. The application should use a
class called TmpFirstname that stores a single temperature, but provides functions to set and get
that
temperature using any of the three units: Celsius, Fahrenheit, or Kelvin. In the code for
getTmpFromUser, given below, you need to use the three set functions. Your job is to implement
the
TmpFirstname class, and then write a main function that uses the three get functions to complete
this
task. I have included a separate static function header(not part of the TmpFirstname class), called
getTmpFromUser(), that takes the TmpFirstname object as its argument. Put this function in the
same
file where you put main(). The getTmpFromUser function prompts the user for the temperature
and the
type of units and reads them in. It also detects if the user gives bad input and provides the user
with an
opportunity to try again. When the getTmpFromUser function returns, the tmpfirstname object
which
was passed as an argument should contain a temperature. In your main function, call the
getTmpFromUser function, and then, using the three get functions of the tmpfirstname object,
report
the temperature in each of the three units. If written correctly, the output should look exactly as
shown
5. below.
Output on console:
I will ask you for a temperature and its units. Use C for Celsius, F for Fahrenheit or K for
Kelvin.
Enter a Temperature followed by the units: 0 K
The temperature in Celsius is -273.15
The temperature in Fahrenheit is -459.67
The temperature in Kelvin is 0.0
Please read the following scenario to make sure you understand what is meant by an abstract
temperature. Imagine this scenario. I give my Temperature object to John to take the temperature.
He
is comfortable using Fahrenheit and his thermometer reads in Fahrenheit. So he sets the
temperature in
Fahrenheit. Then I give the same Temperature object to Helga. Helga is accustomed to Celsius,
and her
lab equipment uses Celsius. So she uses the getCelsius to get the temperature that John just
measured.
In addition, she wants to compute something that involves the temperature, but the formula she
has is
written to use Kelvin. So she gets the same temperature for her formula, but this time she gets it in
Kelvin. Now supposing the formula tells her that the temperature they want is a little different, but
they
only have it in Kelvin. So she sets that temperature in Kelvin and sends it back to John. John looks
at
what that is in Fahrenheit so he can work on getting the right temperature. The point is that the
object
is THE TEMPERATURE and having a Temperature object makes it possible for anybody to see
THE
TEMPERATURE without having to know anything about who set it or how they set it. We call that
an
ABSTRACTION. It gives us what we need, while hiding unnecessary details from those who use it.
Yes,
there is conversion involved. But that is just part of the job of hiding the detail of which units are
used
inside the class for storage. In other words, there is no "convert" function. Conversion is just what
we
may or may not to do in a get or set in order to make it work as an abstraction.
For the TempFirstname class, you have to decide which type of units to use for the private
storage, and
then provide getters and setters with conversions to and from that value for the other choices. You
can
find the conversion formulas by searching them (e.g. formula for fahrenheit to celsius conversion).
6. // Function: getTmpFromUser
// Purpose: Prompt user for a temperature and put it in the argument object
// Parameter: tempObject is a TmpFirstname object
// Returns: nothing
// public static void getTmpFromUser(TmpFirstname tmpObject)
Develop this function onto the end of your main file (the one where you have main()).In your main,
declare an object of your Temperature class, call getTmpFromUser with your object, and then
write
three lines with sysout to output the lines shown in the console output above.
You can use the same main to test BffrIntFirstname class also.
Ty for your help!!!