Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Java Objects Containing Objects: Build a Smartphone & App Store with Composition

Learn Java composition by modeling a Smartphone and its App Store. Step-by-step tutorial with switch, if-else, for loops, and inner objects.

Java objects containing objects Java composition tutorial ICS 141 lab 7 Java switch statement example Java if else example Java for loop example Java AppStore class Java Smartphone class object oriented programming Java Java toString override Java getters and setters composition in Java Java driver class Java lab assignment help Java programming for students Java real-world examples

Introduction: Why Objects Containing Objects Matters in 2026

In Java, one of the most powerful concepts is composition—creating objects that contain other objects. Think of your smartphone: it contains a battery, a camera, and an app store. Each of those is an object with its own state and behavior. In this tutorial, we'll build a Smartphone that contains an AppStore, mirroring the ICS 141 Lab 7 assignment. We'll also review switch statements, if-else, and for loops using a trendy example: a Fitness Tracker app that logs daily steps. By the end, you'll understand how to design classes that hold other objects, just like real-world systems.

Part 1: Reviewing Switch and If-Else with a Fitness Tracker

Before diving into composition, let's refresh control flow. Imagine you're building a Fitness Tracker that calculates calories burned based on activity type. Use a switch statement to map activity codes to calorie rates.

int activityCode = 2; // 1=Running, 2=Cycling, 3=Swimming
double caloriesPerMinute;
switch (activityCode) {
    case 1:
        caloriesPerMinute = 10.5;
        break;
    case 2:
        caloriesPerMinute = 8.0;
        break;
    case 3:
        caloriesPerMinute = 7.2;
        break;
    default:
        caloriesPerMinute = 0;
        System.out.println("Invalid activity");
}

Now, add an if-else to check if the user is in a calorie deficit based on their goal. This is similar to the calculator assignment where you decide operation based on user input.

int goalCalories = 500;
int burnedCalories = 450;
if (burnedCalories >= goalCalories) {
    System.out.println("Goal reached! Great job!");
} else {
    System.out.println("Keep moving, you're " + (goalCalories - burnedCalories) + " away.");
}

Part 2: For Loops – Simulating a Workout Week

Use a for loop to simulate a week of workouts. This mirrors the assignment's loop to print numbers 1-10. Here we print daily step counts.

int[] steps = {5000, 7000, 3000, 8000, 10000, 6000, 9000};
for (int i = 0; i < steps.length; i++) {
    System.out.println("Day " + (i+1) + ": " + steps[i] + " steps");
}

Part 3: Objects Containing Objects – Smartphone & AppStore

Now for the main event: composition. We'll create two classes: AppStore (the contained object) and Smartphone (the container). This directly follows the assignment: list 5 objects that contain other objects (e.g., Car contains Engine, Library contains Book). We'll implement one: Smartphone contains AppStore.

Step 1: Create the AppStore Class

The AppStore knows its name, number of apps, and rating. It can download an app, update an app, and print a description.

public class AppStore {
    // Instance variables
    private String storeName;
    private int numberOfApps;
    private double rating;

    // Constructor
    public AppStore(String storeName, int numberOfApps, double rating) {
        this.storeName = storeName;
        this.numberOfApps = numberOfApps;
        this.rating = rating;
    }

    // Getters and Setters
    public String getStoreName() { return storeName; }
    public void setStoreName(String storeName) { this.storeName = storeName; }
    public int getNumberOfApps() { return numberOfApps; }
    public void setNumberOfApps(int numberOfApps) { this.numberOfApps = numberOfApps; }
    public double getRating() { return rating; }
    public void setRating(double rating) { this.rating = rating; }

    // Methods
    public void downloadApp(String appName) {
        System.out.println("Downloading " + appName + " from " + storeName);
    }

    public void updateApp(String appName) {
        System.out.println("Updating " + appName + " via " + storeName);
    }

    public String getDescription() {
        return storeName + " has " + numberOfApps + " apps with rating " + rating;
    }

    // toString
    @Override
    public String toString() {
        return "AppStore{name='" + storeName + "', apps=" + numberOfApps + ", rating=" + rating + "}";
    }
}

Step 2: Create the Smartphone Class (Contains AppStore)

The Smartphone knows its model, operating system, and the AppStore it contains. It can make a call and browse the web.

public class Smartphone {
    // Instance variables
    private String model;
    private String os;
    private AppStore appStore; // Contained object

    // Constructor
    public Smartphone(String model, String os, AppStore appStore) {
        this.model = model;
        this.os = os;
        this.appStore = appStore;
    }

    // Getters and Setters
    public String getModel() { return model; }
    public void setModel(String model) { this.model = model; }
    public String getOs() { return os; }
    public void setOs(String os) { this.os = os; }
    public AppStore getAppStore() { return appStore; }
    public void setAppStore(AppStore appStore) { this.appStore = appStore; }

    // Methods that use the contained object
    public void makeCall(String contact) {
        System.out.println("Calling " + contact + " from " + model);
    }

    public void browseWeb(String url) {
        System.out.println("Browsing " + url + " on " + model);
    }

    // toString
    @Override
    public String toString() {
        return "Smartphone{model='" + model + "', os='" + os + "', appStore=" + appStore + "}";
    }
}

Step 3: Driver Class to Test

Create a Driver class that instantiates both objects and calls all methods.

public class Driver {
    public static void main(String[] args) {
        // Create AppStore object
        AppStore googlePlay = new AppStore("Google Play", 3000000, 4.5);

        // Create Smartphone object that contains the AppStore
        Smartphone phone = new Smartphone("Pixel 9", "Android 16", googlePlay);

        // Call methods on AppStore
        googlePlay.downloadApp("ChatGPT");
        googlePlay.updateApp("Maps");
        System.out.println(googlePlay.getDescription());
        System.out.println(googlePlay);

        // Call methods on Smartphone
        phone.makeCall("Mom");
        phone.browseWeb("assignmentchef.com");
        System.out.println(phone);
    }
}

Why This Matters: Real-World Analogies

In 2026, every major tech product uses composition. Your favorite AI assistant (like Siri or Alexa) contains a voice recognition engine, a natural language processor, and a music player. A video game character contains weapons, armor, and inventory. Even a smart home contains sensors, lights, and thermostats. By mastering objects containing objects, you're learning to model complex systems the way professional developers do.

Common Mistakes and Tips

  • Forgetting to instantiate the contained object: The container's constructor should receive an already-created contained object or create one internally. In our example, we passed the AppStore to the Smartphone constructor.
  • Not using getters/setters: Always encapsulate instance variables. This protects data integrity.
  • Ignoring toString(): Override toString() for debugging and printing object details.
  • Null pointer exception: Ensure the contained object is not null before calling its methods.

Conclusion

You've built a complete Java composition example: a Smartphone containing an AppStore. Along the way, you reviewed switch, if-else, and for loops with a fitness tracker analogy. This mirrors the ICS 141 Lab 7 assignment perfectly. Practice by creating other compositions—like a Library containing Books or a Car containing Engine. The more you compose, the more natural object-oriented design becomes.

Remember: in Java, every object is a building block. Combine them wisely to create powerful, reusable code.