Medication of ArrayDemo.java to include iterative menu
Activity 1-1
Modify ArrayDemo.java to include this iterative menu:
[1] Display contents of the array using for loop (by index, ascending)
[2] Display contents of the array using do loop (by index, ascending)
[3] Display contents of the array using do while loop (by index, ascending)
[4] Display contents of the array using value (by index, descending)
[5] Exit
Code :
class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declares an array of integers
anArray = new int[10]; // allocates memory for 10 integers
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);
}
}
Re: another activity that i need some guidance
can someone help me or assist me please.....^:)^
Re: another activity that i need some guidance
Quote:
Originally Posted by
xyldon27
can someone help me or assist me please.....^:)^
okay, just wait for my code.
Cheers,
Truffy
Re: another activity that i need some guidance
thanks.. i really need it.. because my heads hurts today because of the heat of the sun.. but still im trying to figure it out..
Re: another activity that i need some guidance
Quote:
Originally Posted by
xyldon27
thanks.. i really need it.. because my heads hurts today because of the heat of the sun.. but still im trying to figure it out..
Hi xyldon27,
Here's the code for you.
Hope it helps.
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @authorTruffy
*/
class ArrayDemo {
public void forsortIntArray() {
int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
Arrays.sort(arrayToSort);
for (int i = 0; i < arrayToSort.length; i++)
System.out.println(arrayToSort[i]);
}
public void doLoopsortIntArray() {
int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
Arrays.sort(arrayToSort);
int loopvar = 0; // Declare and initialize the loop counter
do {
System.out.println(arrayToSort[loopvar]); // Print the variable
loopvar = loopvar + 1; // Increment the loop counter
} while (loopvar < 9); // Test and loop
}
public void whileLoopsortIntArray() {
int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
Arrays.sort(arrayToSort);
int loopvar = 0; // Declare and initialize the loop counter
while (loopvar < 9){
System.out.println(arrayToSort[loopvar]);
loopvar = loopvar + 1;
}
}
public void sortIntArray() {
int[] intArray = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
// Arrays.sort(intArray);
// translate int array over to an Integer Array
Integer[] integerArray = new Integer[intArray.length];
for (int i = 0; i < intArray.length; i++)
{
integerArray[i] = new Integer(intArray[i]);
}
// sort with an anonymous Comparator object
Arrays.sort(integerArray, new Comparator<Integer>()
{
public int compare(Integer o1, Integer o2)
{
return o2.intValue() - o1.intValue();
}
});
System.out.println(Arrays.toString(integerArray));
}
public static void main(String[] args) {
ArrayDemo arr = new ArrayDemo();
System.out.println("[1] Display contents of the array using for loop (by index, ascending)");
System.out.println("[2] Display contents of the array using do loop (by index, ascending)");
System.out.println("[3] Display contents of the array using while loop (by index, ascending)");
System.out.println("[4] Display contents of the array using value (by index, descending)");
System.out.println("[5] Exit\n");
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String input = sc.next();
if(input.equals("5")){
System.out.println("Bye! Bye!");
System.exit(0);
}else if(input.equals("1")){
arr.forsortIntArray();
}else if(input.equals("2")){
arr.doLoopsortIntArray();
}else if(input.equals("3")){
arr.whileLoopsortIntArray();
}else if(input.equals("4")){
arr.sortIntArray();
}
System.out.println("Enter cmd: ");
}
}
}
Quote:
output
compile-single:
run-single:
[1] Display contents of the array using for loop (by index, ascending)
[2] Display contents of the array using do loop (by index, ascending)
[3] Display contents of the array using while loop (by index, ascending)
[4] Display contents of the array using value (by index, descending)
[5] Exit
1
100
200
300
400
500
600
700
800
900
Enter cmd:
2
100
200
300
400
500
600
700
800
900
Enter cmd:
3
100
200
300
400
500
600
700
800
900
Enter cmd:
4
[900, 800, 700, 600, 500, 400, 300, 200, 100]
Enter cmd:
5
Bye! Bye!
Cheers,
Truffy. 8->
Re: another activity that i need some guidance
I'LL TEST IT RIGHT AWAY ^_^ THANK YOU SO MUCH
Re: another activity that i need some guidance
Quote:
Originally Posted by
xyldon27
I'LL TEST IT RIGHT AWAY ^_^ THANK YOU SO MUCH
if you are satisfied and that is correct, please mark as solved.
Cheers,
Truffy. :P
Re: another activity that i need some guidance
it work thank u so much.. cheers for Truffy
Re: another activity that i need some guidance
Quote:
Originally Posted by
xyldon27
it work thank u so much.. cheers for Truffy
please mark it solved then. :)