Create an empty constructor when wrong parameters - java

class classname {
private int value;
public classname(int value) {
if(value > 20 || value < 1) {
//make object null
}
else {
this.value = value;
}
}
}
Basically, when the parameter is not in the range I want to make an empty object.
something like:
classname newclass = new classname(100);
if(newclass == null) {
//this is what i want
}

Instead of initialising an object with null, you should throw an IllegalArgumentException, e.g.:
if(value > 20 || value < 1) {
throw new IllegalArgumentException("Value must be between 1 and 20");
}
This would prevent the initialisation and return correct error message to the user. Also, this is considered as best practice (e.g. try calling Integer.parseInt("abc");)

For this look at factory design. You should create a factory class and let that factory return the class instance. Inside factory implementation you can write the logic based on parameter.
Look at this post.
Create Instance of different Object Based on parameter passed to function using C#

You should create a factory method which either return instance if argument is valid or null and hide the constructor using private:
class YourClass {
private int value;
// Factory method
public static YourClass newYourClass(int value) {
if(value > 20 || value < 1)
return null;
else
return new YourClass(value);
}
private YourClass(int value) {
this.value = value;
}
}

You can make it in this way
public class MyClass {
private int value; // if -1 not in the range!
public MyClass(int value) {
if (value > 20 || value < 1) {
this.value = -1;
} else {
this.value = value;
}
} //end of constructor
} //end of the MyClass

you can do somthing like this
class classname {
private Integer value;
public classname(Integer value) {
this.value = value <1 || value>20 ? null : value;
}
}

public class CustomObjectFactory
{
private int value;
CustomObjectFactory(int value)
{
this.value = value;
}
public CustomObjectFactory getInstance()
{
System.out.print(value);
if(value<10)
{
System.out.print("if"+value);
return null;
}else
{
System.out.print("else"+value);
return new CustomObjectFactory(value);
}
}
}

Related

Check if primitive has been set

Given a very simple class:
class MyClass {
int id;
double value;
MyClass(int id) {
this.id = id;
}
void setValue(double v) {
value = v;
}
boolean isValueUnassigned() {
return value == 0;
}
}
To check if value has not been assigned yet, is it OK if I just do return value == 0; since a double is 0 by default?
You should go for wrapper class for double which is Double. For Double data type default value would be null. So that there would not be any ambiguity. If value is null, then it's not assigned any value.
Well, yes primitive double is set to 0.0 by default. But if you simply do return value == 0; you can't be sure if someone called setValue(0) before, but it is a valid assignment too. If you want to be 100% sure if someone called the setValue() I would suggest something like this:
class MyClass {
private int id;
private double value;
private boolean valueSet; // is false by default
public MyClass(int id) {
this.id = id;
}
public void setValue(double v) {
value = v;
valueSet = true;
}
public boolean isValueSet() {
return valueSet;
}
}
Adding to what #Harshal has already said. Code for something like that would look like:
class MyClass {
private int id;
private Double value;
public MyClass(int id) {
this.id = id;
}
public void setValue(double v) {
value = v;
}
public double getValue() {
//Check for null pointer and return
if(value == null)
return <whatever you want>;
return value.doubleValue();;
}
public boolean isValueSet() {
return (value == null ? false : true);
}
}
You can use Double to reinitialize the double using following
class MyClass {
int id;
Double value;
MyClass(int id) {
this.id = id;
}
void setValue(Double v) {
value = v;
}
boolean isValueUnassigned() {
return value == null ? false : true;
}
}
Explanation
The main issue here is that, whatever double value you choose, e.g. 0 or -1, it could actually be a valid value set by the user. In which case your application would falsely return that it was not set yet, while it was.
What you need is called a sentinel value, i.e. a special value that indicates this case. Typically there are 3 approaches:
Flag
Introduce a simple boolean flag boolean isSet which you initialize to false and set to true once it was set.
This approach is good and really fast. But does not scale well if you, for example, start to introduce hundreds of such values for which you need to represent "not set yet".
double value;
boolean isValueSet = false;
void setValue(double value) {
this.value = value;
isValueSet = true;
}
boolean isValueUnassigned() {
return !isValueSet;
}
Object wrapper
Object variables can, additionally to their actual values/instances also refer to null. This can be used as sentinel to indicate the special case.
So you could go for having the value internally represented as Double instead of double, starting with null.
The disadvantage is that an object introduces quite some memory and performance overhead compared to a simple primitive. In this case it does not really matter but if you scale this up to a couple of thousands of them, you would definitely start to feel the impact.
Double value = null;
void setValue(double value) {
this.value = value; // auto-boxing
}
boolean isValueUnassigned() {
return value == null;
}
Sentinel value
If you application naturally allows that some values can never be used, you can use them as sentinel to indicate the case. A common example would be an age field for which you would not allow the user to set it to negative values. Then you can use, for example -1 to indicate it.
This approach is quite common and efficient. But it obviously is not always applicable and it is also not necessarily the most readable/maintainable approach.
double value = -1;
void setValue(double value) {
if (value < 0) {
throw new IllegalArgumentException("Negative values are not allowed");
}
this.value = value;
}
boolean isValueUnassigned() {
return value == -1;
}
The value is assigned when the object is created. You don't need a method to check if the value has been assigned because the answer is always yes.
Found the cleanest and clearest way to express it:
class MyClass {
int id;
Optional<Double> value;
MyClass(int id) {
this.id = id;
this.value = Optional.empty();
}
void setValue(double v) {
value = Optional.of(v);
}
double getValue() {
if (isValueUnassigned) {
throw new RuntimeException("Value has not been assigned");
}
return value.get();
}
boolean isValueUnassigned() {
return value.isEmpty();
}
}
a double value is 0 by default , but you can pass -1 to it .
double value = -1;
for check :
if (value!= -1) {
// To Do
}

Casting Function Return Type Based on Class Field

I've got a class that looks something like.
public class ParseValue {
public String value;
public final Class classType;
}
And I'd like to make a function that does a conversion and returns a casted value.
public T parseValue(ParseValue parseInfo) {
if(parseInfo.classType == String.class) {
return parseInfo.value;
} else if (parseInfo.classType == Double.class) {
return Double.valueOf(parseInfo.value);
}
}
Right now I can have this function return an Object and then cast it upon getting the result, but is there a way to make the function do the cast based on the input ParseValue's classType field?
The safest way to do it is to make ParseValue generic:
public class ParseValue<T> {
public String value;
public final Class<T> classType;
public T parseValue() {
Object result;
if (classType == String.class) {
result = value;
} else if (classType == Double.class) {
result = Double.valueOf(value);
} else {
throw new RuntimeException("unknown value type");
}
return classType.cast(result);
}
}

i don't know why there is an error in my for loop?

i dont understand why there is an error(The method findNodeByNode(ITreeNode<>) is undefined for the type NODETYPE) in my for loop. A college has exactly the same code, but he has no errors?
Please help me
public class GenericTreeNode<NODETYPE> extends Object implements ITreeNode<NODETYPE> {
NODETYPE nodeValue;
String label;
private LinkedList<NODETYPE> children;
public GenericTreeNode(String label, NODETYPE value)
{
this.label=label;
this.nodeValue=value;
children= new LinkedList<NODETYPE>();
}
public boolean checkNodeByValue(NODETYPE value) {
if(this.nodeValue.equals(value))
{
return true;
}
else
return false;
}
public ITreeNode<NODETYPE> findNodeByValue(NODETYPE searchValue) {
if(this.checkNodeByValue(searchValue))
{
return this;
}
if(this.isLeaf())
{
return null;
}
long length = this.children.size();
int i;
for(i=0; i < length; i++)
{
this.children.get(i)).findNodeByValue( searchValue);
}
return null;
}
You have two consecutive parentheses:
get(i))
Change the sentence by:
this.children.get(i).findNodeByValue( searchValue);
findNodeByValue(...) belongs to the class GenericTreeNode<NODETYPE>, but you are calling it on an instance of NODETYPE in your LinkedList<NODETYPE>. You can only call it on an instance of GenericTreeNode<NODETYPE> (or just call it if doing it on this instance of the class)

Java Return between a int or String(unknown return type)

My program loads information from a text file and creates an array of an object with the information whether it is a integer or a string.
I then want the object to return either a String or an Integer depending on whether the object is holding a integer value or a string value.
edit...
So here is my type class that holds either a int if the field in the text file is a number, or a string if the field is a word, and this is held in a Type array.
public class Type {
private String name;
private int value;
public Type(String name) {
this.name = name;
}
public Type(int value) {
this.value = value;
}
public String getName() {
return this.name;
}
public int getValue() {
return this.value;
}
public boolean isInt() {
boolean isInt = false;
if (this.value != 0) {
isInt = true;
return isInt;
}
return isInt;
}
}
So in my array could be either a Int or a String, i want to return the datatype without any long statements in my main class.
If you strictly want only to get the specific values, you could add a method to your Type class and get the values from this method, ugly but does what you want:
public <T> T getDynamicValue(Type t) {
if (isInt()) {
return (T) ((Integer) t.getValue());
} else {
return (T) t.getName();
}
}
use of it:
List<Type> dynamicList = Arrays.asList(new Type[]{new Type(1), new Type(2), new Type("dog")});
for (Type t : dynamicList) {
System.out.println("T -> " + t.getDynamicValue(t));
}
If you want to perform some manipulation with this data, you have to make an instanceof check and Cast it, for instance some splitting (or String methods) with the name value...
You can't choose the type of object to return at runtime. Your only option is to return an Object. You can check if it's a String or an int using this code, for example:
if(object instanceof String) {
//... it's a string
}
else {
//...otherwise it's an int
}
If you are reading all inputs into String instances, you will need to test the values against Integer.parseString(value) to find out if it is actually an Integer.
You could try to cast the object into an Integer and catch the ClassCastException:
try {
int i = (Integer) object;
}
catch (ClassCastException e){
String s = (String) object;
}
When I have this type of problem, I sometimes solve it by turning the problem around and using a callback-style solution.
For example:
for ( Type t : array ) {
t.process( callback );
}
Where the callback looks like this:
interface Callback {
public void processInt(....);
public void processString(....);
}
You can then either implement the process method either with an if (isInt()) callback.processInt() else callback.processString(), or if you change the definition of Type you can use the inheritance tree to do it for you.
For example:
interface Type {
public void process( Callback cb );
}
class IntType implements Type {
public void process( Callback cb ) {
cb.processInt(...);
}
}
class StringType implements Type {
public void process( Callback cb ) {
cb.processString(...);
}
}

Enum type conversion to int

i am trying to use the following code...
The Enum class i am using is
public enum AccountType {
kAccountTypeAsset(0x1000),
kAccountTypeAssetFixed(0x1010),
private int value;
private AccountType(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
}
public AccountType accountType = kAccountTypeAsset;
integerToDB(accountType);
...
/*************************/
public Object integerToDB (Integer i )
{
if(i == -1)
{
return null;
}
return i;
}
How can i use
accountType
as integer.
integerToDB(accountType.getValue()); ?
Since your enum has implemented a getValue method, you can use accountType.getValue() to get the integer value stored in accountType.

Categories

Resources