This site requires JavaScript, please enable it in your browser!
Greenfoot back
dROS
dROS wrote ...

2019/12/4

Exercise 10.48.

dROS dROS

2019/12/4

#
How do i do this? Please help me ; Add two buttons to change voume up and down Requirement: • there should be no static data used in any of the classes in your homework. • all data in all classes must be private. • if you declare an reference object, this object must be initialized with a new statement.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        addObject(new Music(),100,200);
        addObject(new MusicUp(),380,300);
        addObject(new MusicDown(),550,300);
    }
}

//music class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Music here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Music extends Actor
{
     private GreenfootSound sound;
    
     
     
     
   

     
     public Music(){//constructor
         sound= new GreenfootSound("music.mp3");
         
         
        }
        
     
    /**
     * Act - do whatever the Music wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {   
       if (Greenfoot.mouseClicked(this)) {
     
            //this is for music to play
            if(sound.isPlaying()){//bollean type to stop music
                sound.pause();
                setImage("pause.png");

            }
            else{
                sound.play();
                setImage("play.png");

            }  //end of the music play  
        }
}
}

//Music Down
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MusicDown here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MusicDown extends Actor
{
    /**
     * Act - do whatever the MusicDown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MusicUp here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MusicUp extends Actor
{
    /**
     * Act - do whatever the MusicUp wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}

danpost danpost

2019/12/5

#
dROS wrote...
How do i ... Add two buttons to change voume up and down
Do your MusicUp and MusicDown buttons have their images, yet? If so, give the buttons something to do.
dROS dROS

2019/12/6

#
Here are the requirments, i dont know how to approach it . can you please help me apply the “singleton” design pattern. Singleton design pattern means you only have one instance created of a class type, and in this homework, you should create only one instance of the GreenfootSound class, which is the only music piece we are manipulating with play/pause button, and the volume up and down button. Then to manipulate this only instance among different classes/files, you need to resort to the encapsulation principle, and create public getter and/or setters for this object, so that other classes can reply on the public getter/setter to access this object. One common mistake in this homework I have seen is: student creates separate instances of the GreenfootSound objects in different classes, and students fail to notice that these instances are independent of each other, for example, one instance of GreenfootSound object is to be controlled by the play/pause button, and the other instance is to be controlled by the volume up button, etc. Then the consequence is that: when you click the up button, you increase the volume of the GreenfootSound object, but the object whose volume you just increased has nothing to do with the GreenfootSound object that is being used by the play/pause button, which is a typical violation of the “singleton” design pattern. The “singleton” principle also applies to other classes, such as, you should only create one instance for the play/pause button, and only one instance for the bar display, and only one instance for volume up button, and you should share one current volume variable among all different classes. If you have multiple variables indicating the sound object’s current volume in difference classes, then you need to synchronized them, which means that if you increase or decrease the volume in one class, you need to populate the change to other classes where they also have a variable indicting the current volume level. , we cannot use the getVolume() method to retrieve the current average volume level, and since you cannot use method getVolume() to obtain the average volume level of the sound track being played, then you need to declare a private data member to store the current average volume level. And when the volume increase or volume decrease button is pressed, you need to modify the data that stored the current average volume, and then use method setVolume(int level)to set the modified average volume to the sound track. Also, pay attention to the valid data range of the volume that you can set.
danpost danpost

2019/12/6

#
Where did you learn about the "singleton" design pattern. I could not find it in the book.
dROS dROS

2019/12/6

#
from professor but i didn't get anything
danpost danpost

2019/12/6

#
dROS wrote...
from professor but i didn't get anything
Since I do not know what your professor's idea of a "singleton" design pattern is without static data, I guess I cannot help. Sorry. :(
dROS dROS

2019/12/6

#
its okay but i think he means. *i have to store sound.setVolume(volume) level in private data member? *using a setVolume (int level ) setter i have to increase the value of private variable it means when it detect mouse click on "MusicUp class object" it should increase the value private varaible value in music class which is volume.
//music class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Music here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Music extends Actor
{
     private GreenfootSound sound;
     private int volume=50;
     public Music(){//constructor
         sound= new GreenfootSound("music.mp3");
         sound.setVolume(volume);
        }
      public int getVolume(){
        return volume;
    }

    public void setVolume(int level)
    {
        volume = volume +level;
        if(volume >=0 && volume<=100){
        this.volume=volume;
        }
        
    }
   public void add(){
       sound.setVolume(volume);
    }
     
    /**
     * Act - do whatever the Music wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {   
        add();
       if (Greenfoot.mouseClicked(this)) {
            setLocation(getX(), getY());
            //this is for music to play
            if(sound.isPlaying()){//bollean type to stop music
                sound.pause();
                setImage("pause.png");

            }
            else{
                sound.play();
                setImage("play.png");

            }  //end of the music play  
        }
}
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MusicUp here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MusicUp extends Actor
{
    /**
     * Act - do whatever the MusicUp wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)) {
            Music music = new Music();
            music.setVolume(50);
        }
    }    
}



danpost danpost

2019/12/6

#
Each time the MusicUp button is clicked, a new instance of Music is created. The Music class is def not designed in any type of "singleton" pattern.
You need to login to post a reply.