I'm given class A and class C, and I'm supposed to write class B which makes the output of the main in class C always successful! .
Sine there is a random boolean variable, I get a good result only when it's false, when it's true I don't get an output.
I'm not allowed to make any changes in class A or C.
I understand that the problem is in the third if in class C, but what changes I can make in class B to prevent from getting into this if?
public class A {
public String foo(String s) {
return s;
}
}
public class B extends A {
public A getA(boolean flag){
A a = new A();
if (flag){
return(a);
}
else{
return (a);
}
}
}
package sw1.riddles.second;
import java.util.Random;
public class C {
public static void main(String[] args) {
String input = args[0];
B b = new B();
Random random = new Random();
boolean randomBool = random.nextBoolean();
A a = b.getA(randomBool);
if (randomBool) {
if (!input.equals(a.foo(input))) {
return;
}
} else {
if (!(input+input).equals(a.foo(input))) {
return;
}
}
System.out.println("success!");
}
}
The class B should be as follows:
public class B extends A{
public String foo(String s){
return s+s;
}
public A getA(boolean flag){
if (flag){
A a = new A();
return(a);
}
else{
B b = new B();
return (b);
}
}
}
maybe solution:
public class B extends A {
public A getA(boolean flag){
if (flag){ //true -> usual A with foo == input
return new A(); //or this
}
//false-> custom A with foo == input + input
return new A(){
#Override
public String foo(String s) {
return s+s;
}
};
}
}
Related
I have the following code
public class A {
public class B {
}
public boolean wasCreatedFromMe(B obj) {
// I want to implement this method
}
}
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
B b1 = a1.new B();
a1.wasCreatedFromMe(b1); // true
a2.wasCreatedFromMe(b1); // false
}
}
I would like to implement that method above which determines if the object was created from this Outer Class instance. Is there a way to use instanceof or some type of Class<> magic to do that?
I do NOT want to do any of the following:
Use data structures
// inside class A
Set<B> childObjs = new HashSet<>();
public B() {
childObjs.add(this);
}
Ask inner class object
// inside class A
public boolean wasCreatedFromMe(B obj) {
return obj.parent() == this;
}
class B {
public A parent() {
return A.this;
}
}
The problem is that I want a super class of A and three subclasses , B,C,D.
and there's a method that only the super class A has , and none of the subclasses have that method and the method has different results in each sub class.
I also can't Override any methods.
how do I code this without using enum or giving any attributes to any class?
for example :
public class {
public String method1(){
//what each class type should do
}
}
public class B extends{
}
public static void main(String[] args) {
A[] elements = {new A(),new B()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i].method1());
}
}
results eg:
A1
B1
Method overriding is the best solution i think.
But if you want to have one function in class A, you can use istanceof to verify the class
if(object instnaceof A) doSomething ..
class A {
public String method1() {
if(this instanceof B) {
return "Class B";
//do something for class B
} else if (this instanceof C) {
return "Class C";
//do something for class C
}
return "";
}
}
class B extends A { }
class C extends A { }
public class Main {
public static void main(String[] args) {
A[] elements = {new A(),new B()};
for (int i = 0; i < elements.length; i++)
System.out.println(elements[i].method1());
}
}
You can use Generics like this, but better use overriding
public class A {
public <T extends A> String method1(T classInstance) {
return method(classInstance);
}
private String method(A classInstance) {
return classInstance.toString();
}
private String method(B classInstance) {
return classInstance.toString();
}
}
public class B extends A{
}
public class Application {
public static void main(String[] args) {
A[] elements = {new A (), new B ()};
for (int i = 0; i < elements.length; i++)
{
System.out.println (elements[i].method1(elements[i]));
}
}
}
I have demonstrated an example below for my questions.
class B {
int name;
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
}
class A {
public A() {
// initializing object B
B b = new B();
}
}
class MainClass {
public static void main(String[] args) {
A a = new A();
}
}
How I access the object of B in the Mainclass which is initialized inside the class A Constructor?
One way to achieve this would be to add a getter method inside your A class which exposes the instance of B:
public class A {
private B b;
public A() {
b = new B();
}
public B getB() {
return b;
}
}
Usage:
A a = new A();
B myB = a.getB();
How about
class A {
private B b;
public A() {
// initializing object B
b = new B();
}
public B getB () {
return b;
}
}
from mainClass
A a = new A();
B b = a.getB ();
I have deal with one problem while accessing arraylist element in another class. I have 2 classes: class A and class B.
class A {
private ArrayList<String> temp=new ArrayList<String>();
temp.add("abc");
temp.add("XYZ");
public ArrayList<String> getTemp() {
return this.temp;
}
}
public class B
{
private A a=null;
public b(A aa)
{
this.a = aa;
}
System.out.printLn(a.getTemp.size());//output is 2
System.out.printLn(a.getTemp.get(0));//null
}
Why it is giving me null? Please give brief explanation of this.
Here is a working version of what you are trying to achieve:
A.java
In the A class, you should be adding elements to your ArrayList in the constructor:
public class A {
private ArrayList<String> temp=new ArrayList<String>();
public A() {
temp.add("abc");
temp.add("XYZ");
}
public ArrayList<String> getTemp() {
return this.temp;
}
}
B.java
The constructor name should match the class's:
public class B {
private A a=null;
public B(A aa)
{
this.a = aa;
}
}
App.java
public class App {
public static void main(String[] args) {
A a = new A();
System.out.println(a.getTemp().size());
System.out.println(a.getTemp().get(0));
}
}
Output:
2
abc
Your current code won't even compile.
Furthermore, I can guarantee 100% that if by some magic your code were to compile the output of the first printLn would in no way be 2. It would be null. `
**First Of All Your Code Is Not Impossible to run**
You Can't assign value to instance variable directly in side of class without constructor or method so your modified class A must be like
**A.java**
class A {
private ArrayList<String> temp=new ArrayList<String>();
public A()
{
temp.add("abc");
temp.add("XYZ");
}
public ArrayList<String> getTemp()
{
return this.temp;
}
}
OR Like
class A {
private ArrayList<String> temp=new ArrayList<String>();
public A()
{
initialize();
}
public void initialize()
{
temp.add("abc");
temp.add("XYZ");
}
public ArrayList<String> getTemp()
{
return this.temp;
}
}
And Then As per Above Your Class B will Be
**B.java**
class B
{
private A a=null;
public B(A aa)
{
this.a = aa;
}
}
And Then you have to go for main method like
**Temp.java**
public class Temp {
public static void main(String... args)
{
A a = new A();
B b = new B(a);
System.out.println(a.getTemp().size());//output is 2
System.out.println(a.getTemp().get(0));//abc
}
}
I have a Java program that looks like this.
public class LocalScreen {
public void onMake() {
aFuncCall(LocalScreen.this, oneString, twoString);
}
}
What does LocalScreen.this means in aFuncCall?
LocalScreen.this refers to this of the enclosing class.
This example should explain it:
public class LocalScreen {
public void method() {
new Runnable() {
public void run() {
// Prints "An anonymous Runnable"
System.out.println(this.toString());
// Prints "A LocalScreen object"
System.out.println(LocalScreen.this.toString());
// Won't compile! 'this' is a Runnable!
onMake(this);
// Compiles! Refers to enclosing object
onMake(LocalScreen.this);
}
public String toString() {
return "An anonymous Runnable!";
}
}.run();
}
public String toString() { return "A LocalScreen object"; }
public void onMake(LocalScreen ls) { /* ... */ }
public static void main(String[] args) {
new LocalScreen().method();
}
}
Output:
An anonymous Runnable!
A LocalScreen object
This post has been rewritten as an article here.
It means the this instance of the outer LocalScreen class.
Writing this without a qualifier will return the instance of the inner class that the call is inside of.
The compiler takes the code and does something like this with it:
public class LocalScreen
{
public void method()
{
new LocalScreen$1(this).run;
}
public String toString()
{
return "A LocalScreen object";
}
public void onMake(LocalScreen ls) { /* ... */ }
public static void main(String[] args)
{
new LocalScreen().method();
}
}
class LocalScreen$1
extends Runnable
{
final LocalScreen $this;
LocalScreen$1(LocalScreen $this)
{
this.$this = $this;
}
public void run()
{
// Prints "An anonymous Runnable"
System.out.println(this.toString());
// Prints "A LocalScreen object"
System.out.println($this.toString());
// Won't compile! 'this' is a Runnable!
//onMake(this);
// Compiles! Refers to enclosing object
$this.onMake($this);
}
public String toString()
{
return "An anonymous Runnable!";
}
}
As you can see, when the compiler takes an inner class it converts it to an outer class (this was a design decision made a LONG time ago so that VMs did not need to be changed to understand inner classes).
When a non-static inner class is made it needs a reference to the parent so that it can call methods/access variables of the outer class.
The this inside of what was the inner class is not the proper type, you need to gain access to the outer class to get the right type for calling the onMake method.
Class.this allows access to instance of the outer class. See the following example.
public class A
{
final String name;
final B b;
A(String name) {
this.name = name;
this.b = new B(name + "-b");
}
class B
{
final String name;
final C c;
B(String name) {
this.name = name;
this.c = new C(name + "-c");
}
class C
{
final String name;
final D d;
C(String name) {
this.name = name;
this.d = new D(name + "-d");
}
class D
{
final String name;
D(String name) {
this.name = name;
}
void printMe()
{
System.out.println("D: " + D.this.name); // `this` of class D
System.out.println("C: " + C.this.name); // `this` of class C
System.out.println("B: " + B.this.name); // `this` of class B
System.out.println("A: " + A.this.name); // `this` of class A
}
}
}
}
static public void main(String ... args)
{
final A a = new A("a");
a.b.c.d.printMe();
}
}
Then you will get.
D: a-b-c-d
C: a-b-c
B: a-b
A: a
I know what is your confusion.I am encounter the problem just now, it should have special scene to distinguish them.
class THIS {
def andthen = {
new THIS {
println(THIS.this.## + ":inner-THIS.this.##")
println(this.## + ":inner-this.##")
new THIS {
println(THIS.this.## + ":inner-inner-THIS.this.##")
println(this.## + ":inner-this.##")
}
}
}
def getInfo = {
println(THIS.this.## + ":THIS.this.##")
println(this.## + ":this.##")
}
}
You can see the diff between THIS.this and this in new THIS operation by hashcode( .## )
test in scala console :
scala> val x = new THIS
x: THIS = THIS#5ab9b447
scala> val y = x.andthen
1522119751:inner-THIS.this.##
404586280:inner-this.##
1522119751:inner-inner-THIS.this.##
2027227708:inner-this.##
y: THIS = THIS$$anon$1#181d7f28
scala> x.getInfo
1522119751:THIS.this.##
1522119751:this.##
THIS.this always point to outer THIS class which is refer by val x,but this is beyond to anonymous new operation.