简体   繁体   中英

How do I choose an object in Java to change its fields?

A theatre has spotlights mounted over its stage. Each spotlight can be on, off, or dimmed. Write a class called Spotlight to represent a spotlight. The class should have one field variable, status, to store the spotlight's status ( on, off, or dimmed). As well as a constructor, your class should contain the following methods:

  1. isOff() returns true if the spotlight is off, false otherwise
  2. isOn() returns true if the spotlight is on, false otherwise
  3. isDimmed() returns true if the spotlight is dimmed, otherwise false
  4. on() turns the spotlight on
  5. off() turns the spotlight off
  6. dim() dims the spotlight
  7. toString returns the information about the spotlight as a String.

So this is my class and what I have so far:

class Spotlight
{
    private String status; //the status of the spotlight


    
    //constructor
    public Spotlight(String s)
    {
        status = s;
    }
    
    //get the spotlights status
    public String getStatus()
    {
        return status;
    }

    //set the spotlights status
    public void setStatus(String newS)
    {
        status = newS;
    }

    //method to show that the spotlight is off
    public boolean isOff()
    {
        if (status.equalsIgnoreCase("Off"))
           
        {
            return true;
        }
        else
        {
            return false;
        }
             
       
    }

    //method to show that the spotlight is on
    public boolean isOn()
    {
             if (status.equalsIgnoreCase("On"))
        {
            return true;
        }
        else
        {
            return false;
        }
       
    }

    //method to show that the spotlight is dimmed
    public boolean isDimmed()
    {
              return false;
       
    }

    //method to show that the spotlight is off
    public boolean on()
    {
              if (status.equalsIgnoreCase("On"))
        {
            return true;
        }
        else
        {
            return false;
        }
       
    }

    //method to show that the spotlight is off
    public boolean off()
    {
              if (status.equalsIgnoreCase("Off"))
        {
            return true;
        }
        else
        {
            return false;
        }
       
    }

    //method to show that the spotlight is off
    public boolean dim()
    {
              return false;
       
    }
    

    //return all the information about the employee as a String
    public String toString()
    {
        return "Status: " + this.getStatus(); 
    }
    
}


       Spotlight a = new Spotlight("Off");
       Spotlight b = new Spotlight("Off");
       Spotlight c = new Spotlight("Off");
       Spotlight d = new Spotlight("Off");
       Spotlight e = new Spotlight("Off");


        //print out the information about them 
           System.out.println();
           System.out.println(a.toString());
           System.out.println();
           System.out.println(b.toString());
           System.out.println();
           System.out.println(c.toString());
           System.out.println(); 
           System.out.println(d.toString());
           System.out.println();
           System.out.println(e.toString());
           System.out.println(); 
       
           System.out.println("Select a light to turn on a-e");
           Scanner sc = new Scanner(System.in);
           String input = sc.nextLine();
           Spotlight f = new Spotlight(input);

So far I am able to change the status of the spotlight and let it display my results but if I am using for example twenty spotlight objects how can I choose which light I want to turn on?

The easy way:

List<SpotLight> spotlights = new ArrayList<>();
for(int i = 0; i < 20; i++)
    spotlights.add(new SpotLight("Off"));

int choice = 0;
do {
    System.out.println("Select a light to turn on 1-20");
    choice = sc.nextInt();
} while(choice < 1 || choice > 20);

spotlights.get(choice - 1).on();

System.out.println(spotlights);

See also documentation on List and ArrayList .


As a side note, you should really have your spotlight class use an enum instead of those strings:

public class SpotLight {     
    public enum State {
        On,
        Off,
        Dimmed
    }

    private State state;

    public SpotLight(State state) {
        this.state = state;
    }

    ...
}

Then, for instance, create a spotlight with:

SpotLight spotlight = new SpotLight(SpotLight.State.Off);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM