The Java assignment is to Prompt user for ten integers which are stored in an array, call a method passing the array and the method will display the values in both the original and reverse order.
public class Module7 {
public static void main(String[] args) {
// prompt the user
System.out.println("Welcome to **yourNameHere**\'s 3 in 1 Programming Assignment (Arrays) for Module 7\n");
// create Scanner Object
Scanner input = new Scanner(System.in);
// loop until break
while(true){
// new lines for readability
System.out.print("\n");
// Prompt user for choices
System.out.print("\nEnter 1 Random Array \nEnter 2 Reverse Numbers\nEnter 3 Identical Arrays\nAny other input will exit");
int choice = input.nextInt();
// Consume newline left-over
input.nextLine();
// first feature
if(choice == 1){
System.out.println("You Selected Random Array");
// call the method of feature 1
feature1();
}
else if(choice == 2){
// Prompt user
System.out.println("You Selected Reverse Numbers\n");
feature2();
}
else if(choice == 3){
// Prompt user
System.out.println("You Selected Identical Arrays !");
// call the method of feature 3 providing the message, return value is the Letters message
feature3();
}
else{
// not 1 , 2 or 3
System.out.println("User didn't select 1,2 or 3. Exiting Program\n");
// release object
input.close();
// exit while with break statement
break;
}
} // end of while
} // end main
// create feature1() method here no problems here, this one works
private static void feature1(){
int[] randomArray = new int[100];
for (int i = 0; i< randomArray.length; i++)
randomArray[i] = (int) (Math.random()*100);
for (int i=0; i<randomArray.length; i++) {
double currentMin = randomArray[i];
int currentMinIndex = i;
//not needed for just sorting
for (int j=i+1; j<randomArray.length;j++) {
if (currentMin > randomArray[j]) {
currentMin = randomArray[j];
currentMinIndex = j;
}
}
//swap list[i] with list[currentMinIndex] if necessary
if (currentMinIndex !=i) {
randomArray[currentMinIndex] = randomArray[i];
randomArray[i] = (int) currentMin;
}
}
for (int i=0; i<randomArray.length; i++) {
// print randomArray values
if (i % 10 == 0 && i > 0) {
System.out.println();
}
System.out.print(randomArray[i] + " ");
}
}
//create feature2() method here I can't seem to get it to look like the example here. I can't get the numbers to be able to be entered one at a time with a space between, then show them reversed. It only works if you enter all of 10 numbers consecutively with no spaces.
private static void feature2(){
System.out.print("Please enter 10 numbers ");
java.util.Scanner input = new java.util.Scanner(System.in);
double[] numbers = new double[10];
System.out.print("\nEntered: ");
for (int i = 0; i < numbers.length; i++) {
int number = input.nextInt();
System.out.print("Reversed: ");
reverse(number);
}
}
public static void reverse(int number) {
while (number !=0) {
int remainder = number % 10;
System.out.print(remainder + " ");
number = number/10;
}
}
//create feature3() here. This is where my major problem is. I am terrible with arrays and don't even really know where to begin
private static void feature3()
`The Java assignment is to Prompt user for ten integers which are stored in an array, call a method passing the array and the method will display the values in both the original and reverse order.
I have tried every which way I know (from reading my class book and watching the lecture videos...) but I can not figure out how to write this code. My output is supposed to look like below:
(EDIT) Sorry, will post what I have as soon as I get back to my home computer...this IS homework by the way. I am just terrible with arrays and understanding them. Is there a site that teaches "arrays for dummies"?