Solved

Beginning Java Programming - Sorting Arrays

Posted on 2003-11-15
14
267 Views
Last Modified: 2010-03-31
I'm supposed to be writing a program that stores an array of video titles and their corresponding ID numbers (it needs to be 2 single dimentional arrays).  Then I need to display the list before it is sorted and then display a list sorted by inventory ID number.  I'm not understanding programming at all and need help!!  Here is what I have, any help would be greatly appreciated!
public class SortVideos
{
  public static void main (String[] args)
  {
String[] tapes = {"True Grit", "Enemy of the State", "Lizzie McGuire", "Armageddeon", "The Matrix", "The Embalmer", "Usual Suspects", "Big", "The Lord of the Rings", "Leaving LasVegas"};
      int x;
      System.out.println ("Before sort\n-----------");
      for (x = 0; x < 10; ++x)
            System.out.println(tapes[x]);
      sortVideos(tapes, tapes.length);
      System.out.println("\n\n\nAfter Sort\n----------");
      for (x = 0; x < 10; ++x)
            System.out.println(tapes[x]);                  
      }
            public class Videos
      {
      private int videoNum;
      private char videoName;
      public Videos(char name, int num)
      {
            videoNum = num;
            videoName = name;
      }
      public int getVideoNum()
      {
            return videoNum;
      }
      public void setVideoNum(int id)
      {
            videoNum = id;
      }
      public char getVideoName()
      {
            return videoName;
      }
      public void setVideoName(char r)
      {
            videoName = r;
      }
public static void bubbleSort(Videos[] array, int len)
{
      int a,b;
      Videos temp;
      int highSubscript = len - 1;
      for(a = 0; a < highSubscript; ++a)
        for(b = 0; b < highSubscript; ++b)
          if(array[b].getVideoNum.compareTo(array[b + 1].getVideoNum) >0)
            {
            temp = array[b];
            array[b] = array[b+1];
            array[b + 1] = temp;
                      }
}
}
0
Comment
Question by:mommakitty3
  • 5
  • 3
  • 3
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9754239
Can you post your assignment question *verbatim*?
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9754275
You start by saying '2 single dimensional arrays" for storing the 2 pieces of info there is for a given video ( id and title ); that would be one array for all the id values and another for all the titles.

But then in you sample code you make the Videos class of objects with both pieces of info in them. So there would be only a single Videos[] array with all the information.

That is a conflict we need to have resolved.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9754292
If they have not explicitly told you so (and knowing what the people who set these assignments are like I wouldn't be surprised if they have) they should NOT be in separate arrays.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mommakitty3
ID: 9754440
This is what the assignment says:
Write a program that stores an array of video titles and their corresponding ID numbers in inventory.  Display the list before it is sorted, and then display a list sorted by inventory ID number.  Use two single-dimensional arrays - one for the titles and one for the inventory ID numbers.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9754468
LOL. I wonder if they store their CDs and the CD notes in separate drawers at home. Just keep the parallel arrays, do your bubble sort but in the sort, operate on both arrays simultaneously
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 9754480
Sorry - I meant videos ;-)

String[] tapes = {"True Grit", "Enemy of the State", "Lizzie McGuire", "Armageddeon", "The Matrix", "The Embalmer", "Usual Suspects", "Big", "The Lord of the Rings", "Leaving LasVegas"};

int ids = { 1545, 1287, 7864 ...}

....


You ought to do this for safety:

if (ids.length != tapes.length) {
     // display an error message and exit
}
0
 

Author Comment

by:mommakitty3
ID: 9754600
Ok, now when I compile I'm getting the following error and I'm not sure what it means:

     A:\SortVideos.java:11: cannot resolve symbol
    symbol  : method SortVideos  (java.lang.String[],int)
    location: class SortVideos
          SortVideos(tapes, tapes.length);
            ^
 
0
 
LVL 15

Assisted Solution

by:JakobA
JakobA earned 250 total points
ID: 9754614
Maybe it is a task to set the studens up for later saying "But now see what happens if we use objecoriented thinking in our programming." :-))

Anyway you can be quite 'objectoriented' without using objects. Do it with methods. a method for mos every action you want to perform on those 2 arrays. such as:

void swapTwoCells( int index1, int index2)
      int  tempID = arrayOfId[ index1 ];
      String tempTitle = arrayOfTitle[ index1 ];
      arrayOfId[ index1 ] = arrayOfId[ index2 ];
      arrayOfTitle[ index1 ] = arrayOfTitle[ index2 ];
      arrayOfTitle[ index2 ] = tempTitle;
      arrayOfId[ index2 ] = tempId;
}

With such a method you handle the two walues together as if they were an object instead of separate.

regards JakobA
0
 

Author Comment

by:mommakitty3
ID: 9754808
Thanks to both of you, but I guess this is just not my thing..... I still can't get it to work, I'm awful at programming, and I'm completely frustrated so I'm giving up for today.

0
 
LVL 15

Expert Comment

by:JakobA
ID: 9754842
Yeah. and you are a woman and therefore more spiritual than tecnical.
Phoey. That is just excuses.
Get on with it, and show the attempts you make.
Then we can tell you the why's of it not working so you can fix them (and that is not because we are 'smarter', it's because we have done it a hundred times before (including most any error you may make) :-))

regards JakobA


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9754972
>>Maybe it is a task to set the studens

It would be nice to think so, but the explanation is probably simpler and much more unfortunate ;-)

Not too much code J - for obvious reasons 8-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 53
Java Restore security prompts not working 10 131
Is there a simpler dropbox system? 10 44
wild fly 8 startup error 2 40
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question