4_State_Slides.pptx

4_State_Slides.pptx - 1 Click to edit Master subtitle style...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Click to edit Master subtitle style Comp 401 State Instructor: Prasun Dewan 2 What if BMI Calculations With General Must re-enter height each time! 3 What if BMI Calculations With public double calculateMyBMI( double weight) { final double MY_HEIGHT = 1.77; return ( new ABMICalculator).calculateBMI(weight, MY_HEIGHT); } Must only enter the weight But the height is hardwired! Must create a separate class for each user! General purpose solution that does 4 Caculate two BMIs using one instance of ABMISpreadsheet and changing only the weight BMI Spreadsheet State: Data remembered by an object between computations 5 Instance Variables ABMICalculator Instance calculateBM I Paramete rs Body accesse s ABMISpreadsheet Instance getBMI Instance Variables Body accesse s Belong to a single method Local variable Belong to all methods of an instance Global variable 6 State-less vs. State-full Objects Identical Instances ~ car radios with no presets Different Instances ~ car radios with presets 7 Declaring Instance Variables public class ABMISpreadsheet { double height; ... double weight; ... public double getBMI() { return weight/(height*height); } } Missin g Code No Paramete rs Instance Variables 8 Object Access to a Class public class ABMISpreadsheet { double height; ... double weight; ... public double getBMI() { return weight/(height*height); } } ObjectEditor Outside Access: Variables should not be public (like hidden thoughts) But ObjectEditor 9 Accessing Instance Variables via Public ABMISpreadsheet Instance weight height getWeig ht() setWeig ht() getHeig ht() setHeigh t() getBMI( ) ObjectEditor call s writ es weig ht call s read s heig ht call s read s writ es call s new weig ht new heig ht read s 10 Coding Getter and Setter Methods ABMISpreadsheet Instance weight getWeig ht() setWeig ht() ObjectEditor call s writ es weig ht call s read s new weig ht 11 Function vs. Procedure procedure: deposit function: withdraw 12 Coding Getter and Setter Methods ABMISpreadsheet Instance weight getWeig ht() setWeig ht() ObjectEditor call s writ es weig ht call s read s new weig ht public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } procedure returns nothing function 13 Getter and Setter Methods public class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight( double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; procedure returns nothing function 14 Properties public class ABMISpreadsheet { double height;...
View Full Document

This note was uploaded on 09/25/2010 for the course COMP 004 taught by Professor Staff during the Fall '08 term at UNC.

Page1 / 39

4_State_Slides.pptx - 1 Click to edit Master subtitle style...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online