Java extending class problem

Discussion in 'OT Technology' started by Disguy, Oct 5, 2008.

  1. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,060
    Likes Received:
    0
    I'm using to classes in one package. When I run the program it does not seem to be using the sub class. I believe that everything is coded properly but I could be wrong. For example the print statement in BusinessStudent does not print to screen.

    Package:School
    Super class: Student
    sub class: BusinessStudent

    Here is the full code for both. Let me know what you think.

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package school;
    
    /**
     *
     * @author user
     */
    public class Student {
    
        /**
         * @param args the command line arguments
         */
        private String course1Name;
        private String course2Name;
        private String course3Name;
        private float course1GPA;
        private float course2GPA;
        private float course3GPA;
        private String studentName;
        private String studentMajor;
        private float calcGPA;
    
     
            
        public static void main(String[] args) {
            // TODO code application logic here
            
            Student x = new Student();
            
            x.course1Name = "math";
            x.course1GPA = 2.45f;
            x.course2Name = "cis";
            x.course2GPA = 3.56f;
            x.course3Name = "art";
            x.course3GPA=  1.23f;
            x.studentMajor = "CIS";
            x.studentName = "Fred";
            //calc(x); 
         
                            
            System.out.println(x);
                    
            Student y = new Student();
            
            y.course1Name = "CHEM";
            y.course1GPA = 1.45f;
            y.course2Name = "HIST";
            y.course2GPA = 3.11f;
            y.course3Name = "ENG";
            y.course3GPA=  4.00f;
            y.studentMajor = "SCI";
            y.studentName = "Sally";
            
            System.out.println(y);
                
            
            
        }
        public float getCalcGPA() {
            calcGPA = calc();
            return calcGPA;
        }
    
     
        public float getCourse1GPA() {
            return course1GPA;
        }
    
        public String getCourse1Name() {
            return course1Name;
        }
    
        public float getCourse2GPA() {
            return course2GPA;
        }
    
        public String getCourse2Name() {
            return course2Name;
        }
    
        public float getCourse3GPA() {
            return course3GPA;
        }
    
        public String getCourse3Name() {
            return course3Name;
        }
    
        public String getStudentMajor() {
            return studentMajor;
        }
    
        public String getStudentName() {
            return studentName;
        }
       
        @Override
        public String toString (){
                   
         return ("Hi " + getStudentName() + ". You got a " + getCourse1GPA() +" in " +getCourse1Name() +" a " + getCourse2GPA() +" in " +getCourse2Name() + " a " + getCourse3GPA() +" in " +getCourse3Name()+"." +"Your overall GPA is : " +getCalcGPA());  
         
        } 
    
    
               // Cannot get task 3 to work at all.
        
        public float calc (){
            
            float course1 = getCourse1GPA();
            float course2 = getCourse2GPA();
            float course3 = getCourse3GPA();
            
            float calc= (course1 + course2 + course3)/3;
                         
            return (calc);
        } 
           public void setCalcGPA(float calcGPA) {
            
            this.calcGPA = calcGPA;
        }
           
        public void setCourse1GPA(float course1GPA) {
            this.course1GPA = course1GPA;
        }
    
        public void setCourse1Name(String course1Name) {
            this.course1Name = course1Name;
        }
    
        public void setCourse2GPA(float course2GPA) {
            this.course2GPA = course2GPA;
        }
    
        public void setCourse2Name(String course2Name) {
            this.course2Name = course2Name;
        }
    
        public void setCourse3GPA(float course3GPA) {
            this.course3GPA = course3GPA;
        }
    
        public void setCourse3Name(String course3Name) {
            this.course3Name = course3Name;
        }
    
        public void setStudentMajor(String studentMajor) {
            this.studentMajor = studentMajor;
        }
    
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
    
      
    
    }
    /*
    
    Task 3 – Define a Method and Return a Value
    Step 1 – Create a method named calcGPA which calculates the student's GPA. 
     The method should simply add the three grades together and divide appropriately. 
     The method should return a float.
    
    Step 2 - Output the GPAs for the two student objects you created in the previous task.
    
    Task 4 - Extend the Student class. 
    
    Step 1 - Create a BusinessStudent class which extends the Student class.
    
    Step 2 - The BusinessStudent class should be declared with instance members for businessElective1Name and businessElective2Name, and businessElective1Grade, and businessElective2Grade.
    
    Step 3 - Override the calcGPA method. The overriden method will add the grades for  the course1Grade, course2Grade and course3Grade, plus the businessElective1Grade and businessElective2Grade variables and divide appropriately to arrive at the student's GPA. The method should return a floating point value representing the student's GPA.
    
    Step 4 - In the main method, call the appropriate setters to set the values for the member variables in the BusinessStudent instance. Note that this would include the members inherited from the Student class. Finally, call and output the results of calling the calcGPA method.
    */
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package school ;
    
    /**
     *
     * @author user
     */
    public class BusinessStudent extends Student {
           
        private String businessElective1Name;
        private String businessElective2Name;
        private float businessElective1Grade;
        private float businessElective2Grade;
        private float businessGPA;
        
        public static void main(String[] args){
        
           BusinessStudent x = new BusinessStudent();
        
           x.businessElective1Grade = 4.00f;
           x.businessElective1Name = "Accounting 102";
           x.businessElective2Grade = 3.23f;
           x.businessElective2Name = "Management 101";
           
           System.out.println(x);
            
            
        }
        
        
     // and businessElective1Grade, and businessElective2Grade.
            
        public float calcGPa(){
            
            float course1 = getCourse1GPA();
            float course2 = getCourse2GPA();
            float course3 = getCourse3GPA();
            float course4 = getBusinessElective1Grade();
            float course5 = getBusinessElective2Grade();
            
            float calc= (course1 + course2 + course3+ course4 + course5)/5;
            
            System.out.println("Calc BS: " +calc);
            
            return (calc);
            
        }
         @Override
        public String toString (){
                   
         return ("Hi " + getStudentName() + ". You got a " + getCourse1GPA() +" in " +getCourse1Name() +" a " + getCourse2GPA() +" in " +getCourse2Name() + " a " + getCourse3GPA() +" in " +getCourse3Name()+"." +"Your overall GPA is : " +getCalcGPA());  
         
        } 
    
        public float getBusinessElective1Grade() {
            
            return businessElective1Grade;
        }
    
        public void setBusinessElective1Grade(float businessElective1Grade) {
            this.businessElective1Grade = businessElective1Grade;
        }
    
        public String getBusinessElective1Name() {
            
            return businessElective1Name;
        }
    
        public void setBusinessElective1Name(String businessElective1Name) {
            this.businessElective1Name = businessElective1Name;
        }
    
        public float getBusinessElective2Grade() {
            
            return businessElective2Grade;
        }
    
        public void setBusinessElective2Grade(float businessElective2Grade) {
            this.businessElective2Grade = businessElective2Grade;
        }
    
        public String getBusinessElective2Name() {
          
            return businessElective2Name;
        }
    
        public void setBusinessElective2Name(String businessElective2Name) {
            this.businessElective2Name = businessElective2Name;
        }
    
        public float getBusinessGPA() {
            businessGPA = calc();
            return businessGPA;
        }
    
        public void setBusinessGPA(float businessGPA) {
            this.businessGPA = businessGPA;
        }
    
    }
    /*
    
    Step 4 - In the main method, call the appropriate setters to set the values for 
     the member variables in the BusinessStudent instance. Note that this would include 
     the members inherited from the Student class. Finally, call and output the results 
     of calling the calcGPA method.
    
    */
     
  2. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,060
    Likes Received:
    0
    I'm new to Object programing so thats 90% of it.

    Thats just temporary. Its a work in progress. I have to talk to my professor about why i'm getting errors when I set up the values for x.gpa in the setter.

    On i side note, I'm using netbeans, is it possible its running student.java and not BusinessStudents and I'm not aware of it?
     

Share This Page