Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: how to loop a method?

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to loop a method?

    My goal is to write a class called DrivingLicense that is suppose to return ()isSuspended with the tickets exceed 2.

    public class DrivingLicense {
    String name;
       int  numTickets;
      public DrivingLicense() { 
         name = null;
         numTickets = 0;
      }
        public DrivingLicense (String name, int numTickets){
        this.name = name;
        this.numTickets = numTickets;
        }
       public Boolean isSuspended (){
     
           if (numTickets >= 2)
                return false;
           return false;
     
        }
       public Boolean addTicket() {
          numTickets ++;
          if (numTickets >= 2)
        return true;
          return true; 
       //<--can I get addTicket to run through isSuspended with the new value of numTicket or do I need to //redo this?
       }
    }

    This is the program that calls on my program

    public class DLTest
    {
        // main() method
        public static void main(String[] args)
        {
            // declare and create two DrivingLicense objects
            DrivingLicense d1 = new DrivingLicense();
            DrivingLicense d2 = new DrivingLicense();
           //private String name;
            //public class DrivingLicense (String name){
                   //citizen = name;
            //public void set
     
            // initialize the objects with names and speeding tickets
            d1.name = "Alice";
            d1.numTickets = 2;
     
            d2.name = "Bob";
            d2.numTickets = 0;
     
            // check if Alice or Bob have suspended licenses (more than 2 tickets)
            if ( d1.isSuspended() )
            {
                System.out.println(d1.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d1.name + " does NOT have a suspended license");
            }
     
            if ( d2.isSuspended() )
            {
                System.out.println(d2.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d2.name + " does NOT have a suspended license");
            }
     
            // Alice and Bob both get caught speeding, so add another ticket for each of them
            System.out.println("Bob and Alice are both caught speeding!");
           d1.addTicket();
            d2.addTicket();
     
            // check again if Alice or Bob have suspended licenses (more than 2 tickets)
            if ( d1.isSuspended() )
            {
                System.out.println(d1.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d1.name + " does NOT have a suspended license");
            }
     
            if ( d2.isSuspended() )
            {
                System.out.println(d2.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d2.name + " does NOT have a suspended license");
            }
     
        }
     
    }
    Last edited by LeslieInNorthPole; August 6th, 2012 at 04:53 PM. Reason: clicked too soon, was trying to "see" it before posting


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to loop a method?

    Can you explain what the problem is with the current program? Post the output, explain what is wrong with it and show what it should be.

    Should the isSuspended() method ever return true? How/where/when will it do that?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    LeslieInNorthPole (August 9th, 2012)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to loop a method?

    When does this return true?
    Quote Originally Posted by LeslieInNorthPole View Post
    //...............
       public Boolean isSuspended (){
     
           if (numTickets >= 2)
                return false;
           return false;
        }//.................

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to loop a method?

    When does this return false?
    Quote Originally Posted by LeslieInNorthPole View Post
    //.....................
       public Boolean addTicket() {
          numTickets ++;
          if (numTickets >= 2)
        return true;
          return true; 
       //<--can I get addTicket to run through isSuspended with the new value of numTicket or do I need to //redo this?
       }//....................

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to loop a method?

    Quote Originally Posted by LeslieInNorthPole View Post
    public class DLTest
    {
        // main() method
        public static void main(String[] args)
        {
            // declare and create two DrivingLicense objects
            DrivingLicense d1 = new DrivingLicense();
            DrivingLicense d2 = new DrivingLicense();
           //private String name;
            //public class DrivingLicense (String name){
                   //citizen = name;
            //public void set
     
            // initialize the objects with names and speeding tickets
            d1.name = "Alice";
            d1.numTickets = 2;
     
            d2.name = "Bob";
            d2.numTickets = 0;
     
     
     
    /* The code starting here ---------------------------------------------------------- */
            // check if Alice or Bob have suspended licenses (more than 2 tickets)
            if ( d1.isSuspended() )
            {
                System.out.println(d1.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d1.name + " does NOT have a suspended license");
            }
     
            if ( d2.isSuspended() )
            {
                System.out.println(d2.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d2.name + " does NOT have a suspended license");
            }
    /* and ending here ----------------------------------------------------------------- */
     
     
     
            // Alice and Bob both get caught speeding, so add another ticket for each of them
            System.out.println("Bob and Alice are both caught speeding!");
           d1.addTicket();
            d2.addTicket();
     
     
    /* looks like a repeat of the code starting here ------------------------------------------------ */
            // check again if Alice or Bob have suspended licenses (more than 2 tickets)
            if ( d1.isSuspended() )
            {
                System.out.println(d1.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d1.name + " does NOT have a suspended license");
            }
     
            if ( d2.isSuspended() )
            {
                System.out.println(d2.name + " has a suspended license!");
            }
            else
            {
                System.out.println(d2.name + " does NOT have a suspended license");
            }
    /* and ending here --------------------------------------------------------------------------------- */
     
        }
     
    }
    Any time you have repeated code, you have missed the opportunity to make a method which can be reused.

  7. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to loop a method?

    thanks for the input. I did get it figured out. I didn't need a loop, I needed to find a way to call a method within the same class. Here is the reworked code.

    public class DrivingLicense {
    String  name; 
       int  numTickets, A; 
      public DrivingLicense() { 
      }
        public DrivingLicense (String name, int numTickets){
        this.name = name; 
        this.numTickets = numTickets;
        }
       public Boolean isSuspended (){
           return (numTickets > 2);}          
     public Boolean addTicket(){
               A = numTickets++; 
               A = numTickets; 
               if (numTickets > 2){
                   if (isSuspended ()){ 
                       return (numTickets > 2);
                   }}
          return false;
        }
        }

  8. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to loop a method?

    Quote Originally Posted by LeslieInNorthPole View Post
    ...
    //...
     public Boolean addTicket(){
               A = numTickets++; 
               A = numTickets; 
               if (numTickets > 2){
                   if (isSuspended ()){ 
                       return (numTickets > 2);
                   }}
          return false;
        }//...
    I'm not too sure about this section of code, for the following reasons.
    1) this method returns a boolean. This method adds a ticket to the ticket count. I don't understand what the return boolean would ever be used for.
    2) A = numTickets++; followed by A = numTickets; I don't know what you were trying to do with those two lines of code. It looks to me like all you wanted to do was increase and store the number of tickets. If that is the case just using numTickets++; would do that, and you do not even need the variable A at all.
    3) The next three lines of code:
    if(numTickets > 2) {
    if(isSuspended()) {
    return(numTickets > 2);
    All three lines test if numTickets is greater than 2. Three times in a row. But it seems to me testing that is more like testing isSuspended, and has nothing to do with adding a ticket. I don't think you need this method to return anything really. Just have a side effect of updating numTickets. If you need to check for license suspended after adding a ticket, use the method you wrote for that rather than trying to make one method do two things.

  9. The Following User Says Thank You to jps For This Useful Post:

    LeslieInNorthPole (August 9th, 2012)

  10. #8
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to loop a method?

    1) I changed it to a String, allowing for isSuspended method to be called after the numTickets update.
    2) good point, simple is better
    3) I eliminated the extra tests and changed the addTicket to a string with a return of name just so the program will run.

    Thanks for the second look

  11. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to loop a method?

    Quote Originally Posted by LeslieInNorthPole View Post
    1) I changed it to a String, allowing for isSuspended method to be called after the numTickets update.2) good point, simple is better
    3) I eliminated the extra tests and changed the addTicket to a string with a return of name just so the program will run.
    I would think that method could be as simple as:
    public void addTicket() {
        numTickets++;
    }
    ...where the method does exactly one thing. Raise the number of tickets by one. Methods are not required to always have a return value. That is where void comes in. It basically means you want a method to have a side effect (add 1 to numTickets) when used, and not send any information back to the point where it was called.


    Quote Originally Posted by LeslieInNorthPole View Post
    Thanks for the second look
    You are welcome. We are happy to help any way we can.

Similar Threads

  1. My while (true) loop method only seems to run once!
    By ajfonty in forum Object Oriented Programming
    Replies: 2
    Last Post: May 18th, 2012, 07:46 PM
  2. Help bolean method and while loop !!!!
    By pip0 in forum Loops & Control Statements
    Replies: 15
    Last Post: April 2nd, 2012, 08:29 PM
  3. [SOLVED] Method to reverse String using While Loop
    By Montrell79 in forum Loops & Control Statements
    Replies: 2
    Last Post: February 22nd, 2012, 03:49 PM
  4. POST method in a loop
    By kollyisrealisaac in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 26th, 2011, 11:29 AM
  5. private method , loop , switch , help?
    By wolfgar in forum Loops & Control Statements
    Replies: 9
    Last Post: November 8th, 2009, 11:04 PM