CSC 222: Object-Oriented Programming Fall 2015 · CSC 222: Object-Oriented Programming Fall 2015...

10
1 1 CSC 222: Object-Oriented Programming Fall 2015 Object-oriented design OO design review objects, cohesion, loose coupling, polymorphism, … Model-View-Controller (MVC) pattern netBeans IDE create/edit/run a project, advanced features GUIBuilder, Swing GUI toolkit Object-oriented development OO design is the dominant approach to large-scale software development developing cohesive (& loosely coupled) software objects allows for 1. independent teams to contribute 2. independent testing & debugging 3. easier code reuse polymorphism via interfaces allows for 1. independent development (interface serves as contract) 2. generic code that can work on entire hierarchies of classes polymorphism via inheritance allows for 1. code reuse (can extend existing code without direct access) 2. generic code that can work on entire hierarchies of classes 2

Transcript of CSC 222: Object-Oriented Programming Fall 2015 · CSC 222: Object-Oriented Programming Fall 2015...

1

1

CSC 222: Object-Oriented Programming

Fall 2015

Object-oriented design §  OO design review

objects, cohesion, loose coupling, polymorphism, … §  Model-View-Controller (MVC) pattern §  netBeans IDE

create/edit/run a project, advanced features GUIBuilder, Swing GUI toolkit

Object-oriented development OO design is the dominant approach to large-scale software development

§  developing cohesive (& loosely coupled) software objects allows for 1.  independent teams to contribute 2.  independent testing & debugging 3.  easier code reuse

§  polymorphism via interfaces allows for 1.  independent development (interface serves as contract) 2.  generic code that can work on entire hierarchies of classes

§  polymorphism via inheritance allows for 1.  code reuse (can extend existing code without direct access) 2.  generic code that can work on entire hierarchies of classes

2

2

3

BlueJ à netBeans

BlueJ is an Interactive Development Environment (IDE) designed for novice programmers §  simple to use §  visual interface makes class vs. object distinction clear §  can directly manipulate objects & call methods using mouse clicks §  can also write and execute statements in the Code Pad

however, it is somewhat limiting to experienced programmers

netBeans is a popular, industry-strength IDE for developing software §  free & open-source §  multi-platform (Mac, Windows, Linux) §  multi-language (Java, PHP, C/C++, …) §  has an integrated GUI builder

§  download from netbeans.org

Create a project

4

3

Select location & project name

5

Adding a file to the project

6

4

Select location & file/class name

7

Can then edit the default file

8

5

Completing a project each class in the project is its own file

§  one class must have a public static void main method §  when you run the project, this main method is executed

9

Editor features •  code completion when type OBJECT., see list of method options

•  Source menu •  Format will try to indent selected lines consistently •  Shift Left / Shift Right will manually indent selected lines •  Toggle Comment comments/uncomments selected lines

•  Refactor §  Rename can change a class/variable/method name

throughout the project

•  Run §  Build Main Project compile all classes in the project §  Run Main Project execute the main method

§  note: can also right-click on a file and select Run File

10

6

example: Magic 8-ball create a new project named Magic

§  use the default option to create a default main class named TerminalUI download the Die.java class into the src folder of this project define a new class named Magic8Ball

§  has a 3-sided Die object as a field §  has a getAnswer method that takes a String as input (a question) and returns one of

3 possible answers at random: "Definitely", "No way", or "Outlook hazy"

define a new class named TerminalUI, in the main method §  prompt the user for a question §  call getAnswer and display the answer

11

MVC pattern

model-view-controller is a software pattern used to develop reusable, modular software §  goal: isolate the application-specific logic from the user interface §  allows for independent testing & development, easy updates

for this example: §  the model consists of the logic of the application – Die & Magic8Ball §  the view is the Java terminal window §  the controller is the TerminalUI class with its text-based input/output

§  by separating the logic from the interface, it makes it possible to plug in a different interface, e.g., a Graphical User Interface (GUI)

12

7

GUIBuilder netBeans has an integrated GUI builder built-in

§  can create a (Swing) GUI using a drag-and-drop interface

13

Naming the GUI class

14

8

GUI design view

15

Build interface drag GUI elements from the Palette onto the Design Frame

§  can arrange, resize, alter Properties of the elements §  in particular, right click and change names of elements to be meaningful

(e.g., questionArea, answerButton, answerBox)

16

9

Associate events with elements

17

right click on element, select the event you want to handle

Source view click on the Source button to see the generated code

18

10

Completing the GUI add the Controller code to the GUI class

§  here, Magic8Ball object is a field, initialized in constructor §  code for I/O & method call are entered into the event-handler method

19

Wumpus GUI

20

we could similarly define a GUI for Hunt the Wumpus §  Model: CaveContents + Cave + CaveMaze (note: no I/O in these classes) §  View: window with text area (for output) and buttons (for input) §  Controller: JFrame that connects GUI elements with CaveMaze model