Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Beginning Java Programming - Sorting Arrays

Posted on 2003-11-15
14
265 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
reverse digits of a number using for loop 5 52
table example 4 32
throw exception 21 58
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

829 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