Lab 1
Dr. Suleman Shahid and Dr. Abdul Ali Bangash, Department of Computing Science, LUMS (2026). Dr. Hazel Campbell, Department of Computing Science, University of Alberta (2019, 2023, 2024). Dr. Abram Hindle, Department of Computing Science, University of Alberta (2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023). Alexander Wong, 2019. status: published summary: Lab 1 -- Java, OOP, Android Studio
Lab 1 Slides
Instructions
Download and install Android Studio from the official Android website
https://developer.android.com/studio
Check for specific installation guide unique to your Operating System
https://developer.android.com/studio/install
Walkthrough
-
Create a new LonelyTwitter project (File > New > New Project > Select "Empty Views Activity"). Make sure that the project language is Java, not Kotlin!
-
Create Tweet Class (Click > New > Java Class)
- Make attributes (Date date & String message) (use alt+enter (windows) or Option-Enter (Mac) to include/import any packages)
- Note: Access modifiers
- private= class only
- No modifier = within package
- protected = within package and through inheritance
- public = everyone!
- Note: Access modifiers
- Create two Constructors (one with the only Message and the other with Date+Message as arguments) and use Date = new Date() (current date and time) for the first constructor (the Default value for date).
- Note: Java Object Class (everything extends it, calls its constructor and it has built-in methods like toString())
- Note: the this keyword (message = message doesn't do anything!)
-
Make a regular tweet in MainActivity (pass in an empty string)
-
Getters and setters
-
Inheritance
-
Make ImportantTweet child class (extends Tweet)
- call super in both of ImportantTweet's constructors
- Now have access to the parent's methods and attributes. except constructors! (try and make an important Tweet)
ImportantTweet(String message){
super(message);
}
- Super calls the parent's constructor (there is a hidden call to Object's constructor)
-
Change the Tweet to an ImportantTweet in MainActivity
-
Abstract Stuff
-
Make Tweet Class Abstract
- public abstract class Tweet { ... }
- Note: Abstract classes cannot be instantiated.
- public abstract Boolean isImportant();
- This is an abstract method, it has no implementation and must be overridden by child classes (to add functionality).
- Note: Abstract methods cannot be called using any objects.
- public abstract class Tweet { ... }
-
What if they need to behave differently? @Override isImportant() to create a compile-time check
- You need to override the method in the child class as it is an abstract method in the parent class that has no implementation.
- Overriding the method allows the child class to have its own implementation of the method.
- ex. the Normal Tweet is not important, so it returns false. The ImportantTweet is important, so it returns true.
- The methods in the child classes need to be overridden to behave differently.
-
Make a NormalTweet class, could have many types of tweets
- call super in both of NormalTweet's constructors
- isImportant method should return Boolean.FALSE
- What if we want to use both in our list? (Implicit upcasting)
ArrayList
-
Abstract method and base class so all the classes have the isImportant() method
-
An interface can also be used to force the use of some methods
public interface Tweetable {
public String getMessage();
public Date getDate();
}
- Make Tweet implement Tweetable
Lab1 Participation Exercise Requirements
- Add three new model classes to LonelyTwitter: the first should be an abstract base class which represents the current mood. The second and third should be 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.
- A constructor which sets the date to a default and a constructor which takes a date as an argument should be provided.
- Encapsulation should be followed.
- Each mood should have a method which returns a string representing that mood.
- Your new code should have examples of classes, methods, attributes, access modifiers, encapsulation, constructors, inheritance and abstract base classes.
Submission
LMS
Note: Running the project is not necessary.
- Due date: Check the schedule. (Usually Tuesday 5PM)