Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Beginning Java Programming - Sorting Arrays

Posted on 2003-11-15
14
Medium Priority
?
280 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

926 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