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: Create Object

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Create Object

    I was working on this problem earlier. I have solved it, except for one part. It says that I have to generate a parking ticket from my 'ParkingTicket' class. Right now, in my Demo class, I have just had the system output all of the different parts it needs to generate the ticket, although it doesn't generate the parking ticket, per se. Here is the code I use now to generate the output:

     import java.text.DecimalFormat;
     
    public class CarTest {
     
    	public static void main(String[] args) {
     
    		DecimalFormat money = new DecimalFormat("$0.00");
     
    		ParkedCar vehicle = new ParkedCar("Honda", "Accord", "Black", "452-BTS", 130);
     
    		ParkingMeter meter = new ParkingMeter(60);
     
    		PoliceOfficer copster = new PoliceOfficer("John Doe", 1337);
     
    		ParkingTicket ticket = PoliceOfficer.examine(vehicle, meter);
     
    		if(ticket != null) {
    			//System.out.println(ticket);
    			System.out.println(vehicle.toString());
    			System.out.println(meter.toString());
    			System.out.println(copster.toString());
    			System.out.println("Fine Total: " + money.format((ticket.getFine())));
    		}
    		else {
    			System.out.println("Car is not doing anything wrong!");
    		}
    	}
     
    }

    What I want is in the 'if(ticket!= null) {' area. Rather than having all of those outputs, I would like to just generate the ParkingTicket in the ParkingTicket class. If you know how to do this, I would be really grateful.
    -TitanVex


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Create Object

    I would like to just generate the ParkingTicket in the ParkingTicket class
    Are you sure you want the class to generate an instance of itself?
    Can you explain what you want to do? Your question seems to say you want to remove the println statements.
    And replace them with generating a object. What about the ParkingTicket object that has already been created?
    Do you want to add more data to that object? If so, add some setXXXX methods to the class that will allow you to call them to set the values of variables in the class.

    How will creating an instance of the class replace the printing out of the data?

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

    TitanVex (November 16th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create Object

    Quote Originally Posted by Norm View Post
    Are you sure you want the class to generate an instance of itself?
    Can you explain what you want to do? Your question seems to say you want to remove the println statements.
    And replace them with generating a object. What about the ParkingTicket object that has already been created?
    Do you want to add more data to that object? If so, add some setXXXX methods to the class that will allow you to call them to set the values of variables in the class.

    How will creating an instance of the class replace the printing out of the data?
    I am really bad at explaining because I get confused with some of the terminology. I apologize. However, the assignment says that we must generate a parking ticket (from the ParkingTicket class) if the car's time has expired.

    What I was thinking, was that there would be a way to have the println statements in the ParkingTicket class. Then, in the demo class, I can just call upon those statements.

    I am not sure if that makes sense or not. I still want the println statements, just not where they are currently located. I would rather just call the ParkingTicket object and print out all of that data.

    I am sorry if this doesn't make sense, but thanks for trying.

    -TitanVex

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Create Object

    I think I see what you are asking. After creating an instance of the ParkingTicket class, you want to call a method in the class to print out the details.
    Can you add a method to the class and move the println statements to that method and then call it?

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

    TitanVex (November 16th, 2011)

  7. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create Object

    Well I tried something like that, but because in my println statements, I used the actual objects like 'vehicle, meter, and copster' I can't use that in the ParkingTicket class. I tried changing it to 'ParkedCar.toString()' etc., but that didn't work either. I will give you an example of what I tried:

     public class ParkingTicket {
     
    		private ParkedCar vehicle;
    		private PoliceOfficer copster;
    		private double fine;
    		private int minutes;
    		private double firstFine = 25;
    		private double moreFine = 10;
    		private ParkingMeter meter;
     
    		public ParkingTicket(ParkedCar car, PoliceOfficer cop, double guyFine, ParkingMeter clock) {
     
    			//vehicle = car;
    			//copster = cop;
    			//fine = guyFine;
    			//meter = clock;
     
    			vehicle = car;
    			copster = cop;
    			fine = guyFine;
    			meter = clock;
    		}
     
     
    		public double getTotalFine() {
    			int  time = vehicle.getMinutes() - meter.getMinPurchased();
     
    			if(time <= 60) {
    				fine = firstFine;
    			}
    			else {
    				fine = firstFine + moreFine * (time / 60);
    			}
    			return fine;
    		}
     
    		public double getFirstFine() {
    			return firstFine;
    		}
    		public double getMoreFine() {
    			return moreFine;
    		}
    		public ParkedCar getVehicle() {
    			return vehicle;
    		}
    		public PoliceOfficer getCopster() {
    			return copster;
    		}
     
    		public int getMinutes() {
    			return minutes;
    		}
    		public double getFine() {
    			return fine;
    		}
     
    		public String toString() {
                            String string = 
    			System.out.println(vehicle.toString()) + "\n" +
    			System.out.println(meter.toString()) + "\n" + 
    			System.out.println(copster.toString()) + "\n" +
    			System.out.println("Fine Total: " + "\t" + "\t" + money.format((ticket.getFine())));
                           return string;
    		}
     
    }

    It is that last section there. Is that what you mean? Or something else?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Create Object

    You could override the toString() method or create your own new method like: printTheTicket().

    I can't use that in the ParkingTicket class
    You would need to use the values that are in the ParkingTicket class.

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

    TitanVex (November 16th, 2011)

  10. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create Object

    Quote Originally Posted by Norm View Post
    You could override the toString() method or create your own new method like: printTheTicket().


    You would need to use the values that are in the ParkingTicket class.
    So how would I do that to be any different? Would it be like:
    public String printTheTicket() {
    String string = System.out.println(vehicle.toString());
    return string;
    }

    Or do I somehow have to change the 'vehicle' part of toString. I guess I don't know what my own new method would look like.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Create Object

    I would rather just call the ParkingTicket object and print out all of that data.
    You need to define what you want to happen and where it should happen?
    If you want to have the method return a String, do that.
    If you want to print out the ticket in the class, do that.
    You could do it either way.

    A method named printTheTicket() would not return a String.

  12. #9
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Create Object

    At first when i read your question I wondered if you were trying to access the class directly without instantiating it: That means you must modify it to be static. I hope the above responses helped you anyway.

Similar Threads

  1. Could not create Audio Data object
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: July 12th, 2011, 11:29 AM
  2. create object problem
    By milas in forum Member Introductions
    Replies: 0
    Last Post: February 25th, 2011, 09:24 AM
  3. How do I create an object(function)?
    By amarettoCoffee in forum Object Oriented Programming
    Replies: 3
    Last Post: January 18th, 2011, 06:54 PM
  4. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 05:53 PM
  5. Create a CLOB object with the string value
    By oshoarun in forum JDBC & Databases
    Replies: 0
    Last Post: March 6th, 2010, 01:54 AM