SlideShare a Scribd company logo
1 of 15
2016 Winter
LAB #3
Prepared by: Berk Soysal
2016 Winter
• The operator ”==” is used to compare two objects
regarding their references.
• This means, the operator checks whether
or not these two objects refer to the same place in
memory.
• For example;
String s1 = new String("xyz");
String s2 = new String("xyz");
if(s1 == s2)
System.out.println(“s1==s2 is TRUE");
else
System.out.println(“s1==s2 is FALSE");
Folder Lab3 Lab3 Examples Equality.java
• Note 1: Every time we create a new object, the
object gets its own unique address in the memory.
• Note 2: This type of comparison is called reference
comparison.
2016 Winter
• The intent of the equals method is to compare whether two
objects are semantically the same, if they have the same
content.
• For classes from the Java library (Wrappers, Strings etc.), this
is indeed what will happen.
• For your own class however, you have to provide your own
implementation of equals.
For example;
String s1 = new String("xyz");
String s2 = new String("xyz");
if(s1.equals(s2))
System.out.println(“s1 equals s2 is TRUE");
else
System.out.println(“s1 equals s2 is FALSE");
2016 Winter Folder Lab3 Lab3 Examples Equality.java
• Another important aspect of software development
is the documentation.
• JavaDoc is a format for your Java comments, and a
set of tools for producing Web pages automatically.
• In this lab, we are asking you to document your
code (variables, methods, etc.) using JavaDoc.
2016 Winter
2016 Winter
• Let’s complete the implementation of the (static)
class method String[] findAndReplace(String[] in,
String[] what, String[] with) of the class Utils.
• The method returns a copy of the array in where
each word occurring in the array what has been
replaced by the word occurring at the
corresponding position in the array with.
• The array designated by in must remain
unchanged.
2016 Winter
• JUnit is a simple framework to write repeatable tests. It is an
instance of the xUnit architecture for unit testing
frameworks.
• A set of test cases is written
to test a method’s operation.
• We use various methods of the
Assert Class to test our
findAndReplace() method.
• Only the failed assertions are recorded.
2016 Winter
• Let us implement a class to represent rational
numbers..
• Each rational number consists of a numerator
and a denominator, both of type int. Since each
rational number has its own numerator and
denominator, these must be instance variables.
Solutions-> Rational and TestRational
2016 Winter
• The public setXYZ () and getXYZ() methods are the
access points of the instance variables of a class.
• Normally, these methods are referred as getters
and setters.
• Therefore any class that wants to access the
variables should access them through these getters
and setters.
• An object that has no setter methods, and no other
methods for transforming the state of the object, is
said to be immutable.
2016 Winter
• An object is considered immutable if its state
cannot change after it is constructed. Maximum
reliance on immutable objects is widely accepted as
a sound strategy for creating simple, reliable code.
• Since they cannot change state, they cannot be
corrupted by thread interference or observed in an
inconsistent state.
2016 Winter
• Sometimes, you want to have variables that are
common to all objects. This is accomplished with
the static modifier. Fields that have the static
modifier in their declaration are called class
methods.
• When a number of objects are created from the
same class blueprint, they each have their own
distinct copies of instance methods.
2016 Winter
You can do this to execute a static method:
MyClass.staticMethod();
//Simply refers to the class's static code
But to execute a non-static method, you must do this:
//Create an instance
MyClass obj = new MyClass();
//Refer to the instance's class's code
obj.nonstaticMethod();
2016 Winter
Documentation Exercise !
Add JavaDoc comments for the class Rational.
1. Add JavaDoc comments for all the methods. Each
comment should include a brief description of what the
method does and descriptions of the parameters and the
return value using JavaDoc format.
2. Add a brief description of the class Rational, using the
JavaDoc syntax, make sure to include
the name of the author of the class (you).
2016 Winter
Java Tutorial Lab 3

More Related Content

What's hot

Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaAyush Mishra
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and OverridingMichael Heron
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2Nafis Ahmed
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
Python programming
Python programmingPython programming
Python programmingsirikeshava
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator OverloadingMichael Heron
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overridingPinky Anaya
 
Introduction on Data Structures
Introduction on Data StructuresIntroduction on Data Structures
Introduction on Data StructuresNanthini Kempaiyan
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method OverloadingMichael Heron
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to ScalaRahul Jain
 

What's hot (20)

Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in Scala
 
Simplicitly
SimplicitlySimplicitly
Simplicitly
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Net framework
Net frameworkNet framework
Net framework
 
Python programming
Python programmingPython programming
Python programming
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overriding
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
 
Introduction on Data Structures
Introduction on Data StructuresIntroduction on Data Structures
Introduction on Data Structures
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
30csharp
30csharp30csharp
30csharp
 

Similar to Java Tutorial Lab 3

‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3Mahmoud Alfarra
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glanceKnoldus Inc.
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java conceptsChikugehlot
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsHelen SagayaRaj
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 

Similar to Java Tutorial Lab 3 (20)

Java
JavaJava
Java
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 
Java
JavaJava
Java
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glance
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Java for newcomers
Java for newcomersJava for newcomers
Java for newcomers
 
Java Day-4
Java Day-4Java Day-4
Java Day-4
 
Day 2
Day 2Day 2
Day 2
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Java basics
Java basicsJava basics
Java basics
 
Lesson3
Lesson3Lesson3
Lesson3
 
Lesson3
Lesson3Lesson3
Lesson3
 
CHAPTER 3 part2.pdf
CHAPTER 3 part2.pdfCHAPTER 3 part2.pdf
CHAPTER 3 part2.pdf
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 

More from Berk Soysal

Guest author manual
Guest author manualGuest author manual
Guest author manualBerk Soysal
 
Comparison of BER performances of 64-PSK and 64-QAM in AWGN channels
Comparison of BER performances of  64-PSK and 64-QAM in  AWGN channelsComparison of BER performances of  64-PSK and 64-QAM in  AWGN channels
Comparison of BER performances of 64-PSK and 64-QAM in AWGN channelsBerk Soysal
 
Biomedical project report detecting emg noise
Biomedical project report detecting emg noiseBiomedical project report detecting emg noise
Biomedical project report detecting emg noiseBerk Soysal
 
Self Evaluation Test
Self Evaluation Test Self Evaluation Test
Self Evaluation Test Berk Soysal
 
Java Tutorial Lab 9
Java Tutorial Lab 9Java Tutorial Lab 9
Java Tutorial Lab 9Berk Soysal
 
Java Tutorial Lab 8
Java Tutorial Lab 8Java Tutorial Lab 8
Java Tutorial Lab 8Berk Soysal
 

More from Berk Soysal (6)

Guest author manual
Guest author manualGuest author manual
Guest author manual
 
Comparison of BER performances of 64-PSK and 64-QAM in AWGN channels
Comparison of BER performances of  64-PSK and 64-QAM in  AWGN channelsComparison of BER performances of  64-PSK and 64-QAM in  AWGN channels
Comparison of BER performances of 64-PSK and 64-QAM in AWGN channels
 
Biomedical project report detecting emg noise
Biomedical project report detecting emg noiseBiomedical project report detecting emg noise
Biomedical project report detecting emg noise
 
Self Evaluation Test
Self Evaluation Test Self Evaluation Test
Self Evaluation Test
 
Java Tutorial Lab 9
Java Tutorial Lab 9Java Tutorial Lab 9
Java Tutorial Lab 9
 
Java Tutorial Lab 8
Java Tutorial Lab 8Java Tutorial Lab 8
Java Tutorial Lab 8
 

Recently uploaded

Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 

Recently uploaded (20)

Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 

Java Tutorial Lab 3

  • 1. 2016 Winter LAB #3 Prepared by: Berk Soysal
  • 2. 2016 Winter • The operator ”==” is used to compare two objects regarding their references. • This means, the operator checks whether or not these two objects refer to the same place in memory. • For example; String s1 = new String("xyz"); String s2 = new String("xyz"); if(s1 == s2) System.out.println(“s1==s2 is TRUE"); else System.out.println(“s1==s2 is FALSE"); Folder Lab3 Lab3 Examples Equality.java
  • 3. • Note 1: Every time we create a new object, the object gets its own unique address in the memory. • Note 2: This type of comparison is called reference comparison. 2016 Winter
  • 4. • The intent of the equals method is to compare whether two objects are semantically the same, if they have the same content. • For classes from the Java library (Wrappers, Strings etc.), this is indeed what will happen. • For your own class however, you have to provide your own implementation of equals. For example; String s1 = new String("xyz"); String s2 = new String("xyz"); if(s1.equals(s2)) System.out.println(“s1 equals s2 is TRUE"); else System.out.println(“s1 equals s2 is FALSE"); 2016 Winter Folder Lab3 Lab3 Examples Equality.java
  • 5. • Another important aspect of software development is the documentation. • JavaDoc is a format for your Java comments, and a set of tools for producing Web pages automatically. • In this lab, we are asking you to document your code (variables, methods, etc.) using JavaDoc. 2016 Winter
  • 7. • Let’s complete the implementation of the (static) class method String[] findAndReplace(String[] in, String[] what, String[] with) of the class Utils. • The method returns a copy of the array in where each word occurring in the array what has been replaced by the word occurring at the corresponding position in the array with. • The array designated by in must remain unchanged. 2016 Winter
  • 8. • JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. • A set of test cases is written to test a method’s operation. • We use various methods of the Assert Class to test our findAndReplace() method. • Only the failed assertions are recorded. 2016 Winter
  • 9. • Let us implement a class to represent rational numbers.. • Each rational number consists of a numerator and a denominator, both of type int. Since each rational number has its own numerator and denominator, these must be instance variables. Solutions-> Rational and TestRational 2016 Winter
  • 10. • The public setXYZ () and getXYZ() methods are the access points of the instance variables of a class. • Normally, these methods are referred as getters and setters. • Therefore any class that wants to access the variables should access them through these getters and setters. • An object that has no setter methods, and no other methods for transforming the state of the object, is said to be immutable. 2016 Winter
  • 11. • An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code. • Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state. 2016 Winter
  • 12. • Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called class methods. • When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance methods. 2016 Winter
  • 13. You can do this to execute a static method: MyClass.staticMethod(); //Simply refers to the class's static code But to execute a non-static method, you must do this: //Create an instance MyClass obj = new MyClass(); //Refer to the instance's class's code obj.nonstaticMethod(); 2016 Winter
  • 14. Documentation Exercise ! Add JavaDoc comments for the class Rational. 1. Add JavaDoc comments for all the methods. Each comment should include a brief description of what the method does and descriptions of the parameters and the return value using JavaDoc format. 2. Add a brief description of the class Rational, using the JavaDoc syntax, make sure to include the name of the author of the class (you). 2016 Winter