Giving the object to create as parameta in constuctor - java

sorry for the title I don't know how to say that.
So basically what I want to do is the following:
public class TestClass {
private final String name;
public TestClass(String name) {
this.name = name;
}
public TestClass(TestClass test) {
this = test;
}
public String getName() {
return name;
}
}
So create a class where you can give an object from itself in the constructor
I don't know if that is possible at all.
The problem I have is that I have multiple objects that extend from this class and I want a simple way to pass them to the next class
so when I have 2 classes
TestClass2 extends TestClass
TestClass3 extends TestClass
and I want to create an instance of the testclass3 from the testclass2 one.
they both should have the same name from testcalss
currently, I am doing that like that:
private class TestClass2 extends TestClass{
private final String anotherName;
public TestClass2(String name, String anotherName) {
super(name);
this.anotherName= anotherName;
}
private void createTest3(String whatever) {
new TestClass3(this, whatever);
}
}
private class TestClass3 extends TestClass{
private final String whatever;
public TestClass3(TestClass test, String whatever) {
super(test.getName());
this.whatever = whatever;
}
}
In my case, my base class has not just a name but a lot more values that I then have to submit!
I hope you kinda understand what I want to say. Again sorry I explained that very bad :D
And thank you all thanks in advance for any answers!

Related

Java factory but objects have slightly differrent attributes

I want to program a factory that creates two types of People which are "Employee" and "Entrepreneur". They both share the same basic "Person" attributes but they also implements their unique ones.
The problem is that if i want to return a method or an attribute that is not declared in the "Person" abstract class the program doesn't find it (since obviously I'm generating an object that is type "Person" and not specifically "Employee" or "Entrepreneur" ).
How do i tackle this problem?
This is the Demo class
public class Demo{
public static void main(String[] args){
PersonFactory pf = new PersonFactory();
Person p1 = pf.getPerson("Employee");
p1.presentation();
System.out.println(p1.getComplanyName());
}
}
This is the abstract class
public abstract class Person{
String name;
String surname;
abstract void presentation();
}
Those are the two concrete classes that extend Person
public class Entre extends Person{
int licenseNumber;
#Override
public void presentation(){
System.out.println("hi i'm an Entrepreneur");
}
public int licenseNumber(){
return licenseNumber;
}
}
public class Empl extends Person{
String companyName;
#Override
public void presentation(){
System.out.println("hi i'm an employee");
}
public String getCompanyName(){
return companyName;
}
}
Finally the Factory
public class PersonFactory{
public Person getPerson(String type){
if(type.equalsIgnoreCase("ENTREPRENEUR")){
return new Entre();
}
else if(type.equalsIgnoreCase("Employee")){
return new Empl();
}
return null;
}
}

how should i add an object to a private static ArrayList?

i have Bank class, with a private static ArrayList that stores all the banks. how can i add every new bank created to it?
i'm not allowed to create any new methods or fields, or change any of the method or constructor parameters.
this is my Bank class:
public class Bank {
private static ArrayList<Bank> allBanks=new ArrayList<Bank>();
private String name;
public Bank(String name) {
this.name = name;
}
public String getName() {
return name;
}
and this is my Main class:
public class Main {
public static void main(String[] args) {
new Bank("randomBankName");
}
}
Do it in constructor:
public Bank(String name) {
this.name = name;
allBanks.add(this);
}
WARNING never do it in real project.
You didn't say, that you may not change the visibility of fields, so that would be one way to do this: make the ArrayList public
If you may not do this either, there is a last way, which i'd never do: Reflection.
In most cases, thats really the last way, not recommended!

java - sending variables through nested classes

I have a class that has a variable of type Name.
public class Holder {
private Name name;
private int snumber;
The Name class has two strings called first and last that are assigned values by setter methods. I would like to send over the strings from the Name class to name in the Holder class, but I'm having trouble doing so. I think I've taken a step in the right direction by doing this
public class Holder {
private Name name;
private int snumber;
public void setName(){
name = new Name();
name.getFirst();
name.getLast();
}
but I can't say that I really know what the correct approach is. I also tried name.setFirst(getFirst) but that doesn't work. Any ideas would be appreciated.
The same way you would if the class wasn't nested.
Your setName() method should take a parameter (maybe 2, first and last) and then invoke the name.setFirstName(), name.setLastName() methods.
Right now, your setName() method isn't doing anything.
E.G:
public class Holder
{
private Name name;
private int snumber;
public Holder()
{
this.name = new Name();
}
public void setName(String firstName, String lastName)
{
this.name.setFirst(firstName);
this.name.setLAst(lastName);
}
}
Here is a good article explaining the relationship between Java inner and outer classes:
https://www.tutorialspoint.com/java/java_innerclasses.htm
class Outer_Demo {
// private variable of the outer class
private int num = 175;
// inner class
public class Inner_Demo {
public int getNum() {
System.out.println("This is the getnum method of the inner class");
return num;
}
}
}
public class My_class2 {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Instantiating the inner class
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
System.out.println(inner.getNum());
}
}
Note that the example creates instances of both "Outer_Demo" AND "Inner_Demo (outer.new Inner_Demo();).
Ok, so I figured something out that works.
public class Holder {
private int snumber;
private Name name;
public void setName(Name n){
name=n;
}
public Name getName(){
return name;
}

I need help creating constructors and returning boolean and strings

I need help fixing my code with the basic concepts listed above. To save from clutter, I took a screen shot of the directions here: https://imgur.com/SdiotUi
However, when I run my code it isn't working. I know there are a lot of errors but I'm having trouble fixing them even though I've spent the past few hours googling the correct way to do this.
When I create the first constructors I am not sure if I am assigning the name and legs correctly, I am having trouble returning "true", I get an error calling the parent class taking one argument, and I don't think I am overriding the abstract class correctly.
My code:
public class Animal1 {
private String animalName;
public int numberOfLegs;
public Animal1(String name){
name = animalName;
name = "John";
}
public Animal1(String name, int legs){
name = animalName;
legs = numberOfLegs;
name = "Jack";
legs = 4;
}
public String getName(){
return animalName;
}
public int getLegs(){
return numberOfLegs;
}
public void isAMammal(){
return true;
}
public void isCarnivorous(){
return true;
}
public abstract class getHello{
}
}
public class Cat1 extends Animal1{
public Cat1(String name){
Animal1.name;
}
public abstract class getHello{
return "Meow";
}
}
public class Dog1 extends Animal1{
public Dog1(String name){
Animal1.name;
}
public abstract class getHello{
return "Woof";
}
}
public abstract class Animal1 { // If you want to have an abstract method, declare the class as abstract
private final String animalName;
private final int numberOfLegs; // better of using private and make it final since it's not going to change.
public Animal1(final String name, final int legs){ //better making the input parameters final since they are not supposed to be changed
//name = animalName;
//legs = numberOfLegs;//it assigned the field to an input parameter. that will take no effect on the object created.
animalName = name;
numberOfLegs = legs;
}
public String getName(){
return animalName;
}
public int getLegs(){
return numberOfLegs;
}
public boolean isAnimal(){ //boolean function needs a return type too!!
return true;
}
public boolean isCarnivorous(){
return true;
}
public abstract String getHello(); // an abstract method has same requirement as a normal method besides the abstract modifier. it will need a return type. And it ends with a semicolon
}
public class Cat1 extends Animal1{
public Cat1(final String name){
super(name, 4); //use super to call parent constructor
}
#Override
public String getHello(){
return "Meow";
}
}
public class Dog1 extends Animal1{
public Dog1(final String name){
super(name, 4);
}
#Override
public String getHello(){
return "Woof";
}
}
First, it looks like a few of your methods are declared as classes. I assume you wanted to make them abstract methods. They need to be changed to:
public abstract String getHello();
Note that abstract methods can only be declared in an abstract class. So, you need to redefine Animal1 as abstract.
public abstract class Animal1
Next, when you implement the abstract method, you define it as
public String getHello()
If you are using an IDE like Eclipse it will automatically offer to generate this method.
Finally, when using your constructor in your child classes like Cat1, you are trying to set "name" as if it was a static variable and bypassing the constructor you already had set for Animal1. The best way to correct this is to change the constructor in Cat1 and Dog1 to call the super constructor.
public Cat1(String name){
super(name);
}

How to store an object in to a variable

I am a beginner in JAVA and I could use some help. So I have 3 classes : Client,BankAccount and Test. I need to add into a Client object the details from a BankAccount. This is the code:
public class Client {
private String name;
private String adress;
private BankAccount accounts[];
public Client(String name,String adress,BankAccount accounts){
this.name=name;
this.adress=adress;
this.accounts=accounts;
}
public class BankAccount {
private String numarCont;
private float suma;
public ContBancar(String accNumber,float sum){
this.accNumber=accNumber;
this.sum=sum;
}
public class Test {
public static void main(String[] args) {
Bankaccount b=new Bankaccount("f211s1",200);
Bankaccount b1=new Bankaccount("f23131EUR",5000);
System.out.println(b);
Client c=new Client("John","142VineYard",b);
System.out.println(c);
}
}
A client can have multiple accounts. The problem is that I don't know how can I add the details of object b into object c.
Use a List for your BankAccounts
public class Client {
private String name;
private String adress;
private List<BankAccount> accounts = new ArrayList<BankAccount>();
public Client(String name,String adress,BankAccount account){
this.name=name;
this.adress=adress;
this.accounts.add(account);
}
public void addBankAccount(BBankAccount account){
this.accounts.add(account);
}
}
This way it is a easier to add Accounts.
c.addBankAccount(b);
c.addBankAccount(b1);
Maybe it would help you to take a look at some basic Java tutorials.
It sounds like what you want is inheritance. Inheritance will allow you to use the methods of one class in another class as if it was the same object. The keyword for inheriting in Java is extends used like this:
Public class foo extends foobar

Categories