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 7 of 7

Thread: Improper Way of Achieving Output?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Improper Way of Achieving Output?

    Hey everyone,

    Quick Question Posed to ME



    ----------------------------------------------------------------------------------------------------------------------


    The question:

    Create a Television store that can hold three Television objects in an
    array. Use the Television class below:

    class Television
    {
    boolean isOn;
    } // end class Television

    Use a for loop to print the isOn instance variable for each Television in the
    TelevisionStore. Use a second for loop to change the isOn instance variable for
    each Television to “true”. Finally, use a third for loop to print the isOn instance
    variable for each Television in the TelevisionStore array.

    Sample output:

    Openening the tv store for the day... tv status:
    Television 0 is on? False.
    Television 1 is on? False.
    Television 2 is on? False.

    Turning the tv's on...
    Television 0 is on? True.
    Television 1 is on? True.
    Television 2 is on? True.






    ----------------------------------------------------------------------------------------------------------------------

    So I made a program that does create this output, with this code + the Television Class I was supposed to create:





    public class TelevisionDriver
        {
        public static void main( String[] args )
            {
            String[] Television = { "Television 0", "Television 1", "Television 2", "Television 0", "Television 1", "Television 2" };
            String[] Boolean = { "False", "False", "False", "True", "True", "True"};
     
     
            System.out.println( "Openening the tv store for the day... tv status:");
            for( int x = 0; x < 3; x++ )
                {
                System.out.println( Television[x] + " is on? " + Boolean[x] + "." );
     
                }
            System.out.println( "\n" + "Turning the tv's on..." );
            for( int y = 3; y < 6; y++)
                {
     
                System.out.println( Television[y] + " is on? " + Boolean[y] + "." );
                }
            }
        }






    ----------------------------------------------------------------------------------------------------------------------

    Is this the proper way to do it? I think I was supposed to do something different, even though the output is correct.


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Improper Way of Achieving Output?

    I don't believe that is what your instructor will want to see.

    Is that the whole question? Does it mention any thing about creating a constructor for the television class? Does it ask you to create some form of getter or setter for the instance variable isOn (yep). Can you override the toString method?

    You shouldn't be making an array of Strings. How do you create a new object?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Improper Way of Achieving Output?

    No, this is not the correct way. There was a purpose in asking you to create the Television class, it's a baby step to understanding Object Oriented Programming, or OOP. You will not do well if you turn in the above program.

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Improper Way of Achieving Output?

    Well he did create some Objects just the wrong kind(Strings). The assignment does not call for an array of Strings but for what? I am guessing the instructor provided you with that class. Now when you create an object normally you have getters and setters as mentioned in post #2 which is the correct way of doing things but in this instance on how it is setup it seems you won't really need them.

    If you have any questions or are getting errors come back and ask for help. Make sure to include the full details of the error you are receiving.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Improper Way of Achieving Output?

    Thank you. Would this be proper then? Just making sure I am approaching this right

     
    public class TelevisionDriver
        {
        public static void main( String[] args )
            {
             boolean isOn[] = new boolean[3];
             isOn[0] = false;
             isOn[1] = false;
             isOn[2] = false;
     
             System.out.println( "Opening the tv store for the day... tv status:" + "\n" );
     
             for( int x = 0; x < 3; x++ )
                {
                System.out.println( "Television" + x + " on? " + isOn[x] );
                }
     
             System.out.println( "\n" + "Turning the tv's on..." + "\n" );
     
             for( int y = 0; y < 3; y++ )
                {
                isOn[0] = true;
                isOn[1] = true;
                isOn[2] = true;
     
                System.out.println( "Television" + y + " on? " + isOn[y] );
                }
            }
        }



    Keep in mind, I have only been using java for a week or two VERY lightly. I am not educated in advanced mechanics. I appreciate all constructive, but positive responses.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Improper Way of Achieving Output?

    Im afraid you do not yet get the concept. Take some time and read through this tutorial, about 20 pages worth. (It is large print, it should not take long)
    Hopefully you will see the way an object saves it's own state independent of any other object, and that saving the values the way done in the two previous code samples is not the way to go. Each object should be able to tell you if it is on or off, and not have a permanent value hard coded in somewhere.

  7. #7
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Improper Way of Achieving Output?

    No you need to create TV objects. That class is provided to you by the instructor and you are not using it at all. I'm not sure if it's provided to you as a separate file or he/she expects you to use it in your TelevisionDriver file. Notice that the status of the TV is a property of the TV itself, it is the class level variable called isOn. You will access this property by referring to an instance of each TV you create.

    Start your code by creating these three objects very similarly to how you've created your booleans. Then think about how you will get their isOn status.

    EDIT: Follow the link jps gave you. It's perfect.

Similar Threads

  1. how do I get this output?
    By my21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 13th, 2013, 09:23 PM
  2. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  3. output did not appear
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 27th, 2011, 06:33 AM
  4. Achieving a LineBorder that is also padded
    By caesius in forum AWT / Java Swing
    Replies: 2
    Last Post: April 15th, 2011, 08:37 AM
  5. [SOLVED] Can't get output HELP
    By moodycrab3 in forum Object Oriented Programming
    Replies: 8
    Last Post: February 7th, 2011, 03:07 AM