Professional Documents
Culture Documents
Javalab To Create Calculator
Javalab To Create Calculator
alecalled Calculator.class.
Wecreatedtwoclassesforthisproject.Theyare Main and Calculator.
Theyliveinsourceles Main.java and Calculator.java.TheMain
classcontainsthe main() methodwhereyourprogramstartsrunning.The
main() methodcreatesa Calculator object,whichcontainsallyour
calculatorcode.The main() method could domore:forexampleitcould
create two calculator objects,whichwouldshowtwocalculatorsonthe
screen!
Javaorganizesitslesinto packages.Netbeansputthestatement package
jlab2 atthetopofeachofyourjlab2sourceles.Anycodeyouaddtoyour
projectshouldhavethesamepackagestatementontop.(Wewontdiscuss
thesemanticsofpackagesjustyet.Butknowyethatthisisessential.)
GettingStarted
Hereisallyouneedtodoforyour Main class.Addthefollowingtothe main
methodinyour Main class:
Calculatorc=newCalculator();
Tostartyour Calculator classyoucaneithercopy-pastecodefromyourprevious MultGui.java lefromjlab1,oryoucanstarttypingfresh.
Inordertogetthecodefrom MultGui,select File / openproject or openrecent
project,andthen jlab1.Navigatedowntothesourcepackages,andopenup
MultGui.java.
Copy-pastethecodeyouwanttokeepintoyour Calculator.java class. Be
suretoretain the packagejlab2 and classCalculator statements!Also
besuretorenametheconstructorto Calculator.Andaddthe implements
statementtotheclassdeclaration.
Nowclosethejlab1project!I mean it.
2Buildacalculator
Yourcalculatorshouldlooksomethinglikethis,withtwoinputareaslabeledX
andY,andaresultarea.Itshouldhavethevebuttonsforthefouroperationsand
aclearoperation(whichsetseverythingtozero).
3