Chapter 7: Putting it Together - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 7: Putting it Together

Description:

test often rewrite as needed use good style (comments, spacing, naming) ... press Clear\nand re-key the amount\nand then press Enter\nto complete your deposit.\n – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 22
Provided by: Vinod70
Learn more at: https://www.d.umn.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7: Putting it Together


1
Chapter 7 Putting it Together
  • Method class and parameters
  • pass by value
  • Programming in the large
  • understand task
  • design interface
  • make code modular
  • localize events
  • test often
  • rewrite as needed
  • use good style (comments, spacing, naming)

2
Methods and Parameters
  • Method signature (header)
  • Type methodName(parameterlist)
  • as in void action(Event e, object arg)
  • Formal parameters - names given to values during
    method
  • treat like local variable (can change)
  • format Type Name, Type Name,

3
Method Calls
  • Invoking method on set of arguments
  • format InstanceorClass.methodName(arglist)
  • Argument list
  • list of expressions separated by commas
  • one expression for each parameter name
  • matched in order
  • type must match

4
Passing Parameters
  • All parameters in Java are passed by value
  • Value of expression calculated, copied, then
    given parameter name (no connection to calling
    expression made)
  • Methods cant change those values
  • void swap (int a, int b)
  • int temp a
  • a b b temp
  • let x be 2, y be 3, what is effect of
  • call myInst.swap(x,y)
  • on x,y? - none!

5
Passing Parameters (cont)
  • No way to change??
  • not true, can change parts of a class
  • class variable points to where in memory the
    instance resides, cant change the pointers by a
    method, but can change what they point to
  • void swap (Point a, Point b)
  • int temp a.x a.x b.x b.x temp
  • temp a.y a.y b.y b.y temp
  • swaps the x,y fields of points a and b

6
Rules of Thumb in Programming
  • Understand your task first
  • decide where to store the data
  • decide what sequence of actions should occur
  • know about error conditions
  • break the problem into easy-to-do pieces
  • understand completely before thinking about coding

7
  • Design your interface
  • pick your widgets
  • try to lay them out in an appealing way
  • determine who is responsible for events
  • Code in pieces
  • set up the look of your interface one piece at a
    time
  • dont worry about event handling
  • add events one at a time
  • always compile and check after adding a piece

8
  • Localize your events as much as possible
  • dealing with events on a local level makes the
    code more modular - easier to test in pieces
  • but dont be afraid to use a global event
    handling approach if it is appropriate
  • Try to think of difficult test cases
  • testing is a large part of verification
  • good testing is extremely important
  • Little things matter
  • add comments for readability
  • use good spacing
  • pick good names

9
  • Dont be afraid to rewrite/redesign
  • nobody writes perfect code the first time
  • coding may cause you to rethink design or even
    basic problem understanding issues
  • rewriting can also make your code more readable
  • Covet clarity, not brevity
  • short code doesnt mean good code
  • keep your classes/methods short when you can

10
import java.applet. import java.awt. public
class AnATM extends Applet public final
static int INITIAL 0 public final static int
DEPOSIT 1 public final static int WITHDRAW
2 private int state public final static
String INITIAL_INSTRUCTIONS "Press Deposit
or\nWithdraw buttons to\nmake a
transaction.\nPress Clear to end\nthis
session." public final static String
DONE_INSTRUCTIONS "Thank you for
doing\nbusiness with us.\nHave a nice day.\n\n"
public final static String DEPOSIT_INSTRUCTIONS
"Please key in amount\nof deposit and
then\npress Enter to confirm.\nIf you make
a\nmistake, press Clear\nand re-key the
amount\nand then press Enter\nto complete your
deposit.\n" public final static String
WITHDRAW_INSTRUCTIONS "Please key in amount\nof
withdrawl and then\npress Enter to confirm.\nIf
you make a\nmistake, press Clear\nand re-key the
amount\nand then press Enter\nto complete your
deposit.\n" public final static String
ZERO_PROBLEM "You must enter\na nonzero
amount\nfor your "
11
AmountDisplay adisplay new AmountDisplay()
DigitButton d1 new DigitButton("1",1,adisplay)
DigitButton d2 new DigitButton("2",2,adisplay)
DigitButton d3 new DigitButton("3",3,adispla
y) DigitButton d4 new DigitButton("4",4,adisp
lay) DigitButton d5 new DigitButton("5",5,adi
splay) DigitButton d6 new DigitButton("6",6,a
display) DigitButton d7 new
DigitButton("7",7,adisplay) DigitButton d8
new DigitButton("8",8,adisplay) DigitButton d9
new DigitButton("9",9,adisplay) DigitButton
d0 new DigitButton("0",0,adisplay)
DecimalButton dec new DecimalButton(adisplay)
Button clear new Button("Clear") Button
enter new Button("Enter") Button deposit
new Button("Deposit") Button withdraw new
Button("Withdraw") InstDisplay instructions
new InstDisplay() Panel keypan new Panel()
Panel actpan new Panel() Panel leftside
new Panel() Panel everything new Panel()
12
class DigitButton extends Button private
String dName private int dVal private
AmountDisplay aDisplay public
DigitButton(String name, int val, AmountDisplay
ad) super(name) dName name dVal
val aDisplay ad public boolean
action(Event e, Object arg) if (e.target
instanceof DigitButton)
aDisplay.addDigit(dVal) return true
return super.action(e,arg)
13
class DecimalButton extends Button private
AmountDisplay aDisplay public
DecimalButton(AmountDisplay ad)
super(".") aDisplay ad public
boolean action(Event e, Object arg) if
(e.target instanceof DecimalButton)
aDisplay.addDecimal() return true
return super.action(e,arg)
14
class AmountDisplay extends TextField private
long currentAmount 0 private boolean
decimalSet false private int afterDecimal
0 public AmountDisplay() super(10)
setEditable(false) public float
getCurrentAmount() float tempAmount
(float) currentAmount if (afterDecimal 1)
tempAmount / 10 else if (afterDecimal 2)
tempAmount / 100 return tempAmount
public void displayAmount()
setText(String.valueOf(getCurrentAmount()))
repaint()
15
public void addDigit(int digit) if
((!decimalSet) (afterDecimal lt 2))
currentAmount 10 currentAmount
digit if (decimalSet) afterDecimal
displayAmount() public void
addDecimal() if (!decimalSet)
decimalSet true afterDecimal 0
public void clearAmountDisplay()
currentAmount 0 decimalSet false
afterDecimal 0 displayAmount()
16
class InstDisplay extends TextArea String
currentText "" public InstDisplay()
super(1,15) setEditable(false)
public void clearInstDisplay() currentText
"" setText(currentText) repaint()
public void displayInstructions(String inst)
currentText currentText inst
setText(currentText) repaint()
17
public void init() keypan.setLayout(new
GridLayout(4,3,2,2)) keypan.add(d7)
keypan.add(d8) keypan.add(d9)
keypan.add(d4) keypan.add(d5) keypan.add(d6)
keypan.add(d1) keypan.add(d2)
keypan.add(d3) keypan.add(d0)
keypan.add(dec) actpan.setLayout(new
GridLayout(4,1,2,2)) actpan.add(clear)
actpan.add(enter) actpan.add(deposit)
actpan.add(withdraw) leftside.setLayout(new
BorderLayout(2,4)) leftside.add("North",adisp
lay) leftside.add("West",keypan)
leftside.add("East",actpan)
everything.setLayout(new BorderLayout(4,4))
everything.add("West",leftside)
everything.add("East",instructions)
add(everything) instructions.clearInstDisplay
() disableAll()
deposit.enable() withdraw.enable()
clear.enable() instructions.displayInstructio
ns(INITIAL_INSTRUCTIONS) adisplay.displayAmou
nt()
18
public boolean action(Event e, Object arg)
if (state INITIAL) if (e.target
clear) instructions.clearInstDisplay()
instructions.displayInstructions(DONE_INST
RUCTIONS) instructions.displayInstruction
s(INITIAL_INSTRUCTIONS)
adisplay.displayAmount() return true
else if (e.target deposit)
instructions.clearInstDisplay()
instructions.displayInstructions(DEPOSIT_INSTRUCTI
ONS) adisplay.displayAmount()
enableKeyPad() enter.enable()
deposit.disable() withdraw.disable()
state DEPOSIT return true
else if (e.target withdraw)
instructions.clearInstDisplay()
instructions.displayInstructions(WITHDRAW_INSTRUCT
IONS) adisplay.displayAmount()
enableKeyPad() enter.enable()
deposit.disable() withdraw.disable()
state WITHDRAW return true

19
else if (state DEPOSIT) if (e.target
clear) adisplay.clearAmountDisplay()
return true else if
(e.target enter) if
(adisplay.getCurrentAmount() 0.0)
instructions.clearInstDisplay()
instructions.displayInstructions(ZERO_PROBLEM
"deposit.\n\n") instructions.displayIns
tructions(DEPOSIT_INSTRUCTIONS) return
true else
instructions.clearInstDisplay()
instructions.displayInstructions("Thank you for
your\ndeposit of " String.valueOf(adisplay.getC
urrentAmount()) "\n\n")
instructions.displayInstructions(INITIAL_INSTRUCTI
ONS) disableAll()
deposit.enable() withdraw.enable()
clear.enable() adisplay.clearAmountDisp
lay() adisplay.displayAmount()
state INITIAL return true

20
else if (state WITHDRAW) if (e.target
clear) adisplay.clearAmountDisplay()
return true else if
(e.target enter) if
(adisplay.getCurrentAmount() 0.0)
instructions.clearInstDisplay()
instructions.displayInstructions(ZERO_PROBLEM
"withdraw.\n\n") instructions.displayIn
structions(WITHDRAW_INSTRUCTIONS)
return true else
instructions.clearInstDisplay()
instructions.displayInstructions("Thank you for
your\nwithdrawl of " String.valueOf(adisplay.ge
tCurrentAmount()) "\n\n")
instructions.displayInstructions(INITIAL_INSTRUCTI
ONS) disableAll()
deposit.enable() withdraw.enable()
clear.enable() adisplay.clearAmountDisp
lay() adisplay.displayAmount()
state INITIAL return true
return
super.action(e,arg)
21
public void disableKeyPad() d7.disable()
d8.disable() d9.disable() d4.disable()
d5.disable() d6.disable() d1.disable()
d2.disable() d3.disable() d0.disable()
dec.disable() public void enableKeyPad()
d7.enable() d8.enable() d9.enable()
d4.enable() d5.enable() d6.enable()
d1.enable() d2.enable() d3.enable()
d0.enable() dec.enable() public void
disableAll() disableKeyPad()
clear.disable() enter.disable()
deposit.disable() withdraw.disable()
public void start() repaint()
Write a Comment
User Comments (0)
About PowerShow.com