Welcome back to Software Development!
Review – Class Members Do review slides Finish MenuOption method project Instances and static Create UserInput class
Review – Class Members What are the three main types of class members?
Review – Class Members What are the three main types of class members? Methods
Review – Class Members What are the three main types of class members? Methods – class algorithms
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields – class variables any method of the class can use
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields – class variables any method of the class can use What the book calls “instance variables”
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields – class variables any method of the class can use What the book calls “instance variables” The data or information the class uses to do its job
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields – class variables any method of the class can use What the book calls “instance variables” The data or information the class uses to do its job Properties
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields – class variables any method of the class can use What the book calls “instance variables” The data or information the class uses to do its job Properties – “smart fields”
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields – class variables any method of the class can use What the book calls “instance variables” The data or information the class uses to do its job Properties – “smart fields” Methods that look like fields…you can use them almost like variables
Review – Class Members What are the three main types of class members? Methods – class algorithms The actual code that does the work – the ‘algorithm’ Fields – class variables any method of the class can use What the book calls “instance variables” The data or information the class uses to do its job Properties – “smart fields” Methods that look like fields…you can use them almost like variables They “protect” the class variables (fields)
Review - Scope
Review - Scope What does scope mean?
Review - Scope What does scope mean? The section of code a variable can be used in
Review - Scope What does scope mean? The section of code a variable can be used in Defined by the current block { }
Review - Scope What does scope mean? The section of code a variable can be used in Defined by the current block { } Inner blocks can see variables from outer blocks
Review - Scope What does scope mean? The section of code a variable can be used in Defined by the current block { } Inner blocks can see variables from outer blocks Outer blocks can not see variables from inner blocks
Review – Scope Modifiers
Review – Scope Modifiers We will call them access modifiers
Review – Scope Modifiers We will call them access modifiers What are access modifiers?
Review – Scope Modifiers We will call them access modifiers What are access modifiers? Keywords public and private
Review – Scope Modifiers We will call them access modifiers What are access modifiers? Keywords public and private Control who has access to the members of a class
Review – Scope Modifiers We will call them access modifiers What are access modifiers? Keywords public and private Control who has access to the members of a class public :
Review – Scope Modifiers We will call them access modifiers What are access modifiers? Keywords public and private Control who has access to the members of a class public : everyone has access and can change
Review – Scope Modifiers We will call them access modifiers What are access modifiers? Keywords public and private Control who has access to the members of a class public : everyone has access and can change private :
Review – Scope Modifiers We will call them access modifiers What are access modifiers? Keywords public and private Control who has access to the members of a class public : everyone has access and can change private : only members of same class have access and can change
Reading Questions
Reading Questions What are scope modifiers (public/private)
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)?
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)?
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables?
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters?
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters?
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters? Properties that limit what can be done to a field
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters? Properties that limit what can be done to a field Yesterday’s example
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters? Properties that limit what can be done to a field Yesterday’s example Mario and Jackson “guarding” or “filtering” access to Jake’s grade
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters?
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters?
Reading Questions What are scope modifiers (public/private) What are properties (Set/Get)? What is an instance (static and new)? What are instance variables? What are filters?
The real, the original, the authentic Nestle Toll House Chocolate Chip Cookie Recipe 2 1/4 cups all-purpose flour 1 teaspoon baking soda 1 teaspoon salt 1 cup (2 sticks, 1/2 pound) butter, softened 3/4 cup granulated [white] sugar 3/4 cup packed brown sugar 1 teaspoon vanilla extract 2 eggs 2 cups (12-ounce package) NESTLE TOLL HOUSE Semi-Sweet Chocolate Morsels 1 cup chopped nuts COMBINE flour, baking soda and salt in small bowl. Beat butter, granulated sugar, brown sugar and vanilla in large mixer bowl. Add eggs one at a time, beating well after each addition; gradually beat in flour mixture. Stir in morsels and nuts. Drop by rounded tablespoon onto ungreased baking sheets. BAKE in preheated 375-degree [Fahrenheit] oven for 9 to 11 minutes or until golden brown. Let stand for 2 minutes; remove to wire racks to cool completely. PAN COOKIE VARIATION: PREPARE dough as above. Spread into greased 15"x10" jelly-roll pan. Bake in preheated 375-degree [Fahrenheit] oven for 20 to 25 minutes or until golden brown. Cool in pan on wire rack. FOR HIGH ALTITUDE BAKING (>5,200 feet): INCREASE flour to 2 1/2 cups; add 2 teaspoonfuls water with flour; reduce both granulated sugar and brown sugar to 2/3 cup each. Bake at 375 degrees Fahrenheit, drop cookies for 8 to 10 minutes and pan cookies for 17 to 19 minutes If I give this recipe to you, what good is it? Can you eat this recipe? No, you have to make a cookie from the recipe…
Instance
Instance A class is like a recipe
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…)
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie The cookie is an “instance” of the recipe
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie The cookie is an “instance” of the recipe You have “created” or “declared” a cookie from the recipe
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie The cookie is an “instance” of the recipe You have “created” or “declared” a cookie from the recipe Now you can “use” the cookie
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie The cookie is an “instance” of the recipe You have “created” or “declared” a cookie from the recipe Now you can “use” the cookie You have to create an instance of a class to use it…
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie The cookie is an “instance” of the recipe You have “created” or “declared” a cookie from the recipe Now you can “use” the cookie You have to create an instance of a class to use it… Except…
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie The cookie is an “instance” of the recipe You have “created” or “declared” a cookie from the recipe Now you can “use” the cookie You have to create an instance of a class to use it… Except…Unless…
Instance A class is like a recipe You can’t eat the recipe (unless you like paper…) You have to make a cookie The cookie is an “instance” of the recipe You have “created” or “declared” a cookie from the recipe Now you can “use” the cookie You have to create an instance of a class to use it… Except…Unless…the some part of the class is useful all by itself…
The Coffee Pot Show coffee pot Ask what its function is Make coffee Ask what you have to do to use that function Add water and coffee Can you make the coffee if you don’t add water and coffee grounds?
The Coffee Pot Think of a coffee pot as an object…
The Coffee Pot Think of a coffee pot as an object…a class
The Coffee Pot Think of a coffee pot as an object…a class It has one method
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee()
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater 2) amountOfCoffeeGrounds
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater 2) amountOfCoffeeGrounds The method needs the fields to work
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater 2) amountOfCoffeeGrounds The method needs the fields to work Does this class have any use not requiring the fields?
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater 2) amountOfCoffeeGrounds The method needs the fields to work Does this class have any use not requiring the fields? The glass pot can carry water…
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater 2) amountOfCoffeeGrounds The method needs the fields to work Does this class have any use not requiring the fields? The glass pot can carry water…acts as a water pitcher
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater 2) amountOfCoffeeGrounds The method needs the fields to work Does this class have any use not requiring the fields? The glass pot can carry water…acts as a water pitcher We have a new method
The Coffee Pot Think of a coffee pot as an object…a class It has one method – MakeCoffee() It has two fields 1) amountOfWater 2) amountOfCoffeeGrounds The method needs the fields to work Does this class have any use not requiring the fields? The glass pot can carry water…acts as a water pitcher We have a new method – UseAsWaterPitcher()
Static Methods
Static Methods Some methods do not need to use any of the class fields
Static Methods Some methods do not need to use any of the class fields Examples:
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods:
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods: HitEnterTo()
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods: HitEnterTo() GetString()
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods: HitEnterTo() GetString() GetNumber()
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods: HitEnterTo() GetString() GetNumber() GetMenuOption()
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods: HitEnterTo() GetString() GetNumber() GetMenuOption() These can be static methods
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods: HitEnterTo() GetString() GetNumber() GetMenuOption() These can be static methods Most methods do need to use some of the class fields
Static Methods Some methods do not need to use any of the class fields Examples: Your “get user input” methods: HitEnterTo() GetString() GetNumber() GetMenuOption() These can be static methods Most methods do need to use some of the class fields These can not be static methods
Static Methods
Static Methods static
Static Methods static The keyword static means it doesn’t use any of the class fields
Static Methods static The keyword static means it doesn’t use any of the class fields You can use static methods without creating an instance of the class
Non-static Methods
Non-static Methods To use methods of a class that are not static…
Non-static Methods To use methods of a class that are not static… You must create an instance of the class
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example:
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random…
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum;
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum; Variable name
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum; Variable type Variable name
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum; Variable type Variable name Declaring a variable named rndNum of type Random
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum; Variable type Variable name Declaring a variable named rndNum of type Random You have created the “recipe” … you haven’t baked the cookie yet
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum = new Random();
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum = new Random(); Create the recipe
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum = new Random(); Create the recipe Bake the cookie… …create an instance of the class Random as a variable
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum = new Random(); Create the recipe Bake the cookie… …create an instance of the class Random as a variable Keyword new is used to create an instance of a class
Non-static Methods To use methods of a class that are not static… You must create an instance of the class … as a variable using keyword new Example: the class Random… Random rndNum = new Random(); int a = rndNum.Next(1, 20);
Instance Variables
Instance Variables The term the book uses for fields
Instance Variables The term the book uses for fields Fields are the variables of a class
Instance Variables The term the book uses for fields Fields are the variables of a class You can’t use a class with fields unless you make an instance of the class
Instance Variables The term the book uses for fields Fields are the variables of a class You can’t use a class with fields unless you make an instance of the class Hence…
Instance Variables The term the book uses for fields Fields are the variables of a class You can’t use a class with fields unless you make an instance of the class Hence…instance variables
Instance Variables The term the book uses for fields Fields are the variables of a class You can’t use a class with fields unless you make an instance of the class Hence…instance variables We will simply use the term fields
Creating a class
Creating a class class nameOfClass
Creating a class class nameOfClass { members of the class (fields, properties, methods) }
Creating a class class nameOfClass { members of the class (fields, properties, methods) }
Your task
Your task Make a class containing all your “get user input” methods
Your task Make a class containing all your “get user input” methods Name the class GetUserInput
Your task Make a class containing all your “get user input” methods Name the class GetUserInput This will be a completely static class
Your task Make a class containing all your “get user input” methods Name the class GetUserInput This will be a completely static class Every method in it will be static
Your task Make a class containing all your “get user input” methods Name the class GetUserInput This will be a completely static class Every method in it will be static It will have four methods in it:
Your task Make a class containing all your “get user input” methods Name the class GetUserInput This will be a completely static class Every method in it will be static It will have four methods in it: HitEnterTo()
Your task Make a class containing all your “get user input” methods Name the class GetUserInput This will be a completely static class Every method in it will be static It will have four methods in it: HitEnterTo() GetString()
Your task Make a class containing all your “get user input” methods Name the class GetUserInput This will be a completely static class Every method in it will be static It will have four methods in it: HitEnterTo() GetString() GetNumber()
Your task Make a class containing all your “get user input” methods Name the class GetUserInput This will be a completely static class Every method in it will be static It will have four methods in it: HitEnterTo() GetString() GetNumber() GetMenuSelection()