#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Introduction to Programming, 7th ed., Y. Daniel Yiang, Chapter 21_5


    I need urgent help with two homeworks (Exercise 21_3 & 21_5 creating a separate test code for this code:

    public class Exercise21_5 {
    public static void main(String[] args) {
    Integer[][] numbers = { {1, 2, 3}, {4, 5, 6}
    };
    System.out.println(max(numbers));
    }

    public static<E extends Comparable<E>> E max(E[][] list) {
    E max = list[0][0];

    for (int i = 1; i < list.length; i++) {
    for (int j = 1; j < list[i].length; j++) {
    if (max.compareTo(list[i][j]) < 0) {
    max = list[i][j];
    }
    }
    }

    return max;
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Please edit the post and wrap the code in code tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    New to this site


    Originally Posted by NormR
    Please edit the post and wrap the code in code tags.
    Hi NormR, don't know how to do what you asking Me. However these are both codes:

    // First code

    public class Exercise21_5 {
    public static void main(String[] args) {
    Integer[][] numbers = { {1, 2, 3}, {4, 5, 6}
    };
    System.out.println(max(numbers));
    }

    public static<E extends Comparable<E>> E max(E[][] list) {
    E max = list[0][0];

    for (int i = 1; i < list.length; i++) {
    for (int j = 1; j < list[i].length; j++) {
    if (max.compareTo(list[i][j]) < 0) {
    max = list[i][j];
    }
    }
    }

    return max;
    }
    }

    //2nd code different from the first one. But each needs separate test code but i don't know how to put the exceptions in either code to run a seperate test for each.

    Second:

    public class SelectionSort {

    public static void main(String[] args){

    Integer[] list = {34, 17, 23, 35, 45, 9, 1};
    System.out.println("Original Array: ");
    printArray(list);

    selectionSort(list);
    System.out.println("\nSelection sort:");
    printArray(list);
    Float[] flist = {34.4f, 17.6f, 23.0f};
    selectionSort(list);
    }

    //selection sort
    public static <E extends Comparable<E>> void selectionSort(E[] list)
    {
    for(int i=0; i<list.length -1; i++)
    {
    int iSmallest = i;

    for(int j=i+1; j<list.length; j++)
    {
    if(list[iSmallest].compareTo((list[j])) > 0 )
    {
    iSmallest = j;
    }
    }
    E iSwap = list[iSmallest];
    list[iSmallest] = list[i];
    list[i] = iSwap;

    }
    }

    public static <E> void printArray(E[] list)
    {

    for(int i=0; i<list.length; i++)
    {
    System.out.print(list[i] + ", ");
    }
    }
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Please edit the post and wrap the code in code tags:
    [code] << before the code

    >>>>> the code here

    [/code] <<<< after the code

    It should look like:
    Code:
      int x = 3;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    You mean this? Won't let me edit, no edit option


    // First code

    [ public class Exercise21_5 {
    public static void main(String[] args) {
    Integer[][] numbers = { {1, 2, 3}, {4, 5, 6}
    };
    System.out.println(max(numbers));
    }

    public static<E extends Comparable<E>> E max(E[][] list) {
    E max = list[0][0];

    for (int i = 1; i < list.length; i++) {
    for (int j = 1; j < list[i].length; j++) {
    if (max.compareTo(list[i][j]) < 0) {
    max = list[i][j];
    }
    }
    }

    return max;
    }
    }
    /]


    //2nd code different from the first one. But each needs separate test code but i don't know how to put the exceptions in either code to run a seperate test for each.

    Second:

    [ public class SelectionSort {

    public static void main(String[] args){

    Integer[] list = {34, 17, 23, 35, 45, 9, 1};
    System.out.println("Original Array: ");
    printArray(list);

    selectionSort(list);
    System.out.println("\nSelection sort:");
    printArray(list);
    Float[] flist = {34.4f, 17.6f, 23.0f};
    selectionSort(list);
    }

    //selection sort
    public static <E extends Comparable<E>> void selectionSort(E[] list)
    {
    for(int i=0; i<list.length -1; i++)
    {
    int iSmallest = i;

    for(int j=i+1; j<list.length; j++)
    {
    if(list[iSmallest].compareTo((list[j])) > 0 )
    {
    iSmallest = j;
    }
    }
    E iSwap = list[iSmallest];
    list[iSmallest] = list[i];
    list[i] = iSwap;

    }
    }

    public static <E> void printArray(E[] list)
    {

    for(int i=0; i<list.length; i++)
    {
    System.out.print(list[i] + ", ");
    }
    }
    } /]
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Now edited, agree?


    // First code that was submitted

    [ public class Exercise21_5 {
    public static void main(String[] args) {
    Integer[][] numbers = { {1, 2, 3}, {4, 5, 6}
    };
    System.out.println(max(numbers));
    }

    public static<E extends Comparable<E>> E max(E[][] list) {
    E max = list[0][0];

    for (int i = 1; i < list.length; i++) {
    for (int j = 1; j < list[i].length; j++) {
    if (max.compareTo(list[i][j]) < 0) {
    max = list[i][j];
    }
    }
    }

    return max;
    }
    }
    /]


    //2nd code different from the first one. But each needs separate test code but i don't know how to put the exceptions in either code to run a seperate test for each.

    Second:

    [ public class SelectionSort {

    public static void main(String[] args){

    Integer[] list = {34, 17, 23, 35, 45, 9, 1};
    System.out.println("Original Array: ");
    printArray(list);

    selectionSort(list);
    System.out.println("\nSelection sort:");
    printArray(list);
    Float[] flist = {34.4f, 17.6f, 23.0f};
    selectionSort(list);
    }

    //selection sort
    public static <E extends Comparable<E>> void selectionSort(E[] list)
    {
    for(int i=0; i<list.length -1; i++)
    {
    int iSmallest = i;

    for(int j=i+1; j<list.length; j++)
    {
    if(list[iSmallest].compareTo((list[j])) > 0 )
    {
    iSmallest = j;
    }
    }
    E iSwap = list[iSmallest];
    list[iSmallest] = list[i];
    list[i] = iSwap;

    }
    }

    public static <E> void printArray(E[] list)
    {

    for(int i=0; i<list.length; i++)
    {
    System.out.print(list[i] + ", ");
    }
    }
    } /]

IMN logo majestic logo threadwatch logo seochat tools logo