CZ2002 Object Oriented Design & Programming
AY20/21, Semester 1
TA for labs on OO design principles, taught via Java and C++
Quicklinks for slides
Here are some additional resources that builds on top of the lab materials. It goes beyond what’s covered in the lab manual, but these are additional questions you should think about. Answers are hidden ;)
(Hint: this is written using Markdown)
Lab 1
In general, it’ll be a good habit to start writing docstrings / doc comments and add comments in areas of the code that can’t be made any clearer (i.e. even with good variable and method names). Doing so makes it easier for the TA to understand and mark your work + you’ll eventually need to use Javadoc for your group assignment too.
Appendix A
- It’s important to sit down and understand every component of
public static void main(String[] args)
. This provides an overview and this explainsstatic
in greater detail.
Question 1
- The question requires us to use
switch
statements, but when should you chooseswitch
statements overif-else
(besides the case we see in Question 2)? This discussion about the limitations ofswitch
statements will give you a better idea.
Question 2
- It’s a balance between clarity vs simplicity (or even convoluted conditionals, which you should avoid). The if/else-if conditions shouldn’t overlap too.
Question 3
- Will be a good practice to set the rate as a constant on top, and use clear names like
USD_TO_SGD_RATE
. - You might end up with an issue of your input values being changed before you can use them for the do-while loop. Or you might have found a way to overcome it, but have you fully understood what you did? This points you to the correct understanding.
Question 4
- There are many ways to solve this and there are interesting ways that do it in a few lines (1 loop + 1 if/else). Recursion too.
- It’s kinda like (an easier version of) a basic / early-stage interview question so you should aim to solve such questions quickly.
- Break it down to smaller parts and find patterns that can be implemented using typical coding constructs (if/else, for/while, modulus operator). Think logically and sequentially, like a computer.
Lab 2
You’ve learnt Python and C in previous mods, it’s your 3rd time practicing functions / methods - this lab should be even simpler than Lab 1 and you’ll be fine as long as you check your work against the test cases provided.
Also, recursion can solve many of the sub-tasks.
Lab 3
Here are some areas where OOP are used (in ML/AI):
- In Pytorch, the network architecture is often defined in a class that inherits from
nn.Module
. - Objax is a purely objected orientated NN library!
Lab 4
The slides are basically a deeper investigation into the issues discussed here.
Lab 5
Appendix C
- In the example code found in Appendix C, why do we need
conio.h
even after we includediostream
?- The code works fine even after you comment out the include for
conio.h
and_getch()
, so what’s the purpose of including them? Observe a subtle difference (might be less observable depending on your version of Visual Studio) with vs without it. - Here’s a clue
- Here’s the direction towards a possible answer
- The code works fine even after you comment out the include for
- Think more deeply about the use of
using namespace std;
- what does it do and what issues could it lead to in the future? - Also, should you be using
endl
? What is it doing under the hood?- More fiascos!
- They didn’t really explain about flushing, so here it is
General pointers
(no pun intended)
Installation
- Java is pre-installed in the lab computers, but if you had to install it yourself, you’d have to decide which version to download. As CS students, you should go beyond the typical end-user’s cognitive shortcut of getting the latest version and start asking deeper questions, such as:
- Why are there so many different versions? Why do old versions have to be supported and what are the implication when a version is no longer supported? What’s the difference between major and minor version changes (Hint: backward compatibility)? In your upcoming courses on software engineering, you’ll understand how companies manage complex software and see why these minor differences to a end-user can be a huge headache for developers
- Consider the security implications of the software decision you make. For e.g. it’s less trouble to write software based on a LTS version of Java if your codebase is going to be used for the long term. Eventually, older versions of software can no longer be supported (it takes manpower and manpower = money) and vulnerabilities could be created - the security team will then flag these issues and your SE team will have to patch the codebase (use a more updated version and fix breaking changes)
Java-related issues
- Don’t forget your semi-colons;
- Public class name must be same as filename
- Try not to use generic imports, i.e.
import java.util.*
. Import the specific methods instead. - You might find the
trim()
function useful to handle accidental whitespaces in the input. - Remember to close the scanner with the
close()
function.- While it’s not an major issue for simple scripts and some might even disagree, it’s generally a good practice to be aware about such resource consumption issues and keep them in mind whenever you code, especially when you go out to the industry and write scripts that run at scale. Although that article raised issues about closing the scanner, doing so after taking in all the inputs you need (i.e. you’re sure you don’t need the user’s input anymore) will not lead to any issues.
- Also, most of the time you don’t need multiple scanners
Running scripts
- Besides using the Eclipse IDE to create and run your Java scripts (IDEs hide complexity from you), you should try running them from the terminal as well.
Last updated: 17 October 2020