TO - Lab 01 - Introduction To OOP: Christian E. Portugal-Zambrano
TO - Lab 01 - Introduction To OOP: Christian E. Portugal-Zambrano
Christian E. Portugal-Zambrano
September 16, 2019
1 Introduction
Welcome to Object Oriented Technology, this is and advanced course for Object Oriented
Programming, we need to remember some basic object oriented concepts to get up to
date in the course. In this Laboratory you will learn about some concepts and tools that
will help you through the course.
2 Pre-requisites
You need some OOP experience, but here you will review and learn more concepts related
to the area, throughout the course we will be using Java as primary language but after a
few weeks C++ or Python could be selected. Some knowledge of data structures, loops,
functions, recursion and some math libraries would be required to this course.
3 Programming environment
Don’t worry much about this, as your instructor I will not force you to choose a specific
one, contrary to this I encourage you to select from these or another one you are already
familiarized:
• Atom - https://ide.atom.io
1
• Sublime - https://www.sublimetext.com
I will encourage you to pick one from the described above, as a good programmer try to
be familiarized with the console and compiler of your system. All IDE’s I’ve mentioned
above are multi-platform and GUI based, but if you are a console user probably you will
feel comfortable with VIM or EMACS.
4 OOP: A review
So, we need to review your OOP concepts, but first as we are working with LATEX, pre-
sentation of code into your reports will be easy, you just need to add this at the header
of your document:
\usepackage{ l i s t i n g }
You can find more information about Listing package from CTAN:Listing Let’s get
started!
4.1 Concepts
We need to review some concepts, so please ask this questions:
2
Language Paradigm Inheritance Package Concurrency Garbage C. Typed
C
C++
Java
Python
Javascript
MatLab
R
FORTRAN
GO
LUA
C#
SmallTalk
ADA
Cobol
5 Warm up
Review the code accompanying this practice, this code shows some principles about the
attributes and methods insides classes, as we are working with Java pay attention to the
sintaxis and structure of the code. Remember that to compile your code you need to call
javac and for run call java. Some codes need to be completed:
6 To do
Implement a console game like Battleship, Drunk Bottle or Noughts & Crosses using the
text editors of your preference, you must include your implementation into the report
and try to ask this questions:
3
• Is necessary to implement all the constructors?
• As we are working with Java, make a research about the Java Heap and Stack.
• Design and describe and example that shows how Java pass the parameters as
values.
• Make a research about legibility, easy of use, reusability, and protection concepts
from OOP.
7 Deadline
For this course we have three groups, according to this the deadline for each group is six
days after the lab group scheduled. Remember that plagiarism must be avoided and if it
is detected the grade will be zero and informed to superior authorities. A pdf identified
with you CUI must be sent to [email protected] before deadline, after deadline
you will be qualified under the minimum grade obtained with a maximum of thirteen
and penalized with 1 point per day late. All question and doubts must be done to the
same email.
4
CODE
p u b l i c c l a s s Bird {
p r i v a t e S t r i n g name ; // A t t r i b u t e
private String filum ;
p r i v a t e S t r i n g type ;
p u b l i c Bird ( ) {
name = " C l a u d i o " ;
filum = " Gallo " ;
type = " Casero " ;
}
p u b l i c S t r i n g getFilum ( ) {
return filum ;
}
p u b l i c S t r i n g getType ( ) {
r e t u r n type ;
}
p u b l i c v o i d setType ( S t r i n g _type ) {
type = type ;
}
p u b l i c v o i d s e t F i l u m ( S t r i n g _filum ) {
t h i s . f i l u m = _filum ;
}
5
g a l l i n e r o = new Bird [ _ s i z e ] ;
}
p u b l i c Bird g e t B i r d ( i n t _index ) {
// r e t u r n a b i r d
}
}
p u b l i c c l a s s BirdApp {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// C r e a t i o n ( a l )
Bird c l a u d i o = new Bird ( ) ;
Bird l o c o = new Bird ( " Loco " , " C a r p i n t e r o " , " S a l v a j e " ) ;
// B e h a v i o u r
claudio . ItisNotaDogisaRoster ( ) ;
}
}
p u b l i c c l a s s Car{
p r i v a t e S t r i n g model ;
private String colour ;
p r i v a t e i n t maintenanceCount ;
CarType type ;
p u b l i c Car ( ) {
maintenanceCount = 0 ;
}
p u b l i c Car ( S t r i n g _model , S t r i n g _colour ) {
model = _model ;
c o l o u r = _colour ;
maintenanceCount = 0 ;
}
p u b l i c v o i d model ( S t r i n g _model ) {
model = _model ;
}
p u b l i c void c o l o u r ( S t r i n g _color ){
c o l o u r = _color ;
}
p u b l i c S t r i n g model ( ) {
r e t u r n model ;
}
6
}
p u b l i c v o i d maintenance ( i n t _maintenance ) {
maintenanceCount = _maintenance ;
}
p u b l i c i n t maintenance ( ) {
r e t u r n maintenanceCount ;
}
public c l a s s CarPainter {
public CarPainter (){
p u b l i c v o i d P a i n t ( Car _car , S t r i n g c o l o r ) {
}
}
p u b l i c c l a s s Garage {
private int capacity ;
private int occupied ;
p r i v a t e Car [ ] mygarage ;
p u b l i c Garage ( i n t _ c a p a c i t y ) {
c a p a c i t y = _capacity ;
occupied = 0;
mygarage = new Car [ _ c a p a c i t y ] ;
}
p u b l i c v o i d removeCar ( i n t _index ) {
}
}
p u b l i c c l a s s CarType{
p r i v a t e S t r i n g type ;
private String descriptionType ;
p r i v a t e i n t VIN ;
7
}
...
}
p u b l i c c l a s s CarApp{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t numMaintenance = 0 ;
Car mycar = new Car ( "Camaro" , " A m a r i l l o " ) ;
System . out . p r i n t l n ( " Model : "+mycar . model ( ) ) ;
System . out . p r i n t l n ( " C o l o r : "+mycar . c o l o u r ( ) ) ;
System . out . p r i n t l n ( "#␣ o f ␣ Maintenance : "+mycar . maintenance ( ) ) ;
Maintenance maintenance = new Maintenance ( ) ;
maintenance . m a i n t e n a n c e S e r v i c e ( mycar , numMaintenance ) ;
System . out . p r i n t l n ( "#␣ o f ␣ Maintenance : "+mycar . maintenance ( ) ) ;
System . out . p r i n t l n ( "#␣ o f ␣ Maintenance : "+numMaintenance ) ;
}
}
p u b l i c c l a s s GenerateGrades {
StudentList l i s t ;
p u b l i c GenerateGrades ( S t u d e n t L i s t _ l i s t ) {
l i s t = _ l i s t ; // ????
}
import j a v a . u t i l . ∗ ;
//CLASS i s an a b s t r a c t i o n
p u b l i c c l a s s GenerateRandom {
p r i v a t e i n t min , max ;
// C o n s t r u c t o r
p u b l i c GenerateRandom ( i n t _min , i n t _max) {
min = _min ;
max = _max ;
}
// r e t u r n a random number
public int generate () {
r e t u r n new Random ( ) . n e x t I n t (max ) ;
}
8
p u b l i c c l a s s Student {
p r i v a t e S t r i n g name ;
private String cui ;
private String career ;
p r i v a t e i n t grade ;
p u b l i c v o i d setName ( S t r i n g _name) {
name = _name ;
}
p u b l i c S t r i n g getName ( ) {
r e t u r n name ;
}
p u b l i c getGrade ( ) {
return grade ;
}
p u b l i c s e t G r a d e ( i n t _grade ) {
g r a d e = _grade ;
}
public c l a s s StudentList {
private int size ;
private int capacity ;
p r i v a t e Student [ ] l i s t ;
9
// we have t o r e s i z e
}
}
p u b l i c Student g e t S t u d e n t ( i n t _index ) {
i f ( _index>=0 && _index < c a p a c i t y ) {
r e t u r n l i s t [ _index ] ;
}
return null ;
}
public getCapacity ()
return capacity ;
}
p u b l i c c l a s s Person {
p r i v a t e S t r i n g name ;
p r i v a t e i n t age ;
p u b l i c Person ( ) {
p u b l i c v o i d name ( S t r i n g _name) {
name = _name ;
}
p u b l i c v o i d age ( i n t _age ) {
age = _age ;
}
p u b l i c S t r i n g name ( ) {
r e t u r n name ;
}
p u b l i c i n t age ( ) {
r e t u r n age ;
}
p u b l i c c l a s s PersonApp {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t age = 1 9 ;
Person b r a u l i o = new Person ( " B r a u l i o " , age ) ;
Older b i r t h d a y = new Older ( ) ;
10
System . out . p r i n t l n ( "Name : "+ b r a u l i o . name ( ) ) ;
System . out . p r i n t l n ( "Age : "+ b r a u l i o . age ( ) ) ;
b i r t h d a y . HappyBirthday ( b r a u l i o , age ) ;
System . out . p r i n t l n ( "Age : "+ b r a u l i o . age ( ) ) ;
System . out . p r i n t l n ( "Age : "+age ) ;
}
}
p u b l i c S t r i n g model ( ) {
r e t u r n model ;
}
p u b l i c v o i d model ( S t r i n g _model ) {
model = _model ;
}
p u b l i c void c a p a c i t y ( i n t _capacity ){
c a p a c i t y = _capacity ;
}
p u b l i c v o i d amunition ( i n t _ b u l l e t s ) {
amunition = _ b u l l e t s ;
}
p u b l i c i n t amunition ( ) {
r e t u r n amunition ;
}
11
i f ( amunition !=0 ) {
System . out . p r i n t l n ( " S h o o t i n g ␣ t h e ␣ p i s t o l ␣ o f ␣ model ␣ "+ model ) ;
amunition −=1;
}
e l s e System . out . p r i n t l n ( " P i s t o l ␣ w i t h o u t ␣ amunition " ) ;
}
}
p u b l i c c l a s s ReloadGun {
public void reload ( P i s t o l _pistol , i n t _bullets ) {
System . out . p r i n t l n ( " R e l o a d i n g ␣ "+ _ p i s t o l . model ( ) ) ;
i f ( _ p i s t o l . amunition ( ) + _ b u l l e t s > _ p i s t o l . c a p a c i t y ( ) ) {
_ p i s t o l . amunition ( _ p i s t o l . c a p a c i t y ( ) ) ;
}
else{
_ p i s t o l . amunition ( _ p i s t o l . amunition ( ) + _ b u l l e t s ) ;
}
}
}
p u b l i c c l a s s ShotterApp {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
P i s t o l magnum = new P i s t o l ( "Magnum" , 9 ) ;
magnum . s h o o t ( ) ;
magnum . s h o o t ( ) ;
magnum . s h o o t ( ) ;
magnum . s h o o t ( ) ;
System . out . p r i n t l n ( " Amunition : " + magnum . amunition ( ) ) ;
12
ReloadGun r e l o a d = new ReloadGun ( ) ;
r e l o a d . r e l o a d (magnum , 3 ) ;
System . out . p r i n t l n ( " Amunition : " + magnum . amunition ( ) ) ;
S i l e n c e r s i l e n c e r = new S i l e n c e r ( ) ;
i f (magnum . s i l e n c e r ( ) == f a l s e ) {
i f ( s i l e n c e r . m o u n t S i l e n c e r (magnum)== t r u e ) {
System . out . p r i n t l n ( "Mounted␣ s i l e n c e r ␣ t o ␣ "
+ magnum . model ( ) ) ;
}
}
}
}
13