Lab 1
Lab 1 Slides
CMPUT 301 W26 - Lab 1: Java, OOP, and Android Studio!
1. Setup Instructions
-
Download and install Android Studio from the official Android website.
-
Refer to the installation guide unique to your Operating System.
-
Refer to the lab 1 slides for more information.
2. Walkthrough
The following walkthrough can also be found in this GitHub Repository. Fork this repository for the submission of the Lab exercise.
-
Create a new
PetShopproject on Android Studio (File > New > New Project > Select "Empty Views Activity").[!WARNING] Make sure that the project language is Java, not Kotlin!
-
Create a new
Petclass by navigating to File > New > Java Class -
Add two attributes to the
Petclass:String nameDate birthDate- use Alt + Enter (Windows) or Option + Return (Mac) to import any packages
- Java coding conventions declares that all attributes are
privateby default
[!IMPORTANT] Access modifiers:
private: class-only accessprotected: package and inheritance accesspublic: universal accessNo modifier: package-level access
-
Create two constructors for the
Petclass:- Only
nameas argument. UseDate = new Date()(current date) for the Default date value. - Both
nameandbirthDateas arguments.
[!NOTE]
- All Java classes implicitly extend the
Objectclass (java.lang.Object), which provides basic methods like toString(), equals(), and hashCode() that can be overridden. thisrefers to the current instance of the class and is used to distinguish between instance variables and constructor parameters.
- Only
-
Make a regular
Petin MainActivity by passing in an empty string:java Pet pet = new Pet(""); -
Generate getters and setters for the
Petclass.- Right-click -> Generate -> Getter and Setter -> Shift + Right-click all attributes -> Ok
-
Create a
Catchild class that extends thePetclass.- Include a
super()call in Cat's constructors. CatinheritsPet's methods and attributes, but requires its own constructors. Try to callsuper()in the child's constructor:
java public Cat(String name) { super(name); }super()calls the parent's constructor (there is a hidden call to Object's constructor).
- Include a
-
Make the Pet Class Abstract
- Change the
Petclass declaration to the following:java public abstract class Pet { ... }
[!NOTE] Abstract classes cannot be instantiated directly - they can only be used as base classes for inheritance. You must create concrete subclasses to create objects.
- Change the
Petto aCatin MainActivity.
java Cat cat = new Cat("Lucy");- Add an abstract method for speaking in the
Petclass. It has no implementation and must be overridden by a child classes to add functionality.
java public abstract String speak();[!NOTE] Abstract methods have no implementation and cannot be called directly. They must be overridden by concrete subclasses before they can be used through objects of those subclasses.
- Change the
-
Method Overriding
Catmust override the abstractspeak()method fromPetclass.- The
@Overrideannotation ensures correct method overriding at compile-time. - Each child class can implement
speak()differently based on its needs.
java // In Cat class @Override public String speak() { return "meow"; // Cats meow } -
Make a
Dogsubclass ofPet- call
super()in both of Dog's constructors. speak()method should return"bark".- What if we want to use both in our list? (hint - implicit upcasting)
- Add the following to
MainActivity:
```java Dog dog = new Dog("Snoopy"); ArrayList
petList = new ArrayList (); // Can store both Cat and Dog objects // since they both inherit from Pet petList.add(cat); petList.add(dog); ```
- call
-
Make a
Scorpionsubclass ofPet- call
super()in both of Scorpion's constructors. speak()method should return"hiss".- What if we want to use both in our list? (hint - implicit upcasting)
- Add the following to
MainActivity:
java Scorpion scorpion = new Scorpion("Scorponok"); petList.add(scorpion); - call
-
Interface Implementation
- Abstract method and base class so all the classes have the
speak()method. - An interface can also be used to force the use of some methods.
java public interface Pettable { public Void pet(); }Petshould not implementPettablebecauseScorpionshould not be pettable- Make
CatandDogclasses implementPettableclass. - All classes that implement this interface must provide implementations for these methods.
java ArrayList<Pettable> pettablePets = new ArrayList<Pettable>(); pettablePets.add(cat); pettablePets.add(dog); pettablePets.add(scorpion); // This should produce an error - Abstract method and base class so all the classes have the
3. Lab Participation Exercise
- Add three new model classes to
PetShop: - An abstract base class which represents the current
Mood. - Two non-abstract classes which represent different moods (Ex: happy, sad, etc.) and inherit from the abstract class.
- Each mood should have a date, and getters and setters to access the date.
- Provide two constructors:
- One that sets the date to a default
- One that takes a date as an argument
- Follow proper encapsulation principles.
- Each mood should have a method which returns a string representing that mood.
- Your new code should demonstrate:
- Classes
- Methods
- Attributes
- Access modifiers
- Encapsulation
- Constructors
- Inheritance
- Abstract base classes
- Update the
README.mdfile with your details and references/collaborators. - Update the
LICENSE.mdfile with your full name.
[!CAUTION] Make sure to commit and push your code to the GitHub repository before the deadline!
Submission
Submit the repository link to Canvas
Note: Running the project is not necessary.
- Due date: Check the schedule. (Usually Friday after the lab at 5PM)