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

Thread: java question on objects

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

    Default java question on objects

    /**
     * This class represents a television, which can be turned on and off,
     * change channels, and change volume
     *
     * @author 
     */
    class Television
    {
    private int channel;
    private int volume;
    private boolean power;
     
    // Constructor
     
    	/**
    	 * Creates a new Television
    	 */
    	public Television(int channel, int volume)
    	{
    		channel=0;
    		volume=0;
     
    	}
     
     
    // Instance Methods
     
    	/**
    	 * Turn the TV on or off, to whatever it wasn't before
    	 */
    	public void switchOnOff(String power)
    	{
    		power = on;
    	}
     
     
    	/**
    	 * Change the channel to the given channel number;
    	 * If the new channel is not between 1 and 5 inclusive, then nothing happens
    	 */
    	public void changeChannel(int channel)
    	{
    		if(channel>=1 && channel<=5)
    		;
    	}
     
     
    	/**
    	 * Increase the volume level by 1;
    	 * If the tv is already at maximum volume, nothing happens
    	 */
    	public void increaseVolume(int volume)
    	{
    		volume++;
    	}
     
     
    	/**
    	 * Decrease the volume level by 1;
    	 * If the tv is already at minimum volume, nothing happens
    	 */
    	public void decreaseVolume(int volume)
    	{
    		volume++;
    	}
     
     
    // Accessor methods
     
    	/**
    	 * Returns true if the TV is on,
    	 * else, returns false
    	 */
    	public boolean isOn()
    	{
    		// ### REPLACE THE NEXT LINE OF CODE
    		return false;
    	}
     
     
    	public int getChannel()
    	{
    		// ### REPLACE THE NEXT LINE OF CODE
    		return -1;		
    	}
     
     
    	public int getVolume()
    	{
    		// ### REPLACE THE NEXT LINE OF CODE
    		return -1;		
    	}
    }
    can someone help me finish this code.....................


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: java question on objects

    Hello joe98, welcome to the forums.

    How can we help you finish this code when you have given us no indication of what is left to be finished?
    When posting in forums like this, you need to give as much information as possible in order to guarantee quality replies.

    Let us know what you are stuck on and we can take it from there.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: java question on objects

    retun power ;
    return channel ;
    return volume ;
    respectively.

    /**
     * This class represents a television, which can be turned on and off,
     * change channels, and change volume
     *
     * @author 
     */
    class Television
    {
    private int channel;
    private int volume;
    private boolean power;
     
    // Constructor
     
    	/**
    	 * Creates a new Television
    	 */
    	public Television(int channel, int volume)
    	{
    		channel=0;
    		volume=0;
                    power = false ;  //power shud be off initially
     
    	}
     
     
    // Instance Methods
     
    	/**
    	 * Turn the TV on or off, to whatever it wasn't before
    	 */
    	public void switchOnOff(Boolean power)
    	{
    		this.power = power ; //  this means this classes in particular 
                                                   //(The power var declared above
                                                   // the other power is argument to the function
    	}
     
     
    	/**
    	 * Change the channel to the given channel number;
    	 * If the new channel is not between 1 and 5 inclusive, then nothing happens
    	 */
    	public void changeChannel(int channel)
    	{
    		if(channel>=1 && channel<=5)
    		             this.channel = channel;
    	}
     
     
    	/**
    	 * Increase the volume level by 1;
    	 * If the tv is already at maximum volume, nothing happens
    	 */
    	public void increaseVolume(int volume)
    	{
    		this.volume++;
    	}
     
     
    	/**
    	 * Decrease the volume level by 1;
    	 * If the tv is already at minimum volume, nothing happens
    	 */
    	public void decreaseVolume(int volume)
    	{
    		this.volume--; //why the hell decrease volume should ++ ?? silly ?
    	}
     
     
    // Accessor methods
     
    	/**
    	 * Returns true if the TV is on,
    	 * else, returns false
    	 */
    	public boolean isOn()
    	{
     
    		return power ;
    	}
     
     
    	public int getChannel()
    	{
     
    		return channel;		
    	}
     
     
    	public int getVolume()
    	{
     
    		return volume;		
    	}
    }

    also note power is boolean so the argument of isOnOff() should not be boolean.

    If i am not wrong, u are asked to characterize a real life thing on OOP perspective typical CSE question, i faced them a lot in my last year. If i am wrong then plz 4give me.
    Last edited by dumb_terminal; April 12th, 2011 at 04:02 PM.

Similar Threads

  1. Java Sockets, Bytes / Objects
    By AdamD in forum Java Networking
    Replies: 1
    Last Post: January 31st, 2011, 03:25 PM
  2. Java Question
    By NiCKYmcd in forum Java Theory & Questions
    Replies: 1
    Last Post: August 25th, 2010, 08:47 AM
  3. Question for Java experts please
    By XElCaballoX in forum Java Theory & Questions
    Replies: 2
    Last Post: August 19th, 2010, 01:43 AM
  4. [SOLVED] java Reflection Question - I am lost.
    By prain in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2010, 02:43 PM
  5. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM