Here is my assignment:

Write a class named 'Video' that has two attributes, a title (type String) and rating (type int). Then write an application class named 'VideoStore' that first creates an array consisting of 5 video objects, then display the list three times; first unsorted, then sorted by title and finally sorted by rating.

I understand an array, i understand how to make an array, and i understand how to display the array. My problem is that i don't understand how to combine the two arrays to create this list that my instructor is looking for. I am not looking for a straight answer, but if there is any resources that you can post or maybe a dummy code that i can use for reference that would be great. Thanks everyone

Recommended Answers

All 19 Replies

What it appears you need to do is simply create one array, of type Video. Video will be a class you have to write, simple enough: Constructor with the two parameters and then return methods.

Once this class and the arrays are constructed you can construct new Video objects and load them into an array.

What it appears you need to do is simply create one array, of type Video. Video will be a class you have to write, simple enough: Constructor with the two parameters and then return methods.

Once this class and the arrays are constructed you can construct new Video objects and load them into an array.

That is the problem that i am having. I am not understanding those parts of it. I am very new and the instructor isnt very helpful. And when you say to have the constructor with two paramerters how would i do that?

Thanks again for the help!

Your class is named Video. The Video class has a String and an int as data members. Your other class is VideoStore. It has an array of videos as its only data member. If you know how to create an array, then you should have no problems.

Video[] theVideos = new Video[howeverManyVideosYouHave];

If you wanted to make a class called Car, it might look like this

public class Car{
//The data members
public String color;
public int weight;

//The constructor
public Car(String theColor, int theWeight){
color = theColor;
weight = theWeight;
}
}

Now, consider a 'Car Dealership' where the Car Dealership typically has a lot of Cars...

public class CarDealership{
Car[] theCars;

public CarDealership(Car[] cars){
theCars = cars;
}
}
commented: Good examples +12

Constructor for class video would appear as:
public Video(String name, int rating)
{
///store name and rating to instance fields
}
Then you would have methods such as:
public String getVideoName()
{
return //instance field of video name
}

Instance fields if you're unaware are just global variables:
private String videoName //for example.

I will post what i have in just a little bit. I think I have an idea of what i need to do. Maybe when i post what i have it will help with what I am doing wrong.

Thanks everyone for the help

LevelSix-

Thank you. I will post something within the hour. If i have problems...well i will be back

Thank you again!

hollywood - if you need more help, look at the example I provided.

There is only one array involved that you need to use. Video class represent an object that has to be stored in array.
I'm not gone do Video class for you as that is simple and judging by assignment topic you should be familiar with it. So assuming you already created your Video class this is skeloton of what you supposed to do

public class VideoTest {

    public static void main(String[] args){
    	Video[] video = new Video[5];
    	video[0] = new Video("Fast and Furious", 1);
    	video[1] = new Video("Save private Rayne", 5);
    	
    	printVideoList(video);
    	printByTitle(video);
    	printByRating(video);
    }
    
    private static void printVideoList(Video[] video){}
    
    private static void printByTitle(Video[] video){}
    
    private static void printByRating(Video[] video){}
}
  1. printVideoList() - uses just simple loop mechanism to print the list
  2. printByTitle() - need to be first sorted by title and then printed out
  3. printByRating() - need to be first sorted by rating and then printed out

Two things, if you clever you will reuse printVideoList() to help you out after sorting. Two, few days ago I made a post in regards of simple sorting that can be reused too...

commented: Good examples +12

Alright...maybe i don't know as much as I claim or think :(. I am still having major problems. I have got the video class done (I think....I'll post what i have), now i am having issues with the VideoStore class...
I think i am going to become a seamstress....

Here is what i have so far. Maybe I am way off.

public class Video {

    String[] movieTitle = new String[5];
    int[] movieRating = new int[5];

    public Video(){

    }


    public Video(String[] j_movieTitle, int[] j_movieRating){

        movieTitle = j_movieTitle;
        movieRating = j_movieRating;
    }

    public void setMovie(String[] j_movieTitle){
        movieTitle = j_movieTitle;
    }

    public String[] getMovie() {
        return(movieTitle);
    }

    public void setRating(int[] j_movieRating) {
        movieRating = j_movieRating;
    }

    public int[] getRating() {
        return(movieRating);
    }

}

You do not have a main method, so how do you expect your code to do anything? Write your main method, following Peter's advice, then get back to us

@hollywoood69 - as stated before you do not need two arrays. Create Video class that way so it can store two parameters TITLE and RATING (you already have example of this in BestJewSinceJC post, that is number 4). Than in your application create array that will hold 5 Video objects as its elements. Develop 3 "workers" methods as I already showed you and that is.

I spoke with the instructor and he said he wants the two arrays in the code. (I told him what i thought about the 1 array, but he killed that idea). I have started to figure it out. I needed to sort it using a bubble sort method. i figured out the first part and that was the ratings, but am having an issues sorting the titles. I did start over so my code is different from the first one i posted earlier but here is what i have thus far, any more help will be great.

public class VideoTitleRating {
    
    public VideoTitleRating() {
    }
    
    
    public void sortRatingArray (int [] pIntArr) {
        
		int	rating;
		int	temp = -1;
		
		for (int a = 0; a < pIntArr.length - 1; a++){
            
			for (rating = 0; rating < pIntArr.length - 1; rating++) {
                
				if (pIntArr [rating] > pIntArr [rating + 1]) {
                    
					temp = pIntArr [rating];
					pIntArr [rating] = pIntArr [rating + 1];
					pIntArr [rating + 1] = temp;
				}
			}
		}
	}

	public void displayArray (int [] pIntArray)
	{
		for (int rating = 0; rating < pIntArray.length; rating++)
		{
			System.out.println ("Array[" + rating + "]: " + pIntArray [rating]);
		}
	}


    public static void main(String[] args) {

        String [] videoTitle = {"Terminator 3", "Clerks 2", "Wedding Crashers",
        "Mallrats", "My Best Friends Wedding" };
        int[] videoRating = {3, 5, 2, 5, 4};


        BubbleSort sortIntArray = new BubbleSort ();
		System.out.println ("Before Sorting");
		sortIntArray.displayArray (videoRating);

		sortIntArray.sortIntArray (videoRating);

        System.out.println ("\nAfter Sorting");
		sortIntArray.displayArray (videoRating);
		System.exit (0);
    }

}

You have a class called VideoTitleRating, containing a method called displayArray. You create an object of type BubbleSort and then call its displayArray function. Are you intending to call VideoTitleRatings's displayArray function instead?

Is there a reason you have a BubbleSort class? How about making a BubbleSort method within your VideoTitleRating class? If your professor is demanding two arrays instead of one array, has he/she told you why and what those two arrays are supposed to be? You have an array of titles and array of ratings. People are suggesting you make a class that has a rating and the title as data members, but I guess if you're not allowed to do that, then you're not allowed to do that.

You have a function called sortRating array that is never called, as far as I can tell, and a BubbleSort class that you haven't posted. Please post everything so we can run it.

I spoke with the instructor and he said he wants the two arrays in the code. (I told him what i thought about the 1 array, but he killed that idea).

Either you are lying and trying to have it your way, because bellow assignment is clear to me

Write a class named 'Video' that has two attributes, a title (type String) and rating (type int). Then write an application class named 'VideoStore' that first creates an array consisting of 5 video objects, then display the list three times; first unsorted, then sorted by title and finally sorted by rating.

or that teacher doesn't know what he/she is doing (unlikely, but still possible)

Good luck with sorting because with your simple arrays you will need extra coding to manage the link between movie title and the rating of that movie title (if you actually decide to do it, because your code from post above says otherwise).

A small tutorial with examples about objects in arrays

Video video = new Video(); // ONE instance of Video

Video [] videos = new Video[5]; // this is an array

// videos is an array and should be treated as an array

// videos[0] is a Video but it is null because you haven't initialize it

