How to set private variable in java bean - java

I have a java bean class such as :
public class EncBean {
private String name;
private String ReversedBinary;
private String ConcatenatedData;
public String getReversedBinary() {
return ReversedBinary;
}
public void setReversedBinary(String ReversedBinary) {
this.ReversedBinary = ReversedBinary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getConcatenatedData() {
return ConcatenatedData;
}
public void setConcatenatedData(String name) {
this.ConcatenatedData = ConcatenatedData;
}
}
and I tried to set a value for the private java bean field(ConcatenatedData) as follow :
public EncBean conctdat(){
EncBean encBean4 = new EncBean();
encBean4.setConcatenatedData(inputkey.concat(var));
return encBean4;
}
and in main i tried to access this value as:
mainenc concatdata =new mainenc();
EncBean encbeandata = concatdata.conctdat();
System.out.println("concatenated data is: "+encbeandata.getConcatenatedData());
but it gives me null
concatenated data is: null

You should fix implementation of setConcatenatedData() to :
public void setConcatenatedData(String name) {
this.ConcatenatedData = name; // instead of this.ConcatenatedData = ConcatenatedData
}

The first one is:
public void setConcatenatedData(String name) {
this.ConcatenatedData = name;
}
Second one, you should double check if inputkey.concat(var) is null.

You can do it by Getter & Setter.
This will help:
setConcatenatedData(String name) {
this.ConcatenatedData = name;
}

Related

Get all objects with the same constructor as integer?

I got the following class:
public class Possibility {
private String name;
public Possibility(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
If I now have many classes that extend "Possibility", how can I find how many instances exist of classes that extend Possibility?
You can use a static field as a counter in Possibility class and use it to increment as the objects are created. This is more efficient and secure than using reflection.
package so;
public class Possibility {
private static int counter = 0;
private String name;
public Possibility(String name) {
counter += 1;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Possibility1 p1 = new Possibility1("p1");
Possibility2 p2 = new Possibility2("p2");
System.out.println(Possibility.counter);
}
}
Possibility1
package so;
public class Possibility1 extends Possibility {
public Possibility1(String name) {
super(name);
}
}
Possibility2:
package so;
public class Possibility2 extends Possibility {
public Possibility2(String name) {
super(name);
}
}
Possibility3
package so;
public class Possibility3 extends Possibility {
public Possibility3(String name) {
super(name);
}
}
The Reflections library provides a pretty easy way to do this:
int numSubTypes = reflections.getSubTypesOf(Possibility.class).size();
you must create an integer attribute in Possibility class and and you can get this integer from another class that extends from Possibility, like this:
class Possibility{
public int someInteger;
//getter
public int getSomeInteger(){
return this.someInteger;
}
}
class someClass extends Possibility{
public void someMethode(){
Possibility possibility = new Possibility("someName");
//get someInteger
possibility.getSomeInteger();
}
}

How to set boolean Array from hashtables´s Object

I have this :
public class NewClass {
String DNI;
String name;
boolean asist [][]=new boolean[3][4];
public NewClass(String DNI, String name) {
this.DNI = DNI;
this.name = name;
for (int i=0;i<asist.length;i++)
{
for(int j=0;j<asist[0].length;j++)
{
asist[i][j]=true;
}
}
}
public String getDNI() {
return DNI;
}
public void setDNI(String DNI) {
this.DNI = DNI;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean[][] getAsist() {
return asist;
}
public void setAsist(boolean[][] asist) {
this.asist = asist;
}
and I saved this code in HashTable < String,Object> has=new HashTable();.
Ok then I want to set asist[0][0] and i dont know how to do this I tryed this:
has.get(key).setAsistencia([0][0]false)
Thanks and sorry for my bad english
You were pretty close.
First you need to retrieve the array and then set the respective part of it to false.
has.get(key).getAsist()[0][0] = false;

Return a key-value list, key is attribute and value is attribute's value from other Class

I have a Class A with name and value attributes.
public class A {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
I have another Class B, such as
public class B {
private String attribute01;
private String attribute01;
private String attribute01;
public String getAttribute01() {
return attribute01;
}
public void setAttribute01(String name) {
this.attribute01 = name;
}
...
}
I would like to return a list with A type, having attribute01 key and where value is getAttribute01() from B, such as ({attribute01, getAttribute01()},{attribute02, getAttribute02()}).
How to implement it?.
Thanks in advance.
Actually I can use a very stupid way, such as
public List<A> keyvalueList(final B objB) {
List<A> list = new ArrayList<>();
A objA = new A();
objA.setName("attribute01");
objA.setValue(objB.getAttribute01());
list.add(objA);
objA = new A();
objA.setName("attribute02");
objA.setValue(objB.getAttribute02());
list.add(objA);
...
return list;
}
Part of them hard coding, obvious it is not a smart way, any proposal.
I wrote sample code for List.Please check my code that is ok to use or not.I added another extra class C.in C,it has two attribute String nameFromA and String attFromB.You should add this C object to list.Following is sample code.
public class A {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class B {
private String att1;
private String att2;
private String att3;
public String getAtt1() {
return att1;
}
public void setAtt1(String att1) {
this.att1 = att1;
}
public String getAtt2() {
return att2;
}
public void setAtt2(String att2) {
this.att2 = att2;
}
public String getAtt3() {
return att3;
}
public void setAtt3(String att3) {
this.att3 = att3;
}
}
public class C {
private String namefromA;
private String attfromB;
public String getNamefromA() {
return namefromA;
}
public void setNamefromA(String namefromA) {
this.namefromA = namefromA;
}
public String getAttfromB() {
return attfromB;
}
public void setAttfromB(String attfromB) {
this.attfromB = attfromB;
}
}
public class Test {
public static void main(String args[]){
C c = new C();
A a = new A();
B b = new B();
a.setName("A1");
b.setAtt1("100");
c.setNamefromA(a.getName());
c.setAttfromB(b.getAtt1());
List list = new ArrayList();
//use generic
list.add(c);
}
}
if you don't want to use class C,then you can use Test class like that
import java.util.ArrayList;
import java.util.List;
public class Test {
private String nameFromA;
private String valueFromB;
public Test(String nameFromA, String valueFromB) {
super();
this.nameFromA = nameFromA;
this.valueFromB = valueFromB;
}
public static void main(String args[]){
A a = new A();
B b = new B();
a.setName("A1");
b.setAtt1("100");
Test test = new Test(a.getName(),b.getAtt1());
List list = new ArrayList();
list.add(test);
}
}
This is my opinion only.Please check it is ok or not.

How to add value from another class (java)

I have two classes: profesor and subject
public class Profesor {
private int numbClassroom;
public Profesor(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String ToString(){
return "Number of classroom: "+numbClassroom;
} }
The second class is:
public class Subject{
String name;
Profesor lecturer = new Profesor();
Date yearOfStudy;
public void Dodeli(Profesor p){
??????
}}
I do not know how to add professor like a lecturer to a current subject
Like this? I don't see any problem.
public void Dodeli(Profesor p){
lecturer = p;
}
Profesor lecturer = new Profesor();
No need to instantiate lecturer. Just declare it. Then have getter/setter methods for it
Then you can assign Professor to Subject
Subject subj = new Subject("OOP"); //assuming you have corresponding constructor
subj.setLecturer(new Professor()); //or if you have existing prof object
Maybe require something like this : try to encapsulate your code
public class Professor {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Subject{
private String name;
private Professor professor;
private int numbClassroom;
private Date yearOfStudy;
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Professor getProfesor() {
return professor;
}
public void setProfesor(Professor profesor) {
this.professor = profesor;
}
public void Dodeli(){
System.out.println("Pofessor "+getProfesor().getName()+" is teaching "+getName()+" in Room NO :"+getNumbClassroom());
}
}
public class TestImpl {
public static void main(String arr[])
{
Subject subject = new Subject();
Professor professor = new Professor();
subject.setName("Biology");
professor.setName("MR.X");
subject.setNumbClassroom(1111);
subject.setProfesor(professor);
subject.Dodeli();
}
}

XMLEncoder - Runtime Exception

I have been playing around with serialization-XML in java and am a little stuck. When I run this program I get two exceptions and I am not sure what the cause is:
java.lang.InstantiationException: Ship
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Ship);
Continuing ...
I suspect that there is something wrong with the class that I am trying to serialize because when I use an example of the internet it works fine.
Can someone please point out what mistake I am making.
Main:
public class Main {
private static final String XMLLocation = "xmlTest.xml";
static ObjectSerializationToXML serializer = new ObjectSerializationToXML();
public Main() {
// TODO Auto-generated constructor stub
}
/**
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Ship ship = new Ship("name", "324");
serializer.serializeObjectToXML(XMLLocation, ship);
}
}
Object Serialization-XML Class:
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ObjectSerializationToXML {
/**
* <span id="IL_AD10" class="IL_AD">This method</span> saves (serializes) any java bean object into xml file
*/
public void serializeObjectToXML(String xmlFileLocation,
Object objectToSerialize) throws Exception {
FileOutputStream os = new FileOutputStream(xmlFileLocation);
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(objectToSerialize);
encoder.close();
}
/**
* Reads Java Bean Object From XML File
*/
public Object deserializeXMLToObject(String xmlFileLocation)
throws Exception {
FileInputStream os = new FileInputStream(xmlFileLocation);
XMLDecoder decoder = new XMLDecoder(os);
Object deSerializedObject = decoder.readObject();
decoder.close();
return deSerializedObject;
}
}
Object To Serialize (My object that causes the exception):
public class Ship {
private String name;
private String yearBuilt;
public Ship(String name, String yearBuilt) {
this.name = name;
this.yearBuilt = yearBuilt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYearBuild() {
return yearBuilt;
}
public void setYearBuild(String yearBuild) {
this.yearBuilt = yearBuild;
}
#Override
public String toString() {
return "ship [name=" + name + ", yearBuilt=" + yearBuilt + "]";
}
}
Object To Serialize (example from the internet that works):
public class MyBeanToSerialize {
private String firstName;
private String lastName;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Whenever any class containing parameterized constructor and trying to serialize, then it should be only instantiated by default Constructor. So, XMLEncoder requires an object to serialize it by default constructor.
Ship class must implement the default constructor while it is containing parameterized constructor because whenever Ship class becomes to serializable, it would be looking for default constructor to instantiate for XMLEncoder.
Please find corrected Ship class as per below.
public class Ship {
private String name;
private String yearBuilt;
public Ship(String name, String yearBuilt) {
this.name = name;
this.yearBuilt = yearBuilt;
}
//Default constructor must be implemented for XMLEncoder serializing
public Ship() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYearBuild() {
return yearBuilt;
}
public void setYearBuild(String yearBuild) {
this.yearBuilt = yearBuild;
}
#Override
public String toString() {
return "ship [name=" + name + ", yearBuilt=" + yearBuilt + "]";
}
}

Categories