Modify the same data structure when called from another method [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Lets say I have 2 classes:
class A {
public static void main(String[] args) {
Class B cB = new ClassB();
cB.modifyMethod(parameter);
}
}
class B {
private ArrayList<String> dataStucture = new ArrayList<String>();
public void modifyMethod(int parameter) {
//...............
}
}
What I intend to do is to modify dataStructure 3 times. Each time based on a new parameter and called form classA. So if I call one time modifyMethod it will create the list I want, but when I call it secondly I want it to work on the same list, not to create a new one.
can you help me fix this?

If that's what you want then you can make dataStructure static
private static ArrayList<String> dataStucture = new ArrayList<String>();
It will be shared among all instances of B.

Class B is only instantiated once in Class A, so all modifications to the ArrayList datastructure will be for that particular instance of datastructure as well.
Do something like this:
class A {
public static void main(String[] args) {
B cB = new B();
cB.modifyMethod(parameter);
}
}
class B {
private ArrayList<String> dataStucture = new ArrayList<String>();
public void modifyMethod(String parameter) {
dataStructure.add(parameter);
}
}
I changed the type of parameter in modifyMethod to String since the ArrayList dataStucture is String. I also changed the way you instantiated your class B, you don't need to keyword Class in front of B to make instantiate it.

Related

Does Factory Pattern suits the best in my case? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a Feeder class that contains a method that feed data into a list, given data, the list and a custom class :
default void feedData(CustomData myData, List list, Class c){
ObjectMapper objectMapper = new ObjectMapper();
if(myData!= null) {
for (Map<String, Object> map : myData.getData()) {
list.add(objectMapper.convertValue(map, c));
}
}
}
I want to implement a design pattern because I have several classes that can should feed data regarding specific classes.
I thought about Factory pattern, is that a good idea?
Class 1 would be like:
public void feed(CustomData myData){
feedData(myData, myField, CustomClass1.class);
}
Class 2 :
public void feed(CustomData myData){
feedData(myData, myField2, CustomClass2.class);
}
etc.
And then, an interface for example IFeeder where I declare
void feed(CustomData myData);
But where can I put the feedData method? Inside this interface?
If yes, I should declare it as default, but default keyword is for a default method that return always the same result, isn't it?
Thanks for any advice.
public static <T> void feedData(CustomData myData, List<T> list, Class<T> clazz){
ObjectMapper objectMapper = new ObjectMapper();
if(myData!= null) {
for (Map<String, Object> map : myData.getData()) {
list.add(objectMapper.convertValue(map, clazz));
}
}
}
I believe this should work. This can be a static method in some util class.
I am assuming that all the other classes that you are referring can implement a common interface.

Access control exercise - java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to print the attributes from TestCar class by creating a public hackCar method in class Terminal. The hackCar method needs to take a TestCar as a parameter and print the attributes of TestCar. The caveat for this assignment is that I cannot touch anything in the TestCar class.
I am still struggling with printing the two private attributes in TestCar. How can I print the two private attributes from Test Car class by using the Test Car object as the parameter in the hackCar method?
Story class:
class Story {
public static void main(String args[]) {
TestCar testCar = new TestCar();
Terminal terminal = new Terminal();
terminal.hackCar(testCar);
}
}
class Terminal {
public void hackCar(TestCar other) {
System.out.println(other.doorUnlockCode);
System.out.println(other.hasAirCondition);
System.out.println(other.brand);
System.out.println(other.licensePlate);
}
}
class TestCar {
private int doorUnlockCode = 602413;
protected boolean hasAirCondition = false;
String brand = "TurboCarCompany";
public String licensePlate = "PHP-600";
}
Thanks!
Private fields are called 'private' because there is no way to get them. But you can make public getter for them:
class TestCar {
// Your 4 fields here...
public int getDoorUnlockCode() {
return this.doorUnlockCode;
}
}
Then in hackCar method change
System.out.println(other.doorUnlockCode); to this: System.out.println(other.getDoorUnlockCode());
So now you can access field doorUnlockCode through public getter.
Do the same for protected field hasAirCondition
Your methods Terminal.getdoorUnlockCode() and Terminal.getAirCondition() can't get to fields from another object, they must be in TestCar object

Java: Override protected String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I don't know Java.
Just need a quick fix if possible.
How to override protected String?
I have:
public class Something {
protected String changeMe = "this";
public String viewForm() {
...
return getForm();
}
public String getForm() {
return changeMe;
}
}
myfile:
public class MySomething extends Something {
public String viewForm() {
return super.viewForm();
}
}
How to use changeMe = "that" in class MySomething?
Thanks.
Actually, others have answered your question. In case, you are really new to Java struggling to figure it out, the simplest answer to your question is just put your above mentioned statement in the constructor of the derived class:
Solution one (actually bad solution):
public class MySomething extends Something {
public MySomething() {
changeMe = "that"
}
}
A better solution for your problem is to define a setter method in the class Something as follows (in case you have access to its source code):
public void setChangeMe(String newValue) {
changeMe = newValue;
}
and you can change the value by calling
Something s = new Something();
s.setChangeMe("that");
the word override is a hint for the compiler, with that you let it know that you're writing code that at your own way implements the method of a parent class or the interface.
having said that overriding a variable/attribute/field is a wrong use of the terminology, and makes not much sense
to answer your question: you can access toso the changeMe object because it is not defined as private... so you can change the value directly..
Example:
public static void main(String args[]) {
MySomething ms = new MySomething();
System.out.println(ms.changeMe);
ms.changeMe = "that";
System.out.println(ms.changeMe);
}

Difference between this and .this? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the difference between this and .this when calling functions? And, what happens when this or this. is used?
Example:
class reference
{
public void object()
{
reference obj = new reference();
this.obj();
}
}
The Class.this syntax is useful when you have a non-static nested class that needs to refer to its enclosing class's instance.It is only used in cases where there is an inner class, and one needs to refer to the enclosing class
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
A good example
public class TestForThis {
String name;
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
class TestForDotThis {
String name ="in";
String getName() {
return TestForThis.this.name;
}
}
public static void main(String[] args) {
TestForThis t = new TestForThis();
t.setName("out");
System.out.println(t.getName());
TestForThis.TestForDotThis t1 = t.new TestForDotThis();
System.out.println(t1.getName());
}
}
Output will be
out
out

Design pattern to convert a class to another [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a class called GoogleWeather, I want to convert it to another class CustomWeather.
Is there any design pattern which helps you to convert classes?
In that case I'd use a Mapper class with a bunch of static methods:
public final class Mapper {
public static GoogleWeather from(CustomWeather customWeather) {
GoogleWeather weather = new GoogleWeather();
// set the properties based on customWeather
return weather;
}
public static CustomWeather from(GoogleWeather googleWeather) {
CustomWeather weather = new CustomWeather();
// set the properties based on googleWeather
return weather;
}
}
So you don't have dependencies between the classes.
Sample usage:
CustomWeather weather = Mapper.from(getGoogleWeather());
There is one critical decision to make:
Do you need the object that is generated by the conversion to reflect future changes to the source object?
If you do not need such functionality, then the simplest approach is to use a utility class with static methods that create a new object based on the fields of the source object, as mentioned in other answers.
On the other hand, if you need the converted object to reflect changes to the source object, you would probably need something along the lines of the Adapter design pattern:
public class GoogleWeather {
...
public int getTemperatureCelcius() {
...
}
...
}
public interface CustomWeather {
...
public int getTemperatureKelvin();
...
}
public class GoogleWeatherAdapter implements CustomWeather {
private GoogleWeather weather;
...
public int getTemperatureKelvin() {
return this.weather.getTemperatureCelcius() + 273;
}
...
}
Besides, You can also use new Java8 feature 'Function' from java.util.function'.
More detailed explanation is provided in http://www.leveluplunch.com/java/tutorials/016-transform-object-class-into-another-type-java8/ . Kindly have a look!

Categories

Resources