Home > database >  define constructors in java
define constructors in java

Time:09-27

I am working on a java code to create a class Students and I should define two constructors and one has to be with parameters and one without parameters I have already done the one with parameters but I am having a problem understanding how to do the one without parameters This is what I have to do: setStudent()that takes three parameters: a string name, an integer grade, and a double cgpa value. It stores these parameters into the three member variables of the class. getNamethat returns the value stored in the member variable name. getGradethat returns the value stored in the member variable grade. getCGPAthat returns the value stored in the member variable cgpa. printStudent that displays the values of the three member variables. I have done most but I do not know what to do with the last thing printStudent.

My class :

    public class Students{
       private String Name;
       private int Grade;
       private double CGPA;
       public Students(String Name, int Grade, double CGPA){
          this.Name = Name;
          this.Grade = Grade;
          this.CGPA = CGPA;
       }
       public String getName(){
          return Name;
       }
       public void setName(String Name){
          this.Name = Name;
       }
       public int getGrade(){
          return Grade;
       }
       public void setGrade(int Grade){
          this.Grade = Grade;
       }
       public double getCGPA(){
          return CGPA;
       }
       public void setCGPA(double CGPA){
          this.CGPA = CGPA;
       }
    }

and that is my main :

    public class LAB4EX1{
       public static void main(String [] args){
          Students student1 = new Students("Nasser", 90, 3.4);
          Students student2 = new Students("Adnan", 92, 3.72);
          Students student3 = new Students("Mohammed", 91, 3.5);
       }
    }

I need to make it print the output for me. Any help would be really appreciated.

CodePudding user response:

You have do declare a constructor without any parameters and override toString method:

public class Students{
        private String Name;
        private int Grade;
        private double CGPA;
        public Students(String Name, int Grade, double CGPA){
            this.Name = Name;
            this.Grade = Grade;
            this.CGPA = CGPA;
        }
        public Students(){ // empty constructor
        }
        public String getName(){
            return Name;
        }
        public void setName(String Name){
            this.Name = Name;
        }
        public int getGrade(){
            return Grade;
        }
        public void setGrade(int Grade){
            this.Grade = Grade;
        }
        public double getCGPA(){
            return CGPA;
        }
        public void setCGPA(double CGPA){
            this.CGPA = CGPA;
        }

        @Override
        public String toString() {
            return "Students{"  
                    "Name='"   Name   '\''  
                    ", Grade="   Grade  
                    ", CGPA="   CGPA  
                    '}';
        } // toString() for printing your three fields
    }
  • Related