I am new to Java (taking it to get warmed up for C and C+++ as I am very rusty.)

I am writing a class that is suppose run the file DLTest.java(written by the instructor) that is suppose to keep track of two licenses with name and tickets. DrivingLicense, ().addTicket, ().name,().numTickets, and ().isSuspended are all objects in this class.

My program is suppose to "name" driver one and two(Alice/Bob), tying their existing tickets(2/0) so that the DLTest can make this output:
Alice does NOT have a suspended license
Bob does NOT have a suspended license

Bob and Alice are both caught speeding!
(this program doesn't loop and at this point it is suppose to add a ticket to each driver to make this output)
(my problem is getting the numTicket to increase by one)
Alice has a suspended license!

Bob does NOT have a suspended license

public class DrivingLicense {
String name;
   int  numTickets, a;
 boolean isSuspended, addTicket;

  public DrivingLicense() { 
     name = null;
     numTickets = 0;
  isSuspended = addTicket;
  }
    public DrivingLicense (String name, int numTickets){
    this.name = name;
    this.numTickets = numTickets;
    }
    public boolean isSuspended() {
        if (numTickets <= 2) {
            return true;}
        return false;} 
     public boolean addTicket() {
         numTickets = a;      
         if (a+1 >= 2) {
             return false;}
         return true;
        }
     }

I don't understand why it is forcing me to do have to make a return statement twice, I can't seem to get d1.license to become d1.isSuspended after the "ticket".

Recommended Answers

All 4 Replies

public boolean isSuspended() {
if (numTickets <= 2) {
            return true;}
        return false;}

you need to have two return statements, because true will not always be returned, while the method that calls that method actually expects a vale to be returned.

what if numTickets is 3? your method wouldn't return anything. if nothing is returned, that's not the same as 'false' which should be returned there.

as soon as you put a returntype other than void, the compiler will only accept methods that will return a result for every possible occurence. so, it will test your if statement for both true and false. without the second return statement, it will return a value for true, yet not for false.

What's the variable "a" for? You use it but never give it a value.
What's the boolean "addTicket" for? - What are you saying when you say "addTicket is true"?
When/where do you ever update isSuspended?

Finally if you are getting a compiler diagnostic about return statements then post the entire message. What you posted above doesn't really tell us anything.

This is the program it is suppose to run:

public class DLTest
{
    public static void main(String[] args)
    {
        DrivingLicense d1 = new DrivingLicense();
        DrivingLicense d2 = new DrivingLicense();
        d1.name = "Alice";
        d1.numTickets = 2;
        d2.name = "Bob";
        d2.numTickets = 0; //if I change bob's tickets to three, I can get the opposite effect for just bob.

        // 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");
        }   
    }
}

The objects in the class I am trying to write are needed to run this program (which I did not write)The program I posted originally is the entire program and it almost works... I was having problems getting the program to allow objects to have values, that is why I had int a = to numTicket, I don't know how to reset the value for isSuspended as it doesn't loop and I already used it as an object. I was trying to get addTicket = isSuspended so that I could change the boolean, therefore getting a different return. I am not getting any errors at this time, just a program that isn't suspending any licenses. If I alter the main program to a different ticket number, I can alter the results but I still find that the first run of the dl looks just like the second.

As to the boolean, the only answer that has any affect on the outcome if the very first return in the program, all of the others do absolutely nothing. If I don't put in the extra return lines, the program won't run.

Very interesting - but it doesn't answer any of my questions.
The only Object in your class is the String name. You seem very confused about Objects, primitives, variables, values and methods - I recommend you review these fundamental topics before trying to write any more Java.
"it almost works" is pure self-deception. It doesn't work. Until you get it working, you have no idea how many bugs are remaining.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.