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

im try starting coding about this but i just got error,
i use some if condition and still cant get through..
please help me.

Recommended Answers

All 5 Replies

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.

Oh,
Ok ill try this code. thanks

Member Avatar for coil

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.