Parameters, Graphical Output COMP 102 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Parameters, Graphical Output COMP 102

Description:

... 'number of tutor hours = ' tutorHours); LabRequirements: using ... set up the window and canvas. JFrame frame = new JFrame('O and X'); frame.setSize(600, 400) ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 16
Provided by: pon9
Category:

less

Transcript and Presenter's Notes

Title: Parameters, Graphical Output COMP 102


1
Parameters, Graphical OutputCOMP 102 6 2008
T2
2
Menu
  • Method with parameters
  • Simple Graphical Output
  • Administrivia
  • Next Friday 5 test (only counts if it helps)

3
Another Java Program
  • / Program for converting between temperature
    scales /
  • public class TemperatureConverter
  • / Print out the conversion formula /
  • public void printFormula()
  • System.out.println("centigrade (fahrenheit -
    32) 5/9")
  • / Convert from fahrenheit to centigrade /
  • public void fahrenToCent(int fahren)
  • int cent
  • cent (fahren - 32) 5 / 9
  • System.out.println(fahren F -gt " cent
    C")

4
Getting values into methods
  • Prompting the user for values
  • A statement in the method code can read a value
    that the user types in the terminal window.
  • Needs a Scanner object to do the reading
  • Parameters
  • Declare the type and name of a variable in the
    (.) of the method.
  • When the method is called, you must pass a value
    for each parameter
  • Bluej lets you call a method and will ask you to
    enter the value(s)

5
LabRequirements using a parameter
  • public class LabRequirements
  • public void printRequirements(int numStudents)
  • int numStreams (numStudents 1) / 33 1
  • System.out.println("number of streams "
    numStreams)
  • double tutorHours numStreams 2 1.5
  • System.out.println("number of tutor hours "
    tutorHours)

6
Call the method printRequirements
  • What happens if we call the method
  • LabRequire1 . printRequirements(104)
  • public void printRequirements(int numStudents)
  • int numStreams (numStudents-1) / 33 1
  • System.out.println("number of streams "
    numStreams)
  • int tutorHours numStreams 3 2
  • System.out.println("number of tutor hours "
    tutorHours)

7
LabRequirements with parameters
  • / Computes and prints lab requirements,
  • Requires Course Code, as well as number of
    students/
  • public void printRequirements (String code, int
    numStudents)
  • int streams (numStudents 1) / 33 1
  • double tutorHours streams 2 1.5
  • System.out.printf(" s needs d streams and 4.2f
    tutor hours\n",
  • code, streams,
    tutorHours)

8
Syntax diagram for Methods
lttypegt
public
ltmethod namegt
void
(
)

lttypegt
ltparameter namegt
,
ltstatementgt

9
Graphical Output
  • Need a window on the screen
  • (not the Terminal window)
  • a JFrame object
  • Needs an object we can call drawing methods on.
  • a DrawingCanvas object
  • Attached to the JFrame
  • Note textbook uses an Applet object and a
    Graphics object, 2.7 2.9
  • Applets are more complicated
  • Same methods (almost)

JFrame JFrame-52
DrawingCanvas DrawingCanvas-6
drawRect fillRect drawLine
10
Using JFrame and DrawingCanvas
  • / Draw a Os and Xs board in a window on the
    screen /
  • public void drawOandX()
  • // set up the window and canvas
  • JFrame frame new JFrame(O and X)
  • frame.setSize(600, 400)
  • DrawingCanvas canvas new DrawingCanvas()
  • frame.getContentPane().add(canvas,
    BorderLayout.CENTER)
  • frame.setVisible(true)
  • // draw the board
  • canvas.drawRect( , , , )
  • canvas.drawLine( , , , )
  • canvas.drawLine( , , , )
  • canvas.drawLine( , , , )
  • canvas.drawLine( , , , )

11
Arguments for drawing methods
  • coordinates from the left and from the top
  • Lines start and end coordinates
  • Shapes top, left, width, height

12
Using DrawingCanvas
  • Read the textbook about colours and drawing
    actions
  • LL 2.7, 2.9
  • Read the documentation for DrawingCanvas
  • (on the Java documentation pages, off the comp102
    home page)
  • Sketch what you want to draw first
  • Identify the methods you will call
  • Work out the arguments of the method calls.

13
Drawing with variables
size
  • int left 50
  • int top 40
  • int size 30
  • int right left (size 3)
  • int bot top (size 3)
  • canvas.drawRect( left , top , size 3 ,
    size 3 )
  • canvas.drawLine( left , top size , right
    , top size )
  • canvas.drawLine( left , topsize2 , right ,
    topsize2 )
  • canvas.drawLine( leftsize , top , leftsize ,
    bot )
  • canvas.drawLine( leftsize2 , top ,
    leftsize2 , bot )

(left, top)
, , , ) , , , ) , , , ) , , ,
) , , , )
14
Drawing with variables
  • int left 50
  • int top 40
  • int size 30
  • int right left (size 3)
  • int bot top (size 3)
  • canvas.drawRect( left, top, size 3, size
    3 )
  • canvas.drawLine( left, topsize, right,
    topsize )
  • canvas.drawLine( left, topsize2, right,
    topsize2 )
  • canvas.drawLine( leftsize, top, leftsize,
    bot )
  • canvas.drawLine( leftsize2, top, leftsize2,
    bot )
  • left 200
  • top 40
  • right left (size 3)
  • bot top (size 3)
  • canvas.drawRect( left, top, size 3, size
    3 )
  • canvas.drawLine( left, topsize, right,
    topsize )
  • canvas.drawLine( left, topsize2, right,
    topsize2 )
  • canvas.drawLine( leftsize, top, leftsize,
    bot )
  • canvas.drawLine( leftsize2, top, leftsize2,
    bot )

15
Libraries
  • Your program must specify all the libraries that
    you use
  • import comp100.
  • import java.util.
  • import javax.swing.
  • import java.awt.BorderLayout

16
Drawing with variables
  • Draw two boards, one at (10, 10) and another at
    (200, 10)
  • public void drawOandX()
  • JFrame frame new JFrame(O and X)
  • frame.setSize(600, 400)
  • DrawingCanvas canvas new DrawingCanvas()
  • frame.getContentPane().add(canvas,
    BorderLayout.CENTER)
  • frame.setVisible(true)
  • // draw the boards
  • this.drawBoard(10, 10, canvas)
  • this.drawBoard(200, 10, canvas)
  • Calling another method on the same object
  • Passing two different sets of arguments

17
Drawing with variables
  • public void drawBoard(int left, int top,
    DrawingCanvas canvas)
  • int size 30
  • int right left (size 3)
  • int bot top (size 3)
  • canvas.drawRect( left , top , size 3 ,
    size 3 )
  • canvas.drawLine( left , top size , right
    , top size )
  • canvas.drawLine( left , topsize2 , right ,
    topsize2 )
  • canvas.drawLine( leftsize , top , leftsize ,
    bot )
  • canvas.drawLine( leftsize2 , top ,
    leftsize2 , bot )

, , , ) , , , ) , , , ) , , ,
) , , , )
(left, top)
size
18
Review of Scanner
  • Input from user via terminal window
  • Make a Scanner object that wraps up System.in
  • Use System.out.print() callsto prompt the
    user for the next value.
  • Call next(), nextInt(), nextDouble(), nextLine()
    etcto get the next value the user typed.
  • Scanner scan new Scanner ( System.in )
  • System.out.print( Enter name )
  • String name scan.nextLine( )
  • System.out.print( Enter age )
  • int age scan.nextInt( )

19
Methods on Scanner objects
  • Look up the Documentation!
  • A Scanner breaks its input into tokens
  • (strings of characters separated by whitespace)
  • Some methods on Scanner objects
  • public String next ( ) get and return next token
    as a String
  • public int nextInt ( ) get and return next token
    as an int (if its an integer)
  • public double nextDouble ( ) get and return next
    token as a double (if its a double)
  • public String nextLine() get and return rest of
    line as a String.
  • Note, these methods return a value!
  • type of value returned is specified by the type
    before method name

20
Methods that return values.
  • If methods return a value, you can use the value
  • String ans scan.next ( )
  • int age scan.nextInt ( )
  • System.out.println(n scan.nextLine ( ))
  • You can ignore the value
  • scan.nextLine() // read and throw away the
    rest of the line.
  • Methods can return a value and/or have some
    effect
  • the return type of the method says what kind of
    value it returns
  • PrintStream public void println ( String str )
  • Scanner public int nextInt ( ) .
  • public String nextLine ( ) .
  • public boolean hasNextInt ( ) .
  • public void close ( ) .

21
? More kinds of Expressions
  • Calls to Methods that return a value
  • scan.nextInt()
  • scan.next()
  • scan.nextLine()
  • Creating a new object
  • new Scanner(System.in)
  • new LabRequirements()

ltClass namegt
(
)
new
ltargumentgt
,
22
Input and Output
  • Output
  • Use the System.out object to print on the
    terminal window
  • Use a DrawingCanvas object to draw graphics in a
    window
  • Use fancy user interface objects, like
    JOptionPane.
  • Input
  • Use Bluejs special facilities for calling
    methods with arguments
  • Use a Scanner object and the System.in object to
    get input from user typing in the terminal
    window.
  • Use fancy user interface objects, like
    JOptionPane
  • Use even fancier user interface objects to
    getinput from the mouse, buttons, sliders, etc

?
?
?
23
Input and Output
  • Output
  • Use the System.out object to print on the
    terminal window
  • Use a DrawingCanvas object to draw graphics in a
    window
  • Use fancy user interface objects, like
    JOptionPane.
  • Input
  • Use Bluejs special facilities for calling
    methods with arguments
  • Use a Scanner object and the System.in object to
    get input from user typing in the terminal
    window.
  • Use fancy user interface objects, like
    JOptionPane
  • Use even fancier user interface objects to
    getinput from the mouse, buttons, sliders, etc

?
?
?
Write a Comment
User Comments (0)
About PowerShow.com