6 Replies - 6819 Views - Last Post: 23 June 2012 - 12:25 PM Rate Topic: -----

#1 CodingInBoise   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 16-June 12

Multiple classes, polymorphism, complete mess of code

Posted 21 June 2012 - 02:29 PM

I have been bumbling through this code for a while now, and I am sure I have taken it in the wrong direction more than once. This is a homework assignment, and I am in an online course that has a textbook that is not very helpful. I have the code separated out into 3 different classes, and will post it here in a minute.

In a previous post about this same problem, I had some particular questions, and I think that they have been answered. To see that topic, please go here: Inheritance and Polymorphism

Since I have fixed the majority of the initial problems from my previous post, I am now making a new post, with more accurate questions.

NOTE TO MODERATORS: If posting the actual homework question from the book like this is inappropriate, please remove it, and let me know how to post the information in a more appropriate manner.

Here is the question as it appears in the book:

Quote

In Chapter 6, the class Clock was designed to implement the time of day
in a program. Certain applications—in addition to storing hours, minutes,
and seconds—might require you to store the time zone. Derive the class
ExtClock from the class Clock by adding a data member to store the time
zone. Add the necessary methods and constructors to make the class functional.
Also, write the definitions of the methods and the constructors.
Finally, write a test program to test your class.


My understanding of this is that I need to take the source code for the class Clock (as shown below), and create a new class ExtClock that extends Clock. It should add functionality for having a timeZone as well as the hours, minutes, and seconds.

I am well aware of the fact that my code does not match up properly, but that is due to me experimenting with various things to see what will happen if I change them. I will post in three parts, the first one is the class Clock that I was provided as a starting point, the other two are my own invention, (with a little help from the wonderful people here on this forum). All three of these classes are in the same project folder and were created using NetBeans 6.8 (if it matters).

Here is my code:

public class Clock
{
    private int hr;  //store hours
    private int min; //store minutes
    private int sec; //store seconds

       //Default constructor
       //Postcondition: hr = 0; min = 0; sec = 0
    public Clock()
    {
        setTime(0, 0, 0);
    }

       //Constructor with parameters, to set the time
       //The time is set according to the parameters.
       //Postcondition: hr = hours; min = minutes;
       //               sec = seconds
    public Clock(int hours, int minutes, int seconds)
    {
        setTime(hours, minutes, seconds);
    }

       //Method to set the time
       //The time is set according to the parameters.
       //Postcondition: hr = hours; min = minutes;
       //               sec = seconds
    public void setTime(int hours, int minutes, int seconds)
    {
        if (0 <= hours && hours < 24)
            hr = hours;
        else
            hr = 0;

        if (0 <= minutes && minutes < 60)
            min = minutes;
        else
            min = 0;

        if (0 <= seconds && seconds < 60)
            sec = seconds;
        else
            sec = 0;
    } //end setTime

       //Method to set the hours
       //The hours are set according to the parameters.
       //Postcondition: hr = hours;
    public void setHours(int hours)
    {
        if (0 <= hours && hours < 24)
            hr = hours;
        else
            hr = 0;
    } //end setHours

       //Method to set the minutes
       //The minutes are set according to the parameters.
       //Postcondition: min = minutes;
    public void setMinutes(int minutes)
    {
        if (0 <= minutes && minutes < 60)
            min = minutes;
        else
            min = 0;
    } //end setMinutes

       //Method to set the seconds
       //The seconds are set according to the parameters.
       //Postcondition: sec = seconds;
    public void setSeconds(int seconds)
    {
        if (0 <= seconds && seconds < 60)
            sec = seconds;
        else
            sec = 0;
    } //end setSeconds

       //Method to return the hours
       //Postcondition: the value of hr is returned
    public int getHours()
    {
        return hr;
    } //end getHours

       //Method to return the minutes
       //Postcondition: the value of min is returned
    public int getMinutes()
    {
        return min;
    } //end getMinutes

       //Method to return the seconds
       //Postcondition: the value of sec is returned
    public int getSeconds()
    {
        return sec;
    } //end getSeconds

       //Method to increment the time by one second
       //Postcondition: The time is incremented by one second
       //If the before-increment time is 23:59:59, the time
       //is reset to 00:00:00
    public void incrementSeconds()
    {
        sec++;

        if (sec > 59)
        {
            sec = 0;
            incrementMinutes(); //increment minutes
        }
    } //end incrementSeconds

       //Method to increment the time by one minute
       //Postcondition: The time is incremented by one minute
       //If the before-increment time is 23:59:53, the time
       //is reset to 00:00:53
    public void incrementMinutes()
    {
        min++;

        if (min > 59)
        {
            min = 0;
            incrementHours(); //increment hours
        }
    } //end incrementMinutes

       //Method to increment the time by one hour
       //Postcondition: The time is incremented by one hour
       //If the before-increment time is 23:45:53, the time
       //is reset to 00:45:53
    public void incrementHours()
    {
        hr++;

        if (hr > 23)
            hr = 0;
    } //end incrementHours

       //Method to compare two times
       //Postcondition: Returns true if this time is equal to
       //               otherClock; otherwise returns false
    public boolean equals(Clock otherClock)
    {
         return (hr == otherClock.hr
                && min == otherClock.min
                && sec == otherClock.sec);
    } //end equals

       //Method to return the time as a string.
       //Postcondition: Time is returned as a string in the form
       //               hh:mm:ss
    public String toString()
    {
        String str = "";

        if (hr < 10)
             str = "0";
        str = str + hr + ":";

        if (min < 10)
             str = str + "0" ;
        str = str + min + ":";

        if (sec < 10)
             str = str + "0";
        str = str + sec;

        return str;
    } //end toString

       //Method to copy time
       //Postcondition: The instance variables of otherClock
       //               copied into the corresponding data
       //               are members of this time.
       //               hr = otherClock.hr;
       //               min = otherClock.min;
       //               sec = otherClock.sec;
    public void makeCopy(Clock otherClock)
    {
        hr = otherClock.hr;
        min = otherClock.min;
        sec = otherClock.sec;
    } //end makeCopy

       //Method to return a copy of time
       //Postcondition: A copy of the object is created and
       //               a reference of the copy is returned
    public Clock getCopy()
    {
        Clock temp = new Clock(hr, min, sec);

        return temp;
    } //end getCopy
}//end of class



public class ExtClock extends Clock
{
    //declare private variables
    private String timeZone;

    public ExtClock()
    {
        setTime(0, 0, 0, "unknown");
    }
    public ExtClock(int hours, int minutes, int seconds, String zone)
    {
        super();
        timeZone = zone;
    }
    public void setTime(int hours, int minutes, int seconds, String zone)
    {
        timeZone = zone;
    }
    public void setTimeZone(String zone)
    {
        timeZone = zone;
    }
    //create public method to get the variable timeZone
    public String getTimeZone()
    {
        return timeZone;
    }
    //override method for reading out the complete time to include time zone?
    @Override
    public String toString()
    {
        return (super.toString() + " " + timeZone);
    } //end toString
}//end of class



public class Main
{
    public static void main(String[] args)
    {
        //instantiate a clock here?
        ExtClock clockOne = new ExtClock();
        System.out.println(ExtClock.class.toString());//really not sure about this one
        // ? ? ? call constructor here ? ? ?
    }//end of main
}//end of class



What I am asking for:

1: Any logic errors, and a nudge in the right direction on how to fix them.
2: Anything that is outright missing, and a suggestion on the logic of what needs to be put there, (not necessarily code).
3: Anything I did particularly well, and why it is good.
4: Any other comments or suggestions you might have for me.

Thank you in advance for your time!

This post has been edited by CodingInBoise: 21 June 2012 - 02:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple classes, polymorphism, complete mess of code

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Multiple classes, polymorphism, complete mess of code

Posted 21 June 2012 - 02:59 PM

Initial thoughts:

Think of a better name for your "test program" class than "Main". Consider "TestClock", "TestExtClock", or similar. Give your classes names that describe what they do.

The purpose of your Main class is to thoroughly test ExtClock. At a minimum, create an instance to exercise each constructor, then use an instance of ExtClock to test each of its methods.

The purpose of a toString() method is to provide a String representation of the object when needed, like in a println statement. As such, your statement,

System.out.println(ExtClock.class.toString())

is unnecessary. You would instead write something like:

System.out.println( clockOne );

Try that and see what happens.

One of the purposes of using the parent class' constructor is to take advantage of the code that's already been written there (among many others). To that end, your constructor,

    public ExtClock(int hours, int minutes, int seconds, String zone)
    {
        super();
        timeZone = zone;
    }

should be expanded to:
    public ExtClock(int hours, int minutes, int seconds, String zone)
    {
        super( hours, minutes, seconds );
        timeZone = zone;
    }

Rename your Main class, create a couple instances of an ExtClock, and then expand it to include the code necessary to thoroughly test the methods in ExtClock.

Come back when you have more questions. I will add that something I think you did particularly well is interpret the assignment and follow the directions it provided (up to writing the test class), and I don't think it's the bumbling mass of code you represented it to be. Boringly competent, actually, but with room for improvement like everything I write.
Was This Post Helpful? 1
  • +
  • -

#3 CodingInBoise   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 16-June 12

Re: Multiple classes, polymorphism, complete mess of code

Posted 21 June 2012 - 04:38 PM

Thank you! Your response was very helpful!

My code is greatly improved by making the changes you have suggested, but it still needs some more work. The class Clock has not changed, so I will not re-post it. My concerns are with the following:

1: I think my public void setTimeZone(String zone) needs more code
2: I am not quite sure how to go about testing the methods for the objects

The following is the code for my ExtClock class, followed by the code for my TestExtClock class:

public class ExtClock extends Clock
{
    //declare private variables
    private String timeZone;

    public ExtClock()
    {
        setTime(0, 0, 0, "unknown");
    }
    public ExtClock(int hours, int minutes, int seconds, String zone)
    {
        super(hours, minutes, seconds);
        timeZone = zone;
    }

	// NOTE: the method below needs more code, I think...
    public void setTime(int hours, int minutes, int seconds, String zone)
    {
        // 	I think something should go here to represent
		//		the hours, minutes, and seconds...
		timeZone = zone;
    }
    // NOTE: the method above needs more code, I think...

    public void setTimeZone(String zone)
    {
        timeZone = zone;
    }
    //create public method to get the variable timeZone
    public String getTimeZone()
    {
        return timeZone;
    }
    //override method for reading out the complete time to include time zone?
    @Override
    public String toString()
    {
        return (super.toString() + " " + timeZone);
    }
}//end of class


public class TestExtClock
{
    public static void main(String[] args)
    {
        //instantiate a few ExtClock objects
        ExtClock clockOne = new ExtClock(7, 30, 22, "Mountain");
        ExtClock clockTwo = new ExtClock();//tests default constructor
        ExtClock clockThree = new ExtClock(14, 7, 35, "Central");
        //test methods on ExtClock objects ?
        
        //print out the values of the ExtClock objects
        System.out.println(clockOne);
        System.out.println(clockTwo);
        System.out.println(clockThree);
    }//end of main
}//end of class


As always, I appreciate anyone who spends their time helping me!
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Multiple classes, polymorphism, complete mess of code

Posted 21 June 2012 - 05:23 PM

You haven't exercised the setTime(), setTimeZone(), or getTimeZone() methods in TestExtClock. You can check those methods by using setTime(), setTimeZone() ,and getTimeZone() on the 3 instances you created, describing the changes being made and then showing the instances before and after. Make challenging tests that require your program to react correctly to odd inputs.

This would be a great opportunity for you to be introduced to the use of a testing framework like JUnit, but if it's not being used in your course, you'll hopefully be introduced to the concept soon. IMO, the earlier it's required in a programmer's training, the better. Requiring the use of a testing framework early provides new programmers with a different perspective on testing and a much better understanding of acceptable test coverage.

You've done a pretty good job of determining the functionality that must be added to ExtClock to provide the same functionality as in Clock, but give it another look. Does the ExtClock class need its own versions of the methods equals(), makeCopy(), and getCopy() to include the additional data member, timeZone? Remember to add tests for any methods you add.

For your comments that ask questions:

ExtClock.setTime(): Yes, setting the time should include changes to hours, minutes, and seconds. The super class method can/should be used.

Also, don't forget that your assignment requires you to "write the definitions of the methods and the constructors." I'm not sure what's expected of you, but it doesn't appear that you've done that yet. I suggest you use the Clock class as a guide unless you've been given specific instructions.
Was This Post Helpful? 2
  • +
  • -

#5 CodingInBoise   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 16-June 12

Re: Multiple classes, polymorphism, complete mess of code

Posted 22 June 2012 - 03:15 PM

Thank you! Your post was quite helpful!

ONLY for the purposes of this exercise, I will assume that the user will never enter incorrect data (I know how absurd it would be to actually assume that, but this exercise is meant to teach polymorphism, not error handling).

That said, I have implemented most of your suggestions, and the ones I have not put in my code are just going to have to be left out. The classes I am taking last only 4 weeks, and this assignment was due LAST weekend.

I will be using Recursion next, and will probably need some help before I am done. If the person who has been helping me in this post would like, I can send them a copy of my code. I don't want to post the completed version of my homework online for just anyone to see. Not because I care, but because my school might. Also, anyone who has moderator status, or expert in Java status, I would be happy to share my solution with you as well. Obviously anyone at that level is not going to be needing to take as basic of a course in Java as this one is.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Multiple classes, polymorphism, complete mess of code

Posted 22 June 2012 - 07:00 PM

public class ExtClock extends Clock
{
    //declare private variables
    private String timeZone;

    public ExtClock()
    {
        setTime(0, 0, 0, "unknown");
    }
    public ExtClock(int hours, int minutes, int seconds, String zone)
    {
        super(hours, minutes, seconds);
        timeZone = zone;
    }


should be
public class ExtClock extends Clock
{
    //declare private variables
    private String timeZone;

    public ExtClock()
    {
        this(0, 0, 0, "unknown");
    }
    public ExtClock(int hours, int minutes, int seconds, String zone)
    {
        super(hours, minutes, seconds);
        timeZone = zone;
    }


Was This Post Helpful? 0
  • +
  • -

#7 CodingInBoise   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 16-June 12

Re: Multiple classes, polymorphism, complete mess of code

Posted 23 June 2012 - 12:25 PM

Thank you for your reply!

I am a little unclear about why that would be better. I accept that you know way more about it than me, but why does my code still work with setTime(0, 0, 0, "unknown"); and why would your version this(0, 0, 0, "unknown"); work better/be more efficient?

Thank you for your time!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1