Results 1 to 5 of 5
Thread: Generic Array
 04202014, 12:39 AM #1Member
 Join Date
 Apr 2014
 Posts
 5
 Rep Power
 0
Generic Array
Hi I was recently given an assignment to work on which is as follows,
Java Code:class MyDataStructure implements Comparable<MyDataStructure> { // your data public int compareTo(MyDataStructure other) { } } After define the data structure, you can sort a List of your types using Collections.sort: List<MyDataStructure> list = ... ... Collections.sort( list ); Homework Doing homework often makes students understand knowledge deeply. As a student of UESTC, WCM usually has much homework to do. Every day he gets a set of problems from teachers. Problem i will take ti time to complete. Given a schedule (i.e., an ordering of the problems), let Ci denote the finishing time of problem i. For example, if problem j is the first to be done, we would have Cj = tj . Each problem i also has a given weight wi that represents its importance to the student's mastering of the related knowledge. He wants to order the problems to minimize the weighted sum of the completion times, namely w1C1 + w2C2 + w3C3… + wnCn. You should design an efficient algorithm to solve this problem. That is, you are given a set of n problems with a processing time ti and a weight wi for each problem. You want to order the problems so as to minimize the weighted sum of the completion times, Σ(i=1..n)wiCi. Example: Suppose there are two problems: the first takes time t1=2 and has weight w1=12, while the second problem takes time t2=3 and has weight w2=4. Then doing problem 1 first would yield a weighted completion time of 12*2+4*5=44, while doing the second problem first would yield a larger weighted completion time of 4*3+12*5=72. 44 is the minimum of the weighted sum of completion times, Σ(i=1..n)wiCi. Input The input contains an integer T on the first line, which indicates the number of test cases. Each test case consists of three lines. The first line contains one integer n,(0 < n ≤ 1000),which means the number of the problems. The second line contains n numbers, t1,t2,…,tn,(0 < ti ≤ 20), ti means the time problem i would take. The third line contains n numbers, w1,w2,…wn,(0 < wi ≤ 20), wi means the weight of problem i. Output For each test case output one line containing a single integer, which represents the minimum of the weighted sum of the completion times, Σ(i=1..n)wiCi. Sample Input 1 2 2 3 12 4 Sample Output 44
Java Code:import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Homework { static class MyDataStructure implements Comparable<MyDataStructure> { static List<Homework> time = new ArrayList<Homework>(); static List<Homework> weight = new ArrayList<Homework>(); public static void main(String[] args) { // TODO Autogenerated method stub int testcases = 0; Scanner kb = new Scanner(System.in); testcases = kb.nextInt(); for(int i = 0; i < testcases; i++) { int numberofassinments = 0; numberofassinments = kb.nextInt(); for(int j = 0; j < numberofassinments; j++) { Homework n = kb.nextInt(); time.add(n); System.out.print(time); } for(int j = 0; j < numberofassinments; j++) { Homework n = kb. weight.add(n); System.out.print(weight); } } } @Override public int compareTo(MyDataStructure arg0) { // TODO Autogenerated method stub return 0; } List<MyDataStructure> list = new ArrayList<MyDataStructure>(); Scanner kb = new Scanner(System.in); } }
 04202014, 12:54 AM #2
Re: Generic Array
kb.next(); doesn't seem to work.
How does the user know to enter data from the keyboard and what kind of data?If you don't understand my response, don't ignore it, ask a question.
 04202014, 01:07 AM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 15
Re: Generic Array
First, your code has lots of problems. And the instructions are not only unclear but incomplete (they don't wrap around). That makes it hard for anyone to help you. Please repost the assignment.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04202014, 02:13 AM #4Member
 Join Date
 Apr 2014
 Posts
 5
 Rep Power
 0
Re: Generic Array
HI Jim,
Thanks for your reply, below is the assignment
The purpose of this project is to make you familiar with Comparable interface. There is only one method to implement in this interface called compareTo(T o). T is the generic type that could be any class in java. When you define a data structure in java, you can provide this interface by adding implements clause to the end of class definition
class MyDataStructure implements Comparable<MyDataStructure> {
// your data
public int compareTo(MyDataStructure other) {
}
}
After define the data structure, you can sort a List of your types using Collections.sort:
List<MyDataStructure> list = ...
...
Collections.sort( list );
Homework
Doing homework often makes students understand knowledge deeply. As a student of UESTC, WCM usually has much homework to do. Every day he gets a set of problems from teachers. Problem i will take ti time to complete. Given a schedule (i.e., an ordering of the problems), let Ci denote the finishing time of problem i. For example, if problem j is the first to be done, we would have Cj = tj . Each problem i also has a given weight wi that represents its importance to the student's mastering of the related knowledge. He wants to order the problems to minimize the weighted sum of the completion times, namely w1C1 + w2C2 + w3C3… + wnCn.
You should design an efficient algorithm to solve this problem. That is, you are given a set of n problems with a processing time ti and a weight wi for each problem. You want to order the problems so as to minimize the weighted sum of the completion times, Σ(i=1..n)wiCi.
Example: Suppose there are two problems: the first takes time t1=2 and has weight w1=12, while the second problem takes time t2=3 and has weight w2=4. Then doing problem 1 first would yield a weighted completion time of 12*2+4*5=44, while doing the second problem first would yield a larger weighted completion time of 4*3+12*5=72. 44 is the minimum of the weighted sum of completion times, Σ(i=1..n)wiCi.
Input
The input contains an integer T on the first line, which indicates the number of test cases. Each test case consists of three lines. The first line contains one integer n,(0 < n ≤ 1000),which means the number of the problems. The second line contains n numbers, t1,t2,…,tn,(0 < ti ≤ 20), ti means the time problem i would take. The third line contains n numbers, w1,w2,…wn,(0 < wi ≤ 20), wi means the weight of problem i.
Output
For each test case output one line containing a single integer, which represents the minimum of the weighted sum of the completion times, Σ(i=1..n)wiCi.
Sample Input
1
2
2 3
12 4
Sample Output
44
Sample Input 2
2
3
4 2 1
7 8 2
5
7 2 1 9 12
5 5 4 1 2
Sample Output 2
71
144
[/CODE]
 04202014, 07:14 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 15
Re: Generic Array
Well, for you generic array you need to first decide what values will be stored in each the MyDataStructure class. You then need to implement you Comparable<T> interface. Once your class is created you can then create a List like the following:
List<MyDataStructure> myList = new ArrayList<>();
Read about the List<T> and Comparable<T> interface in the JDK API.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Creating two typed generic array
By pyler in forum New To JavaReplies: 6Last Post: 12022013, 06:26 PM 
help with implementing Generic Array class
By aqeel2010 in forum New To JavaReplies: 2Last Post: 11292011, 09:40 AM 
Writing generic array class
By aqeel2010 in forum New To JavaReplies: 6Last Post: 11292011, 07:01 AM 
Creating an array of generic type
By colerelm in forum New To JavaReplies: 1Last Post: 10192011, 04:58 AM 
Generic array
By eva in forum New To JavaReplies: 3Last Post: 12232007, 01:12 AM
Bookmarks