Solved

Beginning Java Programming - Sorting Arrays

Posted on 2003-11-15
14
268 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
[X]
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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 76
Notify sent to other threads in Java 9 54
Java class and jar 3 61
Read CLOB data from Oracle using JAVA 3 43
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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

738 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