Você está na página 1de 13

National University of San Agustin

System Engineer School

TO - Lab 01 - Introduction to OOP

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:

• Visual Code - https://visualstudio.microsoft.com/es/

• 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:

• ¿What is a fundamental data type?

• ¿Is a fundamental data type the same that a class?

• ¿What do you understand about a class?

• ¿What would be the difference between a structured type and a class?

• ¿What would be the difference between an object and a class?

• ¿Is there some difference between a package and a namespace?

• ¿A variable and an attribute are the same ?

• ¿A function is the same that a method?

Now, is necessary to make some research for fill this table:

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:

• Bird.java, Gallinero.java and BirdApp.java show a simple App to instance Birds.

• Car.java, CarPainter.java, Garage.java, CarType.java and CarApp.java show a rec-


ommend structure to implement responsibilities between classes.

• GenerateGrades.java, GenerateRandom.java, Student.java and StudentList.java show


how to give specific tasks to the object oriented classes.

• Person.java and PersonApp.java show a simple example of object instantiation.

• Pistol.java, ReloadGun.java, SilencerApp.java and ShotterApp.java show how to


separate responsibilities between classes.

All the code was appended to the end of this practice.

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:

• How many constructors are there?

3
• Is necessary to implement all the constructors?

• How much memory you are using?

• What is the difference between Class, Package, Module and Artifact.

• After the program finished, the memory was released?

• A constructor can be private?

• 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.

• Describe the concept of encapsulation.

• 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 cportugalz@unsa.edu.pe 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 Bird ( S t r i n g _name , S t r i n g _filum , S t r i n g _type ) {


name = _name ;
f i l u m = _filum ;
type = _type ;
}

p u b l i c S t r i n g getName ( ) { // Method <−> G e t t e r


r e t u r n name ;
}

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 setName ( S t r i n g _name) { // Method <−> S e t t e r


name = _name ;
}

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 ;
}

public void ItisNotaDogisaRoster (){


System . out . p r i n t l n ( " H i j o . ␣ c h i c o , ␣ e s t o ␣ no ␣ e s ␣un␣ p e r r o , ␣ e s ␣un␣ g a l l o " ) ;
}
}

public class Gallinero {


p r i v a t e Bird [ ] gallinero ;
private int cantidad ;
public G a l l i n e r o ( i n t _size ){
cantidad = _size ;

5
g a l l i n e r o = new Bird [ _ s i z e ] ;
}

p u b l i c v o i d addBird ( Bird _bird ) {

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 ;
}

public String colour (){


return colour ;

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 ;
}

p u b l i c v o i d carType ( CarType _cartype ) {


//make copy o f _carType ;
}
}

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 addCar ( Car _car ) {

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 ;

p u b l i c CarType ( S t r i n g _type , S t r i n g _ d e s c r i p t i o n , 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 ; // ????
}

public void generate (){


GenerateRandom rand = new GenerateRandom ( 0 , 2 0 ) ;
f o r ( i n t i =0; i < l i s t . c a p a c i t y ( ) ; i ++){
l i s t [ i ] . s e t G r a d e ( rand . g e n e r a t e ( ) ) ;
}
}
}

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 Student ( S t r i n g _name , S t r i n g _cui , S t r i n g _ c a r e e r ) {


name = _name ;
c u i = _cui ;
t h i s . c a r e e r = _career ;
}

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 ;

public StudentList (){


s i z e =0;
c a p a c i t y =3;
l i s t = new Student [ c a p a c i t y ] ;
}

public StudentList ( i n t _size ){


s i z e =0;
capacity = _size ;
l i s t = new Student [ c a p a c i t y ] ;
}

p u b l i c v o i d addStudent ( Student _student ) {


i f ( s i z e <c a p a c i t y ) {
l i s t [ s i z e ] = _student ;
s i z e ++;
}
else{

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 Person ( S t r i n g _name , i n t _age ) {


name = _name ;
age = _age ;
}

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 ) ;
}
}

public class Pistol {


private int capacity ;
p r i v a t e S t r i n g model ;
p r i v a t e i n t amunition ;
private boolean s i l e n c e r ;

public Pistol (){


}
p u b l i c P i s t o l ( S t r i n g _model , i n t _ c a p a c i t y ) {
model = _model ; c a p a c i t y = _ c a p a c i t y ; amunition = _ c a p a c i t y ;
silencer = false ;
}

p u b l i c S t r i n g model ( ) {
r e t u r n model ;
}

public int capacity () {


return capacity ;
}

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 ;
}

public boolean s i l e n c e r (){


return s i l e n c e r ;
}

public void s i l e n c e r ( boolean _silencer ) {


silencer = _silencer ;
}

public void shoot (){

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 ) ;
}
}
}

public class Silencer {


public Silencer (){

public boolean mountSilencer ( P i s t o l _pistol ) {


i f ( _ p i s t o l . s i l e n c e r ( ) != t r u e ) {
_pistol . s i l e n c e r ( true ) ;
return true ;
}
return f a l s e ;
}

public boolean unmountSilencer ( P i s t o l _pistol ){


i f ( _ p i s t o l . s i l e n c e r ( ) == t r u e ) {
System . out . p r i n t l n ( "Unmounting ␣ s i l e n c e r ␣ t o ␣ "
+ _ p i s t o l . model ( ) ) ;
_pistol . s i l e n c e r ( f a l s e ) ;
return true ;
}
return f a l s e ;
}
}

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

Você também pode gostar