cs 200 - programming i: classes · classes uml class definingclasses...

Post on 05-Feb-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS 200 - Programming I: Classes

Marc Renault

Department of Computer SciencesUniversity of Wisconsin – Madison

Fall 2019TopHat Sec 3 (1:20 PM) Join Code: 682357TopHat Sec 4 (3:30 PM) Join Code: 296444

Classes UML

Classes

Classes UML

Class

Defining ClassesClasses provide a way of defining new types.They are data structures that:

can group member variables.can group member methods.

publicmembers are accessible outside the class.privatemembers are only accessible inside the class.

Creating instances of classesMyClass varName = new MyClass(arg1, ...)

Creating instances of your own classes work just as thepredefined Java classes.

1/23

Classes UML

Class

Defining ClassesClasses provide a way of defining new types.They are data structures that:

can group member variables.can group member methods.

publicmembers are accessible outside the class.privatemembers are only accessible inside the class.

Creating instances of classesMyClass varName = new MyClass(arg1, ...)

Creating instances of your own classes work just as thepredefined Java classes.

1/23

Classes UML

Class

Defining ClassesClasses provide a way of defining new types.They are data structures that:

can group member variables.can group member methods.

publicmembers are accessible outside the class.privatemembers are only accessible inside the class.

Creating instances of classesMyClass varName = new MyClass(arg1, ...)

Creating instances of your own classes work just as thepredefined Java classes.

1/23

Classes UML

TopHat Question 1What is the output when executed?

public class MyClass {

public int a;

public MyClass () {a = 5;

}

public static void main(String [] args) {MyClass b = new MyClass ();b.a = 10;System.out.print(b.a);

}}

2/23

Classes UML

TopHat Question 2What is the output when executed?

public class MyClassBis {

private int a;

public MyClassBis () {a = 5;

}

public static void main(String [] args) {MyClassBis b = new MyClassBis ();b.a = 10;System.out.print(b.a);

}}

3/23

Classes UML

TopHat Question 3What is the output when UseMyClass is executed?public class UseMyClass {

public static void main(String [] args) {MyClass b = new MyClass ();b.a = 10;System.out.print(b.a);

}

}

public class MyClass {

public int a;

public MyClass () {a = 5;

}

public static void main(String [] args) {MyClass b = new MyClass ();b.a = 10;System.out.print(b.a);

}}

4/23

Classes UML

TopHat Question 4What is the output when UseMyClassBis is executed?public class UseMyClassBis {

public static void main(String [] args) {MyClassBis b = new MyClassBis ();b.a = 10;System.out.print(b.a);

}

}

public class MyClassBis {

private int a;

public MyClassBis () {a = 5;

}

public static void main(String [] args) {MyClassBis b = new MyClassBis ();b.a = 10;System.out.print(b.a);

}}

5/23

Classes UML

Class Interface

Class InterfacePublic view of the class.publicmethods and members that can be accessed fromoutside.

Good Practice: Keep Things privateCode smell: indecent exposure.private unless reason to make public.Encapsulation is one of the fundamental principals inobject-oriented programming (OOP).

6/23

Classes UML

Class Interface

Class InterfacePublic view of the class.publicmethods and members that can be accessed fromoutside.

Good Practice: Keep Things privateCode smell: indecent exposure.private unless reason to make public.Encapsulation is one of the fundamental principals inobject-oriented programming (OOP).

6/23

Classes UML

Member Variables

