Docsity

Java Passing References and Point Class - Prof. Brian F. Hanks, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 – Lecture 27
Last time we looked at some class methods, and what happens when the methods are
called. Our last example looked what happens when the FP are a reference type.
Let's look at Demo3.java
And at PassingReferences.java
All types are passed by value - it does not matter if the type of the formal parameter is a
primitive type or a reference type. This means that if we change the value of a formal
parameter, that change is not reflected back in the calling method.
However,
Let’s look at another example: PassingReferences2.java
So, we can see that method f is working with a distinct JFrame, but method g is working
with the reference that was passed to it.
Repeat KEY POINT:
If we change the value that is referenced by a reference type, that change is reflected
back in the caller. This is a subtle but important distinction!
Instance Methods
Remember, instance methods require an object - we invoke instance methods using an
object's name followed by '.' and the method name.
String s = "Hello";
int l = s.length();
JFrame myFrame = new JFrame( "The Title" );
myFrame.setSize( 200, 300 );
Instance methods are defined without the keyword 'static':
[public] <returnType> <Identifier> ( <Parameter List> ) <MethodBody>
So: class method -- use static
instance method - no static
Let's create a class Point that represents points on a two dimensional grid. Each point has
an x and y coordinate.
What methods do Points need?
pf2

Partial preview of the text

Download Java Passing References and Point Class - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 27

Last time we looked at some class methods, and what happens when the methods are called. Our last example looked what happens when the FP are a reference type. Let's look at Demo3.java And at PassingReferences.java All types are passed by value - it does not matter if the type of the formal parameter is a primitive type or a reference type. This means that if we change the value of a formal parameter, that change is not reflected back in the calling method. However , Let’s look at another example: PassingReferences2.java So, we can see that method f is working with a distinct JFrame, but method g is working with the reference that was passed to it. Repeat KEY POINT: If we change the value that is referenced by a reference type, that change is reflected back in the caller. This is a subtle but important distinction! Instance Methods Remember, instance methods require an object - we invoke instance methods using an object's name followed by '.' and the method name. String s = "Hello"; int l = s.length(); JFrame myFrame = new JFrame( "The Title" ); myFrame.setSize( 200, 300 ); Instance methods are defined without the keyword 'static': [public] ( ) So: class method -- use static instance method - no static Let's create a class Point that represents points on a two dimensional grid. Each point has an x and y coordinate. What methods do Points need?

  • A constructor that takes values for x and y
  • A constructor with no arguments that creates a Point at the origin (0, 0).
  • A toString() method
  • Accessor methods o A method to get the value of x o A method to get the value of y
  • Mutator methods o A method to change the value of x o A method to change the value of y o A method to move a point to a new position o A method to translate a point by a given factor (e.g., multiply x and y by 2) Keyword this Let's look at a class that defines a point on a two-dimensional grid - see Point.java. Look at the constructor that takes two arguments public Point( double xValue, double yValue ) I'd really like to give the formal parameters the names x and y instead of having to make up new names for the same thing - points really have an x value and a y value. So, I'd like to declare Point like this: public Point( double x, double y ) What happens if I do it like this? However, Java provides a way for us to do what I want - the keyword this. 'this' is a reference to the current object. (Try to draw a picture). So, we can modify our code to use this. I can also use this to invoke the current object's constructor from another constructor. So, I can rewrite the no-argument constructor to use this(0,0); Exercise Write a class Rectangle that has the following methods:
  • A constructor that takes the values for height and width of the Rectangle (as doubles)
  • A method to 'rotate' the Rectangle. This method should exchange the width and height - for example, if the rectangle was 3 by 5, this modifies it to become 5 by
  • Accessor methods to getHeight and GetWidth
  • Mutator methods setHeight and SetWidth
  • A toString method