Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Array Problems

  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Array Problems

    Well, I've searched through a lot of Java forums for my problem and to no avail have found no answers. So through my long journey I've come to all you java guru's for help with my struggles! haha Here goes:

    The basic idea of my program:
    Creating a program to bill two kinds of patients (Hospital and Clinic) differently and show the bill of all the patients in a JOptionpane using classes so it can be easily edited to add for more patients.

    The problem:
    I'm receiving errors at HospitalPatient and ClientPatient inside the array. It says "cannot instantiate the type ClinicPatient" and vice versa for the HospitalPatient.

    My program Structure:
    I've got a HospitalPatient class and ClientPatient class that extends from a Patient class which extends from a Person class.

    I'm not sure as to why this is coming up as an error. If you need to see the other class code I'll gladly put them up, but didnt want to make the post too long if it wasn't necessary....


    Code:
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    public abstract class TesterClass extends Person{
       public static void main( String[] args )
       {
          String output = "";
    
          // create a formatting object
          DecimalFormat precision2 = new DecimalFormat( "$0.00" );
    
          // Patient Array
          Patient[] patientArray=new Patient[4];
            patientArray[0]= new ClinicPatient("first name","lastName", "address", "city", "state", "zip", "ssn" );
          	patientArray[1]= new HospitalPatient("first name","lastName", "address", "city", "state", "zip", "ssn" );
          	patientArray[2]= new ClinicPatient("first name","lastName", "address", "city", "state", "zip", "ssn" );
          	patientArray[3]= new HospitalPatient("("first name","lastName", "address", "city", "state", "zip", "ssn" );
        
          	output="";
          	for (int i=0; i<patientArray.length; i++;{
        	output+=patientArray[i].toString();
        	output += "\n\n*****\n\n";
        }
    
          JOptionPane.showMessageDialog( null, output,"Billing Report", JOptionPane.INFORMATION_MESSAGE );
    
          System.exit( 0 );
       }
    }

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    Need the other code. My guess, is that you don't have proper constructors in your ClientPatient and HospitalPatient classes.

  • #3
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Here's my HospitalPatient code:
    Code:
    public abstract class HospitalPatient extends Patient {
    	   private String roomNumber;
    	   private int daysOfStay;
    	   private static double roomRate;
    
    	   
    	   // Constructor
    	   public  HospitalPatient( String doctor, String room, int days, double rate, String firstName, String lastName, String address, String city, String state, String zip, String ssn ){
    	   
    		   super(doctor, firstName, lastName,  address,  city,  state,  zip,  ssn); 
    		   roomNumber=room;
    		   daysOfStay=days;
    		   roomRate=rate;
    	   }
    	      
    	   //No argument Constructor
    	   public HospitalPatient(){
    	}
    
    	   // Set the roomNumber
    	   public void setroomNumber( String room ) {
    		   roomNumber = room;
    		 }
    	   
    	   // Set the days of stay
    	   public void daysOfStay( int days ) {
    			 daysOfStay = ( days >= 1 ? days : 0 );
    		 }
    	   
    	   // Set the room rate
    	   public static void roomRate( double rate ) {
    		   roomRate = ( rate >= 200 && rate <  500 ? rate : 200 );
    		 }
    	   
    	   // Return the room number
    	   public String getroomNumber() {
    			 return roomNumber;
    		 }
    	  
    	   // Return the days of stay
    	   public int daysOfStay() {
    			 return daysOfStay;
    		 }
    
    	   // Return the room rate
    	   public static double roomRate() {
    			 return roomRate;
    		 }
    
    	   //Determine the Bill of Hospital Patient
    	   public double computeBill() {
    			 return daysOfStay*(roomRate+125);
    	   }
    	   
    		public String toString() {
    		return super.toString() + daysOfStay + "-day hospital stay attended by : " + computeBill();
    			 }
    }
    and ClinicPatient code

    Code:
    public abstract class ClinicPatient extends Patient {
    	   private String appointmentDate;
       
    	   // Constructor
    	   public  ClinicPatient( String doctor, String appointment, String firstName, String lastName, String address, String city, String state, String zip, String ssn ){
    	   
    		   super(doctor, firstName, lastName,  address,  city,  state,  zip,  ssn); 
    		   appointmentDate=appointment;
    	   }
    	   //No argument Constructor
    	   public ClinicPatient(){
    	}
    
    	   // Set the appointment Date
    	   public void setappointmentDate( String a ) {
    		   appointmentDate = a;
    		 }
    
    	   // Return the appointment Date
    	   public String getappointmentDate() {
    			 return appointmentDate;
    		 }
    	   	
    	   //Determine the Bill of Clinic Patient
    	   public double computeBill() {
    			 return (125);
    	   }
    	   
    	   public String toString() {
    	return super.toString() + "Clinic visit with: on " + appointmentDate + computeBill();
    }
    }

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    You are not using your constructors properly. For example, the constructor in your ClinicPatient class takes 9 Strings as arguments, and you are only providing 7 Strings when you initialize them. You will need to provide the other 2 Strings to get them to function properly. Furthermore, your HospitalPatient class constructor takes 11 arguments, 9 of which are Strings, and 2 of them are integers. You need to pass those parameters, in the same order that you have them.

    Here is an example of a simple constructor:

    PHP Code:
    public class myClass
    {
        public 
    MyClass(String myStringint myIntdouble myDouble)
        {
            
    //code here to initialize your object
        
    }


    To use this constructor, you would use a method call such as:

    Code:
    MyClass object = new MyClass("This is a string", 5, 10.5);
    Notice that when calling the constructor, you must use the same data types that the constructor accepts.

  • #5
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Ok, I changed it, but I still have the same error....

    PHP Code:
    Patient[] patientArray=new Patient[4];
            
    patientArray[0]= new ClinicPatient("Dr Sushel""March 4, 2002""Tiller""Adams""4846 chandler rd""cumming""GA""30040""3234242233" );
              
    patientArray[1]= new HospitalPatient("dr weiner""2209"3180"Tiller","Adams""4846 chandler rd""cumming""GA""30040""3234242233" );
              
    patientArray[2]= new ClinicPatient("Dr Sushel""March 4, 2002""Tiller","Adams""4846 chandler rd""cumming""GA""30040""3234242233" );
              
    patientArray[3]= new HospitalPatient("dr weiner""23432"5180"Tiller","Adams""4846 chandler rd""cumming""GA""30040""3234242233" ); 

  • #6
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    Why are they abstract classes? You cannot instantiate an abstract class. Remove the abstract from the class definition and try it again.

  • #7
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    That worked thank you!

    But, I do have another problem now...

    I had to add the doctor's name to the clinicPatient output, I only get an error for doctorName and no other variables. Does everything look alright with the new variable doctorName? why am I a getting an error here?

    Heres my new code with doctorName added in:

    ClinicPatient
    Code:
    public class ClinicPatient extends Patient {
    	   private String appointmentDate;
    
    	   // Constructor
    	   public  ClinicPatient( String doctorName, String appointment, String firstName, String lastName, String address, String city, String state, String zip, String ssn ){
    
    		   super(doctorName, firstName, lastName,  address,  city,  state,  zip,  ssn);
    		   appointmentDate=appointment;
    	   }
    	   //No argument Constructor
    	   public ClinicPatient(){
    	}
    
    	   // Set the appointment Date
    	   public void setappointmentDate( String a ) {
    		   appointmentDate = a;
    		 }
    
    	   // Return the appointment Date
    	   public String getappointmentDate() {
    			 return appointmentDate;
    		 }
    
    	   //Determine the Bill of Clinic Patient
    	   public double computeBill() {
    			 return (125);
    	   }
    
    	   public String toString() {
    	return super.toString() + "Clinic visit with:" + doctorName + "on " + appointmentDate + computeBill();
    }
    }
    Patient
    Code:
    public class Patient extends Person {
    	   private String doctorName;
    
    	   // Constructor
    	   public Patient( String doctorName, String firstName, String lastName, String address, String city, String state, String zip, String ssn ) {
    		   super(firstName, lastName, address, city, state, zip, ssn);
    		   this.doctorName = doctorName;
    	   }
    	   //No argument Constructor
    	   public Patient(){
    	}
    
    	   // Set the doctor name
    	   public void setdoctorName( String d ) {
    		   doctorName = d;
    		 }
    
    	   // Return the doctor name
    	   public String getdoctorName() {
    			 return doctorName;
    		 }
    
    	   public String toString() {
    			 return super.toString();
    		 }
    
    	   public abstract double computeBill();
    }
    There arent any errors on the Patient class just the clinicPatient class...
    Last edited by Ty44ler; 02-25-2008 at 02:39 PM.

  • #8
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    What is the error you are getting? Post any information you have related to the error, it will be much easier to find the problem that way.

  • #9
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    "The field Patient.doctorName is not visible"

  • #10
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    try making the variable protected instead of private in the Patient class.

  • Users who have thanked brad211987 for this post:

    Ty44ler (02-25-2008)

  • #11
    New Coder
    Join Date
    Feb 2008
    Posts
    57
    Thanks
    8
    Thanked 0 Times in 0 Posts
    yep, that worked! Thank you very much!
    Last edited by Ty44ler; 02-25-2008 at 03:15 PM.


  •  

    Posting Permissions

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