Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: First post, I have a question regarding this program

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default First post, I have a question regarding this program

    Hey all so I am new to this website and Java in general. I have some C++ experience and some BASIC but that's about it. Anyways, I am a CS major and I am a Java class that focuses heavily on OOP. We are using the IDE BlueJ (Not sure if you have heard of it, seems like an obscure only for learning IDE). Well, we have a really kind of dumb program that just displays a time in 12 hour format. In its current state, this program just displays it in in hour:minute format but our professor wants us to display it hour:minute:second format. This code is made in two different classes, one class creating objects out of the second class. I will post both classes and tell you what the issue is.

    public class ClockDisplay
    {
        private NumberDisplay hours;
        private NumberDisplay minutes;
        private String displayString;    // simulates the actual display
     
        /**
         * Constructor for ClockDisplay objects. This constructor 
         * creates a new clock set at 00:00.
         */
        public ClockDisplay()
        {
            hours = new NumberDisplay(24);
            minutes = new NumberDisplay(60);
            updateDisplay();
        }
     
        /**
         * Constructor for ClockDisplay objects. This constructor
         * creates a new clock set at the time specified by the 
         * parameters.
         */
        public ClockDisplay(int hour, int minute)
        {
            hours = new NumberDisplay(24);
            minutes = new NumberDisplay(60);
            setTime(hour, minute);
        }
     
        /**
         * This method should get called once every minute - it makes
         * the clock display go one minute forward.
         */
        public void timeTick()
        {
            minutes.increment();
            if(minutes.getValue() == 0) {  // it just rolled over!
                hours.increment();
            }
            updateDisplay();
        }
     
        /**
         * Set the time of the display to the specified hour and
         * minute.
         */
        public void setTime(int hour, int minute)
        {
            hours.setValue(hour);
            minutes.setValue(minute);
            updateDisplay();
        }
     
        /**
         * Return the current time of this display in the format HH:MM.
         */
        public String getTime()
        {
            return displayString;
        }
     
        /**
         * Update the internal string that represents the display.
         */
        private void updateDisplay()
        {
            displayString = hours.getDisplayValue() + ":" + 
                            minutes.getDisplayValue();
        }
    }

    public class NumberDisplay
    {
        private int limit;
        private int value;
     
        /**
         * Constructor for objects of class NumberDisplay.
         * Set the limit at which the display rolls over.
         */
        public NumberDisplay(int rollOverLimit)
        {
            limit = rollOverLimit;
            value = 0;
        }
     
        /**
         * Return the current value.
         */
        public int getValue()
        {
            return value;
        }
     
        /**
         * Return the display value (that is, the current value as a two-digit
         * String. If the value is less than ten, it will be padded with a leading
         * zero).
         */
        public String getDisplayValue()
        {
            if(value < 10) {
                return "0" + value;
            }
            else {
                return "" + value;
            }
        }
     
        /**
         * Set the value of the display to the new specified value. If the new
         * value is less than zero or over the limit, do nothing.
         */
        public void setValue(int replacementValue)
        {
            if((replacementValue >= 0) && (replacementValue < limit)) {
                value = replacementValue;
            }
        }
     
        /**
         * Increment the display value by one, rolling over to zero if the
         * limit is reached.
         */
        public void increment()
        {
            value = (value + 1) % limit;
        }
    }


    Well, as you see the program does what I described (if using BlueJ, I have no idea what this will do on other IDEs). Now this is the original program. I think I got it to work earlier using seconds and having it increment properly and what not, BUT the tricky part is our professor wants us to get rid of the whole displayString field. I have yet to figure out a way to properly display the time without that field. That field is involved in a few methods for formatting display and what not and I can't figure out a work around here. Now I'm not asking you to do my homework, I would be upset if you did actually. I love learning and I love figuring out problems, but I'm stumped here. So, if you guys/gals could at least give me some pointers and get me moving in the right direction that would be great! And thanks ahead of time for the help, and sorry for the long post, but I like to be descriptive!

    Looking forward to being part of this community


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: First post, I have a question regarding this program

    our professor wants us to get rid of the whole displayString field.
    Where do you currently display the time? How does the prof want the time to be displayed?

    How do you execute this code? Where is the main method and the calls to methods in your code?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: First post, I have a question regarding this program

    Well, this is where I think this code is BlueJ specific (I really hate using this bluej, seems like a waste of time but its what I gotta do). In BlueJ, this is no main method, it just runs off of classes that you make and each class can call other classes and you know. There might be some embedded main method in there but to my knowledge you can't access it. Now that we got that out of the way, in BlueJ, when you have a method that returns something, that value is just returned in a window that bluej creates. But, without using a String value to format it, I can no longer do that so I thought maybe I could put it in the terminal window. I tried - System.out.println( hours + ":" + minutes +":" + seconds); but that gave me build errors. I am very new to java so I'm just applying my knowledge of other languages to tried to figure this out but it is not working.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: First post, I have a question regarding this program

    Sorry, I know nothing about bluej.
    I'm used to working with the javac compiler and the java program to execute java programs.
    The javac compiler creates one or more class files, one of which has a main method.
    The java command is used to execute the class file with the main method.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: First post, I have a question regarding this program

    I would much rather be working with that lol, bluej seems like a joke

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: First post, I have a question regarding this program

    Perhaps the professor knows that you can't copy and paste code onto a forum when you use bluej, to keep students from getting too much help on a forum.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: First post, I have a question regarding this program

    I think asking more experience people for help is a big part of learning, but copy and paste isnt. She shouldn't really care though, I dont think many people choose the major so they can cheat their way through and not learn anything, I doubt they'd even pass the class that way

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: First post, I have a question regarding this program

    I guess you need to ask the prof why you are required to use bluej.

Similar Threads

  1. Question About A Program
    By torres9 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 15th, 2011, 08:56 PM
  2. java applet program question (on getting path fails)
    By hellocheese in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2011, 04:34 PM
  3. Question about writing a phonebook sorting program
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: March 25th, 2011, 09:25 AM
  4. Question about my coin toss program
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2010, 04:14 AM
  5. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM