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.
LevelSix
Junior Poster in Training
59 posts since May 2008
Reputation Points: 22
Solved Threads: 3
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;
}
}
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
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.
LevelSix
Junior Poster in Training
59 posts since May 2008
Reputation Points: 22
Solved Threads: 3
hollywood - if you need more help, look at the example I provided.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
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){}
}
- printVideoList() - uses just simple loop mechanism to print the list
- printByTitle() - need to be first sorted by title and then printed out
- 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...
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
@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.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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 meWrite 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).
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
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
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
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.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
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
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448