I have a class PointDensity that is implemented as:
import java.sql.Date;
public class PointDensity {
private int id_place;
private String algorithm;
private Date mission_date;
private int mission_hour;
private int x;
private int y;
public PointDensity(int id_place, String algorithm, Date mission_date, int mission_hour, int x, int y) {
this.id_place = id_place;
this.algorithm = algorithm;
this.mission_date = mission_date;
this.mission_hour = mission_hour;
this.x = x;
this.y = y;
}
#Override
public boolean equals(Object obj){
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PointDensity other = (PointDensity) obj;
return id_place == other.id_place
&& algorithm.equals(other.algorithm)
&& mission_date.equals(other.mission_date)
&& mission_hour == other.mission_hour
&& x == other.x
&& y== other.y;
}
#Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + y;
result = prime * result + ((algorithm == null) ? 0 : algorithm.hashCode());
result = prime * result + x;
result = prime * result + ((mission_date == null) ? 0 : mission_date.toString().hashCode());
result = prime * result + mission_hour;
result = prime * result + id_place;
return result;
}
}
I read some stuff and get values from a database. And then I want to use a HashMap() in order to store a PointDensity object as key and the times it has been encountered as a value. However, it never finds that the object is the same.
Map<PointDensity, Integer> pointDensities = new HashMap<>();
while (resultSet.next()){
PointDensity pointDensity =
new PointDensity(
resultSet.getInt(1),
resultSet.getString(2),
new Date(timestampValues.getLong(i)),
new java.util.Date(timestampValues.getLong(i)).getHours(),
xValues.getInt(i),
yValues.getInt(i)
);
if (pointDensities.containsKey(pointDensity)){
//IT NEVER ENTERS HERE!!!
System.out.println("exists");
int times = pointDensities.get(pointDensity);
pointDensities.replace(pointDensity, times++);
}else{
pointDensities.put(pointDensity, 1);
}
}
Thanks in advance :)
The following code works for me, your hashcode and equals methods seem OK. The error must be in code you aren't showing us, or in the database data itself.
public static void main( String[] args ) {
Map<PointDensity, Integer> pointDensities = new HashMap<>();
PointDensity pointDensity = new PointDensity( 10, "TEST",
new Date( 3, 1, 2019 ), new java.util.Date().getHours(),
42, 24 );
pointDensities.put( pointDensity, 1 );
System.out.println( "pointDensity equal to self:"
+ pointDensity.equals( pointDensity ) );
if( pointDensities.containsKey( pointDensity ) ) {
//IT NEVER ENTERS HERE!!!
System.out.println( "exists" );
int times = pointDensities.get( pointDensity );
pointDensities.replace( pointDensity, times++ );
} else {
pointDensities.put( pointDensity, 1 );
}
}
}
Output:
run:
pointDensity equal to self:true
exists
BUILD SUCCESSFUL (total time: 0 seconds)
Related
I'm having this error:
The method getValue() is undefined for the type Object
*This is the code in error: In the line:
final String materialName = BasePlugin.getPlugin().getItemDb().getName(new ItemStack(((Object) ((Map)dataMap).entrySet().iterator().next()).getValue().getItemType(), 1));
Complete Code
public class LandMap
{
private static final int FACTION_MAP_RADIUS_BLOCKS = 22;
private static final Material[] BLACKLISK;
#SuppressWarnings("rawtypes")
public static boolean updateMap(final Player player, final HCF plugin, final VisualType visualType, final boolean inform) {
final Location location = player.getLocation();
final World world = player.getWorld();
final int locationX = location.getBlockX();
final int locationZ = location.getBlockZ();
final int minimumX = locationX - 22;
final int minimumZ = locationZ - 22;
final int maximumX = locationX + 22;
final int maximumZ = locationZ + 22;
final Set<Claim> board = new LinkedHashSet<Claim>();
boolean subclaimBased;
if (visualType == VisualType.SUBCLAIM_MAP) {
subclaimBased = true;
}
else {
if (visualType != VisualType.CLAIM_MAP) {
player.sendMessage(ConfigurationService.RED + "Not supported: " + visualType.name().toLowerCase() + '.');
return false;
}
subclaimBased = false;
}
for (int x = minimumX; x <= maximumX; ++x) {
for (int z = minimumZ; z <= maximumZ; ++z) {
final Claim claim = plugin.getFactionManager().getClaimAt(world, x, z);
if (claim != null) {
if (subclaimBased) {
board.addAll(claim.getSubclaims());
}
else {
board.add(claim);
}
}
}
}
if (board.isEmpty()) {
player.sendMessage(ConfigurationService.RED + "No claims are in your visual range to display.");
return false;
}
for (final Claim claim2 : board) {
if (claim2 == null) {
continue;
}
final int maxHeight = Math.min(world.getMaxHeight(), 256);
final Location[] corners = claim2.getCornerLocations();
final List<MemoryBlockLocation> shown = new ArrayList<MemoryBlockLocation>(maxHeight * corners.length);
for (final Location corner : corners) {
for (int y = 0; y < maxHeight; ++y) {
shown.add(new MemoryBlockLocation(world, corner.getBlockX(), y, corner.getBlockZ()));
}
}
final Object dataMap = plugin.getVisualiseHandler().generate(player, shown, visualType, true);
if (((Map)dataMap).isEmpty()) {
continue;
}
final String materialName = BasePlugin.getPlugin().getItemDb().getName(new ItemStack(((Object) ((Map)dataMap).entrySet().iterator().next()).getValue().getItemType(), 1));
if (!inform || claim2.getFaction() == null) {
continue;
}
player.sendMessage(ConfigurationService.YELLOW + claim2.getFaction().getDisplayName((CommandSender)player) + ConfigurationService.YELLOW + " owns land " + ConfigurationService.GRAY + " (displayed with " + materialName + ")" + ConfigurationService.YELLOW + '.');
}
return true;
}
public static Location getNearestSafePosition(final Player player, final Location origin, final int searchRadius) {
return getNearestSafePosition(player, origin, searchRadius, false);
}
public static Location getNearestSafePosition(final Player player, final Location origin, final int searchRadius, final boolean stuck) {
final FactionManager factionManager = HCF.getPlugin().getFactionManager();
final Faction playerFaction = factionManager.getPlayerFaction(player.getUniqueId());
final int max = ConfigurationService.BORDER_SIZES.get(origin.getWorld().getEnvironment());
final int originalX = Math.max(Math.min(origin.getBlockX(), max), -max);
final int originalZ = Math.max(Math.min(origin.getBlockZ(), max), -max);
final int minX = Math.max(originalX - searchRadius, -max) - originalX;
final int maxX = Math.min(originalX + searchRadius, max) - originalX;
final int minZ = Math.max(originalZ - searchRadius, -max) - originalZ;
final int maxZ = Math.min(originalZ + searchRadius, max) - originalZ;
for (int x = 0; x < searchRadius; ++x) {
if (x <= maxX) {
if (-x >= minX) {
for (int z = 0; z < searchRadius; ++z) {
if (z <= maxZ) {
if (-z >= minZ) {
final Location atPos = origin.clone().add((double)x, 0.0, (double)z);
final Faction factionAtPos = factionManager.getFactionAt(atPos);
if (factionAtPos == null || (!stuck && playerFaction != null && playerFaction.equals(factionAtPos)) || !(factionAtPos instanceof PlayerFaction)) {
final Location safe = getSafeLocation(origin.getWorld(), atPos.getBlockX(), atPos.getBlockZ());
if (safe != null) {
return safe.add(0.5, 0.5, 0.5);
}
}
final Location atNeg = origin.clone().add((double)x, 0.0, (double)z);
final Faction factionAtNeg = factionManager.getFactionAt(atNeg);
if (factionAtNeg == null || (!stuck && playerFaction != null && playerFaction.equals(factionAtNeg)) || !(factionAtNeg instanceof PlayerFaction)) {
final Location safe2 = getSafeLocation(origin.getWorld(), atNeg.getBlockX(), atNeg.getBlockZ());
if (safe2 != null) {
return safe2.add(0.5, 0.5, 0.5);
}
}
}
}
}
}
}
}
return null;
}
private static Location getSafeLocation(final World world, final int x, final int z) {
Block highest = world.getHighestBlockAt(x, z);
Material type = highest.getType();
if (Arrays.asList(LandMap.BLACKLISK).contains(type)) {
return null;
}
while (!type.isSolid()) {
if (highest.getY() <= 1 || Arrays.asList(LandMap.BLACKLISK).contains(type)) {
return null;
}
highest = highest.getRelative(BlockFace.DOWN);
type = highest.getType();
}
return highest.getRelative(BlockFace.UP).getLocation();
}
static {
BLACKLISK = new Material[] { Material.LEAVES, Material.LEAVES_2, Material.FENCE_GATE, Material.WATER, Material.LAVA, Material.STATIONARY_LAVA, Material.STATIONARY_WATER };
}
}
Imports:
import org.bukkit.entity.*;
import net.tutorialesaful.hardcorefactions.faction.claim.*;
import net.tutorialesaful.hardcorefactions.*;
import net.tutorialesaful.hardcorefactions.util.location.*;
import net.tutorialesaful.framework.*;
import net.tutorialesaful.hardcorefactions.visualise.*;
import org.bukkit.inventory.*;
import org.bukkit.command.*;
import org.bukkit.*;
import net.tutorialesaful.hardcorefactions.faction.type.*;
import java.util.*;
import org.bukkit.block.*;
This is a class of bukkit
what object generate() method returns? Could you add generate() method to your question?
you try to cast it to an instance of the Object
((Object) ((Map)dataMap).entrySet().iterator().next()).getValue()...
which (as #MadProgrammer said) doesn't have getValue() method. If you need the Map you should to cast it to the Map and use the get(Object key) method
final String materialName = BasePlugin.getPlugin().getItemDb().getName(new ItemStack(((Map) ((Map)dataMap).entrySet().iterator().next()).get(**the key**).getItemType(), 1));
But in this case you must know the key.
I'm trying to call a divide method that's overridden from an interface class. All similar methods (add, subtract, multiply) work just fine, except divide for some reason isn't found. Here's what I have below:
Arithmetic Interface class
public interface Arithmetic {
public Object add(Object obj);
public Object subtract(Object obj);
public Object multiply(Object obj);
public Object divide(Object obj);
}
Number class (Arithmetic methods are overridden below, divide being the last one)
public class Number implements Comparable,Arithmetic{
/**
* A string representing a non-negative number
*/
private String value;
/**
* An integer greater than or equals to 2.
*/
private int base;
/**
* - for negative numbers and otherwise null
*/
private char sign;
/**
* creates 0 base 2.
*/
public Number()
{
value = "0";
base = 2;
}
/**
* creates a number in a specified based using the specified parameters.
* #param num a string representing a non-negative number with digit
* written in uppercase letters.
* #param radix the base of the number
*/
public Number(String num, int radix) throws InvalidNumberException
{
String list = ".-0123456ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Boolean pars = false;
for (int i = 0; i < list.length(); i++) {
if (num.indexOf(list.charAt(i)) != -1) {
pars = true;
}
}
if (radix < 1 || radix > 35) {
throw new InvalidNumberException("Number invoked with a radix less than 1 or greater than 35.");
} else if ((num.indexOf(".") != num.lastIndexOf(".")) || (num.indexOf("-") != num.lastIndexOf("-"))) {
throw new InvalidNumberException("Number invoked with more than one dash or period in string.");
} else if (num.indexOf("-") != 0) {
throw new InvalidNumberException("Number invoked with dash not at beginning of string.");
} else if (num.indexOf(".") == num.length()) {
throw new InvalidNumberException("Number invoked with period at end of string.");
}
else if (pars == true) {
throw new InvalidNumberException("Number invoked with invalid character.");
}
Boolean test=false;
for (int i = 0; i < num.length(); i++) {
if ((num.charAt(i) != '-') || (num.charAt(i) != '.')) {
if (toValue(num.charAt(i)) >= radix) {
test=true;
throw new InvalidNumberException("Number invoked with digit greater or equal to radix.");
}
}
}
if(test==false){
value = num;
base = radix;
}
}
/**
* Converts a digit to its integer equivalent
* #param digit 0...9 or A...Z to be converted
* #return the integer equivalent of the specified digit.
*/
private int toValue(char digit)
{
return Character.getNumericValue(digit);
}
/**
* Converts this integer to its equivalent digit
* #param anInt an integer between 0-35
* #return the digit equivalent to an integer
*/
private char toDigit(int anInt)
{
if(anInt >= 0 && anInt <= 9)
return (char)(anInt + 48);
else if(anInt >= 10 && anInt <= 35)
return (char)(anInt + 55);
else
return (char)(-1);
}
/**
* converts a number to its decimal equivalent (double).
* #return the decimal equivalent of a number
*/
private double toDouble()
{
if (base == 10)
return Double.parseDouble(value);
int periodIndex = value.indexOf('.');
double base10Num = 0;
int i;
int radix = base;
String whole;
if (periodIndex >=0)
whole = value.substring(0, periodIndex);
else
whole = value;
int j=0;
int wholeLength = whole.length();
for (i=wholeLength-1; i>=0; i--)
base10Num = base10Num + toValue(whole.charAt(i))*(int)Math.pow(radix,wholeLength-i-1);
if (periodIndex >= 0)
{
String fract = value.substring(periodIndex+1,value.length());
int fractLength = fract.length();
for (i=0; i<fractLength; i++)
{
base10Num = base10Num + toValue(fract.charAt(i))*Math.pow(radix,-i-1);
}
}
if (sign == '-')
base10Num *= -1;
return base10Num;
}
/**
* converts a decimal number (double) to its equivalent representation
* in a given base.
* #param dec the decimal (base 10) number
* #radix the base to convert the number to.
* #return the equivalent number in the specified base
*/
private Number doubleToNumber(double dec, int radix)
{
if (radix == 10)
return new Number(Double.toString(dec),radix);
String numSign="";
if (dec < 0)
{
numSign = "-";
dec = -dec;
}
int whole = (int)dec;
double fract = dec - whole;
String numStr = "";
while (whole != 0)
{
numStr = toDigit(whole%radix) + numStr;
whole = whole / radix;
}
if (fract != 0)
{
numStr += ".";
int tolerance = 20;
int precision = 0;
while (precision < tolerance && fract != 0)
{
fract = fract * radix;
numStr += toDigit((int)(fract));
precision++;
fract = fract - (int)fract;
}
}
return new Number(numSign+numStr,radix);
}
/**
* Gives a string representation of this number in the
* format number[base].
* #return a string representation of this number
*/
#Override
public String toString()
{
return String.format("%s%s[%d]", this.sign, this.value, this.base);
}
#Override
public boolean equals(Object obj)
{
if (!(obj instanceof Number))
return false;
return (this.toDouble()==((Number)obj).toDouble());
}
#Override
public int compareTo(Object obj) throws IllegalArgumentException
{
if (!(obj instanceof Number))throw new IllegalArgumentException();
else if(this.toDouble()<((Number)obj).toDouble()) return -1;
else if(this.toDouble()>((Number)obj).toDouble()) return 1;
else return 0;
}
/**
* This method adds two numbers from two different objects
* #param obj the number of the object inputted from user
* #return a new added number object
*/
#Override
public Object add(Object obj) throws IllegalArgumentException {
if (!(obj instanceof Number)) {
throw new IllegalArgumentException();
} else if (this.base != ((Number) obj).base) {
throw new IllegalArgumentException();
} else {
return doubleToNumber((this.toDouble() + ((Number) obj).toDouble()), ((Number) obj).base);
}
}
/**
* This method subtracts two numbers from two different objects
* #param obj the number of the object inputted from user
* #return a new subtracted number object
*/
#Override
public Object subtract(Object obj) throws IllegalArgumentException {
if (!(obj instanceof Number)) {
throw new IllegalArgumentException();
} else if (this.base != ((Number) obj).base) {
throw new IllegalArgumentException();
} else {
return doubleToNumber((this.toDouble() - ((Number)obj).toDouble()), ((Number) obj).base);
}
}
/**
* This method multiplies two numbers from two different objects
* #param obj the number of the object inputted from user
* #return a new multiplied number object
*/
#Override
public Object multiply(Object obj) throws IllegalArgumentException{
if (!(obj instanceof Number)) {
throw new IllegalArgumentException();
} else if (this.base != ((Number) obj).base) {
throw new IllegalArgumentException();
} else {
return doubleToNumber(this.toDouble() * ((Number)obj).toDouble(), ((Number) obj).base);
}
}
/**
* This method divides two numbers from two different objects
* #param obj the number of the object inputted from user
* #return a new divided number object
*/
#Override
public Object divide(Object obj) throws IllegalArgumentException {
if (!(obj instanceof Number)) {
throw new IllegalArgumentException();
} else if (this.base != ((Number) obj).base) {
throw new IllegalArgumentException();
} else if (((Number) obj).toDouble() == 0) {
throw new IllegalArgumentException();
} else {
return doubleToNumber((this.toDouble() / ((Number) obj).toDouble()), ((Number) obj).base);
}
}
}
And finally my demo class, NumberDemo
public class NumberDemo
{
public static void main(String[] args)
{
Number a = new Number("12.25", 8);
Number b = new Number("13.75", 8);
System.out.println(a.toString() + " + " + b.toString() + " = " +
a.add(b));
a = new Number("ABC.75", 16);
b = new Number("18.5F", 16);
Number c = new Number("2.FB", 16);
System.out.println("(" + a.toString() + " - " + b.toString() + ") / " +
c.toString() + " = " + (a.subtract(b)).divide(c));
a = new Number("3.45", 9);
b = new Number("32.25", 9);
c = new Number("3.05", 9);
System.out.println(a.toString() + "(" + b.toString() + " + " +
c.toString() + " = " + a.multiply(b.add(c)));
a = new Number("10111.11", 2);
b = new Number("1100110.01", 2);
c = new Number("-101", 2);
System.out.println("(" + a.toString() + " - " + b.toString() + ") / " +
c.toString() + " = " + (a.subtract(b)).divide(c));
a = new Number("-5", 8);
b = new Number("5", 8);
System.out.println(a.toString() + " x " + b.toString() + " = " +
a.multiply(b));
a = new Number("-0.5", 8);
b = a;
System.out.println(a.toString() + " x " + b.toString() + " = " +
a.multiply(b));
}
}
When I call all the arithmetic methods, they appear to be called just fine, however calling divide brings me the error tooltip:
cannot find symbol
symbol: method divide(Number)
location: class Object
I can't seem to find the reason for this, can anyone spot it?
This is my first time using Stockoverflow, so please let me know if anything's apart from the format norm.
i have this class thats going to fill a list with All employees that are pre made in an array. I can populate an ArrayList with employees but the only problem is that i get a few "Duplicate" entries, i use quotes cause they are not EXACTLY the same but they could share the same name or employee number but may not have the same hire year or salary ect.
heres the employee class :
public class Employee {
public String EmployeeName;
public String EmployeeNumber;
public int hireyear;
public double WeeklyEarning;
public Employee()
{
EmployeeName = null;
EmployeeNumber = null;
hireyear = 0;
WeeklyEarning = 0;
}
public static final String[] Empnum = new String[] {
"0001-A", "0002-B","0003-C","0004-D","0002-A",
"0003-B","0004-C","0005-D","0011-A", "0012-B",
"0013-C","0014-D","0121-A", "0122-B","0123-C",
"0321-A", "0312-B","1234-D","4321-C","1122-D"};
public static final String[] Ename = new String[] {
"Josh", "Alex", "Paul", "Jimmy", "Josh", "Gordan", "Neil", "Bob",
"Shiv", "James", "Jay", "Chris", "Michael", "Andrew", "Stuart",
"Dave","Benjamin","Dan","Brian","Michelle"};
public String getEmployeeName()
{
return this.EmployeeName;
}
public String getEmployeeNumber()
{
return this.EmployeeNumber;
}
public int gethireyear()
{
return this.hireyear;
}
public double getWeeklyEarning()
{
return this.WeeklyEarning;
}
public String setEmployeeName(String EName)
{
return this.EmployeeName = EName;
}
public String setEmployeeNumber(String ENumber)
{
return this.EmployeeNumber = ENumber;
}
public int setEmployeehireyear(int Ehireyear)
{
return this.hireyear = Ehireyear;
}
public double setEmployeeweeklyearning(double Eweeklyearning)
{
return this.WeeklyEarning = Eweeklyearning;
}
public String toString(){
String data = "\n Employee Name : " + EmployeeName + " \n Employee Number: " + EmployeeNumber + " \n Hire Year : " + hireyear + "\n Weekly Earnings : " + WeeklyEarning;
return data;
}
public boolean equals(Object o){
if(this == null){
return false;
}
if(this == o){
return true;
}
if(!(o instanceof Employee)){
return false;
}
Employee temp = (Employee) o;
if(this.getEmployeeName().equals(temp.getEmployeeName())){
return true;
}
if(this.getEmployeeNumber().equals(temp.getEmployeeNumber())){
return true;
}
if(this.gethireyear() == temp.gethireyear()){
return true;
}
if(this.getWeeklyEarning() == temp.getWeeklyEarning()){
return true;
}
return false;
}
}
Heres the generateList method that will populate the list:
public ArrayList<Employee> generateEmpList(){
empList = new ArrayList <Employee>();
Random empPicker = new Random();
for(int i = 0; i < 20; i++){
int id = empPicker.nextInt(20);
if(id < 12) // roll for production worker
{
//System.out.println("Adding Production Worker");
ProductionWorker temp = new ProductionWorker();
temp = temp.generateProductionWorker();
prodWorker = temp;
empList.add(prodWorker);
}
else //roll for Shift supervisor
{
//System.out.println("Adding Shift supervisor");
ShiftSupervisor supervisor = new ShiftSupervisor();
supervisor = supervisor.generateShiftSupervisor();
shiftWorker = supervisor;
empList.add(shiftWorker);
}
}
Iterator iterator = empList.iterator();
while (iterator.hasNext()) {
System.out.println("");
System.out.println(iterator.next());
}
return empList;
}
and also which could be helpful is the "generateProductionWorker()" and shiftSupervisor methods - to keep it short ill only post prod worker method cause they are basically the same:
public ProductionWorker generateProductionWorker(){
Random rng = new Random();
int numberOfEmployeeNames = Ename.length;
ProductionWorker tempPworker = new ProductionWorker();
String employeeName = Ename[rng.nextInt(numberOfEmployeeNames)];
tempPworker.setEmployeeName(employeeName);
int numberOfEmployeeNumbers = Empnum.length;
String employeeNumber = Empnum[rng.nextInt(numberOfEmployeeNumbers)];
tempPworker.setEmployeeNumber(employeeNumber);
int yearHired = rng.nextInt(35) + 1980;
tempPworker.setEmployeehireyear(yearHired);
double weeklySalary = rng.nextInt((100) * 100);
tempPworker.setEmployeeweeklyearning(weeklySalary);
int hourlyRate = rng.nextInt(20) + 10;
tempPworker.setHourlyRate(hourlyRate);
return tempPworker;
}
I'm sure I'm missing something trivial but any ideas why i get similar entries when i have a list of 20 names and numbers?
ex:
empname - josh
empnum - 0000-A
hireyear - 1994
salary - 40,000
empname - josh
empnum - 0000-A
hireyear - 1999
salary - 60,500
any advice would help, Thanks!
Look at the equals method in your Employee class. If their name are the same, you return true, which means these are equals. The same is for the other attributes. You must replace your if statements.
I agree with Georgi, you equals method is the culprit.
Currently it is returning true after the first if statement at the line that reads
if(this.getEmployeeName().equals(temp.getEmployeeName())){
return true;
}
Because it is a return statement it stops the method from continuing to the other statements. You might try this:
public boolean equals(Object o){
if(this == null){
return false;
}
if(this == o){
return true;
}
if(!(o instanceof Employee)){
return false;
}
//set all the elements in the array to false and change to true when true.
boolean [] doesItMatch = new boolean[4];
doesItMatch[0] = false;
doesItMatch[1] = false;
doesItMatch[2] = false;
doesItMatch[3] = false;
Employee temp = (Employee) o;
if(this.getEmployeeName().equals(temp.getEmployeeName())){
doesItMatch[0] = true;
}
if(this.getEmployeeNumber().equals(temp.getEmployeeNumber())){
doesItMatch[1] = true;
}
if(this.gethireyear() == temp.gethireyear()){
doesItMatch[2] = true;
}
if(this.getWeeklyEarning() == temp.getWeeklyEarning()){
doesItMatch[3] = true;
}
int check = 0;
//Now that you have checked all the values, check the array. Using a simple counter.
for(int i = 0; i < doesItMatch.length; i++){
if(doesItMatch[i]){
check++;
} else {
check--;
}
}
//The counter should be 4 when the if statements above are all true. Anything else is false.
if(check == 4){
return true;
} else {
return false;
}
}
This method now checks each of the attributes in the Employee class. (Name, Number, Hire year and so on. If you create more attributes to the class it is easy to add more elements to the array just be sure to set them to false.)
Hope this helps
This also would take a little maintenance if you expanded the Employee class so you might want to find a way to make it a little easier on yourself.
Suppose I have a Point class with fields x,y,z,name.
Objects of this class must be inserted in a Hash Set in two different ways depending on the user choice:
equals() checks equality of x,y,z fields
equals() checks equality of x,y,z,name fields
So I wonder what is the better way to organize such equals() redefinition on the fly during program execution?
You can create 2 Classes: Point and NamedPoint and override the equals() and hashcode() methods.
Here is an example in pseudo code:
class Point{
String name
int x,y,z
equals{
...
}
hashCode{
...
}
}
class NamedPoint extends Point{
equals{
}
hashCode{
}
}
Another option is to use one class, but add a new boolean switch, which changes the behavior of equals and hashCode.
P.S. I recommend you to read What issues should be considered when overriding equals and hashCode in Java?. You must be careful overriding these methods. It is useful to use the eclipse feature "Source --> Generate hashCode() and equals().." and after that to change the behavior of the generated methods.
P.S.2: Grigory Kalabin's link describes, what are the problems that might occure doing this.
If you can change from using HashSet to a TreeSet and add a custom comparator:
public class Point implements Comparable<Point> {
private double x;
private double y;
private double z;
private String name;
public Point( final double x, final double y, final double z, final String name ) {
setX( x );
setY( y );
setZ( z );
setName( name );
}
public boolean equals( final Point point ){
final boolean isEqual =
( this.getX() == point.getX()
&& this.getY() == point.getY()
&& this.getZ() == point.getZ() )
|| ( this.getName().equals( point.getName() ) );
// System.out.println( this.toString() + " == " + point.toString() + " = " + isEqual );
return isEqual;
}
/// #return the x
public double getX() { return x; }
/// #return the y
public double getY() { return y; }
/// #return the z
public double getZ() { return z; }
/// #return the name
public String getName() { return name; }
/// #param x the x to set
public void setX(final double x) { this.x = x; }
/// #param y the y to set
public void setY(final double y) { this.y = y; }
/// #param z the z to set
public void setZ(final double z) { this.z = z; }
/// #param name the name to set
public void setName(final String name) { this.name = name; }
public String toString() {
final StringBuffer str = new StringBuffer();
str.append( '(' );
str.append( getX() );
str.append( ',' );
str.append( getY() );
str.append( ',' );
str.append( getZ() );
str.append( ',' );
str.append( getName() );
str.append( ')' );
return str.toString();
}
public double distanceFromOriginSquared(){
return this.getX()*this.getX()
+ this.getY()*this.getY()
+ this.getZ()*this.getZ();
}
#Override
public int compareTo( final Point point ) {
if ( this.getName().equals( point.getName() ) )
return 0;
final double td = this.distanceFromOriginSquared();
final double pd = point.distanceFromOriginSquared();
if ( td < pd ) return -1;
if ( td > pd ) return +1;
if ( this.getX() < point.getX() ) return -1;
if ( this.getX() > point.getX() ) return +1;
if ( this.getY() < point.getY() ) return -1;
if ( this.getY() > point.getY() ) return +1;
return 0;
}
}
Running this:
public static void main( final String[] args ){
Point[] pts = {
new Point( 1, 1, 1, "1" ),
new Point( 2, 2, 2, "2" ),
new Point( 3, 3, 3, "3" ),
new Point( 1, 1, 1, "4" ),
new Point( 4, 4, 4, "2" )
};
TreeSet<Point> ps = new TreeSet<Point>();
for ( Point p : pts )
ps.add( p );
System.out.println( ps );
}
Gives an output of
[(1.0,1.0,1.0,1), (2.0,2.0,2.0,2), (3.0,3.0,3.0,3)]
So the last two values are not entered into the TreeSet as they are rejected by the comparator.
So I have this compiler class that compiles some .mjava files but others it fails on and wondering if anyone can help me figure out why. I have two methods that break for two different files. The first consts.mjava file I try to compile is:
// demo of true local and global variables
int glob0;
int glob1;
final int two = 2;
final int three = 3;
main() {
int loc1;
int loc2;
int loc3;
final int four = 4;
glob0 = three;
//print("glob0=", glob0, "\n");
loc1 = glob0*two+1;
glob1 = glob0*loc1;
loc2 = glob1+1;
loc3 = glob1*loc2/four;
print("glob0=", glob0, " (should be 3)\n");
print("glob1=", glob1, " (should be 21)\n");
print("loc1=", loc1, " (should be 7)\n");
print("loc2=", loc2, " (should be 22)\n");
print("loc3=", loc3, " (should be 115)\n");
}
When I try to compile this with my compiler class it breaks here:
private void compileFactor() {
if (isIdent(theToken)) {
String ident = theToken;
theToken = t.token();
IdentInfo theInfo = symTable.lookup(ident);
boolean its_a_variable = theInfo.isVar(); ***//Breaks Here for consts.mjava Null Exception***
int theAddr;
boolean isGlobal = theInfo.getIsGlobal();
int constValue;
int theNumber = 0;
if (its_a_variable) { // pld12: CHANGE THIS!!
theAddr = theInfo.getAddr();
isGlobal = theInfo.getIsGlobal();
if (theAddr == -1) t.error("undeclared identifier used in expr: "+ident);
if (isGlobal) cs.emit(Machine.LOAD, theAddr);
else cs.emit(Machine.LOADF, theAddr);
} else {
constValue = theInfo.getValue();
if (constValue == 0)
t.error("undeclared identifier used in expr: "+ident);
else {
cs.emitLOADINT(theNumber);
}
}
} else if (isNumber(theToken)) {
int theNumber = new Integer(theToken).intValue();
cs.emitLOADINT(theNumber);
theToken = t.token();
} else if (equals(theToken, "(")) {
accept("(");
compileExpr();
accept(")");
}
}
The next locs.mjava file I try to run breaks on this method:
private void compileIdentStmt() {
String ident = theToken;
boolean isGlobal = true;
int location = 0;
int entryPoint = 0;
IdentInfo varInfo = null;
if (!isIdent(ident)) t.error("expected identifier, got " + theToken);
theToken = t.token();
if (equals(theToken, "=")) {
accept("=");
varInfo = symTable.lookup(ident);
if (varInfo.isVar() == true) { ***//Breaks Here on locs.mjava: Null Exception***
location = varInfo.getAddr();
isGlobal = varInfo.getIsGlobal();
}
/*
if (varInfo==null) {
location = GHack(ident);
isGlobal = true;
}
if (location == -1) {
location = LHack(ident);
isGlobal = false;
}
/* */
compileExpr();
if (isGlobal) cs.emit(Machine.STOR, location);
else cs.emit(Machine.STORF, location);
accept(";");
} else if (equals(theToken, "(")) {
varInfo = symTable.lookup(ident);
if (varInfo.isProc() == true) {
entryPoint = varInfo.getEntryPoint();
dprint("call to function " + ident + "; generating JSR to location " + entryPoint);
accept("(");
}
/*
if (!equals(theToken, ")")) {
compileExpr();
while (equals(theToken, ",")) {
accept(",");
compileExpr();
}
}
/* */
accept(")");
accept(";");
cs.emit(Machine.JSR, entryPoint);
} else t.error("expected \"=\" or \"(\", got " + theToken);
}
I will even supply my lookup method from my symTable() to help:
public IdentInfo lookup(String ident) {
IdentInfo ii;
if (HMLocal != null) {
ii = HMLocal.get(ident);
if (ii != null) {
return ii;
}
ii = HMGlobal.get(ident);
if (ii != null) {
return ii;
}
}
return null;
}
If you're getting NullPointerExceptions then it's because theInfo and varInfo are null in your examples.
After
IdentInfo theInfo = symTable.lookup(ident);
you should check if theInfo is null before trying to work with it, since your lookup method clearly states it can return null.