Docsity

Understanding Program Structure: main(), Classes, and Interfaces in Java, Slides of Java Programming

Partial preview of the text

Download Understanding Program Structure: main(), Classes, and Interfaces in Java and more Slides Java Programming in PDF only on Docsity!

6: Design, Debugging,

Interfaces

Assignment 5: main()

Programs start at a main() method, but

many classes can have main()

public class SimpleDraw { /* ... stuff ... / public static void main ( String args []) { SimpleDraw content = new SimpleDraw (new DrawGraphics ()); / ... more stuff ... */ } }

public class DrawGraphics { BouncingBox box ; public DrawGraphics () { box = new BouncingBox ( 200 , 50 , Color. RED ); } public void draw ( Graphics surface ) { surface. drawLine ( 50 , 50 , 250 , 250 ); box. draw ( surface ); } }

public class DrawGraphics { BouncingBox box; // a field or member variable public DrawGraphics () { box = new BouncingBox ( 200 , 50 , Color. RED ); } public void draw ( Graphics surface ) { surface. drawLine ( 50 , 50 , 250 , 250 ); box. draw ( surface ); } }

public class DrawGraphics { public void draw ( Graphics surface ) { surface. drawLine ( 50 , 50 , 250 , 250 ); box. draw ( surface ); surface. fillRect ( 150 , 100 , 25 , 40 ); surface. fillOval ( 40 , 40 , 25 , 10 ); surface. setColor ( Color. YELLOW ); surface. drawString ( "Mr. And Mrs. Smith" , 200 , 10 ); } }

public class DrawGraphics { ArrayList < BouncingBox > boxes = new ArrayList < BouncingBox >(); public DrawGraphics () { boxes. add (new BouncingBox ( 200 , 50 , Color. RED )); boxes. add (new BouncingBox ( 10 , 10 , Color. BLUE )); boxes. add (new BouncingBox ( 100 , 100 , Color. GREEN )); boxes. get ( 0 ). setMovementVector ( 1 , 0 ); boxes. get ( 1 ). setMovementVector (- 3 , - 2 ); boxes. get ( 2 ). setMovementVector ( 1 , 1 ); } public void draw ( Graphics surface ) { for ( BouncingBox box : boxes ) { box. draw ( surface ); } } }

What is a good program?

Correct / no errors

Easy to understand

Easy to modify / extend

Good performance (speed)

Consistency

Writing code in a consistent way makes it

easier to write and understand

Programming “style” guides: define rules

about how to do things

Java has some widely accepted

“standard” style guidelines

Good Class Design

Good classes: easy to understand and use

  • Make fields and methods private by default
  • Only make methods public if you need to
  • If you need access to a field, create a

method:

public int getBar() { return bar; }

Debugging

The process of finding and correcting an

error in a program

A fundamental skill in programming

Step 1: Donʼt Make Mistakes

Donʼt introduce errors in the first place

• Reuse: find existing code that does

what you want

• Design: think before you code

• Best Practices: Recommended

procedures/techniques to avoid

common problems

Design: Pseudocode

A high-level, understandable description

of what a program is supposed to do

Donʼt worry about the details, worry about

the structure

Design

Visual design for objects, or how a

program works

Donʼt worry about specific notation, just

do something that makes sense for you

Scrap paper is useful

SimpleDraw DrawGraphics ArrayList BouncingBox BouncingBox BouncingBox