public class HockeyPlayer {

private String name;private int goals;private int assists;private int gamesPlayed;private double toi;private double penaltyMin;

public HockeyPlayer () {name = "";initMemberVars ();

}

public HockeyPlayer(String name) {this.name = name;initMemberVars ();

}

private void initMemberVars () {goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public String getName () {return name;

}

public void setName(String name) {this.name = name;

}

public int getGoals () {return goals;

}

public int getAssists () {return assists;

}

public int getPoints () {return goals + assists;

}

public int getGamesPlayed () {return gamesPlayed;

}

public double getToi () {return toi;

}

public double getPenaltyMin () {return penaltyMin;

}

public void addPenaltyTime(double time) {penaltyMin += time;

}

public void addGoal () {goals ++;

}

public void addAssist () {assists ++;

}

public void addToi(double time) {toi += time;

}

public void addGamePlayed () {gamesPlayed ++;

}

}

FieldsFields are membervariables.Non-static membervariables are instancevariables.

PropertiesFields that can be set bythose using the class.

Default ValuesUninitialized membervariables are given defaultvalues.

7/23

Classes UML

Member Variables

public class HockeyPlayer {

private String name;private int goals;private int assists;private int gamesPlayed;private double toi;private double penaltyMin;

public HockeyPlayer () {name = "";initMemberVars ();

}

public HockeyPlayer(String name) {this.name = name;initMemberVars ();

}

private void initMemberVars () {goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public String getName () {return name;

}

public void setName(String name) {this.name = name;

}

public int getGoals () {return goals;

}

public int getAssists () {return assists;

}

public int getPoints () {return goals + assists;

}

public int getGamesPlayed () {return gamesPlayed;

}

public double getToi () {return toi;

}

public double getPenaltyMin () {return penaltyMin;

}

public void addPenaltyTime(double time) {penaltyMin += time;

}

public void addGoal () {goals ++;

}

public void addAssist () {assists ++;

}

public void addToi(double time) {toi += time;

}

public void addGamePlayed () {gamesPlayed ++;

}

}

FieldsFields are membervariables.Non-static membervariables are instancevariables.

PropertiesFields that can be set bythose using the class.

Default ValuesUninitialized membervariables are given defaultvalues.

7/23

Classes UML

Member Variables

public class HockeyPlayer {

private String name;private int goals;private int assists;private int gamesPlayed;private double toi;private double penaltyMin;

public HockeyPlayer () {name = "";initMemberVars ();

}

public HockeyPlayer(String name) {this.name = name;initMemberVars ();

}

private void initMemberVars () {goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public String getName () {return name;

}

public void setName(String name) {this.name = name;

}

public int getGoals () {return goals;

}

public int getAssists () {return assists;

}

public int getPoints () {return goals + assists;

}

public int getGamesPlayed () {return gamesPlayed;

}

public double getToi () {return toi;

}

public double getPenaltyMin () {return penaltyMin;

}

public void addPenaltyTime(double time) {penaltyMin += time;

}

public void addGoal () {goals ++;

}

public void addAssist () {assists ++;

}

public void addToi(double time) {toi += time;

}

public void addGamePlayed () {gamesPlayed ++;

}

}

FieldsFields are membervariables.Non-static membervariables are instancevariables.

PropertiesFields that can be set bythose using the class.

Default ValuesUninitialized membervariables are given defaultvalues.

7/23

Classes UML

Member Instance Methodsprivate void initMemberVars () {

goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public String getName () {return name;

}

public void setName(String name) {this.name = name;

}

public int getGoals () {return goals;

}

public int getAssists () {return assists;

}

public int getPoints () {return goals + assists;

Instance MethodsNon-static methods whosebehaviour depends on theparticular object instance.

8/23

Classes UML

Constructorspublic class HockeyPlayer {

private String name;private int goals;private int assists;private int gamesPlayed;private double toi;private double penaltyMin;

public HockeyPlayer () {name = "";initMemberVars ();

}

public HockeyPlayer(String name) {this.name = name;initMemberVars ();

}

private void initMemberVars () {goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

ConstructorSpecial method with noreturn type.Same name as the class.Called when creating aninstance.Can be overloaded.

Default ConstructorIf no constructor isdefined, Java provides adefault constructor with noparameters.It is good practice to definea constructor.

9/23

Classes UML

Constructorspublic class HockeyPlayer {

private String name;private int goals;private int assists;private int gamesPlayed;private double toi;private double penaltyMin;

public HockeyPlayer () {name = "";initMemberVars ();

}

public HockeyPlayer(String name) {this.name = name;initMemberVars ();

}

private void initMemberVars () {goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

ConstructorSpecial method with noreturn type.Same name as the class.Called when creating aninstance.Can be overloaded.

Default ConstructorIf no constructor isdefined, Java provides adefault constructor with noparameters.It is good practice to definea constructor.

9/23

Classes UML

What is this?

public HockeyPlayer(String name) {this.name = name;initMemberVars ();

}public void setName(String name) {

this.name = name;}

this keywordthis is a reference to the object (or the current instance).The object is an implicit parameter of all instance methods.

10/23

Classes UML

What is this?this keyword

this is a reference to the object (or the current instance).The object is an implicit parameter of all instance methods.Can also be used to call other constructors:

public HockeyPlayer () {name = "";goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public HockeyPlayer(String name) {this ();this.name = name;

}10/23

Classes UML

Mutators, Accessors, and Helpersprivate void initMemberVars () {

goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public int getPoints () {return goals + assists;

}

public void addGoal () {goals ++;

}

Helpersprivatemethod usedwithin the class.

AccessorsMethods that access themember variables withoutchanging them.

MutatorsMethods that change themember variables.

11/23

Classes UML

Mutators, Accessors, and Helpersprivate void initMemberVars () {

goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public int getPoints () {return goals + assists;

}

public void addGoal () {goals ++;

}

Helpersprivatemethod usedwithin the class.

AccessorsMethods that access themember variables withoutchanging them.

MutatorsMethods that change themember variables.

11/23

Classes UML

Mutators, Accessors, and Helpersprivate void initMemberVars () {

goals = 0;assists = 0;gamesPlayed = 0;toi = 0.0;penaltyMin = 0.0;

}

public int getPoints () {return goals + assists;

}

public void addGoal () {goals ++;

}

Helpersprivatemethod usedwithin the class.

AccessorsMethods that access themember variables withoutchanging them.

MutatorsMethods that change themember variables.

11/23

Classes UML

Getters and Setterspublic String getName () {

return name;}

public void setName(String name) {this.name = name;

}

GetterAccessor method thatretrieves a property.

SetterMutator method to changea property.

Best PracticeGood for properties.Violates OOP encapsulation when used for all fields.Why getter and setter methods are evilhttps://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html

12/23

Classes UML

Getters and Setterspublic String getName () {

return name;}

public void setName(String name) {this.name = name;

}

GetterAccessor method thatretrieves a property.

SetterMutator method to changea property.

Best PracticeGood for properties.Violates OOP encapsulation when used for all fields.Why getter and setter methods are evilhttps://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html

12/23

Classes UML

Getters and Setterspublic String getName () {

return name;}

public void setName(String name) {this.name = name;

}

GetterAccessor method thatretrieves a property.

SetterMutator method to changea property.

Best PracticeGood for properties.Violates OOP encapsulation when used for all fields.Why getter and setter methods are evilhttps://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html 12/23

Classes UML

TopHat Question 5

What is the output?

public class UseHockeyPlayer {

public static void main(String [] args) {HockeyPlayer h = new HockeyPlayer("Auston Matthews");System.out.print(h);

}

}

a. Auston Matthewsb. HockeyPlayer@5e265ba4

13/23

Classes UML

TopHat Question 6What is the output when executed?

public class SimpleClass {

public int x;

public SimpleClass(int x) {this.x = x;

}

public String toString () {return "" + x;

}

public static void main(String [] args) {SimpleClass a = new SimpleClass (10);SimpleClass b = new SimpleClass (10);b.x = 20;System.out.print(a + " " + b);

}}

14/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

s

Heap

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

s

y: 0

StaticA:x: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

s

y: 0

StaticA:x: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

s

y: 1

StaticA:x: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

s

y: 1

StaticA:x: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

s

y: 1

StaticA:x: 2

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

st

y: 1

StaticA:x: 2

y: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;StaticA t

= new StaticA ();

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

st

y: 1

StaticA:x: 2

y: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;StaticA t

= new StaticA ();t.x = 3;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

st

y: 1

StaticA:x: 3

y: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;StaticA t

= new StaticA ();t.x = 3;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

st

y: 1

StaticA:x: 3

y: 0

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;StaticA t

= new StaticA ();t.x = 3;t.y = 4;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

st

y: 1

StaticA:x: 3

y: 4

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;StaticA t

= new StaticA ();t.x = 3;t.y = 4;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

st

y: 1

StaticA:x: 3

y: 4

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;StaticA t

= new StaticA ();t.x = 3;t.y = 4;StaticA.x = 5;

15/23

Classes UML

Static Member VariablesStatic Member Variables

Class variablesStored in a special part of the heap.

Stack

st

y: 1

StaticA:x: 5

y: 4

Heap class StaticA {public static int x;public int y;

}public class StaticEx {

public staticvoid main(String [] args){

StaticA s= new StaticA ();

s.y = 1;s.x = 2;StaticA t

= new StaticA ();t.x = 3;t.y = 4;StaticA.x = 5;

15/23

Classes UML

TopHat Question 7

What is the output when executed?

public class SimpleStaticClass {

public static int x = 10;

public String toString () {return "" + x;

}

public static void main(String [] args) {SimpleStaticClass a = new SimpleStaticClass ();SimpleStaticClass b = new SimpleStaticClass ();b.x = 20;System.out.print(a + " " + b);

}}

16/23

Classes UML

TopHat Question 8

What is the output?

public class StaticEx2 {

private static int i = 0;

public static int f() {return ++i;

}

public static void main(String [] args) {System.out.print(f() + " " + f() + " " + f());

}

}

17/23

Classes UML

UML

Classes UML

Unified Modeling Language (UML)UML

A modeling language used in software engineering.A visual way to describe a software system.

Some UML DiagramsUse Case DiagramClass DiagramInstance Diagram

UML Resourceshttp://agilemodeling.com/

https://www.uml-diagrams.org/

Online Drawing Tool:http://www.umlet.com/umletino/umletino.html

18/23

Classes UML

Unified Modeling Language (UML)UML

A modeling language used in software engineering.A visual way to describe a software system.

Some UML DiagramsUse Case DiagramClass DiagramInstance Diagram

UML Resourceshttp://agilemodeling.com/

https://www.uml-diagrams.org/

Online Drawing Tool:http://www.umlet.com/umletino/umletino.html

18/23

Classes UML

Unified Modeling Language (UML)UML

A modeling language used in software engineering.A visual way to describe a software system.

Some UML DiagramsUse Case DiagramClass DiagramInstance Diagram

UML Resourceshttp://agilemodeling.com/

https://www.uml-diagrams.org/

Online Drawing Tool:http://www.umlet.com/umletino/umletino.html

18/23

Classes UML

Use Case DiagramBP1 MastermindBP1 Mastermind

Choose the random seed

Enter a guess

Choose to play againPlayer

Use Case DiagramSimple graphical representation of the system.Describes how the external actors (users) interact with thesystem.

19/23

Classes UML

Class Diagram

5..* 0..1

1..*

0..1 2..*

0..1

HockeyPlayer

name: Stringgoals: intassists: int

addGoal()setName(name: String)

Goalie

saves: intshots: int

getSavePct(): double

HockeyTeam

HockeyLeague

plays for I

plays f

or Ibelongs to

I

Class DiagramDescribes the structure of the system at the class level.

20/23

Classes UML

Instance DiagramHockeyLeague

name = NHL

wildHockeyTeam

name =Wildlocation = Minnesota

hawksHockeyTeam

name = Blackhawkslocation = Chicago

HockeyTeam

name = Maple Leafslocation = Toronto

HockeyPlayer

name = Auston MatthewsGoals = 10Assists = 6

Goalie

name = Frederik Andersensaves = 505shots = 540

Object / Instance DiagramDescribes the state of the system at the object level at agiven instance in time. 21/23

Classes UML

UML ExerciseDraw the use-case and class diagrams for the following UsedCar Sale system.

Vehicles are identified by a VIN, colour, engine type, gastype, and transmission type.Each vehicle has certificate recording the number ofkilometres, date of last inspection, number of accidents.Vehicles are sub-divided into:

cars, noting the number of passengerstrucks, noting the weight classvans, noting the number of passenger and the weight class

Clients are registered, indicating their name, address andphone number.Clients can search for and buy the used vehicles.Clients sign purchase contracts indicating the purchasedate, sale price, number of payments.

22/23

Classes UML

Further Reading

COMP SCI 200: Programming IzyBooks.com, 2015.zyBook code:WISCCOMPSCI200Fall2019

Chapter 12. Creating ClassesChapter 13. More Classes

23/23

Appendix References

Appendix

Appendix References

References

Appendix References

Image Sources I

https://brand.wisc.edu/web/logos/

http://www.zybooks.com/

24/23

top related