videos[0] = new Video();
videos[0].setTitle("Some Title");

So your method will take as argument an array of Videos, you will iterate, get the title from each video and do the sorting

I spoke with the instructor and he said he wants the two arrays in the code. (I told him what i thought about the 1 array, but he killed that idea)

Probably the instructor means that in 1 array will have the list with the videos, and at the other array you will have the sorted one

Your project description makes it clear that you are supposed to make a class to contain the information, then have an array of that class type in your Video Store. I don't know why your professor would disagree with his own project description, either way, I'm done with this now.

Hey Guys. Thank you all for all the guidance and help. I think i have the most of this figured out. The only issue that i am having is sorting with the bubble sort i created to sort the title alphabetically. Any last help would be great. Again thank you all for the support and help thus far.

public class VideoTitleRating {
    
    public VideoTitleRating() {
    }

    public void sortRatingArray (int [] ratingArry, String[] videoArry) {

		String tmpTitle;
        int	rating;
		int	temp = 1;
		
		for (int a = 0; a < ratingArry.length - 1; a++){
			for (rating = 0; rating < ratingArry.length - 1; rating++) {
                if (ratingArry [rating] < ratingArry [rating + 1]) {
					temp = ratingArry [rating];
					ratingArry [rating] = ratingArry [rating + 1];
					ratingArry [rating + 1] = temp;
				}
			}
		}

        for(int i = 0; i < videoArry.length; i++) {
            for(int j = 0; j < videoArry.length -1 -i; j++) {
                if(videoArry[j].compareTo(videoArry[j + i]) > 0) {
                    tmpTitle = videoArry[j];
                    videoArry[j] = videoArry[j + 1];
                    videoArry[j+1] = tmpTitle;
                }
            }
        }
	}

    public void displayArray (int [] ratingArry, String[] videoArry){

		for (int rating = 0; rating < ratingArry.length; rating++){
			System.out.println (ratingArry [rating] + " - " + videoArry[rating]);
		}
        for(int tmpTitle = 0; tmpTitle < videoArry.length; tmpTitle++) {
            System.out.println (ratingArry [tmpTitle] + " - " + videoArry[tmpTitle]);
        }
	}


    public static void main(String[] args) {

        String [] videoTitle = {"Terminator 3", "Clerks 2", "Wedding Crashers",
        "Mallrats", "My Best Friends Girl" };
        int[] videoRating = {3, 5, 2, 5, 4};


        VideoTitleRating sortIntArray = new VideoTitleRating ();
		System.out.println ("Video Rating and Movie Title Unsorted");
		sortIntArray.displayArray (videoRating, videoTitle);

		sortIntArray.sortRatingArray (videoRating, videoTitle);

        System.out.println ("\nVideo Rating and Movie Title Sorted by Rating");
		sortIntArray.displayArray (videoRating, videoTitle);


        System.out.println("\nVideo Rating and Movie Title Sorted by Title");
        sortIntArray.displayArray(videoRating, videoTitle);
        System.exit (0);

    }

}

Actually the code that does the sorting is wrong and it would be easier to use objects to store the videos and do the sorting once you hear why your code is wrong.

The 2 array are not indepedent, the 1st element of the title array is linked with the 1st element of the rating array:
title: {"Terminator", "Vendetta"}
rating: {5, 4}

"Terminator" got a 5 and "Vendetta" got a 4. But when you sort them the arrays will look like this:
title: {"Terminator", "Vendetta"}
rating: {4, 5}

So "Terminator" will end up with a 4

If you are to sort them your way, you will need 2 methods to do the sorting.
One will sort based on title but when you swap the elements of the title array you will need to swap the elements of the rating array based on the title.
And the other method will sort based on the rating and again when you swap the elements of one array, equivalant swapping must take place for the other array

If you have array of objects and do the sorting by rating, you will do something like this:

videos[i].getRating() < videos[i + 1].getRating()

And the swapping will swap the object which has the rating AND the title that is linked with that rating

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.