Results 1 to 17 of 17
  1. #1
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default java question on classes & objects

    Hey guys, this is the question

    Write a Television class, which represents a television (TV). The television behaves as
    follows.
    At all times (including when the TV is switched off), the television remembers (knows):
    • Whether it is on or off
    • The channel number it is currently set to. The channel number is an integer
    between 1 and 5 inclusive.
    • The volume it is currently set to. This is an integer between 0 and 10 inclusive.
    Initially (the very first time the TV is created in the program) the TV is set to:
    • Off
    • Channel 1
    • Volume 5
    The following points are actions that can be performed on the TV. Each point below
    should be represented by just 1 instance method.
    • The TV can be turned on or off
    • The channel can be set to any channel between 1 and 5. Setting the channel to
    another integer causes no change to the television
    • The volume can be increased by 1. But if the volume is already at maximum, then
    no change occurs.
    • The volume can be decreased by 1. But if the volume is already at minimum, then
    no change occurs.
    Your Television class must have only 3 accessor methods for the following:
    • Whether it is on or off
    • The current channel number
    • The current volume level
    You should also test your Television class by using it in another class
    (TelevisionTester), just like in the previous questions. However, you should only submit
    the Television class.


    my code so far is

    /**
    * This class represents a television, which can be turned on and off,
    * change channels, and change volume
    *
    * @author
    */
    class Television
    {


    public Television()
    {

    }


    public void switchOnOff()
    {

    }


    public void changeChannel(int newChannel)
    {

    }


    /**
    * Increase the volume level by 1;
    * If the tv is already at maximum volume, nothing happens
    */
    public void increaseVolume()
    {

    }


    /**
    * Decrease the volume level by 1;
    * If the tv is already at minimum volume, nothing happens
    */
    public void decreaseVolume()
    {

    }


    // 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;
    }
    }



    cheers:)
    Last edited by joe98; 04-05-2011 at 09:05 AM.

  2. #2
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    hmmm...i think i need another class so i can test this

  3. #3
    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 haven't really done anything with the code. All the methods are designed right, they just need a body, try doing that and then post your code again. Please use code tags as well.

    [ code]
    CODE GOES HERE
    [/code]

    What are you stuck on if you can't do it? This is much easier than you may think.

    Don't worry about the tester class just yet. However the tested class will look sort of like this
    Java Code:
    public class Tester{
      public static void main(String[] args){
        //create tv
        //print all initial values
        //make changes
        //print changes
      }
    }

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    So you're asking us to help you complete a homework assignment? I don't see how you're supposed to learn if you're asking for someone else to complete your work.

    However a good idea that I like to do before I start a project is draw up a flow chart of what I want it to do. Which is specified in your assignment, and then from there I decide that I need a class to represent each function.

    It looks like your classes are set up to the minimum that is required, so why not start by writing it out and then if it doesn't work ask us for more help?

  5. #5
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    i will try finishin the code guys...thanks for d tips

  6. #6
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    this is my code so far...

    /**
    * 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 changeVolume;
    private int changeChannel;
    private int newChannel


    // Instance Variables




    /**
    * Creates a new Television
    */
    public Television(int channel)
    {
    channel=number;


    }


    // Instance Methods

    /**
    * Turn the TV on or off, to whatever it wasn't before
    */
    public void switchOnOff()
    {
    if

    }


    /**
    * 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 newChannel)
    {
    if (channel<1)
    return;
    if(channel>5)
    return;
    }


    /**
    * Increase the volume level by 1;
    * If the tv is already at maximum volume, nothing happens
    */
    public void increaseVolume(int max)
    {
    if (max=0)
    for(int max=0; max<=5; max++)

    }


    /**
    * Decrease the volume level by 1;
    * If the tv is already at minimum volume, nothing happens
    */
    public void decreaseVolume(int min, int max)
    {
    if (min<max)
    return;
    }


    // 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;
    }
    }

  7. #7
    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 getting closer. One tip I would like to suggest is to pay special attention to he assignment details, it tells you EVERYTHING you need to do. Including what the instance variables should be(hint: there should only be 3)

    view this link
    Need Serious Help on Odometer exercise
    I wrote a very long post which took someone through a large description of building a class.

    Also, I suggest you read your text book, it will contain lots of details.

    Another great link is the java tutorials
    The Really Big Index

    I am being extremely generous with the first link, it has the beginning steps of a TV, (forgot that was the example I used) it is however; quite different than what yours is, but it should give you a great stepping stone.
    Last edited by sunde887; 04-05-2011 at 09:44 AM.

  8. #8
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    so this is my code, i need help i guess...


    /**
    * 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 changeVolume;
    private int changeChannel;

    // Instance Variables




    // Constructor

    /**
    * Creates a new Television
    */
    public Television()
    {
    channel=0;
    volume=0;
    }


    // Instance Methods

    /**
    * Turn the TV on or off, to whatever it wasn't before
    */
    public void switchOnOff()
    {

    }


    /**
    * 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;
    }
    else
    {
    ;
    }
    return channel;
    }


    /**
    * Increase the volume level by 1;
    * If the tv is already at maximum volume, nothing happens
    */
    public void increaseVolume(int volume)
    {
    this.volume=volume;
    }



    public void decreaseVolume(int volume)
    {
    this.volume=volume;
    }



    /**
    * Returns true if the TV is on,
    * else, returns false
    */
    public boolean isOn()
    {
    // ### REPLACE THE NEXT LINE OF CODE
    return false;
    }


    public int getChannel()
    {

    return -1;
    }


    public int getVolume()
    {
    return -1;
    }
    }

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

    Default

    Read the assignment again, the things a tv knows about itself are what it's instance variables should be. You have one correct instance variable out of the 3.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Also, can you use code tags please?

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I don't actually see much difference... Just from the names of your classes I believe you need a bit more meat in between the brackets.

  12. #12
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    ok help me out if there r ny errors

    /**
    * 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 int 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 volume;
    }
    }

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Stop dumping your code and expecting us to fix it. Ask a specific question. If you get errors, post the EXACT error messages.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    And use the code tags, as you were advised in #3

    db

  15. #15
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    PHP Code:
    /**
     
     */
    class Television
    {
    	private int channel; // different channel numbers on a tv
    	private int volume; // volume of the tv
    	private boolean power; // whether tv is on or off
    
    // Constructor
    
    	/**
    	 * Creates a new Television
    	 */
    	public Television()
    	{
    		on = false;
    		channel = 1;
    		volume = 5;
    		
    	}
    
    
    // Instance Methods
    
    	/**
    	 * Turn the TV on or off, to whatever it wasn't before
    	 */
    	public void switchOnOff()
    	{
    		// turn on tv at first or to whatever it wasnt before
    		if(on=true)
    			on=false;
    		else
    			on=true;
    	}
    
    
    	/**
    	 * 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)
    	{
    		int randomChannel=2; // create a random channel number
    		if (channel <= 5 && channel >= 1 && randomChannel >= 1 && randomChannel <= 5)
    			newChannel=randomChannel;
    		else
    		;
    
    	}
    
    
    	/**
    	 * Increase the volume level by 1;
    	 * If the tv is already at maximum volume, nothing happens
    	 */
    	public void increaseVolume(int volume)
    	{
    		if(volume>=0 && volume<10)
    			volume++; // increment of 1 that is to increase volume by 1
    	}
    
    
    	/**
    	 * Decrease the volume level by 1;
    	 * If the tv is already at minimum volume, nothing happens
    	 */
    	public void decreaseVolume(int volume)
    	{
    		if(volume>0 && volume<=10)
    			volume+=-1; // decrease the volume level by 1
    		else
    		;
    	}
    
    
    // Accessor methods
    
    	/**
    	 * Returns true if the TV is on,
    	 * else, returns false
    	 */
    	public boolean isOn()
    	{
    		return on;
    	}
    
    
    	public int getChannel()
    	{
    		return channel;		
    	}
    
    
    	public int getVolume()
    	{
    		return volume;
    	}
    }
    this is a class file...r there ny errors
    Last edited by joe98; 04-08-2011 at 09:42 AM.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    No.
    You tell us.
    Does it compile?
    Does it run?
    Does it give the result you expect?

    Do not expect us to code review!

  17. #17
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    it does compile, it doesnt run duh ....it doesnt have a main method...just wonderin if this is a correct way or simpler way

Similar Threads

  1. OOP Objects and Classes help
    By richlan in forum New To Java
    Replies: 5
    Last Post: 03-05-2011, 05:50 PM
  2. Objects & Classes - Beginner question
    By jonytek in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 12:49 PM
  3. Classes and Objects Help
    By collin389 in forum New To Java
    Replies: 1
    Last Post: 12-14-2009, 12:44 AM
  4. classes as objects
    By kroiz in forum New To Java
    Replies: 4
    Last Post: 07-25-2009, 05:22 AM
  5. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM

Posting Permissions

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