Java Inheritence in classes

Gather data for class Student in it's constructor.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1638 Views - Last Post: 01 August 2010 - 10:48 AM Rate Topic: -----

#1 Guest_Paul*


Reputation:

Java Inheritence in classes

Posted 31 July 2010 - 02:45 PM

I have a program with person class() instructor class() and student class(). with both student and instructor get input from the user for lastName & firstName years teaching and years in school. I have the instructor complete. All the user input and output are in main that is where the teacher wanted it. I need to put all the user input and output for student needs to be in the instructor in the student class(). I have menu selection in main with 1 for instructor and 2 for student. When the user inputs 2 for student I need to know how to instantiate the object in my else if (getPerson.equals "2"). I can set it up to work in main just fine. But when I go to move over to the student class. How do I get it to jump in to the constructor in student class once it gets into my else if statement.

//main this is where the program starts off getting the user to enter 1 for instructor or 2 for student.
//the do while will keep them in the if, else if, if. Our teacher wants the data for instructor in MAIN.
//the data for student must be in the constructor for student.
//currently the program runs fine, but I hve no clue how to instantiate an object to get into the student //constructor. I hope you understand and can help. I have never asked for help before today.
//main follows

package unit05lab;
import javax.swing.*;

/**
 *
 * @author impaler
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int iWork;
        Student Name = new Student();
        
        

      
        boolean correctInput = false;
        do

        {   correctInput = false;
            String getPerson = JOptionPane.showInputDialog(
                    " Please Select 1 for Instructor or 2 for Student: ");

            if(getPerson.equals ("1"))
            {
               Instructor Teacher = new Instructor();
               //Instructor Teacher = new Instructor(int setYears);
               String getInstructorFirstName = JOptionPane.showInputDialog(" Please Enter the Teachers last Name: ");
               String getInstructorLastName = JOptionPane.showInputDialog(" Please Enter the Teachers Last Name: ");
               Teacher.setName( getInstructorFirstName, getInstructorLastName);
               JOptionPane.showMessageDialog(null, " Thank You " + Teacher.getFirstName() + " " + Teacher.getLastName() +
                       " for making the world a beter place ");
               String getYearInTeaching = JOptionPane.showInputDialog( "Please Enter the number of years" + " " + Teacher.getFirstName()+ " " +
                       Teacher.getLastName() + " has been teaching ");
               //String getYearInTeaching = JOptionPane.showInputDialog( " PLease Enter the amount of years you have been teaching:");
               iWork = Integer.parseInt(getYearInTeaching);
               Teacher.setYears(iWork);
               JOptionPane.showMessageDialog(null,  "Thank you " + Teacher.getFirstName() + " " + Teacher.getLastName() +
                       " for donating " + Teacher.getYearInTeaching()+ " years to teaching ");
            }
            else if (getPerson.equals ("2"))
            {
                
                
                //Student Name = new Student();
//                String getStudentFirstName = JOptionPane.showInputDialog(" Please Enter the Students First Name: ");
//                String getStudentLastName = JOptionPane.showInputDialog(" Pleae Enter the Students last Name: ");
//                Name.setName(getStudentFirstName, getStudentLastName);
//                JOptionPane.showMessageDialog(null, " " + Name.getFirstName() + " " + Name.getLastName() + " is a Student! ");
//                String getYearsInSchool = JOptionPane.showInputDialog( " Please enter the years you ahve been a student " + Name.getFirstName() +
//                        " " + Name.getLastName() + " " ) ;
//                iSchool = Integer.parseInt(getYearsInSchool);
                
            }
            else
            {
                   correctInput = true;
                   JOptionPane.showMessageDialog(null, " You did not inter a correct answer plaease try again ");
            }
            } while (correctInput);

        }

}

//student class the public student is where he wants the data. 
//what I can't figure out is where once the user enter 2 at the menu for student the program dies 
//i need to know how to get it to jump into the constructor

//class Student follows

package unit05lab;
import javax.swing.*;
/**
 *
 * @author impaler
 */
public class Student extends Person {
    
    

    private int iYearsInSchool;
    private int iSchool;
    

    


    public Student()
    {
        
        Student Name = new Student();
        JOptionPane.showMessageDialog(null, " Please enter the Students name ");
        String getStudentFirstName = JOptionPane.showInputDialog(" Please Enter the Students First Name: ");
        String getStudentLastName = JOptionPane.showInputDialog(" Pleae Enter the Students last Name: ");
        Name.setName(getStudentFirstName, getStudentLastName);
        JOptionPane.showMessageDialog(null, " " + Name.getFirstName() + " " + Name.getLastName() + " is a Student! ");
        String getYearsInSchool = JOptionPane.showInputDialog( " Please enter the years you ahve been a student " + Name.getFirstName() +
                " " + Name.getLastName() + " " ) ;
        iSchool = Integer.parseInt(getYearsInSchool);
        Name.setStudentYears(iSchool);
        JOptionPane.showMessageDialog(null, + Name.iYearsInSchool );
        
    }
     public void setStudentYears(int iYearsInSchoolToSet)
     {
         iYearsInSchool = iYearsInSchoolToSet;
     }

     public int getYearsInSchool()
    {
        return iYearsInSchool;
    }

}


This post has been edited by macosxnerd101: 31 July 2010 - 05:42 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 1

Replies To: Java Inheritence in classes

#2 cneajna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 10

Re: Java Inheritence in classes

Posted 31 July 2010 - 03:06 PM

I have a program with person class() instructor class() and student class(). with both student and instructor get input from the user for lastName & firstName years teaching and years in school. I have the instructor complete. All the user input and output are in main that is where the teacher wanted it. I need to put all the user input and output for student needs to be in the instructor in the student class(). I have menu selection in main with 1 for instructor and 2 for student. When the user inputs 2 for student I need to know how to instantiate the object in my else if (getPerson.equals "2"). I can set it up to work in main just fine. But when I go to move over to the student class. How do I get it to jump in to the constructor in student class once it gets into my else if statement.

main this is where the program starts off getting the user to enter 1 for instructor or 2 for student.

the do while will keep them in the if, else if, if. Our teacher wants the data for instructor in MAIN.

the data for student must be in the constructor for student.
currently the program runs fine, but I hve no clue how to instantiate an object to get into the student constructor. I hope you understand and can help. I have never asked for help before today.
main follows

package unit05lab;
import javax.swing.*;

/**
*
* @author impaler
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int iWork;
Student Name = new Student();




boolean correctInput = false;
do

{ correctInput = false;
String getPerson = JOptionPane.showInputDialog(
" Please Select 1 for Instructor or 2 for Student: ");

if(getPerson.equals ("1"))
{
Instructor Teacher = new Instructor();
//Instructor Teacher = new Instructor(int setYears);
String getInstructorFirstName = JOptionPane.showInputDialog(" Please Enter the Teachers last Name: ");
String getInstructorLastName = JOptionPane.showInputDialog(" Please Enter the Teachers Last Name: ");
Teacher.setName( getInstructorFirstName, getInstructorLastName);
JOptionPane.showMessageDialog(null, " Thank You " + Teacher.getFirstName() + " " + Teacher.getLastName() +
" for making the world a beter place ");
String getYearInTeaching = JOptionPane.showInputDialog( "Please Enter the number of years" + " " + Teacher.getFirstName()+ " " +
Teacher.getLastName() + " has been teaching ");
//String getYearInTeaching = JOptionPane.showInputDialog( " PLease Enter the amount of years you have been teaching:");
iWork = Integer.parseInt(getYearInTeaching);
Teacher.setYears(iWork);
JOptionPane.showMessageDialog(null, "Thank you " + Teacher.getFirstName() + " " + Teacher.getLastName() +
" for donating " + Teacher.getYearInTeaching()+ " years to teaching ");
}
else if (getPerson.equals ("2"))
{


//Student Name = new Student();
// String getStudentFirstName = JOptionPane.showInputDialog(" Please Enter the Students First Name: ");
// String getStudentLastName = JOptionPane.showInputDialog(" Pleae Enter the Students last Name: ");
// Name.setName(getStudentFirstName, getStudentLastName);
// JOptionPane.showMessageDialog(null, " " + Name.getFirstName() + " " + Name.getLastName() + " is a Student! ");
// String getYearsInSchool = JOptionPane.showInputDialog( " Please enter the years you ahve been a student " + Name.getFirstName() +
// " " + Name.getLastName() + " " ) ;
// iSchool = Integer.parseInt(getYearsInSchool);

}
else
{
correctInput = true;
JOptionPane.showMessageDialog(null, " You did not inter a correct answer plaease try again ");
}
} while (correctInput);

}

}

//student class the public student is where he wants the data.
//what I can't figure out is where once the user enter 2 at the menu for student the program dies
//i need to know how to get it to jump into the constructor

//class Student follows

package unit05lab;
import javax.swing.*;
/**
*
* @author impaler
*/
public class Student extends Person {



private int iYearsInSchool;
private int iSchool;





public Student()
{

Student Name = new Student();
JOptionPane.showMessageDialog(null, " Please enter the Students name ");
String getStudentFirstName = JOptionPane.showInputDialog(" Please Enter the Students First Name: ");
String getStudentLastName = JOptionPane.showInputDialog(" Pleae Enter the Students last Name: ");
Name.setName(getStudentFirstName, getStudentLastName);
JOptionPane.showMessageDialog(null, " " + Name.getFirstName() + " " + Name.getLastName() + " is a Student! ");
String getYearsInSchool = JOptionPane.showInputDialog( " Please enter the years you ahve been a student " + Name.getFirstName() +
" " + Name.getLastName() + " " ) ;
iSchool = Integer.parseInt(getYearsInSchool);
Name.setStudentYears(iSchool);
JOptionPane.showMessageDialog(null, + Name.iYearsInSchool );

}
public void setStudentYears(int iYearsInSchoolToSet)
{
iYearsInSchool = iYearsInSchoolToSet;
}

public int getYearsInSchool()
{
return iYearsInSchool;
}

}



Edited by macosxnerd101: Welcome to DIC! :) Please post your code between code tags, like so: :code:. Also, please properly format your code so that we can more easily follow your logic.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Java Inheritence in classes

Posted 31 July 2010 - 03:39 PM

Constructors work more or less the same as any other method, in that you can define them to accept parameters and overload them. But first, you need to understand how classes work. Classes model Objects. So if you model a Student, you should be defining attributes (instance variables) and behaviors (methods) that model a Student. User input should be handled separately from the class, never a part of it.

I cover class design in my tutorial Moving Away From Parallel Arrays, if you want to check it out.

Also, please properly format your code, as I cannot read it or follow your logic.
Was This Post Helpful? 1
  • +
  • -

#4 cneajna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 10

Re: Java Inheritence in classes

Posted 31 July 2010 - 07:45 PM

I just need to know how instantiate in the else if statement. So I can get into the constructor.
This is the class direction. I don't know how to get the compiler to look in the student constructor after the user inputs 2 for student.

I don't what u mean by correct format I just copied and pasted. Where do I put the
 
I don't get it.

I would like to use this site. NO I did not read all the rules sorry.

I just learned what

 





does now i know..

ok here is code cleaned up and placed in the code blocks...I hope you can help...
package unit05lab;
import javax.swing.*;

/**
 *
 * @author impaler
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int iWork;
        //Student oYearsInSchool;
        //Student oSchool;

        
        

      
        boolean correctInput = false;
        do

        {   correctInput = false;
            String getPerson = JOptionPane.showInputDialog(
                    " Please Select 1 for Instructor or 2 for Student: ");

            if(getPerson.equals ("1"))
            {
               //Create the object Teacher:
               Instructor Teacher = new Instructor();

               //Get the input for the Teachers First Name:
               String getInstructorFirstName = JOptionPane.showInputDialog(" Please Enter the Teachers last Name: ");

               //Get the input for the Teachers last Name:
               String getInstructorLastName = JOptionPane.showInputDialog(" Please Enter the Teachers Last Name: ");

               //Object Teacher will get set to 2 new variables:
               Teacher.setName( getInstructorFirstName, getInstructorLastName);

               //Thank the teacher for his/her service:
               JOptionPane.showMessageDialog(null, " Thank You " 
                       + Teacher.getFirstName()
                       + " " + Teacher.getLastName()
                       + " for making the world a beter place ");

               //Get the number of years as a teacher:
               String getYearInTeaching = JOptionPane.showInputDialog( "Please Enter the number of years"
                       + " " + Teacher.getFirstName()+ " "
                       + Teacher.getLastName()
                       + " has been teaching ");

               //Integer iwork get put into a variable:
               iWork = Integer.parseInt(getYearInTeaching);

               // Sets years as a teacher to iWork:
               Teacher.setYears(iWork);

               // Message that thank the teacher for the years of service and calls them by name:
               JOptionPane.showMessageDialog(null,  "Thank you " 
                       + Teacher.getFirstName()
                       + " " + Teacher.getLastName()
                       + " for donating " + Teacher.getYearInTeaching()
                       + " years to teaching ");
            }
            else if (getPerson.equals ("2"))
            {
                //I Feel this is where i need something force the compiler to :
                //run the student constructor:
            }
            else
            {
                   correctInput = true;
                   JOptionPane.showMessageDialog(null, " You did not inter a correct answer plaease try again ");
            }
            } while (correctInput);

        }

}



//This is the student class
package unit05lab;
import javax.swing.*;
/**
 *
 * @author impaler
 */
public class Student extends Person
{
    private int iSchool;
    private int iYearsInSchool;

    public Student()
    {

        Student Name = new Student();


        JOptionPane.showMessageDialog(null, " Please enter the Students name ");


        String getStudentFirstName = JOptionPane.showInputDialog(" Please Enter the Students First Name: ");


        String getStudentLastName = JOptionPane.showInputDialog(" Pleae Enter the Students last Name: ");


        Name.setName(getStudentFirstName, getStudentLastName);


        JOptionPane.showMessageDialog(null, " " 
                + Name.getFirstName()
                + " " + Name.getLastName()
                + " is a Student! ");


        String getYearsInSchool = JOptionPane.showInputDialog( " Please enter the years you ahve been a student " 
                + Name.getFirstName() + " "
                + Name.getLastName() + " " ) ;


        iSchool = Integer.parseInt(getYearsInSchool);


        Name.setStudentYears(iSchool);


        JOptionPane.showMessageDialog(null, + Name.iYearsInSchool );
    }
     public void setStudentYears(int iYearsInSchoolToSet)
     {
         iYearsInSchool = iYearsInSchoolToSet;
     }

     public int getYearsInSchool()
    {
        return iYearsInSchool;
    }

}


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Java Inheritence in classes

Posted 31 July 2010 - 07:52 PM

When you instantiate an Object, the constructor is automatically called. Here are some examples of instantiations:
ArrayList<Integer> list = new ArrayList<Integer>();
String s = new String("123");
StringBuffer sb = new StringBuffer(s);



The class names with the parentheses next to the new keyword are the constructor invocations, where you can pass parameters.

Also, notice how your instructor set up the Instructor object section. Notice how all user input was handled in the main() method and NOT in the class. As I said in my last post, it is very poor OO practice to handle user input within the class, as they are two separate tasks and abstractions. The Student class should handle encapsulating Student data, and another class should be responsible for managing the Student(s), including user input.
Was This Post Helpful? 1
  • +
  • -

#6 cneajna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 10

Re: Java Inheritence in classes

Posted 31 July 2010 - 07:56 PM

//Here is the Student Class with the methods set up.
package unit05lab;
import javax.swing.*;
/**
 *
 * @author impaler
 */
public class Student extends Person
{
    private int iSchool;
    private int iYearsInSchool;

    public Student()
    {

        Student Name = new Student();


        JOptionPane.showMessageDialog(null, " Please enter the Students name ");


        String getStudentFirstName = JOptionPane.showInputDialog(" Please Enter the Students First Name: ");


        String getStudentLastName = JOptionPane.showInputDialog(" Pleae Enter the Students last Name: ");


        Name.setName(getStudentFirstName, getStudentLastName);


        JOptionPane.showMessageDialog(null, " " 
                + Name.getFirstName()
                + " " + Name.getLastName()
                + " is a Student! ");


        String getYearsInSchool = JOptionPane.showInputDialog( " Please enter the years you ahve been a student " 
                + Name.getFirstName() + " "
                + Name.getLastName() + " " ) ;


        iSchool = Integer.parseInt(getYearsInSchool);


        Name.setStudentYears(iSchool);


        JOptionPane.showMessageDialog(null, + Name.iYearsInSchool );
    }
     public void setStudentYears(int iYearsInSchoolToSet)
     {
         iYearsInSchool = iYearsInSchoolToSet;
     }

     public int getYearsInSchool()
    {
        return iYearsInSchool;
    }

}



//This is the Main.Java

package unit05lab;
import javax.swing.*;

/**
 *
 * @author impaler
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int iWork;
        //Student oYearsInSchool;
        //Student oSchool;

        
        

      
        boolean correctInput = false;
        do

        {   correctInput = false;
            String getPerson = JOptionPane.showInputDialog(
                    " Please Select 1 for Instructor or 2 for Student: ");

            if(getPerson.equals ("1"))
            {
               //Create the object Teacher:
               Instructor Teacher = new Instructor();

               //Get the input for the Teachers First Name:
               String getInstructorFirstName = JOptionPane.showInputDialog(" Please Enter the Teachers last Name: ");

               //Get the input for the Teachers last Name:
               String getInstructorLastName = JOptionPane.showInputDialog(" Please Enter the Teachers Last Name: ");

               //Object Teacher will get set to 2 new variables:
               Teacher.setName( getInstructorFirstName, getInstructorLastName);

               //Thank the teacher for his/her service:
               JOptionPane.showMessageDialog(null, " Thank You " 
                       + Teacher.getFirstName()
                       + " " + Teacher.getLastName()
                       + " for making the world a beter place ");

               //Get the number of years as a teacher:
               String getYearInTeaching = JOptionPane.showInputDialog( "Please Enter the number of years"
                       + " " + Teacher.getFirstName()+ " "
                       + Teacher.getLastName()
                       + " has been teaching ");

               //Integer iwork get put into a variable:
               iWork = Integer.parseInt(getYearInTeaching);

               // Sets years as a teacher to iWork:
               Teacher.setYears(iWork);

               // Message that thank the teacher for the years of service and calls them by name:
               JOptionPane.showMessageDialog(null,  "Thank you " 
                       + Teacher.getFirstName()
                       + " " + Teacher.getLastName()
                       + " for donating " + Teacher.getYearInTeaching()
                       + " years to teaching ");
            }
            else if (getPerson.equals ("2"))
            {
                //this is where I assume I need get the compiler to some how go into the 
                //constructor really I have no clue how to do this is where I need help.
            }
            else
            {
                   correctInput = true;
                   JOptionPane.showMessageDialog(null, " You did not inter a correct answer plaease try again ");
            }
            } while (correctInput);

        }

}


This post has been edited by macosxnerd101: 31 July 2010 - 09:48 PM
Reason for edit:: Added ending code tag.

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Java Inheritence in classes

Posted 31 July 2010 - 07:58 PM

Duplicate topics merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#8 cneajna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 10

Re: Java Inheritence in classes

Posted 31 July 2010 - 08:02 PM

View Postmacosxnerd101, on 31 July 2010 - 06:52 PM, said:

When you instantiate an Object, the constructor is automatically called. Here are some examples of instantiations:
ArrayList<Integer> list = new ArrayList<Integer>();
String s = new String("123");
StringBuffer sb = new StringBuffer(s);



The class names with the parentheses next to the new keyword are the constructor invocations, where you can pass parameters.

Also, notice how your instructor set up the Instructor object section. Notice how all user input was handled in the main() method and NOT in the class. As I said in my last post, it is very poor OO practice to handle user input within the class, as they are two separate tasks and abstractions. The Student class should handle encapsulating Student data, and another class should be responsible for managing the Student(s), including user input.



I know it is poor practice, but that is what is on the instruction sheet!
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Java Inheritence in classes

Posted 31 July 2010 - 08:08 PM

Are you sure, because your instructor set up his demo with the Instructor class differently? Could you post the instructions so we can better help you? :)
Was This Post Helpful? 0
  • +
  • -

#10 cneajna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 10

Re: Java Inheritence in classes

Posted 31 July 2010 - 08:13 PM

View Postcneajna, on 31 July 2010 - 07:02 PM, said:

View Postmacosxnerd101, on 31 July 2010 - 06:52 PM, said:

When you instantiate an Object, the constructor is automatically called. Here are some examples of instantiations:
ArrayList<Integer> list = new ArrayList<Integer>();
String s = new String("123");
StringBuffer sb = new StringBuffer(s);



The class names with the parentheses next to the new keyword are the constructor invocations, where you can pass parameters.

Also, notice how your instructor set up the Instructor object section. Notice how all user input was handled in the main() method and NOT in the class. As I said in my last post, it is very poor OO practice to handle user input within the class, as they are two separate tasks and abstractions. The Student class should handle encapsulating Student data, and another class should be responsible for managing the Student(s), including user input.



I know it is poor practice, but that is what is on the instruction sheet!


not being rude but I know the that I can do
Student Name = new Student();


to fire off the constructor of student.

It currently runs and when the user inputs 2 from the menu it closes.
So even when I had that in it would not get out of the else if statement and jump to the constructor.
I know this is not good code practice but its what teacher wants.

This post has been edited by cneajna: 31 July 2010 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

#11 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Inheritence in classes

Posted 31 July 2010 - 08:19 PM

Quote

 public Student()
    {

        Student Name = new Student();




I seriously doubt this will work at all... In fact, I am positive. You don't instantiate an instance of an object inside that Object's constructor...

public static void main(String[] args){
     Student john = new Student(); //correct way to instantiate
     john.setStudentYears(4);
     System.out.println(john.toString() + " has been in school " + john.getStudentYears() + " years");
}


Was This Post Helpful? 1
  • +
  • -

#12 cneajna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 10

Re: Java Inheritence in classes

Posted 31 July 2010 - 08:23 PM

1 have input dialog
2 set 1 for instructor and 2 for student
3 use input boxes to collect input
4 use message boxes to display all output
5 feed all input and message boxes with strings
6 ask the user the type of person they want to add
7 have them enter 1 or 2
8 range check there answer
9 validate numerical input using exception handlinng
10 kkeep asking until they give correct answer
11 ask for the last and first name of both the instructor and the student
12 store all information in the instructor class
13 create a person class expand them to student and instructor
14 use instructor and student classes to store the input data
15 for instructor gather data in the main method and load the data using get/set function
16 for student class gather the data in the student constructor
17 when displaying output do not print any of the captured data from the class, use set/get methods to get data
from the class and print from the main method.
Was This Post Helpful? 0
  • +
  • -

#13 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Inheritence in classes

Posted 31 July 2010 - 08:28 PM

Mac was exactly right my friend. It's pretty clear to me that you are asking for input in the main method then sending the info to instructor and student. Instructor will have get and set methods for the data while you will send the data to the instance of Student.

//get input for years working instructor and store it in
//int yearsWorked

Instructor bill = new Instructor();
bill.setYearWorked(yearsWorked);

//do the same for Student (i.e. get yearsWorked from input)
Student john = new Student(yearsWorked);



code your classes accordingly

This post has been edited by Luckless: 31 July 2010 - 08:30 PM

Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Java Inheritence in classes

Posted 31 July 2010 - 09:56 PM

I'm honestly surprised you didn't get a StackOverflowError here, as creating a Student object each time a Student object is created is infinite recursion. Your inputs should deal with assigning the values to this Student object (or the instance variables), not creating a new Student object within the Student class. That's what the constructor does.
public Student()
    {

        Student Name = new Student();



I would honestly like to believe, though, that your instructor intends for you to get the user inputs in main(), and pass them to the Student via the constructor rather than getter/setter methods.
Was This Post Helpful? 0
  • +
  • -

#15 cneajna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-July 10

Re: Java Inheritence in classes

Posted 31 July 2010 - 10:33 PM

View PostLuckless, on 31 July 2010 - 07:28 PM, said:

Mac was exactly right my friend. It's pretty clear to me that you are asking for input in the main method then sending the info to instructor and student. Instructor will have get and set methods for the data while you will send the data to the instance of Student.

//get input for years working instructor and store it in
//int yearsWorked

Instructor bill = new Instructor();
bill.setYearWorked(yearsWorked);

//do the same for Student (i.e. get yearsWorked from input)
Student john = new Student(yearsWorked);



I know you guys are better at this than I am but you are telling me things I know.
The get/set in Instructor belongs getYearsTeaching is specific to Instructor.

I asked teach the same thing. He said yes he wants us to get the data input in the student constructor.
When i put the object creation Student Name = new Student in the constructor the compiler never gets there.
When I put it in the else if statement in main. I think I just figured it out.

code your classes accordingly

This post has been edited by macosxnerd101: 01 August 2010 - 08:44 AM
Reason for edit:: Fixed quote tag to remove question from quote block.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2