Download presentation
Presentation is loading. Please wait.
Published byΚλεοπάτρα Σερπετζόγλου Modified over 6 years ago
1
CSE 1030: Implementing Non-Static Features
Mark Shtern
2
Summary Utility Class Contracts Javadoc /** */, tags
@param Parameter_Name Parameter_Description @return Description_Of_Value_Returned @throws Exception_Type Explanation @deprecated @see Package_Name.Class_Name @author Author @version Version_Information
3
Ex03 Write a static method majority() that takes three boolean arguments and returns true if at least two of the arguments have the value true, and false otherwise. Do not use an if statement.
4
Ex04 Write a utility class with two methods
“testWord”: takes a String as an argument and returns false if it has received this argument before, otherwise returns true “getDifferentWordCounter”: returns the number of different words that were tested Write application which calculates how many different words in an input Write application which calculates how many different words in a text file
5
Ex05 Develop ErrUtils utility which helps to print error messages. The utility will have the following interface ErrUtils{ public static void printErr(String error); public static void setBeepMode(boolean); public static boolean getBeepMode(); public static final String DISK_FAILURE = “Disk Failure”; }
6
Summary Boolean expression Utility state Attributes Set &&, ||
private static Set<String> set = new TreeSet<String>() Set add, contains, size, remove, clear HashSet, TreeSet
7
Class Attributes associated with instance
Class needs to be instantiated Every class has a constructor
8
Class Attributes Methods Constructors public Counter(long a) Example
private long counter; Methods public double add (long a) Constructors public Counter(long a)
9
The this Parameter this must be used if a parameter or other local variable with the same name is used in the method Otherwise, all instances of the variable name will be interpreted as local int someVariable = this.someVariable local instance
10
The methods equals, hashCode and toString
Java expects certain methods, such as equals, hashCode and toString, to be in all, or almost all, classes The purpose of equals, a boolean valued method, is to compare two objects of the class to see if they satisfy the notion of "being equal" Note: You cannot use == to compare objects public boolean equals(ClassName objectName) The purpose of the toString method is to return a String value that represents the data in the object public String toString() The purpose of the hashCode method is to return an integer that can serve a hash code for the object – it should be the same for objects that are equals public int hashCode()
11
Tip It is generally better to override toString, equals and hashCode methods with version that are appropriate to the class being define
12
Memory public static void main(String[] args) { int a = 5; Point p = new Point(1.5,1.5); move(5, p); } static int move (int a,Point p){ Point p1 = p; double x = p.getX() + a; double y = p.getY() + a; p1.setLocation(x,y); This !!!!!!!!!!!!!!!!!!!! Note: this parameter
13
compareTo x.compareTo(y) returns 0 if and only if y.compareTo(x) returns 0 x.compareTo(y) < 0 if and only if y.compareTo(x)>0 If x.compareTo(y) <0 & y.compareTo(z)<0 than x.compareTo(z) < 0 If x.compareTo(y) = 0 & y.compareTo(z) = 0 than x.compareTo(z) = 0
14
Ex10 Design and implement a Rectangle class
Attributes Width Height Methods getArea Assessors, Mutators Write application that sorts Rectangles by area
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.