SlideShare a Scribd company logo
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 (20)

ODP
Knolx Session : Built-In Control Structures in Scala
Ayush Mishra
 
PDF
Simplicitly
Martin Odersky
 
PPTX
Java 103 intro to java data structures
agorolabs
 
PPTX
2CPP08 - Overloading and Overriding
Michael Heron
 
PDF
What To Leave Implicit
Martin Odersky
 
PPTX
Method overloading and constructor overloading in java
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Adobe Flash Actionscript language basics chapter-2
Nafis Ahmed
 
PPTX
What To Leave Implicit
Martin Odersky
 
PDF
Preparing for Scala 3
Martin Odersky
 
PPTX
Net framework
Abhishek Mukherjee
 
PPTX
Python programming
sirikeshava
 
PPTX
2CPP13 - Operator Overloading
Michael Heron
 
DOCX
Differences between method overloading and method overriding
Pinky Anaya
 
PPTX
Java Programming
Nanthini Kempaiyan
 
PPTX
Method overloading
Lovely Professional University
 
PDF
Introduction java programming
Nanthini Kempaiyan
 
PDF
Introduction on Data Structures
Nanthini Kempaiyan
 
PPTX
2CPP11 - Method Overloading
Michael Heron
 
PPTX
Introduction to Scala
Rahul Jain
 
PPTX
30csharp
Sireesh K
 
Knolx Session : Built-In Control Structures in Scala
Ayush Mishra
 
Simplicitly
Martin Odersky
 
Java 103 intro to java data structures
agorolabs
 
2CPP08 - Overloading and Overriding
Michael Heron
 
What To Leave Implicit
Martin Odersky
 
Method overloading and constructor overloading in java
baabtra.com - No. 1 supplier of quality freshers
 
Adobe Flash Actionscript language basics chapter-2
Nafis Ahmed
 
What To Leave Implicit
Martin Odersky
 
Preparing for Scala 3
Martin Odersky
 
Net framework
Abhishek Mukherjee
 
Python programming
sirikeshava
 
2CPP13 - Operator Overloading
Michael Heron
 
Differences between method overloading and method overriding
Pinky Anaya
 
Java Programming
Nanthini Kempaiyan
 
Method overloading
Lovely Professional University
 
Introduction java programming
Nanthini Kempaiyan
 
Introduction on Data Structures
Nanthini Kempaiyan
 
2CPP11 - Method Overloading
Michael Heron
 
Introduction to Scala
Rahul Jain
 
30csharp
Sireesh K
 

Similar to Java Tutorial Lab 3 (20)

PPT
Core Java Concepts
mdfkhan625
 
PPTX
Joshua bloch effect java chapter 3
Kamal Mukkamala
 
PPTX
Some Important Methods in JAVA
SAGARDAVE29
 
PDF
Java puzzle-1195101951317606-3
rsmuralirs
 
ODP
Формальная верификация как средство тестирования (в Java)
SQALab
 
PDF
Junit Recipes - Elementary tests (2/2)
Will Shen
 
PDF
Junit Recipes - Elementary tests (1/2)
Will Shen
 
PPT
Ap Power Point Chpt5
dplunkett
 
PPTX
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
PPT
Core java concepts
kishorethoutam
 
PPT
Core java concepts
Chikugehlot
 
PPT
Lesson16
cybernaut
 
PPTX
Core java concepts
laratechnologies
 
DOCX
Core Java Equals and hash code
mhtspvtltd
 
PDF
Java Puzzle
SFilipp
 
PPTX
javaimplementation
FaRaz Ahmad
 
PPTX
Programming picaresque
Bret McGuire
 
PPTX
Collections
sagsharma
 
PPTX
Java tutorial part 4
Mumbai Academisc
 
PDF
It Is Possible to Do Object-Oriented Programming in Java
Kevlin Henney
 
Core Java Concepts
mdfkhan625
 
Joshua bloch effect java chapter 3
Kamal Mukkamala
 
Some Important Methods in JAVA
SAGARDAVE29
 
Java puzzle-1195101951317606-3
rsmuralirs
 
Формальная верификация как средство тестирования (в Java)
SQALab
 
Junit Recipes - Elementary tests (2/2)
Will Shen
 
Junit Recipes - Elementary tests (1/2)
Will Shen
 
Ap Power Point Chpt5
dplunkett
 
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
Core java concepts
kishorethoutam
 
Core java concepts
Chikugehlot
 
Lesson16
cybernaut
 
Core java concepts
laratechnologies
 
Core Java Equals and hash code
mhtspvtltd
 
Java Puzzle
SFilipp
 
javaimplementation
FaRaz Ahmad
 
Programming picaresque
Bret McGuire
 
Collections
sagsharma
 
Java tutorial part 4
Mumbai Academisc
 
It Is Possible to Do Object-Oriented Programming in Java
Kevlin Henney
 
Ad

More from Berk Soysal (6)

PDF
Guest author manual
Berk Soysal
 
PPTX
Comparison of BER performances of 64-PSK and 64-QAM in AWGN channels
Berk Soysal
 
PDF
Biomedical project report detecting emg noise
Berk Soysal
 
PPTX
Self Evaluation Test
Berk Soysal
 
PPTX
Java Tutorial Lab 9
Berk Soysal
 
PPTX
Java Tutorial Lab 8
Berk Soysal
 
Guest author manual
Berk Soysal
 
Comparison of BER performances of 64-PSK and 64-QAM in AWGN channels
Berk Soysal
 
Biomedical project report detecting emg noise
Berk Soysal
 
Self Evaluation Test
Berk Soysal
 
Java Tutorial Lab 9
Berk Soysal
 
Java Tutorial Lab 8
Berk Soysal
 
Ad

Recently uploaded (20)

PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
PDF
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
 
PPTX
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
PPTX
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
PDF
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
PPTX
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
Introduction to web development | MERN Stack
JosephLiyon
 
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 

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