Guest Lecture Notes - PowerPoint PPT Presentation

About This Presentation
Title:

Guest Lecture Notes

Description:

Julia Berg. Jason Codding. Alec Goebel. Kevin Goggins. Julie Hitchcock. Steven Miller. Rachel McManus ... Special method in a class invoked by the new operator ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 14
Provided by: georgeh1
Learn more at: http://web.cs.wpi.edu
Category:
Tags: berg | guest | lecture | notes

less

Transcript and Presenter's Notes

Title: Guest Lecture Notes


1
Guest Lecture Notes
  • Nov 10
  • 10 AM class

2
Outline
  • 1 min Daily Question
  • 5 min Constructor
  • Revisited
  • 10 min Multiple Constructors
  • 5 min Overloading a method
  • 15 min Breakout Linked List Example
  • 5 min Reconvene for solution

3
Daily Question Students
  • Steven MillerRachel McManus
  • Kevin Nolan John Temple
  • Manasi Vartak Steven Washington
  • Andrey YamshchikovAlexander Yeganov
  • Devin Auclair Julia Berg
  • Jason CoddingAlec Goebel
  • Kevin GogginsJulie Hitchcock

4
Afternoon Students
  • Nolan BarrieCraig Blanchette
  • Gregory HoltorfMihai Luca
  • Elliot PenningtonJohn Ramsden
  • Alexander RamsaySamuel Song
  • Caleb Wrobel

5
Design of a Constructor
  • Constructor
  • Special method in a class invoked by the new
    operator
  • Instantiates and initializes object
  • No return needed

public class Temperature / Temperature.
/ double value / Constructor / public
Temperature (double t) this.value t

public class MainProgram public static void
main (String args) Temperature ct new
Temperature(18.32)
Argument List
Parameter List
6
Design of a Constructor
  • A class can have multiple constructors
  • Serve different purposes
  • Take different sets of parameters
  • Provided to make it easier to use class
  • Note use of this keyword
  • Within instance method (or constructor), this
    refers to Object whose instance method is being
    invoked

7
Example Point
Point ct new Point(18.32)
  • package nov10
  • import java.util.Scanner
  • public class Point
  • / x and y coordinates /
  • private double xprivate double y
  • / constructor. /public Point (double xVal,
    double yVal) this.x xVal this.y
    yVal
  • / Alternate constructor. /
  • public Point (String s)
  • Scanner sc new Scanner (s)
  • this.x sc.nextDouble()
  • this.y sc.nextDouble()

Construct a Point when the values of x and y are
known
Invokes this constructor
Construct a Point when you have a String of the
form int int that can be used
Invokes this constructor
Point ct new Point("10 75")
8
Constructor and primitive types
  • Primitive types handled automatically
  • int ? long ? float ? double
  • Arguments will be converted safely

/ constructor. /public Point (double xVal,
double yVal) this.x xVal this.y
yVal
Point p1, p2, p3 // These will all use the same
constructor// on the right, because arguments
can be // safely copied into the waiting
parameter// of type double p1 new Point (2,
3) p2 new Point (10.2, 14) p3 new Point
(8.213f, 10.0f)
p1
p2
p3
9
Overloading
  • When a class has two methods with the same name
    but different parameter lists
  • Easier for programmer to remember
  • You have already seen its use
  • System.out is an object of class PrintStream
  • This class defines the following methods. The
    only difference is the type of the parameter.

public void println (long x) public void println
(double x) public void println (int x) ...
10
After the following four statements, the four
objects exist on the right
Student st1 new Student ("46008", "Ostreicher,
Chris", "10", "ECE") Student st2 new Student
("42009", "Myers, Kevin", "10", "CS") Student
st3 new Student ("10331", "Flaherty, Michelle",
"10", "BB") StudentList list new
StudentList()
Carry out the following statements and draw the
set of objects that result.
list.prepend(st1) list.prepend(st2) list.prepend
(st3)
11
After the following four statements, the four
objects exist on the right
Student st1 new Student ("46008", "Ostreicher,
Chris", "10", "ECE") Student st2 new Student
("42009", "Myers, Kevin", "10", "CS") Student
st3 new Student ("10331", "Flaherty, Michelle",
"10", "BB") StudentList list new
StudentList()
Carry out these statements and draw the set of
objects that result.
list.prepend(st1) list.prepend(st2) list.prepend
(st3)
12
After the following four statements, the four
objects exist on the right
Student st1 new Student ("46008", "Ostreicher,
Chris", "10", "ECE") Student st2 new Student
("42009", "Myers, Kevin", "10", "CS") Student
st3 new Student ("10331", "Flaherty, Michelle",
"10", "BB") StudentList list new
StudentList()
Carry out these statements and draw the set of
objects that result.
list.prepend(st1) list.prepend(st2) list.prepend
(st3)
13
After the following four statements, the four
objects exist on the right
Student st1 new Student ("46008", "Ostreicher,
Chris", "10", "ECE") Student st2 new Student
("42009", "Myers, Kevin", "10", "CS") Student
st3 new Student ("10331", "Flaherty, Michelle",
"10", "BB") StudentList list new
StudentList()
Carry out these statements and draw the set of
objects that result.
list.prepend(st1) list.prepend(st2) list.prepend
(st3)
Write a Comment
User Comments (0)
About PowerShow.com