Question

JAVA please Arrays are a very powerful data structure with which you must become very familiar....

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] );
                }
        }
}

Homework Answers

Answer #1

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] );
       }
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Lab 03 This lab gives you exercises with using two dimensional arrays of Java and practices...
Lab 03 This lab gives you exercises with using two dimensional arrays of Java and practices with using methods that return arrays. What you should do is to download and complete the instructor’s program lab03.java: 1. Implement print2D(A) so that it prints out its 2D array argument A in the matrix form. 2. Implement add2Ds(A,B) so that it creates and returns a new 2D array C such that C=A+B. 3. Implement multiScalar2D(c,A) so that it creates and returns a new...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...