Question

Write a complete Java program which has two methods, one to enter values for an array...

Write a complete Java program which has two methods, one to enter values for an array and another one to sort it using Bubble Sort method.

Draw DIAGRAMS for memory allocation, AR and ARI for the program.

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

import java.util.*;

class BubbleSortDemo
{
int arr[];
Scanner sc;

BubbleSortDemo()
{
sc=new Scanner(System.in);
arr=new int[5];
}

void acceptArray()
{
int i;
System.out.print("\nEnter elements : ");
for(i=0;i<=4;i++)
{
arr[i]=sc.nextInt();
}
}

void sortArray()
{
int i,j,temp;

for(i=0;i<=4;i++)
{
for(j=0;j<=4-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("\n\nSorted elements : \n");
for(i=0;i<=4;i++)
{
System.out.print(arr[i]+"\t");
}
}
}
class BubbleSort
{
public static void main(String args[])
{
BubbleSortDemo bs=new BubbleSortDemo();
bs.acceptArray();
bs.sortArray();
System.out.println("\n");
}
}

Add a comment
Know the answer?
Add Answer to:
Write a complete Java program which has two methods, one to enter values for an array...
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
  • 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...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • Write a program that uses a two-dimensional array to store daily minutes walked and protein intake...

    Write a program that uses a two-dimensional array to store daily minutes walked and protein intake for a week. Prompt the user for 7 days of minutes exercise and protein intake; store in the array. Write a bubble sort to report out the sorted minutes walked values -lowest to highest. Write another to report out the sorted protein intake - highest to lowest. Your program should output all the values stored in the array and then output the 2 sorted...

  • Write a java program that will read the values for 3 matrices A, B, and C...

    Write a java program that will read the values for 3 matrices A, B, and C and store the result of their summation in matrix D. You need to use a method to read matrix values. Use another method to add the three 3x3 matrices (each is a 2 dimensional array with 3 rows and 3 columns). Finally, use a third method to print the value of the summation (matrix D). Write a test program that will cal the three...

  • In this lab, you complete a partially written Java program that includes methods that require multiple...

    In this lab, you complete a partially written Java program that includes methods that require multiple parameters (arguments). The program prompts the user for two numeric values. Both values should be passed to methods named calculateSum(), calculateDifference(), and calculateProduct(). The methods compute the sum of the two values, the difference between the two values, and the product of the two values. Each method should perform the appropriate computation and display the results. The source code file provided for this lab...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

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