• SUMMARY • Choose a topic or subject matter of your choice and model it in code
ID: 3589405 • Letter: #
Question
• SUMMARY
• Choose a topic or subject matter of your choice and model it in code using custom classes and array lists. Fill up the classes with data and then print out the values. You must have two arraylist instance variables in your top level data-holding class (see Details below).
• This lab will involve the following new features:
• ArrayList iteration using “for” each statement
• Composition
• DETAILS
• Choose a genre, and make a custom class that will hold two different ArrayList instance variables that will hold two other custom class types that you will create.
• Example:
• If you picked mobile phone companies, you might create one class called MobilePhoneCompany that has an instance variable called “phones” that is an ArrayList of “Phone” type objects and another instance variable called “customers” that is an ArrayList of “Customer” type objects. See below diagram as an example:
MobilePhoneCompany
+ companyName: String
+ phones : ArrayList<Phone>
+ customers : ArrayList<Customer>
Phone
+ name : String
Customer
+ firstName : String
+ lastName : String
• In your initial main method of your app…
• Instantiate at least two of each of your sub classes (Phone and Customer in above example) and put data into their instance variables (you can directly fill it in code, no need for random generation of data or anything).
• Instantiate the top-level class (MobilePhoneCompany in above example) and instantiate its ArrayList instance variables and fill them with the objects you just created, and fill any other instance variables (such as “companyName” above).
• Do these last two steps at least 3 times. Now you’ll have three top-level objects.
• Now create an ArrayList variable of MobilePhoneCompany type and put these three objects you just created into that ArrayList.
• LOOP through your ArrayList and print out all the values you’ve stored in each MobilePhoneCompany hierarchy.
• This will require nested for each loops :
• For instance, in the example I gave above, I would need to loop through each mobile company:
• Inside that loop…
• I would first print out the companyName.
• Then I would need to add another for each loop that loops through the “phones” ArrayList and print out the phone name inside that loop.
• Then I would add another loop for the “customers” ArrayList and print out firstName and lastName.
• REMINDER: An example of the type of “for each” loop we’re using is the same as in class:
• Example looping through phone companies:
• for(MobilePhoneCompany oCompany: lstMobilePhoneCompanies){...}
• Example using in-class lecture object types if I wanted to loop through Android ArrayList:
• for(Android oAndroid: lstAndroid){
// In each loop, oAndroid is the next Android object in the lstAndroid ArrayList.
// in this loop I could add another for loop to loop through oAndroid.storyLines.
}
• Please try to pick your own genre of data or subject matter, but you can pick the same topic I have above (mobile companies) if you can’t think of anything else.
• But most importantly, whatever topic you pick, have at least as many variables as I’ve outlined in my example.
Explanation / Answer
import java.io.*;
import java.util.*;
class Customer{
public String firstName;
public String lastName;
}
class Phone{
public String name;
}
class MobilePhoneComapny{
public String companyName;
public ArrayList<Phone> phones;
public ArrayList<Customer> customers;
public MobilePhoneComapny(){
phones = new ArrayList<Phone>();
customers = new ArrayList<Customer>();
}
}
public class DemoCustom{
public static void main(String[] args){
Phone p1 = new Phone();
Phone p2 = new Phone();
Phone p3 = new Phone();
Phone p4 = new Phone();
Phone p5 = new Phone();
Phone p6 = new Phone();
Customer c1 = new Customer();
Customer c2 = new Customer();
Customer c3 = new Customer();
Customer c4 = new Customer();
Customer c5 = new Customer();
Customer c6 = new Customer();
MobilePhoneComapny m1 = new MobilePhoneComapny();
MobilePhoneComapny m2 = new MobilePhoneComapny();
MobilePhoneComapny m3 = new MobilePhoneComapny();
ArrayList<MobilePhoneComapny> M = new ArrayList<MobilePhoneComapny>();
p1.name = "pname1";
p2.name = "pname2";
p3.name = "pname3";
p4.name = "pname4";
p5.name = "pname5";
p6.name = "pname6";
c1.firstName = "fname1";
c1.lastName = "lname1";
c2.firstName = "fname2";
c2.lastName = "lname2";
c3.firstName = "fname3";
c3.lastName = "lname3";
c4.firstName = "fname4";
c4.lastName = "lname4";
c5.firstName = "fname5";
c5.lastName = "lname5";
c6.firstName = "fname6";
c6.lastName = "lname6";
m1.companyName = "cname1";
m2.companyName = "cname2";
m3.companyName = "cname3";
m1.phones.add(p1);
m1.phones.add(p2);
m1.customers.add(c1);
m1.customers.add(c2);
m2.phones.add(p3);
m2.phones.add(p4);
m2.customers.add(c3);
m2.customers.add(c4);
m3.phones.add(p5);
m3.phones.add(p6);
m3.customers.add(c5);
m3.customers.add(c6);
M.add(m1);
M.add(m2);
M.add(m3);
System.out.println("-------------------------------------------------------------------- ");
for (int i = 0; i<M.size(); i++){
System.out.println(M.get(i).companyName);
System.out.println("Phones:");
for (int j = 0; j < M.get(i).phones.size(); j++){
System.out.println(M.get(i).phones.get(j).name);
}
System.out.println("Cusomers:");
for (int j = 0; j < M.get(i).customers.size(); j++){
System.out.println(M.get(i).customers.get(j).firstName + " " + M.get(i).customers.get(j).lastName);
}
System.out.println("-------------------------------------------------------------------- ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.