Results 1 to 18 of 18
  1. #1
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by NewbieJavie View Post
    But the problem is that I don't know what to do with the methods getStatus and displayStatus in the classes.

    The instructions are quite clear in this regard:

    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.


    I would just follow each line of the instructions, step by step.

  3. #3
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    I would but I don't know how to =/

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you understand how to do getString overriding for a class? If you do, you can do this, if you don't I'll show you an example of a getString implementation
    Java Code:
    public String getString(){
      return "when this method is called it returns this string";
    }

  5. #5
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Well I don't understand the use of getStatus and displayStatus and how to write the code for them, and my input data are a mix of string and int so how would I store their reference into an array?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are creating an array of status objects, therefore, anything that implements the interface is a type of that interface. This is the power of polymorphism. Each class that implements the interface must also have it's own version of each method in the interface. In this situation, all the classes will have a getStatus that is unique to the class.

  7. #7
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    You are creating an array of status objects, therefore, anything that implements the interface is a type of that interface. This is the power of polymorphism. Each class that implements the interface must also have it's own version of each method in the interface. In this situation, all the classes will have a getStatus that is unique to the class.
    I see, could you help me with the getStatus of the Rectangle Class? Also what would be the type of the status object array? How can I initialize it?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How would you create an array of ints or objects? I wont give you the method since it's nearly identical for the other classes, try coming up with something yourself and post your attempt. I will let you know if what you come up with is close. Also, the input you has is used to create an object via a constructor you create.

  9. #9
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Well this is what I wrote for the Rectangle class's getStatus and displayStatus:

    public int getLength( ){
    return length;
    }
    public int getWidth( ){
    return width;
    }
    public String getStatus() {
    String out, name;
    name = "Rectangle";
    out = name + "\n" + "Length: " + getLength() + "Width: " + getWidth() + "\n";
    return out;
    }
    public void displayStatus() {
    System.out.println(getStatus());

    }

    does it look ok?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That is perfect, although it can be shortened. You don't need intermediary local string variables as you have. You can also access instance variables directly from inside the class, method calls are not needed.
    Java Code:
    public String getStatus(){
      return "Rectangle\nLength = " + length "\nWidth = " + width;
    }
    Will work as well.

  11. #11
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Ok I rewrote the main with fewer things just to test it:


    import javax.swing.*;
    public class testStatus {

    public static void main(String[] args) {

    String in, out, name;
    int id, examCount;
    int[] examScores;
    int age, weight;
    int width, length;

    Status reference[] = new Status[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;
    reference[0] = new Rectangle (length, width);

    for (int index=0; index<reference.length; index++)
    {
    reference[index].displayStatus ( );
    }
    String out1 = "";
    for (int index=0; index<reference.length; index++)
    {
    out1 = out1 + reference[index].getStatus ( );
    }
    JOptionPane.showMessageDialog (null, out1);
    }

    }

    But I get the error below(It displays what I want):

    Rectangle
    Length: 3 Width: 2

    Exception in thread "main" java.lang.NullPointerException
    at testStatus.main(testStatus.java:24)

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The error should go away when you have filled the entire array, try filling the remaining items with rectangles with 0 0 as values.

  13. #13
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Nevermind I solve the problem ^^
    Thank you very much!! your explanations helped me alot with this program.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I am glad to have helped. If you are satisfied with this tread, please mark it solved with the thread tools at the top. However, if you are not done asking questions, please feel free. From here, most of the assignment is just simple repetition with minor twists.

  15. #15
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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() {
    String out,name = "Student";
    int []d = new int[scores.length];
    System.arraycopy(scores, 0, d, 0, scores.length);
    out = name + "\n" + "Id: " + getId() + " " + "Name: " + getName() + " " + "Scores: " + d ;
    return out;
    }
    public void displayStatus() {
    System.out.println(getStatus());
    }
    }

    Here I am having printing the array of scores, what I get in the msg window is:
    Student
    Id: 1 Name: a Scores: [I@67071c84

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That stuff(which I like to call gobbledegook) is what happens when you attempt to directly print an array, or any other object that hasn't overridden toString. That is the object superclasses toString method

    Java Code:
    ClassName@afad24426
    I think the actual code looks like this
    Java Code:
    public String toString(){
      return getClassName() + "@" + toHexString(hashCode());
    }
    I may have messed up a bit, but it's similar to that.

    To print an array or add it to a string you need to loop through and print or concatenate to a string(or StringBuilder)

  17. #17
    NewbieJavie is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Thank you!!!, I think I am finally done with this program xD

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome and I hope you have learned a few things in the process of completing this assignment.

Similar Threads

  1. Interface in Java
    By JAVA03 in forum New To Java
    Replies: 2
    Last Post: 03-03-2011, 08:22 AM
  2. Java app without interface
    By fawzan in forum Advanced Java
    Replies: 9
    Last Post: 12-29-2010, 05:07 PM
  3. Replies: 1
    Last Post: 05-31-2009, 11:57 PM
  4. Replies: 1
    Last Post: 05-31-2009, 03:17 PM
  5. Replies: 16
    Last Post: 05-28-2009, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •