Write a program which takes a list of Subscription objects (each Subscription ob
ID: 3737423 • Letter: W
Question
Write a program which takes a list of Subscription objects (each Subscription object consists of a userId (integer), a programCode (integer), and a seasonId (integer)), and prints a list of all (userId, programCode) pairs, such that: At least two Subscription objects in the input list have the given userId and programCode; and The programCode is not 42. The pairs need to be printed in descending order of number of subscriptions. Ties, i.e. pairs with equal numbers of subscriptions, may be ordered arbitrarily. You may assume that each Subscription object in the input list is valid and unique. Example Below is a list of ten subscription objects.
Each Subscription object is shown as a tuple (userId, programCode, seasonId). [(9600002, 42, 3), (9600001, 17, 3), (9600003, 11, 1), (9600002, 14, 5), (9600001, 17, 1), (9600003, 11, 4), (9600001, 17, 4), (9600002, 42, 6), (9600002, 42, 1)]
The output for this instance should be: 9600001 17 9600003 11
The following is the code given by the tutor. Please follow the code by using Java and try to code as simple as possible.
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Assignment1 {
public static void main(String[] args) {
List<Subscription> subs = loadSubscriptions();
// TODO: your code here
}
public static List<Subscription> loadSubscriptions() {
List<Subscription> subs = new ArrayList<>();
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
Subscription s = new Subscription(in.nextLong(), in.nextInt(), in.nextInt());
subs.add(s);
}
return subs;
}
}
Explanation / Answer
Please find my code:
##########
public class Subscription {
private long userId;
private int programCode;
private int seasonId;
public Subscription() {
// TODO Auto-generated constructor stub
}
public Subscription(long userId, int programCode, int seasonId) {
super();
this.userId = userId;
this.programCode = programCode;
this.seasonId = seasonId;
}
public long getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getProgramCode() {
return programCode;
}
public void setProgramCode(int programCode) {
this.programCode = programCode;
}
public int getSeasonId() {
return seasonId;
}
public void setSeasonId(int seasonId) {
this.seasonId = seasonId;
}
}
################
import java.util.Scanner;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
public class Assignment1 {
public static void main(String[] args) {
List<Subscription> subList = loadSubscriptions();
// TODO: your code here
Set<String> subSet=new HashSet<>();
for(int i=0;i<subList.size();i++){
int count=0;
for(int j=1;j<subList.size();j++){
if(subList.get(i).getUserId()==subList.get(j).getUserId() &&
subList.get(i).getProgramCode()!=42 && subList.get(j).getProgramCode()!=42 &&
subList.get(i).getProgramCode()==subList.get(j).getProgramCode())
{//checks each and every object if it is atleast 2 times in the list and program code is not 42
count++;
}
}
if(count>=2){//if atleast two same objects are found add it to the set
StringBuilder sc=new StringBuilder();//for adding only userid and progrmcode to set
sc.append(subList.get(i).getUserId()+" ");
sc.append(subList.get(i).getProgramCode());
subSet.add(sc.toString());
}
}
List<String> filterList=new ArrayList<>(subSet);//to convert set to list for easy iteration
for(int k=0;k<filterList.size();k++){
System.out.println(filterList.get(k));
}
}
public static List<Subscription> loadSubscriptions() {
List<Subscription> subs = new ArrayList<>();
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
Subscription s = new Subscription(in.nextLong(), in.nextInt(), in.nextInt());
subs.add(s);
}
return subs;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.