❔ I couldn't figure out the solution. (Java)

I had one of those OOP programming problems to solve and I could not figure it out
import java.util.*;


class Food {

private String name;


public String getFood(String name) {
this.name = name;

return name;
}

public Food() {

}

void serveFood() {
System.out.printf("I'm serving s%" , getFood(name));
}



}

class FoodFactory extends Food {


private String name;


public String getFood(String name) {


return name;
}

public void setFood(String name) {

this.name = name;
}

}

class Solution {
public static void main(String[] args) {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
import java.util.*;


class Food {

private String name;


public String getFood(String name) {
this.name = name;

return name;
}

public Food() {

}

void serveFood() {
System.out.printf("I'm serving s%" , getFood(name));
}



}

class FoodFactory extends Food {


private String name;


public String getFood(String name) {


return name;
}

public void setFood(String name) {

this.name = name;
}

}

class Solution {
public static void main(String[] args) {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
I had to make the classes and method that output the expected strings. I couldn't figure it out.
8 Replies
KeezyEnraged
KeezyEnragedOP2y ago
import java.util.*;


class Food {

private String name;


public String getFood(String name) {
this.name = name;

return name;
}

public Food() {

}

void serveFood() {
System.out.printf("I'm serving s%" , getFood(name));
}



}

class FoodFactory extends Food {


private String name;


public String getFood(String name) {


return name;
}

public void setFood(String name) {

this.name = name;
}

}

class Solution {
public static void main(String[] args) {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
import java.util.*;


class Food {

private String name;


public String getFood(String name) {
this.name = name;

return name;
}

public Food() {

}

void serveFood() {
System.out.printf("I'm serving s%" , getFood(name));
}



}

class FoodFactory extends Food {


private String name;


public String getFood(String name) {


return name;
}

public void setFood(String name) {

this.name = name;
}

}

class Solution {
public static void main(String[] args) {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
This was me trying to solve the problem
basically, i am little cat
this is csharp server tho???
Angius
Angius2y ago
This is a server for the better language If you want support with poor man's C#, $java might help
KeezyEnraged
KeezyEnragedOP2y ago
I don't support anything. it was a problem I had to solve.
Angius
Angius2y ago
If you want support with Java, head to the Java server That's where you will get help with Java
KeezyEnraged
KeezyEnragedOP2y ago
Thanks for the link
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server