Help with Java Interface.
I have to write a program that displays the values that the user inputs.
DESCRIPTION
Write a program that takes objects of different classes and provide them the ability to be treated as objects of a common type. Do this for objects of the following three classes:
Rectangle
Sibling
Student
For accomplishing the above, do the following:
Create an interface Status
Implement the interface Status in each of the above three classes.
class Rectangle
Create a class Rectangle. (Use this class from a previous exercise).
class Sibling
Create a class Sibling. (Use this class from a previous exercise).
class Student
Create a class Student. (Use this class from a previous exercise).
Modifications To The Above Classes
In each of the above classes, implement the interface Status. For this purpose, do the following:
· In the class header, add the phrase: implements Status
· Add the method getStatus ( ) in the body of the class.
This method returns the status as a String. The string returned includes:
The name of the class on one line.
The name of all instance variables along with their values on a second line.
For example, for an object of class Rectangle of length 3 and width 2, the String returned should be as below:
“Rectangle\nLength=3, Width=2\n”
· Add the method displayStatus ( ) in the body of the class.
This method displays the status including the following:
The name of the class on one line.
The name of all instance variables along with their values on a second line.
For example, for an object of class Rectangle of length 3 and width 2, output should be as below:
Rectangle
Length=3, Width=2
For implementing the method displayStatus, you can have this method call the method getStatus and then display the String returned by that method.
Input Data:
Data for three Rectangle objects:
3 2
6 4
30 20
Data for two Sibling objects:
Jack 21 130
Judy 24 118
Data for one Student Object:
1,John Adam,3,93,91,100
Output Data:
The output should show the status of each of the object one after the other as below.
Rectangle
length=3, width=2
Rectangle
length=6, width=4
Rectangle
length=30, width=20
Sibling
name=Jack, age=21, weight=130
Sibling
name=Judy, age=24, weight=118
Student
name=John Adam, scores=93, 91, 100
class TestStatus
Create a class TestStatus containing the method main ( ). The main method should provide the following:
· Create an array of size 6 for storing Status references. (Compiler allows you to create such an array).This array will be populated with references of different objects later.
· Create three Rectangle objects, initialize them with data values provided by the user and store their references in the first 3 elements of the Status array. (You can do this because the class Rectangle implements the interface Status and thus a Rectangle object is both of type Rectangle and Status).
· Create two Sibling objects, initialize them with data values provided by the user and store their references in the next two elements of the Status array. (You can do this because the class Sibling implements the interface Status and thus a Sibling object is both of type Sibling and Status).
· Create one Student objects, initialize it with data values provided by the user and store its reference in the next one element of the Status array. (You can do this because the class Student implements the interface Status and thus a Student object is both of type Student and Status).
· Set up a loop to iterate through the array of Status references. (Each reference in the Status array will point to an object).
During each pass through the loop,
Call the method displayStatus ( ) to display status of each object.
· Set up a second loop to iterate through the array of Status references. (Each reference in the Status array will point to an object).
During each pass through the loop,
Call the method getStatus ( ) to get the Status of each object as a String, and accumulate the output into a String.
· On exit from the loop, display the accumulated output.
ALGORITHMS
Implementing Interface Methods
In each class, first implement the method getStatus that returns the status as a String.
Then implement the method displayStatus. From within the method displayStatus, call the method getStatus and display the string returned by it.
CODING
The sample code below can be used in main method.
First Polymorphic Loop
//Below assume that status is an array of Status references.
//Call displayStatus ( ) in a loop as shown below:
for (int index=0; index<status.length; index++)
{
status[index].displayStatus ( );
}
Second Polymorphic Loop
//Below assume that status is an array of Status references.
//Call getStatus ( ) in a loop and accumulate output as shown below.
//After exiting from the loop, display the accumulated output.
String out = “”;
for (int index=0; index<status.length; index++)
{
out = out + status[index].getStatus ( );
}
JOptionPane.showMessageDialog (null, out);
What I have so far is:
My Classes
public class Rectangle implements Status{
//Instance variables (fields)
private int length;
private int width;
//Constructor
public Rectangle (int l, int w ){
length = l;
width = w;
}
public String getStatus() {
return null;
}
public void displayStatus() {
}
}
public class Student implements Status{
private int id;
private String name;
private int [] scores;
public Student (int iden, String n, int[] s){
id=iden;
name = n;
scores = new int[s.length];
//Copy data in it
System.arraycopy(s, 0, scores, 0, s.length);
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getStatus() {
return null;
}
public void displayStatus() {
}
}
public class Sibling implements Status{
private String name;
private int age;
private int weight;
public Sibling (String n, int a, int w){
name = n;
age = a;
weight = w;
}
public String getName ( ){
return name;
}
public int getAge ( ){
return age;
}
public int getWeight ( ){
return weight;
}
public String getStatus() {
return null;
}
public void displayStatus() {
}
}
My Interface:
public interface Status {
public String getStatus();
public void displayStatus();
}
My main:
import javax.swing.JOptionPane;
public class testStatus {
public static void main(String[] args) {
String in, out, name;
int id, examCount;
int[] examScores;
int age, weight;
int width, length;
String[] statusRef;
statusRef = new String [5];
in = JOptionPane.showInputDialog("Enter the length of the rectangle: ");
length = Integer.parseInt(in);
in = JOptionPane.showInputDialog("Enter the width of the rectangle: ");
width = Integer.parseInt(in);
Rectangle r1;
r1 = new Rectangle (length, width);
in = JOptionPane.showInputDialog("Enter the length of the rectangle: ");
length = Integer.parseInt(in);
in = JOptionPane.showInputDialog("Enter the width of the rectangle: ");
width = Integer.parseInt(in);
Rectangle r2;
r2 = new Rectangle (length, width);
in = JOptionPane.showInputDialog("Enter the length of the rectangle: ");
length = Integer.parseInt(in);
in = JOptionPane.showInputDialog("Enter the width of the rectangle: ");
width = Integer.parseInt(in);
Rectangle r3;
r3 = new Rectangle (length, width);
name=JOptionPane.showInputDialog("Input a name: ");
in=JOptionPane.showInputDialog("Input the age: ");
age=Integer.parseInt(in);
in=JOptionPane.showInputDialog("Input the weight: ");
weight=Integer.parseInt(in);
Sibling sib1;
sib1=new Sibling(name, age, weight);
name=JOptionPane.showInputDialog("Input a name: ");
in=JOptionPane.showInputDialog("Input the age: ");
age=Integer.parseInt(in);
in=JOptionPane.showInputDialog("Input the weight: ");
weight=Integer.parseInt(in);
Sibling sib2;
sib2=new Sibling(name, age, weight);
Student[] st= new Student[0];
in = JOptionPane.showInputDialog("Enter student id: ");
id = Integer.parseInt(in);
name = JOptionPane.showInputDialog("Enter student name: ");
in = JOptionPane.showInputDialog("Enter number of exams taken: ");
examCount = Integer.parseInt(in);
//Create examScore array
examScores = new int[examCount];
//Populate the examScores array
for (int j=0; j<examScores.length; j++){
in = JOptionPane.showInputDialog("Enter the exam score: ");
examScores[j] = Integer.parseInt(in);
}
}
}
But the problem is that I don't know what to do with the methods getStatus and displayStatus in the classes. Also I don't know what to do in the main method to store the reference of each object into an array and then later call the methods to display them.