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

An assignment and related resources for learning about the structure of java programs, focusing on the main() method, classes, and interfaces. Students will explore the concept of good program design, debugging techniques, and the use of interfaces for drawing graphics.

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye šŸ‡®šŸ‡³

4.8

(8)

102 documents

1 / 50

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
!
6: Design, Debugging,
Interfaces"
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32

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