
Originally Posted by
alaseier
Please help me to solve this: (
Only three questions....
The questions given below are based on the lecture notes, and are intended to familiarize you with concepts of arrays, ArrayList and inheritance.
1. Arrays
Write a program that read in 16 values from the keyboard and store them into a two dimension array (myArray). The program then calculate the sum of every row elements and every column elements an put this sum into elements of two arrays (one for the rows – rowSum and the other for columns- colSum ) ,then the program print the three arrays.
What kind of array? char, int, long, String, double, short, float, class, etc?
int n ;
System.out.println("Enter the number of rows. ");
n = console.nextInt();
System.out.println("Enter the number of columns. ");
v = console.nextInt();
dataType myArray = new dataType[n][v];
Use the clone method to get others. To get sum of elements in rows:
sum = 0;
int col = 0;
row = // row number;
// this finds the sum of elements in row n;
for (col = 0; col < myArray[row].length; col++)
sum = sum + myArray[col][row];
// sum of each individual row
for (row = 0; row < myArray.length; row++)
{
sum = 0;
for (col = 0; col < myArray[row].length; col++)
sum = sum + myArray[row][col];
// each final sum value is one element
// add the sum values of each row to get array.
// sum of each individual column
int col2, row2, sum2;
col2 = 0;
row2 = 0;
sum2 = 0;
for (col 2= 0; col2< myArray[0].length; col2++)
{
sum 2= 0;
for (row2 = 0; row2 < myArray.length; row2 ++)
sum2 = sum2 + myArray[row2][col2];
// each final sum value is an element;
// add the sum values for all columns to get array.
}
2. ArrayList class
You are to write a simple names book program. A names book contains names of your friends. Store your names book as an ArrayList with entries contains strings that represent the names. Your program must tell you how many friends you have and which one of them has the longest name.
Use compareTo() perhaps.
Could you use a fileReader or something to get them in? If so, throw a FileNotFoundException at the end of the declaration of main.
3. Inheritance
Design and draw the UML class diagram of a set of classes that define the employees of a Hospital, the classes are:
HospitalPerson , Doctor and Nurse.
Where HospitalPerson is a supper class, Doctor and Nurse are subclasses.
Design your classes to satisfy the following requirements:
1. Each HospitalPerson has a name and an idNumber.
2. A Doctor has in addition a specialty attribute, while a Nurse has a
maxDutyHours attribute.
3. We want to have a special print (output information) as following:
a. The name and idNumber of a HospitalPerson works for the Hospital.
b. The specialty of a Doctor.
c. The maxDutyHours of a Nurse.
Sounds more like composition.
Inheritance is an "is a" relationship.
Composition is a "has a" relationship.
Nurse and Doctor are inherited from HospitalPerson yes, as Doctor is a HospitalPerson and Nurse is a Hospital Person.
Public class Doctor Extends HospitalPerson
{
public Doctor()
{
}
}
Also, if you're overriding a method, use method name of superclass like this.
super.methodOfSuperClass(methodsParameters);
It seems you could either have abstract methods, methods defined like so:
public abstract String getName();
public abstract String getIDNumber();
in class HospitalPerson and define them in Doctor and Nurse.....
or you could use composition and use a "has a relationship" for name and IDNumber.