Vous êtes sur la page 1sur 9

CreatingaCalculatorinJava

CongratulationsonbecomingthenewestemployeeofAmazon.com.AtAmazonweprideourselvesbyusingJavaandweunderstandifithasbeenawhile
sinceyouhaveusedit.AsaSoftwareEngineer,itisassumedthatyouarefamiliarwithJava,sotheseinstructionswillbeusedasarefresher.Aftercompletingthese
instructionsyoushouldbeacclimatedandreadytostartworkinginJavawithus.Pleasecompletetheseinstructionsbeforeyouleaveonyourfirstday.Buildinga
calculatorinJavaisafairlycomplexJavaprojectthatcanbemadebyanyseasonedprogrammerwhohasagoodgraspontheJavaprogramminglanguage.

Theseinstructionsapplyto:
Platforms:
Windows2008Server,Windows7,Windows8,WindowsXP,WindowsServer2012,WindowsVista,Windows10
JavaVersions:
7.0

Prerequisites:
ComputerwithaWindows32bitor64bitoperatingsystem,MacOSX,orLinux
JavaSDK7.0
EclipseIDEforJavaDevelopers
ExtensiveKnowledgeofJava
Timerequired:12hours

Resources:
https://netbeans.org/kb/articles/learnjava.html
https://docs.oracle.com/javase/tutorial/

GettingStarted

STEP1:
OpenanewprojectwithinEclipse

STEP2:
CreateanewjavaclassthatextendsJFrameandimplementsActionListener

STEP3:
Importalltheclasseswithinjava.awt,javax.swing,andjava.awt.event.

STEP4:
Declaretheglobalvariables.
STEP4.1:
DeclareaJLabelforthedisplay.
STEP4.2:
Declareanarrayoftenbuttonsforthenumbers.
STEP4.3:
Declareeightmorebuttonsfortheaddition,subtraction,multiply,divide,signchange,decimal,equalandclearbutton.
STEP4.4:
Declareadoublearrayofsizethirteencallednumberstostorenumbers.
STEP4.5:
Declareacharacterarrayofsizetwelvecalledfunctionstostorefunctionsymbols.
STEP4.6:
DeclareanintegercallednumbersNumtokeeptrackofthenumberofnumbersinthearrayinstep4.4.
STEP4.7:
DeclareanintegercalledfunctionsNumtokeeptrackofthenumberoffunctionsymbolsinthearrayinstep4.5.
STEP4.8:
DeclareabooleancalledclearTextandsetittofalsetokeeptrackofwhetherthedisplayshouldbeclearedonthenextbuttonpress.

Figure1:VariableDeclaration

Constructor

STEP5:
Createaconstructorforthecalculator

STEP6:
Setthecalculatorssizetohaveawidthofthreehundredpixelsandaheightoffivehundredpixels

STEP7:
SettheresizabilityoftheJFrametofalse

STEP8:
SetthedefaultcloseoperationoftheJFrametoexitonclose

STEP9:
CreateaGridLayoutwithsixrowsandfourcolumns

STEP10:
SetthelayoutoftheJFrametotheGridLayoutcreatedinstep9.

STEP11:
CreateanarrayofsixJPanelstoserveastherowsofthecalculator.

STEP12:
CreateanewJPanelforeachindexinthearraycreatedinstep11.

Figure2:BeginningtheCalculatorC\constructor

STEP13:
CreateaDimensionforthebuttonsthathasawidthofsixtypixelsandaheightofsixtypixels.

STEP14:
CreateaDimensionforthezerobuttonthathasawidthofonehundredandtwentypixelsandaheightofsixtypixels.

STEP15:
Createalooptocreateandsetupthenumberbuttons.
STEP15.1:
Createanewbuttonforeachindexofthebuttonsaarray.
STEP15.2:
Setthetextofeachbuttontotheintegervalueoftheiteratoroftheloop.
STEP15.3:
Addtheactionlistenertoeachbuttonwithaparameterofthis.
STEP15.4:
Setthepreferredsizeofeachbuttontothedimensioncreatedinstep13.

STEP16:
Setthepreferredsizeofthefirstbuttoninthebuttonsarraytothedimensioncreatedinstep14.

Figure3:NumberButtonSetup

STEP17:
Setuptheaddition,subtraction,multiplication,division,signchange,decimal,equal,andclearbuttons.
STEP17.1:
Createanewbuttonforeachbutton.
STEP17.2:
Setthetextofeachbutton(+foraddition,forsubtraction,xformultiplication,/fordivision,+/forsignchange,.fordecimal,=for
equal,andCforclear).
STEP17.3:
Addtheactionlistenertoeachbuttonwithaparameterofthis.
STEP17.4:
Setthepreferredsizeofeachbutton,excepttheclearbutton,tothedimensioncreatedinstep13.
STEP17.5:
Setthepreferredsizeoftheclearbuttontothedimensioncreatedinstep14.

Figure4:NonnumberbuttonSetup

STEP18:
SetUpthedisplay
STEP18.1:
Setthepreferredsizeofthedisplaytoadimensionthatis240pixelswideand60pixelshigh
STEP18.2:
Setthetextto0
STEP18.3:
Setthebackgroundcolortowhite
STEP18.4:
Setwhetherornotitisopaquetotrue
STEP18.5:
SetthehorizontalalignmenttoSwingConstants.RIGHT
STEP18.6:
SettheverticalalignmenttoSwingConstants.BOTTOM

Figure5:SetupDisplay

STEP19:
SetUpthefirstrowoftherowsarraycreatedinstep11.
STEP19.1:
Addthedisplaytothefirstrowoftherowsarray.
STEP19.2:
AddthefirstrowoftherowsarraytotheJFrame.

STEP20:
SetUpthesecondrowoftherowsarraycreatedinstep11.
STEP20.1:
Addtheclearbuttontothesecondrowoftherowsarray.
STEP20.2:
Addthesignbuttontothesecondrowoftherowsarray.
STEP20.3:
Addthedivisionbuttontothesecondrowoftherowsarray.
STEP20.4:
AddthesecondrowoftherowsarraytotheJFrame.

STEP21:
Setupthethirdrowoftherowsarraycreatedinstep11.
STEP21.1:
Addtheseventhbuttonofthebuttonsarraytothethirdrowoftherowsarray.
STEP21.2:
Addtheeighthbuttonofthebuttonsarraytothethirdrowoftherowsarray.
STEP21.3:
Addtheninthbuttonofthebuttonsarraytothethirdrowoftherowsarray.
STEP21.4:
Addthemultiplicationbuttontothethirdrowoftherowsarray.
STEP21.5:
AddthethirdrowoftherowsarraytotheJFrame.

STEP22:
Setupthefourthrowoftherowsarraycreatedinstep11.
STEP22.1:
Addthefourthbuttonofthebuttonsarraytothefourthrowoftherowsarray.
STEP22.2:
Addthefifthbuttonofthebuttonsarraytothefourthrowoftherowsarray.
STEP22.3:
Addthesixthbuttonofthebuttonsarraytothefourthrowoftherowsarray.
STEP22.4:
Addthesubtractionbuttontothefourthrowoftherowsarray.
STEP22.5:
AddthefourthrowoftherowsarraytotheJFrame.

STEP23:
Setupthefifthrowoftherowsarraycreatedinstep11.
STEP23.1:
Addthefirstbuttonofthebuttonsarraytothefifthrowoftherowsarray.
STEP23.2:
Addthesecondbuttonofthebuttonsarraytothefifthrowoftherowsarray.
STEP23.3:
Addthethirdbuttonofthebuttonsarraytothefifthrowoftherowsarray.
STEP23.4:
Addtheadditionbuttontothefifthoftherowsarray.
STEP23.5:
AddthefifthrowoftherowsarraytotheJFrame.

STEP24:
Setupthesixthrowoftherowsarraycreatedinstep11.
STEP24.1:
Addthezerothbuttonofthebuttonsarraytothesixthrowoftherowsarray.
STEP24.2:
Addthedecimalbuttontothesixthrowoftherowsarray.
STEP24.3:
Addtheequalbuttontothesixthrowoftherowsarray.
STEP24.4:
AddthesixthrowoftherowsarraytotheJFrame.

Figure6:Addrows

STEP25:
SetthevisibilityoftheJFrametotrue.

Figure7:SetVisibility

NumberButtonClickedMethod

STEP26:
DeclareamethodthatisprivateanddoesnotreturnanythingthathasaintegerparameterthatiscallednumberButtonClicked.

STEP27:
Createanifstatementthatreturnstrueifthetextinthedisplayisequalto0ortheclearTextglobalvariableistrue.
STEP27.1:
Setthetextofthedisplayequaltothestringvalueoftheintegerparameteriftheifstatementistrue.
STEP27.2:
SettheclearTextglobalvariabletofalse.

