JAVA please
Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where arrays can be used. After the topic of arrays has been covered create arrays of class Temperature. Write a program that satisfies the next 6 requirements using Temperatures. Create a class similar to the Math class. Put the next 5 static methods in it. (These static methods could also be in the Demo class.) On my website are two example programs that demonstrate passing arrays and returning arrays: Passing Arrays using static methods and ChangeArgumentDemo. Use the random number generator as described on the bottom of page 412 to create 3 arrays of random sizes of 1 to 5 elements.
1) Create a static void method that has an array parameter and is called 3 times to read in Temperature values for each array.
2) Create a static method that computes and returns the average Temperature for each array and is also called 3 times.
3) Create a static method that prints the Temperatures of an array.
4) Create a static helper method that has 3 array parameters and either returns the largest array or the largest size.
5) Create a static method that returns an array of Temperatures that has the same number of elements as the largest of the three arrays. This method will have 3 array parameters and possibly an integer parameter. It determines the largest Temperature value from the three arrays at each index and creates a copy of this Temperature and stores it at that index of the new array. This array is then returned.
6) As this program is running it should generate user friendly text for input and output explaining what the program is doing.
Create a set of test value Temperatures that demonstrate that your program runs correctly. (My website gives sample output to follow.)
Temperature.java
public class Temperature {
private double temp;
private char unit;
public Temperature(double temp, char unit) {
this.temp = temp;
this.unit = unit;
}
public Temperature() {
// TODO Auto-generated constructor
stub
}
public double getTemp() {
return temp;
}
public void setTemp(double temp) {
this.temp = temp;
}
public char getUnit() {
return unit;
}
public void setUnit(char unit) {
this.unit = unit;
}
public Temperature toCelsius() {
double tem = 0;
if(unit=='C')
{
tem=this.temp;
System.out.println(tem);
}
else if(unit=='K')
{
tem=
((temp)-273.15);
}
else if(unit=='F')
{
tem=(temp-32)*5.0/9.0;
}
Temperature t=new
Temperature(tem,'C');
return t;
}
public Temperature toKelvin() {
double tem = 0;
if(unit=='K')
{
tem=this.temp;
}
else if(unit=='C')
{
tem=
temp+273.15;
}
else if(unit=='F')
{
tem=(temp-32)*(5.0/9.0);
}
Temperature t=new
Temperature(tem,'K');
return t;
}
public Temperature toFahrenheit() {
double tem = 0;
if(unit=='F')
{
tem=this.temp;
}
else if(unit=='C')
{
tem=
(temp-32)*(5.0/9.0);;
}
else if(unit=='K')
{
tem=temp *
(9.0/5.0) - 459.67;
}
Temperature t=new
Temperature(tem,'F');
return t;
}
public Temperature add(Temperature temp1) {
double val=0.0;
Temperature t=null;
switch(temp1.getUnit())
{
case 'C':
case 'c':
t=this.toCelsius();
val=t.getTemp()+temp1.getTemp();
t=new
Temperature(val,temp1.getUnit());
break;
case 'F':
case 'f':
t=this.toFahrenheit();
val=t.getTemp()+temp1.getTemp();
t=new
Temperature(val,temp1.getUnit());
break;
case 'K':
case 'k':
t=this.toKelvin();
val=t.getTemp()+temp1.getTemp();
t=new
Temperature(val,temp1.getUnit());
break;
}
return t;
}
public Temperature divide(int num) {
double val=0.0;
Temperature t=null;
val=this.temp/num;
t=new Temperature(val, unit);
return t;
}
public Temperature subtract(Temperature temp2) {
double val=0.0;
Temperature t=null;
switch(getUnit())
{
case 'C':
case 'c':
{
t=temp2.toCelsius();
break;
}
case 'F':
case 'f':{
t=temp2.toFahrenheit();
break;
}
case 'K':
case 'k':{
t=temp2.toKelvin();
break;
}
}
val=this.getTemp()-t.getTemp();
return new
Temperature(val,getUnit());
}
@Override
public String toString() {
return temp+""+unit;
}
}
PassingArraysUsingStaticMethod
import java.util.Scanner; public class PassingArrays { public static void main(String[] args) {//*** create three arrays one of type Bottle and two of type int. Bottle[] bottleArray = new Bottle[3]; int [] integerArray = new int[3]; int [] integerArray2 = new int[6]; System.out.println("Read the bottle array. " ); readBottleArray(bottleArray); System.out.println("Values of bottleArray are:" ); printBottleArray(bottleArray); System.out.println("Read the integer array. " ); //*** this read method takes any size array this one has 3 readIntegerArray(integerArray); System.out.println("Values of integerArray are: " ); printIntegerArray(integerArray); //*** The next method has two array parameters. The method adds //*** the values of the two arrays and returns the answer in //*** a Bottle array. Bottle[] sumBottleArray = addBottleAndIntArray(bottleArray, integerArray); System.out.println("Adding the values of the two array gives:" ); printBottleArray(sumBottleArray); //printBottleArray(bottleArray); System.out.println("Read the second integer array. " ); //*** this read method takes any size array this one has 6 readIntegerArray(integerArray2); System.out.println("Values of integerArray2 are: " ); printIntegerArray(integerArray2); } //*** Method has two array parameters. //*** Method returns a Bottle[]. public static Bottle[] addBottleAndIntArray(Bottle[] bArray, int[] iArray) {//*** Bottle array is created with null pointers to Bottle objects Bottle[] answerArray = new Bottle[bArray.length]; Bottle b = new Bottle(); for(int x = 0; x < bArray.length; x++) { //*** Bottle object b is created //Bottle b = new Bottle(); //*** Bottle b set to integer array value b.setNumUnits(iArray[x]); //*** Bottle b is added to Bottle from bArray[] answerArray[x] = bArray[x].add(b); } return answerArray; } //*** takes any size array public static void readBottleArray(Bottle[]array) { System.out.println("This array has " + array.length + " elements."); Bottle t1; for(int x = 0; x < array.length; x++) { t1 = new Bottle(); t1.read(); array[x] = t1; } } //*** takes any size array private static void printBottleArray(Bottle[] bottleArray) { for(int x = 0; x < bottleArray.length; x++) { System.out.print("Bottle value [" + x + "] is "); System.out.println(bottleArray[x] ); } } //*** takes any size array private static void readIntegerArray(int[]integerArray) { Scanner scan = new Scanner(System.in); System.out.println("This array has " + integerArray.length + " elements."); for(int x = 0; x < integerArray.length; x++) { integerArray[x] = scan.nextInt(); } } //*** takes any size array private static void printIntegerArray(int[] integerArray) { for(int x = 0; x < integerArray.length; x++) { System.out.print("integer value [" + x + "] is "); System.out.println(integerArray[x] ); } } }
import java.util.Scanner;
public class ChangeArgumentDemo
{
public static void main(String[] args)
{//*** create two arrays one of type Bottle and one of
type int.
Bottle[] bottleArray = new
Bottle[3];
int [] integerArray = new
int[3];
System.out.println("Read the bottle
array. " );
readBottleArray(bottleArray);
System.out.println("Values of
bottleArray are:" );
printBottleArray(bottleArray);
System.out.println("Read the
integer array. " );
readIntegerArray(integerArray);
System.out.println("Values of
integerArray are: " );
printIntegerArray(integerArray);
System.out.println("Send each
bottle one at a time to " +
"method
changeBottleValue(). " );
for(int x = 0; x <
bottleArray.length; x++)
{//*** address of a Bottle is
stored at memory location x
//*** see
method
changeBottleValue(bottleArray[x]);
}
System.out.println("Values of
bottleArray[] are: " );
printBottleArray(bottleArray);
System.out.println("Send each
integer one at a time to " +
" method
changeIntegerValue()." );
for(int x = 0; x <
integerArray.length; x++)
{ //*** integer value
stored at memory location x
//*** see
method
changeIntegerValue(integerArray[x]);
}
System.out.println("Values of
integerArray[] are: " );
printIntegerArray(integerArray);
System.out.println("Pass array of
integers and change them. " );
changeIntegerArray(integerArray);
System.out.println("Values of
integerArray[] are: " );
printIntegerArray(integerArray);
}//main
private static void changeIntegerArray(int[]
integerArray)
{
for(int x = 0; x <
integerArray.length; x++)
{ //*** integer value
stored at memory location x
//*** see
method
integerArray[x]
= integerArray[x] * 2;
}
}
public static void readBottleArray(Bottle[]
array)
{
System.out.println("This array has
" + array.length + " elements.");
Bottle t1;
for(int x = 0; x < array.length;
x++)
{
t1 = new
Bottle();
t1.read();
array[x] =
t1;
}
}
//*** address of argument is copied to local Bottle
b.
private static void changeBottleValue(Bottle b)
{//*** Bottle b has its value doubled
b.setNumUnits(b.getNumUnits() *
2);
//*** Bottle b goes out of scope
}
private static void printBottleArray(Bottle[]
bottleArray)
{
for(int x = 0; x <
bottleArray.length; x++)
{
System.out.print("bottle value [" + x + "] is ");
System.out.println(bottleArray[x] );
}
}
private static void
readIntegerArray(int[]integerArray)
{
Scanner scan = new
Scanner(System.in);
System.out.println("This array has
" + integerArray.length + " elements.");
for(int x = 0; x <
integerArray.length; x++)
{
integerArray[x]
= scan.nextInt();
}
}//*** local integer i is created
private static void changeIntegerValue(int i)
{//*** int i is doubled
i = i * 2;
//*** i goes out of scope when method is
finished.
}
private static void printIntegerArray(int[]
integerArray)
{
for(int x = 0; x <
integerArray.length; x++)
{
System.out.print("integer value [" + x + "] is ");
System.out.println(integerArray[x] );
}
}
}
Get Answers For Free
Most questions answered within 1 hours.