Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Another java questiion guys heres my attempt

  • 03-05-2010 7:38pm
    #1
    Registered Users, Registered Users 2 Posts: 310 ✭✭


    [QUOTE](a) Implement an abstract class MediaDevice:
    1) MediaDevice has the following private members: name, battery
    2) MediaDevice must have a constructor which takes in the name of the device as a parameter and sets battery to 100
    3) MediaDevice must have a getter method for name
    4) MediaDevice must have an abstract method play() which takes a (String) mediaTitle as input parameter and returns nothing
    5) MediaDevice must have a drainBattery() method which takes in the amount of battery to be drained and reduces battery by that amount. If the battery is below 20, this method should print out a low battery warning.
    6) MediaDevice must have a printBatteryStatus() method which simply prints out: device name battery is at battery %.

    [(b) MediaDevice a subclasse, iPod.

    1) iPod
    i. iPod has private member variable: storage
    ii. iPod has a constructor which takes name and storage, passes name to the superclass and stores storage as its own member variable
    iii. When the inherited method play() is called, it must print out: name of device is playing media title passed in (see output for exact details). It must then drain the battery by 5
    iv. iPod has a method shuffle which takes no parameters and return nothing. It prints out name of device shuffled storage gigs of music. It must then drain the battery by 5.[/QUOTE]


    heres my attempt
    class TestMediaDevices
    {
    public static void main(String args[])
    {
    iPod pod = new iPod("Jack's iPod", 4);

    pod.play("Fraggle Rock");
    pod.printBatteryStatus();
    pod.shuffle();
    pod.printBatteryStatus();
    }
    }

    public class iPod extends MediaDevice{
    private double storage;
    public iPod(String name , double storage)
    {
    super(name);
    this.storage = storage;
    }
    public void play(String MediaTitle)
    {
    System.out.println(getName() + "is playing " + MediaTitle +
    getName() + "battery is at" + (getBattery() - 5));

    }
    public void shuffle()
    {
    System.out.println( getName()+ "shuffled " + this.storage +
    " gigs of music " + getName() + "battery is at " + (getBattery()- 5));
    }
    }

    public abstract class MediaDevice
    {
    private String name;
    private int battery;

    public MediaDevice(String name)
    {
    this.name = name;
    }

    public String getName()
    {
    return name;
    }
    public abstract void play(String MediaTitle);

    public int drainBattery(int BatteryToBeDrained)
    {
    return BatteryToBeDrained - getBattery();
    }

    public abstract void printBatteryStatus();


    public void setBattery(int battery) {
    this.battery = battery;
    }

    public int getBattery() {
    return battery;
    }
    }


    hope yas can help guys :)


Comments

  • Moderators, Sports Moderators, Regional Abroad Moderators Posts: 2,666 Mod ✭✭✭✭TrueDub


    What's your question?


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Also, it's much more helpful to use the code tags for any code. Easier to read. What's your question?


Advertisement