Question

********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...

********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM

For this lab use the program at the bottom to sort an array using selection sort. Write a selection sort to report out the sorted low values: lowest to highest. Write another to report out the sorted high values – highest to lowest. Last but not least, the program should output all the values in the array AND then output the 2 requested sorted outputs.

-----> MY OLD PROGRAM BELOW

importjava.util.Scanner; // import Scanner

publicclassModule1B {
  
   publicstaticvoidmain(String[] args)
   {
       // declaring the temperatures array
      double[] maxTemp = newdouble[12]; // assigned 12
      double[] lowTemp = newdouble[12]; // assigned 12
      doubleavgHighTemp = 0; // initializing the average high temp to zero
      doubleavgLowTemp = 0; // initializing the avergae low temp to zero
      
      // Create a Scanner object
      Scanner input = newScanner(System.in);
       // Input temperatures one by one
      for(inti = 0; i < 12; i++)
      {
         System.out.print("Enter the highest temperature for month # "+ (i+1) + " : "); // Enter the temp for each high temp of the # month
         maxTemp[i] = Integer.parseInt(input.nextLine()); // Data of the months will enter the maxTemp array
         System.out.print("Please enter the lowest temperature for month # "+ (i+1) + " : "); // Enter the temp for each low temp of the # month
         lowTemp[i] = Integer.parseInt(input.nextLine()); // Data of the months will enter the lowTemp array
        System.out.println();
      }
      avgHighTemp = getAvgHighTemperature(maxTemp); // the average High temp is made up of the getAvgHighTemp method & max temp array
      avgLowTemp = getAvgLowTemperature(lowTemp); // the avgLowTemp is made up of the getAvgLowTemp method & low temp
      System.out.println("Average high temperature is: "+ (int)avgHighTemp); // Average will return as an integer
      System.out.println("Average low temperature is: "+ (int)avgLowTemp); // Average will return as an integer
      
   }
  
   // 1 out of the 2 methods requested to calculate the average high temp
   publicstaticdoublegetAvgHighTemperature(double[] highTemp)
   {
      doubletotal = 0; // initialized total
      for(inti = 0; i < 12; i++)
      {
         total += highTemp[i]; // adds on from the hightemp[i] array
      }
      returntotal/12;
   }
  
   // 1 out of the 2 methods requested to calculate the average low temp
   publicstaticdoublegetAvgLowTemperature(double[] lowTemp)
   {
      doubletotal = 0; // initialized total
      for(inti = 0; i < 12; i++)
      {
         total += lowTemp[i]; // adds on form the lowTemp[i] array
      }
      returntotal/12;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.util.Scanner; // import Scanner

public class Module1B {

public static void main(String[] args)

{

// declaring the temperatures array

double[] maxTemp = new double[12]; // assigned 12

double[] lowTemp = new double[12]; // assigned 12

double avgHighTemp = 0; // initializing the average high temp to zero

double avgLowTemp = 0; // initializing the avergae low temp to zero

// Create a Scanner object

Scanner input = new Scanner(System.in);

// Input temperatures one by one

for(int i = 0; i < 12; i++)

{

System.out.print("Enter the highest temperature for month # "+ (i+1) + " : "); // Enter the temp for each high temp of the # month

maxTemp[i] = Integer.parseInt(input.nextLine()); // Data of the months will enter the maxTemp array

System.out.print("Please enter the lowest temperature for month # "+ (i+1) + " : "); // Enter the temp for each low temp of the # month

lowTemp[i] = Integer.parseInt(input.nextLine()); // Data of the months will enter the lowTemp array

System.out.println();

}

avgHighTemp = getAvgHighTemperature(maxTemp); // the average High temp is made up of the getAvgHighTemp method & max temp array

avgLowTemp = getAvgLowTemperature(lowTemp); // the avgLowTemp is made up of the getAvgLowTemp method & low temp

System.out.println("Average high temperature is: "+ (int)avgHighTemp); // Average will return as an integer

System.out.println("Average low temperature is: "+ (int)avgLowTemp); // Average will return as an integer

selectionSortAsc(lowTemp);

selectionSortDesc(maxTemp);

System.out.println("sorted low values: lowest to highest\n");

for(int i=0; i<lowTemp.length; i++){

System.out.print(lowTemp[i]+" ");

}

System.out.println();

System.out.println("sorted high values: lowest to highest\n");

for(int i=0; i<maxTemp.length; i++){

System.out.print(maxTemp[i]+" ");

}

System.out.println();

}

// 1 out of the 2 methods requested to calculate the average high temp

public static double getAvgHighTemperature(double[] highTemp)

{

double total = 0; // initialized total

for(int i = 0; i < 12; i++)

{

total += highTemp[i]; // adds on from the hightemp[i] array

}

return total/12;

}

// 1 out of the 2 methods requested to calculate the average low temp

public static double getAvgLowTemperature(double[] lowTemp)

{

double total = 0; // initialized total

for(int i = 0; i < 12; i++)

{

total += lowTemp[i]; // adds on form the lowTemp[i] array

}

return total/12;

}

public static void selectionSortAsc(double[] arr){

for(int i=0; i<arr.length; i++){

for(int j=i+1; j<arr.length; j++){

if(arr[i]>arr[j]){

double temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

public static void selectionSortDesc(double[] arr){

for(int i=0; i<arr.length; i++){

for(int j=i+1; j<arr.length; j++){

if(arr[i]<arr[j]){

double temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following is a program from Starting out with C++ by Toni Gaddis. I am getting the following error messages pertain...

    The following is a program from Starting out with C++ by Toni Gaddis. I am getting the following error messages pertaining to the lines: cin>>month[i].lowTemp; and months[i].avgTemp = (months[i].highTemp + month[i].lowTemp)/2; The error messages read as follows: error C2039: 'lowTemp': is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' and it continues. The second error message is identical. The program is as follows: Ch. 11, Assignment #3. pg. 646. Program #4. //Weather Statistics. //Write a program that uses a structure to store the...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • C Part 1 Given a structure that store the highest temperature (HighTemp) and the lowest temperature (LowTemp) of a day:...

    C Part 1 Given a structure that store the highest temperature (HighTemp) and the lowest temperature (LowTemp) of a day: typedef struct {       char DayOfMonth[31];     /*  e.g.  “May 13”,  “July 9”  */       int HighTemp;         /*  the highest temperature of that day  */       int LowTemp;         /* the lowest temperature of that day */ } DayTemp; DayTemp  Temperature[365]; Temperature is an array of structure DayTemp.  Write a function that will return the average of HighTemp for a given number of days (NumberDay)...

  • Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...

    Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. You should also have a parallel array to store the names of the 12 months. The program should read the Month's names, and the temperatures from a given input file called temperature.txt. The program should output the highest and lowest temperatures for the year, and the months that correspond to those temperatures. The program must use the following functions:...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Write and submit the source code for the following program. The program will use an integer...

    Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables: int[] prices = new int[10]; // Array of smartphone prices Assignment Ask the user for the price of each smartphone (using a for loop) Sort the list of smartphones (once) from low to high price...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT