...

View Full Version : Array Problems



Ty44ler
02-22-2008, 07:52 PM
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!: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....



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 );
}
}

brad211987
02-22-2008, 08:17 PM
Need the other code. My guess, is that you don't have proper constructors in your ClientPatient and HospitalPatient classes.

Ty44ler
02-22-2008, 09:53 PM
Here's my HospitalPatient 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


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();
}
}

brad211987
02-22-2008, 10:47 PM
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:



public class myClass
{
public MyClass(String myString, int myInt, double myDouble)
{
//code here to initialize your object
}
}



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


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.

Ty44ler
02-22-2008, 11:30 PM
Ok, I changed it, but I still have the same error....


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", 3, 180, "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", 5, 180, "Tiller","Adams", "4846 chandler rd", "cumming", "GA", "30040", "3234242233" );

brad211987
02-23-2008, 12:21 AM
Why are they abstract classes? You cannot instantiate an abstract class. Remove the abstract from the class definition and try it again.

Ty44ler
02-25-2008, 02:21 PM
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

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


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...

brad211987
02-25-2008, 02:34 PM
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.

Ty44ler
02-25-2008, 02:38 PM
"The field Patient.doctorName is not visible"

brad211987
02-25-2008, 02:51 PM
try making the variable protected instead of private in the Patient class.

Ty44ler
02-25-2008, 02:57 PM
yep, that worked! Thank you very much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum