Solved

alternative ways of writing these lines...

Posted on 2004-10-24
201 Views
Last Modified: 2010-03-31
hello

I have couple of codes, very simple, but I would like to learn other ways to write instead of classis ways which what i have wrote. : )



public void istalking(int data) {

private int setcomm = 0;
 private static String IsSpeakerOn = "no";                             
     private static String IsListenerOn = "no";      //          
                while(IsSpeakerOn == "yes"){ExtraSpeaker.sleep(); }                
                          if (IsListenerOn =="yes")
                      {
                                  IsSpeakerOn ="yes";                                  
                                  setcomm = word;                                 
                                  currentcondition.wake();                  
                      }
                            else
                                        {
                                              IsSpeakerOn ="yes";             
                                              setcomm = word;                      
                                              currentcondition.sleep();            
                                              IsSpeakerOn ="no";                  
                                              IsListenerOn ="no";
                          }
               }
0
Question by:anosTekbot
    9 Comments
     
    LVL 92

    Expert Comment

    by:objects
    thats not valid java code, what exactly is it you are trying to achieve?
    0
     
    LVL 21

    Expert Comment

    by:MogalManic
    The only obvious change I can see is replacing the strings with Booleans:
      public void istalking(int data)
        {
            int setcomm = 0;
                int word = 0;
            boolean IsSpeakerOn = false;
            boolean IsListenerOn = false; //        

            while (IsSpeakerOn == true) {
                ExtraSpeaker.sleep();
            }

            if (IsListenerOn == true) {
                IsSpeakerOn = true;
                setcomm = word;
                currentcondition.wake();
            } else {
                IsSpeakerOn = true;
                setcomm = word;
                currentcondition.sleep();
                IsSpeakerOn = false;
                IsListenerOn = false;
            }
        }

    What are you trying to accomplish?
    0
     

    Author Comment

    by:anosTekbot
    i know it is not a valid code becasue there are other parts, but my concern about the while loop and if statements. other than using booleans ..
    my question is there a way these line of  statements
    0
     
    LVL 92

    Expert Comment

    by:objects
    what is your concern?
    0
     

    Author Comment

    by:anosTekbot
    to know more about loops and statements..
    for example
    i tried to use
    do {
    ...........
    } while (IsSpeakerOn == "yes");

    but when i tested .. it will take longer time than average and cost the machine to terminate ..
    0
     
    LVL 92

    Expert Comment

    by:objects
    > IsSpeakerOn == "yes"

    btw, that should be:

    IsSpeakerOn.equals("yes")
    0
     
    LVL 92

    Accepted Solution

    by:
    the difference between a while and a do while loop is when the test is done.
    in a do it is done at the start of the loop,
    in a do while it is done at the end of loop meaning the loop itself will always get executed at least onse. this would not seem appropriate in your case.
    0
     
    LVL 21

    Expert Comment

    by:MogalManic
    Is this a multi-threaded application?

    What does the WHOLE application do?
    0
     

    Author Comment

    by:anosTekbot
    i c thats why i get terminated after a do while loop
    thanks object and all
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    modThree challenge 4 42
    hasOne  challenge 59 46
    bigHeights  challenge 13 28
    powerN  challenge 3 19
    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now