Solved

Beginning Java Programming - Sorting Arrays

Posted on 2003-11-15
14
259 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now