Results 1 to 4 of 4
  1. #1
    dendoc01 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default problem returning object

    Java Code:
    public class PoliceOfficer
    {
    String officerName;
    double badgeNumber;
    ParkedCar carInfo;
    ParkingMeter meter;
    double timeParked;
    double timePurchased;
    
    
    //constructor to get name and badge number
    PoliceOfficer(String name,double number,ParkedCar car,ParkingMeter met)
    {
    officerName=name;
    badgeNumber=number;
    carInfo=car;
    meter=met;
    
    }
    
    
    public ParkingTicket ticketTest()
    {
    if(carInfo.getMinutesParked()>meter.getMinutesPurchased())
    {
    ParkingTicket ticket=new ParkingTicket(carInfo.getMinutesParked(),meter.getMinutesPurchased(),carInfo);
    }
    return ticket;
    }
    
    
    public String toString()
         { 
             String str=" "+ticketTest()+" the officers name and badge number are "+officerName+" and "+badgeNumber;
             return str;
          }
    Java Code:
    public class ParkingTicket
    {
    private double minutesParked;
    private double minutesPurchased;
    double fineAmount=25;
    ParkedCar carInfo;
    //constructor to get car and meter data
    ParkingTicket(double parked, double purchased,ParkedCar car)
    {
    minutesParked=parked;
    minutesPurchased=purchased;
    carInfo=car;
    }
    
    //method to return the fine amount
    public double getFineAmount()
    {
    for(double i=minutesParked;i<minutesPurchased;i++)
    {
    fineAmount=fineAmount+10;
    }
    return fineAmount;
    }
    
    public String toString()
          {
             String str=carInfo+" your fine amount is "+getFineAmount();
             return str;
          }
    
    
    
    }
    My error message tells me that in the police officer class it can not find ticket where it says "return ticket" inside the ticketTest method
    thanks

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: problem returning object

    Java Code:
    public ParkingTicket ticketTest() {
        if (carInfo.getMinutesParked() > meter.getMinutesPurchased()) {
            ParkingTicket ticket = new ParkingTicket(carInfo.getMinutesParked(), meter.getMinutesPurchased(), carInfo);
        }
        return ticket;
    }
    The ticket object only available inside the if statement. It is not available outside the scope of the if block. You need to move your ticket declaration outside of the if block.

  3. #3
    dendoc01 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: problem returning object

    I tried that but I'm just not sure how to move the ticket declaration outside the if block when i only need to create that object if that if statement is true.

  4. #4
    dendoc01 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: problem returning object

    nevermind i get what your saying i figured it out thanks for the help

Similar Threads

  1. Vector of Object not returning data
    By anthony.rich in forum New To Java
    Replies: 7
    Last Post: 02-17-2012, 06:20 PM
  2. Replies: 1
    Last Post: 06-06-2011, 02:47 PM
  3. Returning an object
    By dom12 in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 12:30 PM
  4. Method, returning reference to an object
    By Saletra in forum New To Java
    Replies: 3
    Last Post: 08-23-2010, 09:22 PM
  5. returning an object from a method
    By bigj in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •