Array Error  | |
February 22nd, 2008, 04:02 PM
|
#1 (permalink)
| | Member
Join Date: Mar 2005 Location: Atlanta, Georgia
Posts: 407
| Array Error
Well, I've come to be stumped and come to all you java guru's of TechIMO to help me out! :thumbsup: 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("Dr Sushel", "March 4, 2002", "first name","lastName", "address", "city", "state", "zip", "ssn" );
patientArray[1]= new HospitalPatient("dr weiner", "2209", 3, 180, "first name","lastName", "address", "city", "state", "zip", "ssn" );
patientArray[2]= new ClinicPatient("Dr Sushel", "March 4, 2002", "first name","lastName", "address", "city", "state", "zip", "ssn" );
patientArray[3]= new HospitalPatient("dr weiner", "2209", 3, 180, "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 );
}
}
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();
}
}
__________________
Frank: Blue do you trust that I do not want to see you die here tonight?
Blue: Yes sir.
Frank: Blue you're my boy!
Last edited by Ty44ler : February 22nd, 2008 at 07:33 PM.
|
| |
February 24th, 2008, 02:21 PM
|
#2 (permalink)
| | Member
Join Date: Oct 2003
Posts: 263
|
hey,
not a java programmer. I only code now in .net ( vb, c#)
if you declare each ClinicPatient or HospitalPatient into its own variable as clinic patient does it create the instance? I am assuming the extends is inheritance. If thats the case maybe your array of Patient is causing the error since the array is strongly typed. Not sure if java works differently with inheritance / strongly typed arrays with inheritance.
see if the following code works:
test ClinicPatient=new ClinicPatient.......
if that works you might want to declare your array with objects not patients. |
| |
February 25th, 2008, 09:23 AM
|
#3 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
That should work. Why does testerclass extend person though?
also..what version of java are you using? |
| |
February 25th, 2008, 03:21 PM
|
#4 (permalink)
| | Member
Join Date: Mar 2005 Location: Atlanta, Georgia
Posts: 407
|
Im running JAVA SE 6....
I figured it out, it was because clinicPatient and hospitalPatient were both abstract classes and couldnt read it. I took that out and voila no error. Thanks for the help though! |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |