#Calling Methods

1 messages · Page 1 of 1 (latest)

delicate galleon
#

Hello guys, I don't want to ask for a code, but I want to ask you to explain me how it works. Basically, my issues may be simple for some of you, but not for me. I have 4 classes: OOP (main), Cashier, Manager and Employee. Cashier and Manager extends Employee, what I need to do is to make instance variables and one method inside of the Employee class and then I'll have to use it in both Cashier and Manager classes, but the problem is that if I try to call the method I've created - it's underlined red and NetBeans doesn't tell me the issue. I asked ChatGPT to take a look at my code to see if there are any issues, but he told me my code has no issues. I forgot to mention that all the setters and getters were also implemented in the code thru refractor --> encapsulate fields.

velvet nicheBOT
#

<@&987246883653156906> please have a look, thanks.

delicate galleon
#

public class Employee {

private String Name;
private String Address;
private String DOB;
private String PPSN;
private int yearsOfService;

public double bonusPercentage() {
    setYearsOfService(Integer.parseInt(JOptionPane.showInputDialog("Enter number of years in service: ")));
    double bonusPercentage;
    if (getYearsOfService() <= 3) {
        bonusPercentage = 0.05;
    } else if (getYearsOfService() <= 7) {
        bonusPercentage = 0.07;
    } else {
        bonusPercentage = 0.10;
    }
    return bonusPercentage;
}

/**
 * @return the Name
 */
public String getName() {
    return Name;
}

/**
 * @param Name the Name to set
 */
public void setName(String Name) {
    this.Name = Name;
}

/**
 * @return the Address
 */
public String getAddress() {
    return Address;
}

/**
 * @param Address the Address to set
 */
public void setAddress(String Address) {
    this.Address = Address;
}

/**
 * @return the DOB
 */
public String getDOB() {
    return DOB;
}

/**
 * @param DOB the DOB to set
 */
public void setDOB(String DOB) {
    this.DOB = DOB;
}

/**
 * @return the PPSN
 */
public String getPPSN() {
    return PPSN;
}

/**
 * @param PPSN the PPSN to set
 */
public void setPPSN(String PPSN) {
    this.PPSN = PPSN;
}

/**
 * @return the yearsOfService
 */
public int getYearsOfService() {
    return yearsOfService;
}

/**
 * @param yearsOfService the yearsOfService to set
 */
public void setYearsOfService(int yearsOfService) {
    this.yearsOfService = yearsOfService;
}

}

This is the code I have no

velvet nicheBOT
#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Hello guys, I don't want to ask for a code, but I want to ask you to explain me how it works. Basically, my issues may be simple for some of you, but not for me. I have 4 classes: OOP (main), Cashier, Manager and Employee. Cashier and Manager extends Emplo

Principles, Inheritance, Polymorphism, and Abstraction. Each class contains different methods that demonstrate various principles of object-oriented programming. To call a method from another class, you first need to create an instance of that class. For example, if you want to call a method from the Inheritance class in your Principles class, you would first create an instance of the Inheritance class:

Inheritance inheritance = new Inheritance();

Then, you can call a method from the Inheritance class using the instance you created:

inheritance.methodName();

This allows you to access the methods in the Inheritance class from your Principles class. Remember to properly import the classes at the beginning of your file using import statements to avoid any compile-time errors.

It's important to understand how classes interact with each other in Java to effectively utilize object-oriented programming principles. By creating instances of classes and calling their methods, you can create modular and reusable code that follows best practices in software development. If you have any specific questions or code examples related to calling methods in Java, feel free to ask for further clarification or assistance.

delicate galleon
#

After renaming

#

So strange

stray marsh
#

you gotta call it within another method

#
class MyClass {
    method(); // will not work
}```
delicate galleon
#

The thing is that it's basically an assigment, but we were told to do it as we've done before, this way

stray marsh
#

by "assignment", i meant "variable assignment", not your coursework assignment

delicate galleon
stray marsh
#

point is: you need to call methods within another method

delicate galleon
#

Ok

#

so basically I create a method here and call a method from another class inside the method

#

right?

stray marsh
#

try it

delicate galleon
#

Looks like it should work now

#

Thanks, I'll try to write a bit of code today and If I'll encounter any other issues, I'll post it here. Many thanks!

stray marsh