Java Homework Help - Defining and using primitive arrays

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
So I submitted my lab and the professor sent it back. His comments have me confused. My submission and his comments are in the 2nd post. Any help would be greatly appreciated.
Here is the original lab requirements:

Lab 21
Defining and using primitive arrays


1. This program "bombs" at runtime. It's SUPPOSED to use a for loop to display the values of its array elements in reverse order as follows:
u
o
i
e
a
See if you can fix it.

Code:
public class Lab21A {
  public static void main(String[] args) {
    char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    for (int i = 5; i >= 1; i--)
      System.out.println(vowels[i]);
  }
}



2. See if you can fill-in the blanks to make this program assign a value of 1 to the first array element, 2 to the second array element, etc.. NOTE: Your code must be able to handle an array of ANY size so be sure to test your answer with an array having a different number of elements to see if it still works.

Code:
public class Lab21B {
  public static void main(String[] args) {
    int[] values = new int[10];
    for (int i = 0; _______________; i++) {
      _____________ = _________;
      System.out.println("Element " + i + " = " + values[i]);
    }
  }
}


3. See if you can supply the missing code to make this program "swap" the values of the first and last array elements. If done correctly, the program's output will be as follows:

d
b
c
a

NOTE: Be flexible in your code so that it can handle an array of ANY size, not just a four element array.

Code:
public class Lab21C {
  public static void main(String[] args) {
    char[] table = {'a', 'b', 'c', 'd'};

    // YOUR CODE GOES HERE

    for (int i = 0; i < table.length; i++) {
      System.out.println(table[i]);
    }
  }
}
 
Last edited:

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
My Submission:

Lab21A
Code:
public class Lab21A {
  public static void main(String[] args) {
    char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    for (int i = 4; i >= 0; i--)
      System.out.println(vowels[i]);
  }
}


Lab21B
Code:
public class Lab21B {
  public static void main(String[] args) {
    int[] values = new int[10];
    for (int i = 0; i < values.length; i++) {
      values[i] = ((i) + 1);
      
      System.out.println("Element " + i + " = " + values[i]);
    }
  }
}

Lab21C
Code:
public class Lab21C {
  public static void main(String[] args) {
    char[] table = {'a', 'b', 'c', 'd'};
    char temp = table[0];
    table[0] = table[3];
    table[3] = temp;

    for (int i = 0; i < table.length; i++) {
      System.out.println(table[i]);
    }
  }
}


Professor's Comments:

Prof said:
"in c you can't hard code things
in a table x
x[0] is the first
x[x.length-1] is the last
you must save one first before swaping"

Prof said:
"You still need to fix c or you will get a lower grade"


Arik said:
"I am kind of confused about what to do. You first commented:

in c you can't hard code things
in a table x
x[0] is the first
x[x.length-1] is the last
you must save one first before swaping
and I thought I fixed C and then you said:
You still need to fix c or you will get a lower grade

Did you mean I have to fix something with A? Is C ok?
Thanks for your time.
-Arik"



Prof said:
"instead of using the word table I use x for the table name? Does this help?"
 
Last edited:

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Your solution for 'c' isn't flexible enough to work for any size array, which is what he doesn't like. He pretty much gave you the solution though.
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
Your solution for 'c' isn't flexible enough to work for any size array, which is what he doesn't like. He pretty much gave you the solution though.

ok, did you find anything wrong with A? from his comments it made me kind of think there was something wrong with both A and C....

I'm pretty sure there is nothing wrong with Lab21A, I think he meant (in "a table x") and not ("in A", table x) (as if referring to Lab21A).
 
Last edited:

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
Your solution for 'c' isn't flexible enough to work for any size array, which is what he doesn't like. He pretty much gave you the solution though.

How about this? Is this worthy of resubmission? It compiles and does what it's suppose to....

Code:
public class Lab21C {
  public static void main(String[] args) {
    char[] table = {'a', 'b', 'c', 'd'};

    char x = table[0];
    table[0] = table[table.length - 1];
    table[table.length - 1] = x;
    
    for (int i = 0; i < table.length; i++) {
      System.out.println(table[i]);
    }
  }
}
 

Schmide

Diamond Member
Mar 7, 2002
5,726
1,014
126
Why not have your loop execute like so?

Code:
    for (int i = values.length-1; i>=0 ; i--)
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
Why not have your loop execute like so?

Code:
    for (int i = values.length-1; i>=0 ; i--)

If you are referring to B, it's because we are suppose to only fill in the blanks and not change anything else.....