# Thread: Generic Array

1. Member
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> {
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```
So i wrote the following code:
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 Auto-generated 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();
System.out.print(time);
}
for(int j = 0; j < numberofassinments; j++) {
Homework n = kb.
System.out.print(weight);
}
}
}
@Override
public int compareTo(MyDataStructure arg0) {
// TODO Auto-generated method stub
return 0;
}
List<MyDataStructure> list = new ArrayList<MyDataStructure>();
Scanner kb = new Scanner(System.in);
}
}```
Can Someone help me, How should i get an input as kb.next(); doesn't seem to work.

2. ## Re: Generic Array

kb.next(); doesn't seem to work.
Please explain what happens.

How does the user know to enter data from the keyboard and what kind of data?

3. Senior 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,
Jim

4. Member
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> {

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]
Originally Posted by jim829
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,
Jim

5. Senior 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,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•