求一个JAVA程序,用eclipse编写,全分送上
要求如下,求牛逼大大们帮帮忙,可以的全部分送上,多谢多谢,在线等CreateaJavaprogramthathastheseclasses:1.Name,whichhas...
要求如下,求牛逼大大们帮帮忙,可以的全部分送上,多谢多谢,在线等
Create a Java program that has these classes:
1. Name, which has fields:
- String lastname
- String firstname
2. Address, which has fields:
- int stnum
- String street
- String city
- String postalCode
3. PatientRecord, which has fields:
- int patientID
- Name patName
- Address patAddr
4. Patients, which has fields:
- int numPatients
- PatientRecord[] patRecords
5. Hospital, which has fields:
- String hospitalName
- Address hospitalAddr
- Patients patientRecs
The main routine, in class MainClass, MUST look like this:
public static void main(String[] args){
Hospital hosp = new Hospital();
hosp.readIn();
System.out.printf("hosp=%s",hosp.toString());
}//main
which creates an instance of class Hospital,
then calls the readIn() function of that Hospital object which will:
1. read in the hospitalName, by calling hospitalName.readIn()
2. read in the hospitalAddr, by calling hospitalAddr.readIn()
3. read in the patRecords, by calling patientRecs.readIn() which
reads in the number of PatientRecord objects and
creates the array of PatientRecord objects.
Then, PatientRecs.readIn() calls readIn() on each
Patient record in array PatientRecords, which will
in turn read in the PatientID,
and call PatientName.readIn(), and PatientAddr.readIn()
After calling hosp.readIn(), main will print out all of
the contents of the hospital object pointed to by printing
hosp.toString(). 展开
Create a Java program that has these classes:
1. Name, which has fields:
- String lastname
- String firstname
2. Address, which has fields:
- int stnum
- String street
- String city
- String postalCode
3. PatientRecord, which has fields:
- int patientID
- Name patName
- Address patAddr
4. Patients, which has fields:
- int numPatients
- PatientRecord[] patRecords
5. Hospital, which has fields:
- String hospitalName
- Address hospitalAddr
- Patients patientRecs
The main routine, in class MainClass, MUST look like this:
public static void main(String[] args){
Hospital hosp = new Hospital();
hosp.readIn();
System.out.printf("hosp=%s",hosp.toString());
}//main
which creates an instance of class Hospital,
then calls the readIn() function of that Hospital object which will:
1. read in the hospitalName, by calling hospitalName.readIn()
2. read in the hospitalAddr, by calling hospitalAddr.readIn()
3. read in the patRecords, by calling patientRecs.readIn() which
reads in the number of PatientRecord objects and
creates the array of PatientRecord objects.
Then, PatientRecs.readIn() calls readIn() on each
Patient record in array PatientRecords, which will
in turn read in the PatientID,
and call PatientName.readIn(), and PatientAddr.readIn()
After calling hosp.readIn(), main will print out all of
the contents of the hospital object pointed to by printing
hosp.toString(). 展开
2个回答
展开全部
这里主要用到了重写toString,和构造函数方便初始化。
下面是所有代码
class Name {
String lastname;
String firstname;
public Name(String lastname, String firstname) {
this.lastname = lastname;
this.firstname = firstname;
}
public Name() {
}
public String toString() {
return "Name: lastname:\t"+lastname+"\tfirstname:"+firstname+"\n";
}
}
class Address {
int stnum;
String street;
String city;
String postalCode;
public Address(int stnum, String street, String city, String postalCode) {
this.stnum = stnum;
this.street = street;
this.city = city;
this.postalCode = postalCode;
}
public Address() {
}
public String toString() {
return "Address: stnum:\t"+stnum+"\tstreet:"+street+"\tcity:"+city+"\tpostalCode:"+postalCode+"\n";
}
}
class PatientRecord {
int patientID;
Name patName;
Address patAddr;
public PatientRecord(int patientID, Name patName, Address patAddr) {
this.patientID = patientID;
this.patName = patName;
this.patAddr = patAddr;
}
public PatientRecord() {
}
public String toString() {
return "PatientRecord: patientID:\t"+patientID+"\tpatName:"+patName+"\tpatAddr:"+patAddr+"\n";
}
}
class Patients {
int numPatients;
PatientRecord[] patRecords;
public Patients(int numPatients, PatientRecord[] patRecords) {
this.numPatients = numPatients;
this.patRecords = patRecords;
}
public Patients() {
}
public String toString() {
String arr="";
for (int i = 0; i < patRecords.length; i++) {
arr+="patRecords["+(i+1)+"]"+patRecords[i];
}
return "Patients:\nnumPatients:"+numPatients+"\npatRecords:\n"+arr+"\n";
}
}
class Hospital {
String hospitalName;
Address hospitalAddr;
Patients patientRecs;
void readIn(){
System.out.println("hospitalName:"+hospitalName+"\nhospitalAddr:"+ hospitalAddr+patientRecs);
}
public Hospital(String hospitalName, Address hospitalAddr,
Patients patientRecs) {
this.hospitalName = hospitalName;
this.hospitalAddr = hospitalAddr;
this.patientRecs = patientRecs;
}
public Hospital() {
}
}
public class Test {
public static void main(String[] args) {
Hospital hosp = new Hospital(
"hospital1",new Address(100,"streethos","sh","200000"),
new Patients(20,
new PatientRecord[]{
new PatientRecord(1,new Name("zhang","san"),new Address(10,"street1","sh","200001")),
new PatientRecord(1,new Name("li","si"),new Address(20,"street2","bj","200002"))
}
)
);
hosp.readIn();
// System.out.printf("hosp=%s", hosp.toString());
}// main
}
下面是所有代码
class Name {
String lastname;
String firstname;
public Name(String lastname, String firstname) {
this.lastname = lastname;
this.firstname = firstname;
}
public Name() {
}
public String toString() {
return "Name: lastname:\t"+lastname+"\tfirstname:"+firstname+"\n";
}
}
class Address {
int stnum;
String street;
String city;
String postalCode;
public Address(int stnum, String street, String city, String postalCode) {
this.stnum = stnum;
this.street = street;
this.city = city;
this.postalCode = postalCode;
}
public Address() {
}
public String toString() {
return "Address: stnum:\t"+stnum+"\tstreet:"+street+"\tcity:"+city+"\tpostalCode:"+postalCode+"\n";
}
}
class PatientRecord {
int patientID;
Name patName;
Address patAddr;
public PatientRecord(int patientID, Name patName, Address patAddr) {
this.patientID = patientID;
this.patName = patName;
this.patAddr = patAddr;
}
public PatientRecord() {
}
public String toString() {
return "PatientRecord: patientID:\t"+patientID+"\tpatName:"+patName+"\tpatAddr:"+patAddr+"\n";
}
}
class Patients {
int numPatients;
PatientRecord[] patRecords;
public Patients(int numPatients, PatientRecord[] patRecords) {
this.numPatients = numPatients;
this.patRecords = patRecords;
}
public Patients() {
}
public String toString() {
String arr="";
for (int i = 0; i < patRecords.length; i++) {
arr+="patRecords["+(i+1)+"]"+patRecords[i];
}
return "Patients:\nnumPatients:"+numPatients+"\npatRecords:\n"+arr+"\n";
}
}
class Hospital {
String hospitalName;
Address hospitalAddr;
Patients patientRecs;
void readIn(){
System.out.println("hospitalName:"+hospitalName+"\nhospitalAddr:"+ hospitalAddr+patientRecs);
}
public Hospital(String hospitalName, Address hospitalAddr,
Patients patientRecs) {
this.hospitalName = hospitalName;
this.hospitalAddr = hospitalAddr;
this.patientRecs = patientRecs;
}
public Hospital() {
}
}
public class Test {
public static void main(String[] args) {
Hospital hosp = new Hospital(
"hospital1",new Address(100,"streethos","sh","200000"),
new Patients(20,
new PatientRecord[]{
new PatientRecord(1,new Name("zhang","san"),new Address(10,"street1","sh","200001")),
new PatientRecord(1,new Name("li","si"),new Address(20,"street2","bj","200002"))
}
)
);
hosp.readIn();
// System.out.printf("hosp=%s", hosp.toString());
}// main
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询