Still can not call my Subclass method returns. J2EE
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 69

Thread: Still can not call my Subclass method returns. J2EE

  1. #1
    Join Date
    Dec 2013
    Posts
    46

    Still can not call my Subclass method returns. J2EE

    I am having problems returning and using my subclass method returns... I am trying to print out the results but am getting just the space allocation only. Any help will greatly appreciated.

    Code:
    package contacts;
    
    
    import java.util.Scanner;
    import java.util.ArrayList;
    
    
    public class Contacts {
    
       
        public static void main(String[] args) 
        {
             
            System.out.println("By enetering B for Business  or P for personal contact: ");
            
            System.out.println("Are you a Business Contact or Personal Contact?: ");
            
            //Storing the users input
            Scanner in = new Scanner(System.in);
            String cont = in.next();      
            
            if (cont.equals("P"))
            {
                System.out.println("Please enter: ");
                        
            }
            
            else if (cont.equals("B"))
            {
              
               
                 Business fno = new Business();
                 Business lno = new Business();
                 Business ado =  new Business();
                 Business pno = new Business();
                 Business emo = new Business();
                 Business jto = new Business();
                 Business orgo = new Business();
                 
                 
                fno.firstName();
                lno.lastName();
                ado.address();
                pno.phoneNumber();
                emo.eMail();
                jto.jobTitle();
                orgo.organization();     
                 
            }
            
            else
            {
                System.out.println("Please choose either B for Business Contact or "
                        + "P as a Personal Contact as you picked an invalid entry.");
               // return;
            }
        }
       
            }
    Code:
    package contacts;
    
    abstract public class ContactInfo 
    {
        public abstract String firstName();
        
        public abstract String lastName();
       
        public abstract String address();
                  
        public abstract String phoneNumber();
       
        public abstract String eMail();      
    }
    Code:
    package contacts;
    
    import java.util.Scanner;
    
    
    public class Business extends ContactInfo 
    {
       
        Scanner in = new Scanner(System.in);
       
        public String firstName()
        {
            System.out.println("Please enter your First Name: ");
            String fName2 = in.next();
            return fName2;
        }
        public String lastName()
        {
            System.out.println("Please enter your Last Name: ");
            String lName2 = in.next();
            return lName2;
        }
        public String address()
        {
            System.out.println("Please enter your Address: ");
            String addre2 = in.next();
            return addre2;
        }
        public String phoneNumber()
        {
            System.out.println("Please enter Phone Number: ");
            String phoneNum2 = in.next();
            return phoneNum2;
        }
        public String eMail()
        {
            System.out.println("Please enter your Email Address: ");
            String eMailAdd2 = in.next();
            return eMailAdd2;
        }
    
     
     
        
       
        public String jobTitle()
        {
            System.out.println("Please enter your job title: ");
            String jobTit = in.next();
            return jobTit;
        }
        public String organization()
        {
            System.out.println("Please enter your organization: ");
            String organ = in.next();
            return organ;
        }
       
       
    }
    Last edited by BigDadd215; February 28th, 2014 at 04:29 AM.

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,449

    Re: Still can not call my Subclass method returns. J2EE

    Please fix the code tags in your post. They use the square brackets: [] not <>
    Norm

  3. #3
    Join Date
    Dec 2013
    Posts
    46

    Re: Still can not call my Subclass method returns. J2EE

    Sorry about that...... I guess I have done too much HTML...LOL

    I have corrected and it looks MUCH better now.

  4. #4
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,449

    Re: Still can not call my Subclass method returns. J2EE

    What are the "results" that you want printed?
    What does the program currently print out? Can you post the output and also show what you want printed?
    Norm

  5. #5
    Join Date
    Dec 2013
    Posts
    46

    Re: Still can not call my Subclass method returns. J2EE

    What I as trying to do is test my Business by printing the results from creating the objects in the main class........ What I need to do, but am confused on how to..is create an arraylist that I can store the users inputed values... Say for example, teacher contact and a student contact... Not to sure how to accomplish using inheritance and or polymorhism..

  6. #6
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,449

    Re: Still can not call my Subclass method returns. J2EE

    create an arraylist that I can store the users inputed values

    What would be stored in the ArrayList? If there are several pieces of data that need to be kept together, then you should define a class to hold those pieces of data. Then in the program after the data that goes together is collected, create an instance of the new class passing its constructor the data it is to save and then save that instance of the class in the ArrayList.

    After all the data is collected, a loop could be used to go through the data saved in the ArrayList and to create the reports.
    Norm

  7. #7
    Join Date
    Dec 2013
    Posts
    46

    Re: Still can not call my Subclass method returns. J2EE

    Can you give a real fast example to help point me down the right direction? I am a little confused.

  8. #8
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,449

    Re: Still can not call my Subclass method returns. J2EE

    I listed several steps, which step are you having problems with? Work on them one at a time.
    Norm

  9. #9
    Join Date
    Dec 2013
    Posts
    46

    Re: Still can not call my Subclass method returns. J2EE

    Code:
    //ArrayList<Employee> staff = new ArrayList<Employee>();
    //staff.add(new Employee("Harry Hacker", . . .));
    package contacts;
    
    
    import java.util.ArrayList;
    /**
     *
     * @author Drew1
     */
    public class BusinessList 
    {
        /**
        private Business[] busList = new Business[100];
        private int i = 0;
        
        public void add(Business d)
        {
            if(i < busList.length)
            {
                busList[i] = d;
                System.out.println("Business Contact added at index " + i);
                i++;
            }
            * */
        ArrayList<Business> busContact = new ArrayList<Business>();
         //contacts.Business.busContact.add(new Business (Business.fName2, Business.lName2, Business.adddre2, Business.phoneNum2, Business.eMailAdd2, Business.jobTitle,Business.jobTit, Business.organ ));
        //private int next1 = 0;
        //Business bus1 = new Business();
       // busContact.add(bus1);
       /** public void add(Business d)
        {
            if (next1 < busContact.size())
            {
                busContact[next1] = d;
                next1++;
            }
        }**/
         ArrayList<Personal> perContact = new ArrayList<Personal>();
         {
              perContact.add("Drew");
        
    }
       
       
    }

    I have commented out some stuff cause the errors, but am I getting on the track.

  10. #10
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,449

    Re: Still can not call my Subclass method returns. J2EE

    What is that code supposed to do?

    When you get errors, don't ignore them, fix them. If you need help, copy the full text and paste it here.
    Norm

  11. #11
    Join Date
    Dec 2013
    Posts
    46

    Re: Still can not call my Subclass method returns. J2EE

    As soon as I can, I will rerun the code in the complier and give you the error..
    The code is supposed ask which kind of contact is, then prompt the the user to enter the information. from there the user should be able to display all contact information based on a selected contact.

  12. #12
    Join Date
    Dec 2013
    Posts
    46

    Re: Still can not call my Subclass method returns. J2EE

    I have changed my code but I do not think that I am adding to the array.
    Code:
        if (cont.equals("P"))
            {
               
               System.out.println("Please enter your First Name");
            String fName = in.next();
            
            System.out.println("Please enter your Last Name");
            String lName = in.next();
            
           in.nextLine();
            System.out.println("Please enter your Address");
             String addre = in.nextLine();
            
             System.out.println("Please enter Phone Number");
            String phoneNum = in.next();
            
             System.out.println("Please enter your Email Address");
            String eMailAdd = in.next();
            
            System.out.println("Please enter your Date of Birth (xx/xx/xxxx");
            String dob = in.next();
            
            System.out.println("Your contact info is: " + fName + ", " + lName + ", "  + addre 
                    + ", " + phoneNum + ", " + eMailAdd + ", " + dob);
    Code:
    public abstract class ContactInfo 
    {
        public abstract void firstName();
        
        public abstract void lastName();
       
        public abstract void address();
                  
        public abstract void phoneNumber();
       
        public abstract void eMail();
        
        //public abstract String jobTitle();
        
        //public abstract String organization();
        
        //public abstract String dateOfBirth();
        
    }
    Code:
    package contacts;
    
    
    public class Personal extends ContactInfo
    {
        
        public void firstName()
        {
           BusinessList.perContact.add(fName);// Error here that a non static variable can not reference a static variable
        }
        public void lastName()
        {
            
            //return lName;
        }
        public void address()
        {
            
            //return addre;
        }
        public void phoneNumber()
        {
           
          
            //return phoneNum;
        }
        public void eMail()
        {
           
            //return eMailAdd;
        }
        public void dateOfBirth()
        {
            
            //return dob;
            
        }
     
        
    }



    Code:
    package contacts;
    
    
    import java.util.ArrayList;
    
    public class BusinessList 
    {
     
        ArrayList<Business> busContact = new ArrayList<Business>();
    
         ArrayList<Personal> perContact = new ArrayList<Personal>();
         {
              perContact.add("Drew");// error here saying not a suitable method
        
    }
       
       
    }

  13. #13
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,449

    Re: Still can not call my Subclass method returns. J2EE

    perContact is takes Personal objects not Strings.
    Norm

  14. #14
    Join Date
    Dec 2013
    Posts
    46

    Re: Still can not call my Subclass method returns. J2EE

    I have went to my Business Class (original idea) and tried to make the ArrayList work, but I still seems to be getting errors... I must be missing something????

    Code:
    package contacts;
    
    
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class Contacts 
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
             //ArrayList<Business> busContact = new ArrayList<Business>();
            
            
            // Asks the user if they are a business contact or personal contact
            System.out.println("By enetering B for Business  or P for personal contact: ");
            
            System.out.println("Are you a Business Contact or Personal Contact?: ");
            
            //Storing the users input
            Scanner in = new Scanner(System.in);
            String cont = in.next();
            
            //System.out.print("You chose: "+ cont + " as your contact.");
            //String fName;
            if (cont.equals("P"))
            {
               
               System.out.println("Please enter your First Name");
            String fName = in.next();
            
            System.out.println("Please enter your Last Name");
            String lName = in.next();
            
           in.nextLine();
            System.out.println("Please enter your Address");
             String addre = in.nextLine();
            
             System.out.println("Please enter Phone Number");
            String phoneNum = in.next();
            
             System.out.println("Please enter your Email Address");
            String eMailAdd = in.next();
            
            System.out.println("Please enter your Date of Birth (xx/xx/xxxx");
            String dob = in.next();
            
            System.out.println("Your contact info is: " + fName + ", " + lName + ", "  + addre 
                    + ", " + phoneNum + ", " + eMailAdd + ", " + dob);
                        
            }
            
            else if (cont.equals("B"))
            {
               
                 ArrayList<Business> busContact = new ArrayList<Business>();
              
                    Business fno = new Business();
                    Business lno = new Business();
                    Business ado =  new Business();
                    Business pno = new Business();
                    Business emo = new Business();
                    Business jto = new Business();
                    Business orgo = new Business();
                 
                 
                fno.firstName();
                lno.lastName();
                ado.address();
                pno.phoneNumber();
                emo.eMail();
                jto.jobTitle();
                orgo.organization();
                
                busContact.add(fno);
                busContact.add(lno);
                busContact.add(ado);
                busContact.add(pno);
                busContact.add(emo);
                busContact.add(jto);
                busContact.add(orgo);
                
                disArrayList(busContact);
            }   
      
        public static void disArrayList (ArrayList<Business> toBeDisp)
                {
                    for (int i =0; i < toBeDisp.size(); i++)
                    {
                        System.out.println(toBeDisp.get(i));
                    }
                }
    Thanks.

  15. #15
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,449

    Re: Still can not call my Subclass method returns. J2EE

    I still seems to be getting errors
    Please copy the full text of the error messages and paste it here.
    Norm

Page 1 of 5 1234 ... LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center