Question

IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and...

IN JAVA

Write a program that uses a two-dimensional array to store daily minutes walked and carb intake for a week. Prompt the user for 7 days of minutes walked and carb intake; store in the array.   Write a sort ( your choice which sort ) to report out the sorted minute values -lowest to highest. Write another to report out the sorted carb intake - highest to lowest. These methods MUST be your original code.   Your program should output all the values in the array and then output the 2 sorted outputs.   You must use an array to receive credit for this assignment.

Your code MUST be your own work and should not be copied from another student, website or any other source. You will be required to complete a sort of an array (or two) in your final lab - making this lab very important to get correct. Make sure you write all the methods listed above for full credit.

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

If you have any problem with the code feel free to comment.

Program

import java.util.Scanner;

public class Test {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);// for taking console input
      
       int[][] ar = new int[7][2];
      
       //taking use input
       for(int i=0; i<ar.length; i++) {
           System.out.print("Enter minutes walked: ");
           ar[i][0] = sc.nextInt();
           System.out.print("Enter carbs: ");
           ar[i][1] = sc.nextInt();
           System.out.println();
       }
      
       System.out.println("Sorting by Minutes Walked");
       sortByWalkingMinutes(ar);
       display(ar);
      
       System.out.println();
      
       System.out.println("Sorting by Carbs");
       sortByCarbs(ar);
       display(ar);
      
       sc.close();
   }
  
   /*using selection sort*/

   public static void sortByWalkingMinutes(int[][] ar) {
       for (int i = 0; i < ar.length; i++) {
           int min = i;
           for (int j = i; j < ar.length; j++) {
               if (ar[min][0] > ar[j][0])
                   min = j;
           }

           //swpaing values
           int temp = ar[min][0];
           int temp2 = ar[min][1];
          
           ar[min][0] = ar[i][0];
           ar[min][1] = ar[i][1];
          
           ar[i][0] = temp;
           ar[i][1] = temp2;
       }
   }
  
   //sorting in reverse order
   public static void sortByCarbs(int[][] ar) {
       for (int i = 0; i < ar.length; i++) {
           int min = i;
           for (int j = i; j < ar.length; j++) {
               if (ar[min][1] < ar[j][1])
                   min = j;
           }

           //swaping values
           int temp = ar[min][1];
           int temp2 = ar[min][0];
          
           ar[min][1] = ar[i][1];
           ar[min][0] = ar[i][0];
          
           ar[i][1] = temp;
           ar[i][0] = temp2;
       }
   }

   //displaying 2d array
   public static void display(int[][] ar) {
       System.out.println("Minutes Walked\tCarbs");
       for(int i=0; i<ar.length; i++) {
           for(int j=0; j<ar[0].length; j++) {
               System.out.print(ar[i][j]+" \t\t");
           }
           System.out.println();
       }
   }
}

Output

<terminated> Test (1) [Java Application] C:\Program FilesJava\jre1.8.0_211\bin\javaw.exe (12-Nov-2019, 2:07:39 pm) LIILC NaIN

Add a comment
Know the answer?
Add Answer to:
IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and...
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
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