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...
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...
[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...
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...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT