I have this simple Bean class and try to set some values with BeanUtils.setProperty Problem is, it seems that String works just fine, but when I try to set a Boolean value it just does not work. I have tried and define the field as public but still not working. Any help? Why is this not working?
public class TestBean {
protected Boolean someBoolean;
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSomeBoolean() {
if (someBoolean == null) {
return true;
} else {
return someBoolean;
}
}
public void setSomeBoolean(Boolean value) {
this.someBoolean = value;
}
public static void main(String[] args) {
TestBean o = new TestBean();
Boolean b = new Boolean(false);
BeanUtils.setProperty(o, "someBoolean", b);
BeanUtils.setProperty(o, "name", "A name");
System.out.println(((TestBean)o).isSomeBoolean());
// Output = true WHY?????
System.out.println(((TestBean)o).getName());
// Output = A name
BeanUtils.setProperty(o, "someBoolean", false);
BeanUtils.setProperty(o, "name", "Another name");
System.out.println(((TestBean)o).isSomeBoolean());
// Output = true WHY????
System.out.println(((TestBean)o).getName());
// Output = Another name
}
}
You need to change it from
protected Boolean someBoolean;
to
protected boolean someBoolean;
You will get more info from here.
Java Beans, BeanUtils, and the Boolean wrapper class
Related
my code:
public class Kuh {
private String name;
private boolean istSatt;
public Kuh(String name, boolean istSatt) {
}
public double gibMilch() {
if (istSatt == true) {
System.out.println(10.0);
return 10.0;
} else {
System.out.println(3.0);
return 3.0;
}
}
public void grasen() {
istSatt = true;
}
public static void main(String[] args) {
Kuh Frida = new Kuh("Frida", true);
Frida.gibMilch();
Frida.grasen();
Frida.gibMilch();
}
}
My problem: I set "istSatt" of the object "Frida" to "true" at creation. So when using the method "gibMilch", it should put out "10". Despite that, it puts out "3", like the boolean would be false, even tho I set it to true. It only puts out "10" after using "grasen".
What did I do wrong?
You are not assigning the constructor parameters to the fields.
public Kuh(String name, boolean istSatt) {
this.name = name;
this.istSatt = istSatt;
}
You need to set your class variable values in your constructor:
public Kuh(String name, boolean istSatt) {
this.name = name;
this.istSatt = istSatt;
}
Here this refers to the class you are instantiating.
Try this instead, as you did not seem to assign to anything on the call to Kuh Frida = new Kuh("Frida", true);
i.e.
public class Kuh {
private String name;
private boolean istSatt;
public Kuh(String name, boolean istSatt) {
this.istSatt=istSatt;
this.name=name;
}
public double gibMilch() {
if (istSatt) {
System.out.println(10.0);
return 10.0;
} else {
System.out.println(3.0);
return 3.0;
}
}
public void grasen() {
istSatt = true;
}
public static void main(String[] args) {
Kuh Frida = new Kuh("Frida", true);
Frida.gibMilch();
Frida.grasen();
Frida.gibMilch();
}
}
I have a class with the boolean field and I want field of only one object to be true at the same time.
I tried to set field of all objects with static method but i could not reach non-static field from static method.(i wasn't aware of logic behind static)
public class ToggleBox
{
private boolean selected;
public ToogleBox()
{
selected=false;
}
public setOnlyTrue()
{
setAllFalse();
selected=true;
}
private static setAllFalse()
{
this.selected=false;
}
}
Is there any trick to do this or should i iterate over all objects of class to change all the fields?
One trick I could think of is having a static member that holds the reference of the one instance that has a true property:
public class MyClass {
private static MyClass trueObject = null;
public void setProperty(boolean value) {
if (value) {
trueObject = this;
} else {
trueObject = null;
}
}
public boolean getProperty() {
return trueObject == this;
}
}
It depends on you requirement when you want to create an instance with trueand a true has already been created
A. set ALL the others to false and the new one to true
public class ToggleBox {
private static List<ToggleBox> listAll = new ArrayList<>();
private boolean selected;
public ToggleBox(boolean bool) {
if (bool) // if require true
for (ToggleBox mo : listAll)
mo.setSelected(false); // set all other to false
listAll.add(this);
this.selected = bool;
}
public void setSelected(boolean bool) { this.selected = bool; }
#Override
public String toString() { return selected + ""; }
public static void main(String argv[]) {
ToggleBox m1 = new ToggleBox(true);
ToggleBox m2 = new ToggleBox(false);
System.out.println(Arrays.toString(listAll.toArray())); // [true, false]
ToggleBox m3 = new ToggleBox(true);
System.out.println(Arrays.toString(listAll.toArray())); // [false, false,true]
}
}
B. refuse to set the new one to true
public class ToggleBox {
private static boolean alreadyTrue = false;
private static List<ToggleBox> listAll = new ArrayList<>();
private boolean selected;
public ToggleBox(boolean bool) {
if (bool) { // if require true
if (alreadyTrue) // if there is already one
bool = false; // it will be false
else // else
alreadyTrue = true; // it's st to true, and remember it
}
this.selected = bool;
}
#Override
public String toString() { return selected + ""; }
public static void main(String argv[]) {
ToggleBox m1 = new ToggleBox(true);
ToggleBox m2 = new ToggleBox(false);
System.out.println(Arrays.toString(listAll.toArray())); // [true, false]
ToggleBox m3 = new ToggleBox(true);
System.out.println(Arrays.toString(listAll.toArray())); // [true, false, false]
}
}
I have an java application where a object reference "Validate.Options" is passed as parameter to the function "ValidateResult(Validate.Options option)" and the function is called iterative. Within this function based on the certain condition the property "enableProcessing" of the passed object gets changed which does not get reset on the next iterate. How can I reset this property?
Below is the sample code.
public interface Validate
{
public List validate();
public class Options implements Serializable
{
public String name;
public boolean enableProcessing = true;
public Options(String name)
{
this.name = name;
}
}
}
public class Coder
{
public String name;
public int age;
public Coder(String name, int age)
{
this.name = name;
this.age = age;
}
public void ValidateResult(Validate.Options option)
{
if(option.name.equals(this.name) && option.enableProcessing)
{
option.enableProcessing = false;
//
//business logic and function call
//
}
}
public static void main(String[] args)
{
Validate.Options options = new Validate.Options("Test");
List<Coder> coders = new ArrayList<Coder>();
Coder coder = new Coder("Test", 28);
Coder coder1 = new Coder("XYZ", 18);
Coder coder2 = new Coder("Test", 16);
coders.add(coder);
coders.add(coder1);
coders.add(coder2);
for(Coder co : coders)
{
co.ValidateResult(options);
}
}
}
If I understood the question well - in your for loop, simply add a line of code to reset the value of your public Validate.Options.enableProcessing field
for(Coder co : coders)
{
//reset options object for the next iteration
options.enableProcessing = true;
co.ValidateResult(options);
}
Make options immutable if you do not want it to be changed:
public class Options implements Serializable
{
public final String name; // final prevents changes
public final boolean enableProcessing = true; // final prevents changes
public Options(String name)
{
this.name = name;
}
}
To locally work with enableProcessing copy its value to a local variable.
public void ValidateResult(Validate.Options option)
{
boolean enableProcessing = option.enableProcessing; // create local copy
if(option.name.equals(this.name) && enableProcessing) // use local copy
{
enableProcessing = false; // only change local copy
//
//business logic and function call
//
}
}
Alternatively create new, fresh Options for each loop:
public static void main(String[] args)
{
List<Coder> coders = Arrays. asList(
new Coder("Test", 28),
new Coder("XYZ", 18),
new Coder("Test", 16)
);
for(Coder co : coders)
{
Validate.Options options = new Validate.Options("Test"); // fresh options for each iteration
co.ValidateResult(options);
}
}
I'm given private List<Wire> inputs and a method public void feed(List<Signal> inSigs). I have to change signals (initialy each signal is ==Signal.X) in the List<Wire> inputs with the inSigs given in the parameter of the method feed(). THat's all I've been having trouble with. How could I change the state of List inputs with passed inSigs (notice: the parameter is of type <Signal>)? I've done smth but constantly getting and underline error under setSignal(x). I'm attached two classes (Gate and Wire below)
import java.util.*;
public abstract class Gate implements Logic {
private List<Wire> inputs;
private Wire output;
private String name;
public Gate(String name, List<Wire> ins, Wire out)
{
this.name = name;
this.output = out;
if(ins.size() == 0 || ins.isEmpty())
throw new ExceptionLogicParameters(false, 1, 0);
else
this.inputs = ins;
}
#Override
public void feed(List<Signal> inSigs)
{
for(Signal x: inSigs)
inputs.setSignal(x);
}
#Override
public void feed(String name)
{
((Wire) inputs).setName(name);
}
}
public class Wire {
private Signal signal;
private String name;
public Wire(String name)
{
this.name = name;
this.signal = Signal.X;
}
#Override
public String toString()
{
return "\""+ this.name+":"+this.signal+"\"";
}
#Override
public boolean equals(Object other)
{
if(other instanceof Wire)
{
Wire leftHandside = (Wire)other;
return this.name.equals(leftHandside.name) && this.signal == leftHandside.signal;
}
else
return false;
}
public Signal getSignal()
{
return this.signal;
}
public String getString()
{
return this.name;
}
public void setSignal(Signal signal)
{
this.signal = signal;
}
public void setName(String name)
{
this.name = name;
}
}
There is a bunch of ambiguity in the way your code and question reads.
I'll assume that the list of signals is the same size as your private list of wires, then:
public void feed(List<Signal> inSigs) {
// Needs precondition that inSigs.size() == input.size()
for (int i = 0; i < inSigs.size(); i++) {
inputs.get(i).setSignal(inSigs.get(i));
}
}
Otherwise you need a way to map your signals to wires, (probably by index).
Probably you need something like this then:
#Override
public void feed(List<Signal> inSigs)
{
if(inSigs.size() != inputs.size()) {
throw new ExceptionLogicParameters(false, 1, 0);
}
int i = 0;
for (Signal x: inSigs) {
inputs.get(i++).setSignal(x);
}
}
I've got a problem with my programm. When i try to compile following i just receive the message:
Tutorium.java:15: error: <identifier> expected
public void settName(vorlesung.lectureName) {
^
So my Code:
Tutorium.java
public class Tutorium {
private Vorlesung vorlesung;
public String tName;
private int tNumber;
public int gettNumber() {
return this.tNumber;
}
public String gettName() {
return this.tName;
}
public void settName(vorlesung.lectureName) {
this.tName = vorlesung.lectureName;
}
public String toString() {
return (this.tName + ", " + this.tNumber);
}
public Tutorium(int tNumber){
this.tNumber = tNumber; } }
Vorlesung.java
public class Vorlesung {
public String lectureName;
private int lectureNumber;
private int lecture;
private Dozent dozent;
private String lecturerlName;
public String getlectureName(){
return this.lectureName;
}
public int lectureNumber(){
return this.lectureNumber;
}
public int lecture(){
return this.lecture;
}
public String getlecturer(){
this.lecturerlName = dozent.lecturerlName;
return this.lecturerlName;
}
public String toString() {
return (this.lectureName + ", " + this.lectureNumber);
}
public Vorlesung(String lectureName, int lecture) {
this.lectureName = lectureName;
this.lecture = lecture +1;
this.lectureNumber = this.lecture -1;
this.lecturerlName = lecturerlName;
}}
My Main-Method:
public class MainVorlesung {
public static void main(String[] args) {
Student student = new Student("STUDENTNAME", "STUDENTLASTNAME", 178, 1);
Vorlesung vorlesung = new Vorlesung("Programmieren", 13341);
Tutorium tutorium = new Tutorium(3);
Dozent dozent = new Dozent("LECTURERFIRSTNAME", "LECTURERLASTNAME", 815);
System.out.println(student.toString());
System.out.println(vorlesung.toString());
System.out.println(tutorium.toString());
System.out.println(dozent.toString());
}}
My goal is to set the value of tName equal the value of vorlesung.lectureName.
Why can't i do this that way?
I appreciate every help. :)
Thanks
For methods, the arguments that you pass in must have a declared value.
In this case, a String. So you need to change your method to this:
public void settName(String newLectureName) {
this.tName = newLectureName;
}
Read more about what a java method is and how to create one here: http://www.tutorialspoint.com/java/java_methods.htm
Change settName to
public void settName(String name) {
this.tName = name;
}
Since your goal is:
My goal is to set the value of tName equal the value of vorlesung.lectureName.
You should get rid of the setName method entirely since it will depend entirely on the vorlesung field and so should not be changeable. You should also get rid of the tName field, and instead change getName() to:
public class Tutorium {
private Vorlesung vorlesung;
// public String tName; // get rid of
private int tNumber;
public String gettName() {
if (vorlesung != null) {
return vorlesung.getlecturer();
}
return null; // or throw exception
}
// *** get rid of this since you won't be setting names
// public void settName(Vorlesung vorlesung) {
// this.tName = vorlesung.lectureName;
// }
I have just now noticed that your Tutorium class does not have and absolutely needs a setVorlesung(...) method.
public void setVorlesung(Vorlesung vorlesung) {
this.vorlesung = vorlesung;
}