Advanced Java Programming University of Calgary in Alberta

Advanced Java Programming University Of Calgary In Alberta-Free PDF

  • Date:08 Aug 2020
  • Views:8
  • Downloads:0
  • Pages:34
  • Size:222.17 KB

Share Pdf : Advanced Java Programming University Of Calgary In Alberta

Download and Preview : Advanced Java Programming University Of Calgary In Alberta


Report CopyRight/DMCA Form For : Advanced Java Programming University Of Calgary In Alberta


Transcription:

Method toString, It s commonly written to allow easy determination of the state. of a particular object contents of important attributes. This method returns a string representation of the state of an. It will automatically be called whenever a reference to an object. is passed as a parameter is passed to the print println method. Location of the online example,home 219 examples advanced toStringExample. www cpsc ucalgary ca tamj 219 examples advanced toStringExample. Class Person Version 1,public class Person,private String name. private int age,public Person name No name age 1,public void setName String aName name aName. public String getName return name,public void setAge int anAge age anAge.
public int getAge return age,CPSC 219 Advanced Java. Class Person Version 2,public class Person2,private String name. private int age,public Person2 name No name age 1,public void setName String aName name aName. public String getName return name,public void setAge int anAge age anAge. public int getAge return age,public String toString.
String temp,temp temp Name name n,temp temp Age age n. return temp,The Driver Class,class Driver,public static void main String args. Person p1 new Person,Person2 p2 new Person2,System out println p1. System out println p2,CPSC 219 Advanced Java,Method equals. It s written in order to determine if two objects of the same class. are in the same state attributes have the same data values. Location of the online example,home 219 examples advanced equalsExample.
www cpsc ucalgary ca tamj 219 examples advanced equalsExample. The Driver Class,public class Driver,public static void main String args. Person p1 new Person,Person p2 new Person,if p1 equals p2 true. System out println Same,System out println Different. p1 setName Foo,if p1 equals p2 true,System out println Same. System out println Different,CPSC 219 Advanced Java.
The Person Class,public class Person,private String name. private int age,public Person name No name age 1,public void setName String aName name aName. public String getName return name,public void setAge int anAge age anAge. public int getAge return age,public boolean equals Person aPerson. boolean flag,if name equals aPerson getName age aPerson getAge.
flag false,return flag,Methods Of Parameter Passing. Passing parameters as value parameters pass by value. Passing parameters as variable parameters pass by reference. CPSC 219 Advanced Java,Passing Parameters As Value Parameters. Pass a copy,of the data,method parameter type p1,Passing Parameters As Reference Parameters. Pass the address of the,parameter refer to the,parameter in the method. method parameter type p1,CPSC 219 Advanced Java,Parameter Passing In Java Simple Types.
All simple types are always passed by value in Java. Type Description,byte 8 bit signed integer,short 16 but signed integer. int 32 bit signed integer,long 64 bit signed integer. float 32 bit signed real number,double 64 bit signed real number. char 16 bit Unicode character,boolean 1 bit true or false value. Parameter Passing In Java Simple Types 2,Location of the online example.
home 219 examples advanced valueParameters, www cpsc ucalgary ca tamj 219 examples advanced valueParameters. public static void main String args,Swapper s new Swapper. System out println num1 num1 tnum2 num2,s swap num1 num2. System out println num1 num1 tnum2 num2,CPSC 219 Advanced Java. Passing Simple Types In Java 2,public class Swapper.
public void swap int num1 int num2,System out println num1 num1 tnum2 num2. Passing References In Java, Reminder References are required for variables that are arrays. or objects, If a reference object or array is passed as a parameter to a method do. changes made in the method continue on after the method is finished. Hint If a reference is passed as a parameter into a method then a. copy of the reference is what is being manipulated in the method. CPSC 219 Advanced Java,An Example Of Passing References In Java. UML Diagram,Location of the online example,home 219 examples advanced referenceParameters.
www cpsc ucalgary ca tamj 219 examples advanced referenceParamete. An Example Of Passing References In Java,The Driver Class. public class Driver,public static void main String args. f1 new Foo,f2 new Foo,s1 new Swap,f1 setNum 1,f2 setNum 2. CPSC 219 Advanced Java,An Example Of Passing References In Java. The Driver Class 2,System out println Before swap t f1 f1 getNum tf2.
s1 noSwap f1 f2, System out println After noSwap t f1 f1 getNum tf2. s1 realSwap f1 f2, System out println After realSwap t f1 f1 getNum tf2. An Example Of Passing References In Java,public class Foo. private int num,public void setNum int newNum,num newNum. public int getNum,return num,CPSC 219 Advanced Java.
An Example Of Passing References In Java,Class Swap. public class Swap,public void noSwap Foo f1 Foo f2. System out println In noSwap t f1 f1 getNum tf2,An Example Of Passing References In Java. Class Swap 2,public void realSwap Foo f1 Foo f2,Foo temp new Foo. temp setNum f1 getNum,f1 setNum f2 getNum,f2 setNum temp getNum.
System out println In realSwap t f1 f1 getNum tf2,End of class Swap. CPSC 219 Advanced Java,References Things To Keep In Mind. If you refer to just the name of the reference then you are. dealing with the reference to an object to an array. This copies an address from one reference into another reference the. original objects don t change, If you use the dot operator then you are dealing with the actual. temp setNum f1 getNum, temp and f2 refer to the same object and using the dot operator changes the. object which is referred to by both references,Other times this may be an issue.
Assignment,Comparisons,Shallow Copy Vs Deep Copies. Shallow copy new term concept should be review, Copy the address from one reference into another reference. Both references point to the same dynamically allocated memory location. f1 new Foo,f2 new Foo,CPSC 219 Advanced Java,Shallow Vs Deep Copies 2. Deep copy new term concept should be review, Copy the contents of the memory location referred to by the reference. The references still point to separate locations in memory. f1 new Foo,f2 new Foo,f1 setNum 1,f2 setNum f1 getNum.
System out println f1 f1 getNum tf2 f2 getNum,f1 setNum 10. f2 setNum 20,System out println f1 f1 getNum tf2 f2 getNum. Comparison Of References Vs Data Objects,Location of the online example. home 219 examples advanced comparisionsReferencesVsObjects. www cpsc ucalgary ca tamj 219 examples advanced comparisionsRefere. ncesVsObjects,public class Person,private int age,public Person age 1. public void setAge int anAge age anAge,public int getAge return age.
CPSC 219 Advanced Java,Comparison Of The References. public class DriverReferences,public static void main String args. Person p1 new Person,Person p2 new Person,p1 setAge 1. p2 setAge p1 getAge,System out println References Same location. System out println References different locations,Comparison Of The Data.
public class DriverData,public static void main String args. Person p1 new Person,Person p2 new Person,p1 setAge 1. p2 setAge p1 getAge,if p1 getAge p2 getAge,System out println Data Same information. System out println Data different information,CPSC 219 Advanced Java. A Previous Example Revisited Class Sheep,public class Sheep.
private String name,public Sheep,System out println Creating No name sheep. name No name,public Sheep String aName,System out println Creating the sheep called n. setName aName,public String getName return name,public void setName String newName name newName. Answer None Of The Above, Information about all instances of a class should not be tracked. by an individual object,So far we have used instance fields.
Each instance of an object contains it s own set of instance. fields which can contain information unique to the instance. public class Sheep,private String name,name Bill name Jim name Nellie. CPSC 219 Advanced Java James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented in Java James Tam Commonly Implemented Methods The particular methods implemented for a class will vary depending upon the application However two methods that are commonly implemented for many classes

Related Books