Exercise Solutions: InheritanceΒΆ

After working through the exercises, your Computer, Laptop, and Smartphone classes should look similar to the following examples.

The Computer class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Computer extends AbstractEntity{
   // computer is my parent class

   private int ram;
   private int storage;
   private boolean hasKeyboard;

   public Computer(int storage, int ram, boolean hasKeyboard) {
      super();
      this.ram = ram;
      this.storage = storage;
      this.hasKeyboard = hasKeyboard;
   }

   public void increaseRAM (int n) {
      this.ram = this.ram + n;
   }

   public void increaseStorage (int x) {
      this.storage = this.storage + x;
   }
}

The Laptop class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Laptop extends Computer {

   private double weight; // laptop weight in pounds.

   public Laptop(int storage, int ram, boolean hasKeyboard, double weight) {
      super(storage, ram, hasKeyboard);
      this.weight = weight;
   }

   public boolean isClunky() {
      if (weight > 5.0) {
         return true;
      }
      else {
         return false;
      }
   }
}

The SmartPhone class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class SmartPhone extends Computer {

   private int numberOfSelfies;

   public SmartPhone(int storage, int ram, boolean hasKeyboard, int numberOfSelfies) {
      super(storage, ram, hasKeyboard);
      this.numberOfSelfies = numberOfSelfies;
   }

   public void takeSelfie() {
      this.numberOfSelfies = this.numberOfSelfies + 1;
   }

   public int getNumberOfSelfies() {
      return this.numberOfSelfies;
   }
}

Back to the exercises