STEP28:
Createanelsestatementthatistheoppositeoftheifstatementinstep27.

STEP29:
Setthetextofthedisplayequaltotheintegerparameteraddedontothetextcurrentlyinthedisplay.

Figure8:NumberButtonClicked

ActionPerformedMethod

STEP30:
DeclareamethodthatispublicanddoesnotreturnanythingthathasanActionEventparameterthatiscalledactionPerformed.

STEP31:
CreateanifstatementthatreturnstrueifthetextinthedisplayisequaltoErrororiftheactionEventperformedistheclearbuttonbeingpressed.
STEP31.1:

Setthetextinthedisplayto0iftheifstatementistrue.
STEP31.2:

SettheglobalvariableclearTexttofalse.

Figure9:Clearingthedisplay

STEP32:
CreateanifstatementthatreturnstrueiftheactionEventperformedisthesignbuttonbeingpressed.
STEP32.1:

Createanifstatementthatreturnstrueifthetextinthedisplayisnotequalto0.
STEP32.1.1:

Createanifstatementthatreturnstrueifthefirstcharacterofthedisplayis.
STEP32.1.1.1:

Setthetextinthedisplaytoallofthetextcurrentlyinthedisplayexceptthefirstcharacter.
STEP32.1.2:

Createanelsestatementthatistheoppositeoftheifstatementinstep32.1.1.
STEP32.1.2.1:

Setthetextinthedisplaytothetextcurrentlyinthedisplayaddedontoa.

Figure10:SignButton

STEP33:
Createanifthatgoeswiththeifstatementinstep32thatreturnstrueifthedisplaytextslengthislessthan25.

Figure11:MaxLength


STEP33.1:
CreateanifstatementthatreturnstrueiftheactionEventperformedisequaltothe0button.
STEP33.1.1:

CreateanifstatementthatreturnstrueiftheglobalvariableclearTextistrue.
STEP33.1.1.1:

Setthetextinthedisplayto0.
STEP33.1.2:

Createanifstatementthatreturnstrueifthetextinthedisplayisnotequalto0.
STEP33.1.2.1:

Setthetextofthedisplayequalto0addedontothetextcurrentlyinthedisplay.

Figure12:ZeroButton

STEP33.2:

CreateanelseifstatementthatreturnstrueiftheactionEventperformedisequaltothe1button.
STEP33.2.1:

CallthenumberButtonClickedmethodandpassitavalueof1.
STEP33.3:

Repeatstep33.2and33.2.1forbutton29.

Figure13:OtherNumberButtons

STEP33.4:

CreateanelseifstatementthatreturnstrueiftheactionEventperformedisequaltothedecimalbutton.
STEP33.4.1:

CreateanifstatementthatreturnstrueiftheglobalvariableclearTextistrue.
STEP33.4.1.1:

Setthetextinthedisplayto0..
STEP33.4.1.2:

SettheglobalvariableclearTexttofalse.
STEP33.4.2:

Createanifstatementthatreturnstrueisthedisplaydoesnotcurrentlycontaina..
STEP33.4.3:

Setthetextofthedisplayequalto.addedontothetextcurrentlyinthedisplay.

Figure14:DecimalButton

STEP33.5:
Createanifstatementthatreturnstrueifthelastcharacterinthedisplaystextisnotequalto+,,x,/,andthedisplaystextislessthan
twentyfourcharacters.
STEP33.5.1:

SettheglobalvariableclearTexttofalse.
STEP33.5.2:

CreateanifstatementtheactionEventperformedisequaltothedivisionbutton.
STEP33.5.2.1:

Setthetextofthedisplayequalto/addedontothetextcurrentlyinthedisplay.
STEP33.5.3:

CreateanifstatementtheactionEventperformedisequaltotheadditionbutton.
STEP33.5.3.1:

Setthetextofthedisplayequalto+addedontothetextcurrentlyinthedisplay.
STEP33.5.4:

CreateanifstatementtheactionEventperformedisequaltothesubtractionbutton.
STEP33.5.4.1:

Setthetextofthedisplayequaltoaddedontothetextcurrentlyinthedisplay.
STEP33.5.5:

CreateanifstatementtheactionEventperformedisequaltothemultiplicationbutton.
STEP33.5.5.1:

Setthetextofthedisplayequaltoxaddedontothetextcurrentlyinthedisplay.

Figure15:FunctionButtons

STEP34:
CreateanelseifstatementthatreturnstrueiftheactionEventperformedisequaltotheequalbutton.
STEP34.1:

Createanifstatementthatreturnstrueifthelastcharacterinthedisplaystextisequalto+,,x,or/.
STEP34.1.1:

SetthetextofthedisplaytoError.
STEP34.2:

Createanelsestatementcorrespondstotheifstatementcreatedinstep34.1.
STEP34.2.1:

CreateavariablecalledprevFunctionIndexandsetitequalto1.
STEP34.2.2:

Createavariablecallediandsetitequalto0.
STEP34.2.3:

Setthetextinthedisplayequaltothetextinthedisplayaddedonto0
STEP34.2.3.1:

Setthevariableithatwascreatedinstep34.2.2to1.

Figure16:EqualButton

STEP34.2.4:
Createawhilestatementthatcontinuesloopingaslongasthevariableiislessthanthelengthofthetextofthedisplay.
STEP34.2.4.1:

Createanifstatementthatreturnstrueifthecharacteratindexinthedisplaystextisequalto+,,x,or/.
STEP34.2.4.1.1:
CreateavariablecalleddisplayNumberasaString.
STEP34.2.4.1.2:
SetthevariabledisplayNumberthatwascreatedinstep34.2.4.1.1tothetextinthedisplaythatisbetweenthe
indexesofthevariableprevFunctionIndex+1andthevariablei.
STEP34.2.4.1.3:
CreateavariablecalledrealNumberasadouble.
STEP34.2.4.1.4:
SetthevariablerealNumberthatwascreatedinstep34.2.4.1.3tothevariabledisplayNumberparsedasadouble.
STEP34.2.4.1.5:
SetthearrayvalueofnumbersattheindexofnumbersNumtothevariablerealNumberthatwascreatedinstep
34.2.4.1.3.
STEP34.2.4.1.6:
IncrementthevariablenumbersNum.
STEP34.2.4.1.7:
Declareavariablecalledfunctionasachar.
STEP34.2.4.1.8:
Setthevariablefunctionthatwascreatedinstep34.2.4.1.7tothecharattheindexofthevalueatvariableiofthe
textinthedisplay.
STEP34.2.4.1.9:
SetthearrayvalueoffunctionsattheindexoffunctionsNumtothevariableoffunctionthatwascreatedinstep
34.2.4.1.7.
STEP34.2.4.1.10:
IncrementthevariablefunctionsNum.
STEP34.2.4.1.11:
SetthevariableprevFunctionIndextothevariablei.

STEP34.2.5:
Repeatsteps34.2.4.1.1through34.2.4.1.6.

Figure17:ParsingtheNumbersandFunctions

STEP34.2.6:
Createaforstatementthatsetsthevariableito0,stopsafterthevariableibecomesgreaterthanfunctionsNum,andincrementsthe
variablei.
STEP34.2.6.1:
Createanifstatementthatistrueifthearrayvalueoffunctionsattheindexofthevalueofvariableiisequaltoxor/.
STEP34.2.6.1.1:
Createanifstatementthatistrueifthearrayvalueoffunctionsattheindexofthevalueofvariableiisequalto
x.
STEP34.2.6.1.1.1:
Setthearrayvalueofthearraycallednumbersattheindexofthevaluetoitothearrayvalueofofthe
arraycallednumbersattheindexofthevalueofimultipliedbythearrayvalueofofthearraycalled
numbersattheindexofthevalueofi+1.
STEP34.2.6.1.2:
Createanelseifstatementthatistrueifthearrayvalueoffunctionsattheindexofthevalueofvariableiisequal
to/.
STEP34.2.6.1.2.1:
Setthearrayvalueofthearraycallednumbersattheindexofthevaluetoitothearrayvalueofofthe
arraycallednumbersattheindexofthevalueofidividedbythearrayvalueofofthearraycalled
numbersattheindexofthevalueofi+1.
STEP34.2.6.1.3:
Createaforstatementthatcreateanintvariablej,initializesittothevariablei+1,stopsafterjbecomesgreater
thannumbersNum,andincrementsthevariablej.
STEP34.2.6.1.3.1:
Setthearrayvalueofthearraycallednumbersattheindexofthevalueofthevariablejtothearray
valueofthearraycallednumbersattheindexofthevalueofthevariablej+1.
STEP34.2.6.1.4:

DecrementnumbersNum.
STEP34.2.6.1.5:
Createaforstatementthatcreateanintvariablej,initializesittothevariablei,stopsafterjbecomesgreaterthan
functionsNum,andincrementsthevariablej.
STEP34.2.6.1.5.1:
Setthearrayvalueofthearraycalledfunctionsattheindexofthevalueofthevariablejtothearray
valueofthearraycalledfunctionsNumattheindexofthevalueofthevariablej+1.
STEP34.2.6.1.6:
DecrementfunctionsNum.
STEP34.2.6.1.7:
Decrementthevariablei.

Figure18:MultiplyandDivide

STEP34.2.6:
Createaforstatementthatsetsthevariableito0,stopsafterthevariableibecomesgreaterthanfunctionsNum,andincrementsthe
variablei.
STEP34.2.6.1:
Createanifstatementthatistrueifthearrayvalueoffunctionsattheindexofthevalueofvariableiisequalto+or.
STEP34.2.6.1.1:
Createanifstatementthatistrueifthearrayvalueoffunctionsattheindexofthevalueofvariableiisequalto
+.
STEP34.2.6.1.1.1:
Setthearrayvalueofthearraycallednumbersattheindexofthevaluetoitothearrayvalueofofthe
arraycallednumbersattheindexofthevalueofimultipliedbythearrayvalueofofthearraycalled
numbersattheindexofthevalueofi+1.
STEP34.2.6.1.2:
Createanelseifstatementthatistrueifthearrayvalueoffunctionsattheindexofthevalueofvariableiisequal
to.
STEP34.2.6.1.2.1:
Setthearrayvalueofthearraycallednumbersattheindexofthevaluetoitothearrayvalueofofthe
arraycallednumbersattheindexofthevalueofidividedbythearrayvalueofofthearraycalled
numbersattheindexofthevalueofi+1.
STEP34.2.6.1.3:
Createaforstatementthatcreateanintvariablej,initializesittothevariablei+1,stopsafterjbecomesgreater
thannumbersNum,andincrementsthevariablej.
STEP34.2.6.1.3.1:
Setthearrayvalueofthearraycallednumbersattheindexofthevalueofthevariablejtothearray
valueofthearraycallednumbersattheindexofthevalueofthevariablej+1.
STEP34.2.6.1.4:

DecrementnumbersNum.
STEP34.2.6.1.5:
Createaforstatementthatcreateanintvariablej,initializesittothevariablei,stopsafterjbecomesgreaterthan
functionsNum,andincrementsthevariablej.
STEP34.2.6.1.5.1:
Setthearrayvalueofthearraycalledfunctionsattheindexofthevalueofthevariablejtothearray
valueofthearraycalledfunctionsNumattheindexofthevalueofthevariablej+1.
STEP34.2.6.1.6:
DecrementfunctionsNum.
STEP34.2.6.1.7:
Decrementthevariablei.

Figure19:AddandSubtract

STEP34.2.7:

Createanifstatementthatistrueifthefirstindexinthearraycallednumbersisequaltopositiveinfinityornegativeinfinity.
STEP34.2.8:

Createanelsestatement.
STEP34.2.8.1:

Createastringcalledresultandsetittothestringofthefirstindexinthearraycallednumbers.
STEP34.2.8.2:

Createastringcalledpatternandsetitequalto([0])\\1*.
STEP34.2.8.3:
Createanifstatementthatistrueifeverythingafterthedecimalinresultmatchesthevariablepatterncreatedinstep
34.2.8.2.
STEP34.2.8.3.1:

Setthetextinthedisplaytotheeverythingbeforethedecimalinthevariableresultcreatedinstep34.2.8.1.
STEP34.2.8.4:

Createanelsestatement.
STEP34.2.8.4.1:

Setthetextinthedisplaytothevariableresult.
STEP34.2.9:

SetthevariableclearTexttotrue.
STEP34.2.10:

SetthevariablenumbersNumequalto0.
STEP34.2.11:

SetthevariablefunctionsNumequalto0.

Figure20:DisplaytheResult

MainMethod

STEP35:
Createamainmethod.

STEP36:
CreateanewCalculatorobject.

Figure21:CreatetheCalculator

Congratulationsyouhavecompletedthistutorial!

Vous aimerez peut-être aussi