简体   繁体   中英

Constructors and methods in java

I want to create the below class

associatename:String
workstatus:String 
associate() :constructor
getassociatename():String
setassociatename(String):void
getworkstatus()String
tracksassociatestatus():int
setworkstatus(String):void

The trackAssociateStatus method takes the number of days as argument and sets the work status of the associate based on the number of days. The first 20 days they learn “C”, the next 20 days they learn “Java” In the Main class invoke the trackAssociateStatus method and find the work status and display the output.

output:The associate abc work status:Project phase

I tried this....But i got error

//associate class
public class associate{
    private int associatename;
    private String workstatus;
    private int days;
    void associate()
    {
         getassociatename();
         setassociatename();
         getworkstatus();
         tracksassociatestatus();
         setworkstatus();
    }
        public int getassociatename()
        {
            return associatename;
        }
        public void setassociatename(int associatename)
        {
            this.associatename=associatename;
        }
        public String getworkstatus()
        {
            return workstatus;
        }
        public void tracksassociatestatus(int days)
        {
            if(days<20)
                setworkstatus("C");
            else
                setworkstatus("Java");
        }
        public void setworkstatus(String workstatus)
        {
            this.workstatus=workstatus;
        }

}

//main class
 associate a =new associate();
        Scanner in=new Scanner(System.in);
        int associateid=0;
        String workstatus=null;
        int days=0;
        System.out.println("Enter the associateid:");
        associateid=in.nextInt();
        a.associateid=(associateid);
        System.out.println("Enter the no of days:");
        days=in.nextInt();
        a.trackassociatestatus();
        System.out.println("The id is "+a.getassocaiteid()+" work status "+a.getworkstatus());

Based on your (seemingly) UML spec, your class would look like the following:


public class Associate {

  private String associateName;
  private String workStatus;

  public Associate() {
    // This constructor is optional, a no-args constructor is added by the compiler to any class not explicitly naming a constructor.
  }

  public String getAssociateName() {
    return associateName;
  }

  public void setAssociateName(String associateName) {
    this.associateName = associateName;
  }  

  public String getWorkStatus() {
    return workStatus;
  }

  public void setWorkStatus(String workStatus) {
    this.workStatus = workStatus;
  }  

  public int tracksAssociateStatus() {
    // TODO write logic here
    return 1; // TODO change to whatever you need to return
  }
}

You were specifying int for getAssociateName , when associateName is a String . This won't work; you need your getter return type to be the same as your field data type, or you need to convert the data to the method's return type. (The former is best practice).

Constructors don't specify a type, the class name is used and the compiler will understand what you want to do (which is return a new instance of the class). Therefore, your void associate() will tell the compiler "create a method called associate that doesn't return anything".

Well, would be nice if you provide the error itself for us.

But meanwhile, have you notice that your tracksassociatestatus method recieves an integer parameter days , and your constructor passes nothing to it?

So try changing your constructor to be something like:

Public associate() {
     getassociatename();
     setassociatename();
     getworkstatus();
     tracksassociatestatus(10);
     setworkstatus();
}

For a cleaner code, check the other answer.

If you still have errors, please share them.

import java.util.*;

public class Associate
{
    private String associateName;
    private int workStatus;
    private int days;
    Scanner sc = new Scanner(System.in);

    public String getAssociateName()
    {
        System.out.println("Enter the Associate id:");
        associateName = sc.nextLine();
        return associateName;
    }

    public void setassociatename(int associatename)
    {
        this.associateName=associateName;
    }
    
    public String tracksAssociatename() 
    {
        return associateName;
    }

    public int getWorkStatus()
    {
        System.out.println("Enter the number of days");
        days = sc.nextInt();
        return days;
    }

    public void setWorkStatus(String workStatus)
    {
        this.workStatus=workStatus;
    }
    enter code here
    public `enter code here`int tracksAssociateStatus() 
    {
        return days;
    }

    public static void main(String args[])
    {
        Associate obj = new Associate();
        obj.getAssociateName();
        obj.getworkstatus();
        System.out.println("The Associate name "+obj.tracksAssociatename()+" work Status "+obj.tracksAssociateStatus());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM