0

I know there are lots of questions related to this compiler error in Java. I have to code the behavior of a simple media player that plays images, audio files and videos. I wrote seven classes, three of them being interfaces. Here's a schematic:

  • AudioI contains volume-related methods: weaker, louder, getVolume and setVolume;
  • VideoI contains brightness-related methods: brighter, darker, getBrightness and setBrightness;
  • MediaI contains general methods for media: play, getTitle, setTitle, getDuration and setDuration.

The other classes are:

  • Audio: it implements AudioI, MediaI and extends Media;
  • Image: it implements VideoI, MediaI and extends Media;
  • Video: it implements VideoI, MediaI and extends Media;
  • Media: it implements AudioI, VideoI and MediaI;
  • InterfaceTest: where I test methods and constructors.

Now, the problem I have is that, even though I think I implemented everything correctly, I get the compiler error in the title. Also, every time I compile, the compiler outputs 4 errors only, related to Audio, Image, Video and Media classes. If I remove one signature in a random interface the compiler switches to another method and tells me it's wrongly implemented. Even if I remove all method signatures except for one, the compiler outputs the error regarding that particular method.

Also, I don't know if this is helpful, I use Java 8 and I don't use an IDE, I compile and run the files from the Windows terminal.

Here's the code:

AudioI interface

public interface AudioI{
  public void louder();
  public void weaker();
  public int getVolume();
  public void setVolume();
}

VideoI interface

public interface VideoI{
  public void brighter();
  public void darker();
  public int getBrightness();
  public void setBrightness();
}

MediaI interface

public interface MediaI{
  public void play();
  public int getDuration();
  public void setDuration();
  public String getTitle();
  public void setTitle();
}

Audio class

public class Audio extends Media implements AudioI, MediaI{
  private int volume;
  private int duration;
  private String title;

  /**
    CONSTRUCTORS
  **/
  Audio(){
    this.title = "audio.mp3";
    this.duration = 100;
    this.volume = 5;
  };

  Audio(String title, int duration){
    this.title = title;
    this.duration = duration;
    this.volume = 5;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/
  public void louder(){
    if(this.volume < 10){
        this.volume++;
    }else{
        System.out.print("Maximum volume reached");
    }
  };

  public void weaker(){
    if(this.volume > 0){
        this.volume--;
    }else{
        System.out.print("Minimum volume reached");
    }
  };

  public int getVolume(){
    return this.volume;
  };

  public void setVolume(int volume){
    if(volume > 9){
        System.out.println("******************************");
        System.out.println("* ERROR: maximum volume is 9 *");
        System.out.println("******************************");
    }else if(volume < 0){
        System.out.println("******************************");
        System.out.println("* ERROR: minimum volume is 0 *");
        System.out.println("******************************");
    }else{
        this.volume = volume;
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITLE: " + this.title);

    System.out.print("VOLUME: >");
    for(i = 0; i < this.volume; i++){
        System.out.print(")");
    }
    System.out.println("");
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    this.duration = duration;
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

Image class

public class Image extends Media implements VideoI, MediaI{
  private String title;
  private int brightness;
  private int duration = 1;

  /**
    CONSTRUCTORS
  **/
  Image(){
    this.title = "img.jpeg";
    this.brightness = 5;
  };

  Image(String title){
    this.title = title;
    this.brightness = 5;
    this.duration = 1;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/
  public void brighter(){
    if(this.brightness < 10){
        this.brightness++;
    }else{
        System.out.print("Maximum brightness reached.");
    }
  };

  public void darker(){
    if(this.brightness > 0){
        this.brightness--;
    }else{
        System.out.print("Minimum brightness reached.");
    }
  };

  public int getBrightness(){
    return this.brightness;
  };

  public void setBrightness(int brightness){
    if(brightness > 10){
        System.out.println("* ERROR: maximum brightness is 10 *");
    }else if(brightness < 1){
        System.out.println("* ERROR: minimum brightness is 1 *");
    }else{
        this.brightness = brightness;
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    System.out.print("LUMINOSITA': >");
    for(i = 0; i < this.brightness; i++){
        System.out.print("*");
    }
    System.out.println("");
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot set image duration");
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

Video class

import java.util.ArrayList;

public class Video extends Media implements VideoI, MediaI{
  private int duration;
  private String title;
  private int brightness;
  private Image buff;
  ArrayList<Image> images = new ArrayList<Image>();

  /**
    CONSTRUCTORS
  **/
  Video(){
    for(int i = 0; i < 100; i++){
        images.add(new Image("img" + i + ".jpeg"));
    }
    this.brightness = 5;
    this.duration = 100;
    this.title = "slideshow.mkv";
  };

  Video(String title, int nOfImages){
    for(int i = 0; i < nOfImages; i++){
        buff = new Image("img" + i + ".jpeg");
        this.images.add(buff);
    }
    this.brightness = this.images.get(0).getBrightness();
    this.duration = nOfImages;
    this.title = title;
  };

  /**
    VIDEOI METHODS IMPLEMENTATION
  **/
  public void brighter(){
    int i;

    if(this.brightness < 10){
        this.brightness++;

        for(i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(this.brightness);
        }
    }else{
        System.out.print("Maximum brightness reached.");
    }
  };

  public void darker(){
    int i;

    if(this.brightness > 0){
        this.brightness--;

        for(i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(this.brightness);
        }
    }else{
        System.out.print("Minimum brightness reached.");
    }
  };

  public int getBrightness(){
    return this.brightness;
  };

  public void setBrightness(int brightness){
    if(brightness < 0){
        System.out.println("**********************************");
        System.out.println("* ERROR: minimum brightness is 0 *");
        System.out.println("**********************************");
    }else if(brightness > 10){
        System.out.println("***********************************");
        System.out.println("* ERROR: maximum brightness is 10 *");
        System.out.println("***********************************");
    }else{
        for(int i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(brightness);
            this.brightness = brightness;
        }
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    System.out.print("LUMINOSITA': >");
    for(i = 0; i < this.brightness; i++){
        System.out.print("*");
    }
    System.out.println("");


    System.out.print("DURATA VIDEO: ");
    for(i = 0; i < this.images.size(); i = i + 10){
        System.out.print("[] ");
    }
    System.out.println("");
  };

  public int getDuration(){
    this.duration = this.images.size();
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot set video duration");
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

Media class

import java.util.ArrayList;

public class Media implements AudioI, VideoI, MediaI{
  private Video video;
  private Audio audio;
  private String title;
  private int duration;
  private int volume;
  private int brightness;
  private boolean hasVideo;
  private boolean hasAudio;

  /**
    CONSTRUCTORS
  **/

  Media(){
    this.title = "Video.mkv";
    this.volume = 5;
    this.brightness = 5;
    this.hasVideo = false;
    this.hasAudio = false;
  };

  Media(String title, Audio audio, Video video){
    this.video = video;
    this.audio = audio;
    this.title = title + ".mkv";
    this.volume = audio.getVolume();
    this.brightness = video.getBrightness();
    if(this.audio.getDuration() >= this.video.getDuration()){
        this.duration = this.audio.getDuration();
    }else{
        this.duration = this.video.getDuration();
    }
    this.hasVideo = true;
    this.hasAudio = true;
  };

  Media(String title, Audio audio){
    this.audio = audio;
    this.title = title + ".mp3";
    this.volume = audio.getVolume();
    this.duration = this.audio.getDuration();
    this.hasVideo = false;
    this.hasAudio = true;
  };

  Media(String title, Video video){
    this.video = video;
    this.title = title + ".sdw";
    this.brightness = video.getBrightness();
    this.duration = this.video.getDuration();
    this.hasVideo = true;
    this.hasAudio = false;
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/

  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    if(this.hasAudio == true){
        System.out.print("VOLUME: >");
        for(i = 0; i < this.audio.getVolume(); i++){
            System.out.print(")");
        }
        System.out.println("");
    }

    if(this.hasVideo == true){
        System.out.print("LUMINOSITA': >");
        for(i = 0; i < this.video.getBrightness(); i++){
            System.out.print("*");
        }
        System.out.println("");
    }

    if(this.hasVideo == true && this.hasAudio == true){
        System.out.print("DURATA AUDIO: ");
        for(i = 0; i < this.video.getDuration(); i = i + 10){
            System.out.print("[] ");
        }
        System.out.println("");
        System.out.print("DURATA VIDEO: ");
        for(i = 0; i < this.audio.getDuration(); i = i + 10){
            System.out.print("[] ");
        }
        System.out.println("");
    }
  };

  public void setAudio(Audio audio){
    this.audio = audio;
  };


  public void setVideo(Video video){
    this.video = video;
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot modify media duration");
  };


  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/

  public void louder(){
    this.audio.louder();
  };

  public void weaker(){
    this.audio.weaker();
  };

  public int getVolume(){
    return this.audio.getVolume();
  };

  public void setVolume(int volume){
    this.audio.setVolume(volume);
  };

  /**
    VIDEOI METHODS IMPLEMENTATION
  **/

  public void brighter(){
    this.video.brighter();
  };

  public void darker(){
    this.video.darker();
  };

  public int getBrightness(){
    return this.video.getBrightness();
  };

  public void setBrightness(int brightness){
    this.video.setBrightness(brightness);
  };
}
Amit
  • 30,756
  • 6
  • 57
  • 88
alemootasa
  • 83
  • 2
  • 9
  • Sadly, no. My professors are a bit old fashioned! But if you think running the code on an IDE could be helpful I'll try right away – alemootasa Apr 29 '17 at 16:44
  • Note: There's no reason to put `;` after the block of a method or constructor. It doesn't actually prevent compilation *(I'm surprised to find)*, but it's not expected or required. – T.J. Crowder Apr 29 '17 at 16:49
  • @T.J.Crowder Yeah I also thought so, but since I'm a noob in Java who knows, maybe it was the problem :) Also, I think the compiler doesn't give me an error for that because it reads the ";" as a null instruction – alemootasa Apr 29 '17 at 16:56
  • @alemootasa: Yup, it's an [empty statement](https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.6). I'm just surprised empty statements are valid there. :-) – T.J. Crowder Apr 29 '17 at 17:07
  • You would see your error if you attached the `@Override` annotation to each of the implementing methods, which you should always do. – Lew Bloch Apr 29 '17 at 17:13
  • It's interesting that `Video` extends `Media`, which contains a `Video` pointer. So what is the meaning of the `Video` member of a `Video` instance? – Lew Bloch Apr 29 '17 at 17:16
  • `if(this.hasVideo == true)` Comparing a `boolean` to a `boolean` to get a `boolean` is somewhat redundant, superfluous, and repetitive. – Lew Bloch Apr 29 '17 at 17:18
  • @LewBloch 1: I didn't know what an annotation was, I didn't get to that part yet, but thank you for the suggestion, I quickly searched what an annotation was and it's very helpful. 2: In the test class I use the constructors in Media to test the code, and I need a Video instance in two cases, that's why the class Video exists. Did I answer you? Frankly I didn't quite get your question, sorry! 3: I didn't find the line you mentioned, but in any case, what would you suggest instead? As I said, I'm a noob at Java, I didn't pick up the style yet – alemootasa Apr 29 '17 at 17:34
  • `if (hasVideo)`, or as a method, if (hasVideo()) {` (I made it a method and`this.` for a method has no effect.) `Video` extends `Media`. `Media` has a `Video` member, so all its subclasses do, too. Thus, `Video` has within it a `Video` member, and has a `setVideo()` method. My question was what it means in the domain logic for a `Video` to have a `Video` member; in other​ words, what does `Video.setVideo(someVideo)` mean? – Lew Bloch Apr 29 '17 at 18:01
  • See the Java Tutorial again for an explanation of annotations, and `@Override` in particular. – Lew Bloch Apr 29 '17 at 18:02

2 Answers2

2

the problem seems to be with your set methods, within the interfaces, you've declared that the set methods will not have a parameter but within the classes that implement those interfaces you instead provided parameters for them i.e you're not implementing the appropriate methods from the interfaces but rather defining a new method completely different to the ones within the interfaces.

The solution is simply to change the set method definitions of the interfaces to match the ones within the classes or vice versa.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Tried that (I thought method signatures didn't require a parameter declaration/specification) but didn't work: if I just add the parameter type between the brackets in methods signatures (like 'void setVolume(int)') the compiler tells me to specify a identifier, if I add a identifier the previous compiler error pops up again – alemootasa Apr 29 '17 at 16:59
  • 1
    And I said a stupid thing: don't know what happened, maybe I didn't save my source code and I got the same compiler message (a huge thanks to my professors that don't let us use an IDE). Sorry for my previous comment, you were obviously correct. Thank you! – alemootasa Apr 29 '17 at 17:02
  • @alemootasa Glad it helped and your problem is now solved ^^. – Ousmane D. Apr 29 '17 at 17:03
-1

Since the Media class already implements the interfaces AudioI, VideoI and MediaI, to the classes Audio and Video don't implements the interfaces and than see that happens

xelander
  • 1
  • 1