can you guys help me with my assignment given to me,

my prof. said ..

create a java class that will accept four Integers
& develop a method that will arrange the accepted integer from highest to lowest.

example:
if you enter random number like: 10 6 7 9

the output is:
10
9
7
6

i use some if condition and still cant get through..

5
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Eric Cute

Post what code you have and we can help you determine where you are going wrong

Well... At DaniWeb, we're not supposed to help you with homework directly, so here's some psuedocode for you to implement in Java:

``````Set up a console reader that will take input (Hint:  java.util.Scanner)
Split the input String at the character space into an array with size 4 (Hint:  String.split(" "))
Convert each String in the array to an int and fill a new array of ints with it (Hint: Int.parseInt(), use an ArayList)
Perform a sorting algorithm like the one below: (Hint: Make a method called swap() that swaps the values of two indices in an array or ArrayList)
While((int #1 < int #2) or (int #2 < int #3) or (int #3 < int #4)) {
if(int #1 < int #2) {
swap(1, 2)
}
if(int #2 < int #3) {
swap(2, 3)
}
if(int #3 < int #4) {
swap(3, 4)
}
}

Print the sorted int array``````

That would work.

Edited by Awesomeness: n/a

Oh,
Ok ill try this code. thanks

That algorithm actually only works for 4 integers, which is alright for this assignment, but you may want to consider writing an algorithm that works for any number of integers.

There are many such algorithms out there, but a simple one is bubble sort.

``````create array of int and get input

do this (length of array - 1) times
for int i from 0 to (length of array - 1)
if int at index i is less than int at index (i + 1)
swap the two
``````

Edited by mike_2000_17: Fixed formatting

Are you allowed to use arrays??

If so then arranging the numbers from lowest to highest will just be

``Arrays.sort(arrayName);``

then doing a for loop from the end of the arrays and outputting the values will give you your expected result.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.