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 11 of 11

Thread: Many Errors

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Many Errors

    I am doing a Parking Ticket Simulator for a Java class.

    I am using NetBeans 6.8 and I keep getting error messages in my code. The two most prominent error messages are: '{' is expected and "non-static variable this cannot be referenced from a static context".

    I have e-mailed my instructor, but I have not got any word back yet. If anyone could help me, it would be greatly appreciated.

    Note: I did put "static" before class, but I have read from numerous sites that no output will be displayed.

    Here is my code thus far, also if see any other errors or nuances that may pop up, please let me know.

    Thank you for your help!
    public static class ParkedCar(){
            private String make;
            private String model;
            private String color;
            private String license;
            private int minutesParked;
     
            public ParkedCar(String make, String model, String color, String license, int minutesParked) {
                this.make = make;
                this.model = model;
                this.color = color;
                this.license = license;
                this.minutesParked = minutes;
            }
     
            public ParkedCar(ParkedCar car2){
                make = car2.make;
                model= car2.model;
                color = car2.color;
                license = car2.license;
                minutesParked = car2.minutesParked;
            }
     
            public String getColor() {
                return color;
            }
     
            public void setColor(String color) {
                this.color = color;
            }
     
            public String getLicense() {
                return license;
            }
     
            public void setLicense(String license) {
                this.license = license;
            }
     
            public String getMake() {
                return make;
            }
     
            public void setMake(String make) {
                this.make = make;
            }
     
            public int getMinutesParked() {
                return minutesParked;
            }
     
            public void setMinutes(int minutesParked) {
                this.minutesParked = minutesParked;
            }
     
            public String getModel() {
                return model;
            }
     
            public void setModel(String model) {
                this.model = model;
            }
     
            @Override
            public String toString(){
                String string = "Car Data: \n Make: " + make + "\n Model: " + model +
                        "\n Color: " + color + "\n License Plate: " + license +
                        "\n Minutes Parked: " + minutesParked;
                return string;
            }
     
    }
        public static class ParkingMeter(){
            private int minutesPurchased;
     
            public ParkingMeter(int minutesPurchased) {
                this.minutesPurchased = minutesPurchased;
            }
     
            public void setMinutesPurchased(int minutesPurchased){
                minutesPurchased = minutesPurchased;
            }
     
            public int getMinutesPurchased(){
                return minutesPurchased;
            }
        }
     
        public static class ParkingTicket(){
            private ParkedCar car;
            private PoliceOfficer officer;
            private double fine;
            private int minutes;
            double BASE_FINE = 25.0;
            double HOURLY_FINE = 10.0;
     
            public ParkingTicket(ParkedCar car, PoliceOfficer officer, double fine, int minutes) {
                this.car = car;
                this.officer = officer;
                this.fine = fine;
                this.minutes = minutes;
            }
     
            public ParkingTicket(ParkingTicket ticket2){
                car = ticket2.car;
                officer = ticket2.officer;
                fine = ticket2.fine;
                minutes = ticket2.minutes;
            }
     
            public void calculateFine(){
                if(ParkedCar.minutesParked - ParkingMeter.minutesPurchased <= 60)
                    fine = BASE_FINE;
                else
                    fine = BASE_FINE + (HOURLY_FINE * ((ParkedCar.minutesParked - ParkingMeter.minutesPurchased) / 60 ));
            }
     
            public double getBASE_FINE() {
                return BASE_FINE;
            }
     
            public void setBASE_FINE(double BASE_FINE) {
                this.BASE_FINE = BASE_FINE;
            }
     
            public double getHOURLY_FINE() {
                return HOURLY_FINE;
            }
     
            public void setHOURLY_FINE(double HOURLY_FINE) {
                this.HOURLY_FINE = HOURLY_FINE;
            }
     
            public ParkedCar getCar() {
                return car;
            }
     
            public void setCar(ParkedCar car) {
                this.car = car;
            }
     
            public double getFine() {
                return fine;
            }
     
            public void setFine(double fine) {
                this.fine = fine;
            }
     
            public int getMinutes() {
                return minutes;
            }
     
            public void setMinutes(int minutes) {
                this.minutes = minutes;
            }
     
            public PoliceOfficer getOfficer() {
                return officer;
            }
     
            public void setOfficer(PoliceOfficer officer) {
                this.officer = officer;
            }
     
        }
     
        public static class PoliceOfficer(){
            private String name;
            private String badgeNumber;
     
            public PoliceOfficer(String name, String badgeNumber) {
                this.name = name;
                this.badgeNumber = badgeNumber;
            }
     
            public PoliceOfficer(PoliceOfficer officer2){
                name = officer2.name;
                badgeNumber = officer2.badgeNumber;
            }
     
            public Patrol(ParkedCar car, ParkingMeter meter){
                ParkingTicket();
            }
     
            @Override
            public String toString(){
                String string = "Officer Data: \n Name: " + name + "\n Badge Number: " + badgeNumber +
                        "\n Minutes Illegally Parked: " + (ParkedCar.minutesParked - ParkingMeter.minutesPurchased) +
                        "Fine: $" + ParkingTicket.fine;
                return string;
            }
    Last edited by copeg; July 16th, 2010 at 06:38 PM. Reason: Please use the code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Many Errors

    The warning and errors you receive should be pretty concise as to where the errors are occurring, so I suggest you read them carefully. Just by glancing at the code there are a couple obvious ones, for instance:
    public static class ParkedCar(){
    ...
    }
    Is not a proper statement (hint: parenthesis).

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Many Errors

    You can only use static with classes declared inside another class.

    Also, when declaring classes, you don't put parenthesis after the class declaration

    public class ParkedCar() // << wrong
    {}
     
    public class ParkedCar // << right
    {}

    Also, how do you you have your files organized? In Java, the name of the main "item" in that file (either a class, enum, or interface) must match the name of that file. Additionally, you can only have one top level item per java source file.

    Look at the the error messages you are getting. Most of the time, they will provide great insite into what is wrong syntactically about the code (Java is much better at this than many languages).

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many Errors

    Thank you guys for the help! I've redone my classes to where I am not receiving the '{' is expected error.

    However, I am still receiving the static context message. I'm a beginner in Java and I am not quite sure what that message means.

    I have looked online, but maybe I am over thinking the solution or I am blind to it.

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many Errors

    PS- It seems I accidentally chopped off the top bit of my code. Maybe this can give you guys some more insight:

    package parkingticketsimulator;
     
    /**
     *
     * @author Wood Family
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            ParkedCar car = new ParkedCar("Ford", "1968", "Blue", "BR459", 125);
     
            ParkingMeter meter = new ParkingMeter(60);
     
            PoliceOfficer officer = new PoliceOfficer("John McClane", "8986");
     
            ParkingTicket ticket = officer.patrol(car, meter);
     
            if(ticket != null)
                System.out.println(ticket);
            else
                System.out.println("No Ticket is Needed!");
        }

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Many Errors

    Please surround your code with [highlight=Java]code goes here[/highlight]. It makes it much easier to read the code.

    You are trying to statically access fields of an instance variable.

    You need to have an instance before you can access those fields.

    Here's an example of something similar to what you're doing, and a simple way to fix this problem. I'll let you fix your code accordingly.
    public class MyClass
    {
        public String name;
     
    // wrong
        public String getNameAbbreviation()
        {
            // String is the class name, not the object we want to get the first character of
            return String.charAt(0);
        }
     
    // right
        public string getName()
        {
            return name.charAt(0);
        }
    }

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Many Errors

    Code tags in the future please.

    Ok, wow, we have some issues here.

    First of all, we need a main. So add something like this to your code (change the classname to whatever you want):
    public class FileOpen
    {
    	public static void main(String args[])
    	{
    	}
    ...
    }
    The main will be what actually runs when the program is executed.

    Now here is the code with some syntax issues fixed. I have commented out what is unnecessary and bolded what needed to be added:
    public class FileOpen
    {
    	public static void main(String args[])
    	{
    	}
     
    	public static class ParkedCar//()
    	{
    		private String make;
    		private String model;
    		private String color;
    		private String license;
    		private int minutesParked;
     
    		public ParkedCar(String make, String model, String color, String license, int minutesParked) 
    		{
    			this.make = make;
    			this.model = model;
    			this.color = color;
    			this.license = license;
    			this.minutesParked = minutes;
    		}
     
    		public ParkedCar(FileOpen car2)
    		{
    			make = car2.make;
    			model= car2.model;
    			color = car2.color;
    			license = car2.license;
    			minutesParked = car2.minutesParked;
    		}
     
    		public String getColor() 
    		{
    			return color;
    		}
     
    		public void setColor(String color) 
    		{
    			this.color = color;
    		}
     
    		public String getLicense() 
    		{
    			return license;
    		}
     
    		public void setLicense(String license) 
    		{
    			this.license = license;
    		}
     
    		public String getMake() 
    		{
    			return make;
    		}
     
    		public void setMake(String make) 
    		{
    			this.make = make;
    		}
     
    		public int getMinutesParked() 
    		{
    			return minutesParked;
    		}
     
    		public void setMinutes(int minutesParked) 
    		{
    			this.minutesParked = minutesParked;
    		}
     
    		public String getModel() 
    		{
    			return model;
    		}
     
    		public void setModel(String model) 
    		{
    			this.model = model;
    		}
     
    		@Override
    		public String toString()
    		{
    			String string = "Car Data: \n Make: " + make + "\n Model: " + model +
    			"\n Color: " + color + "\n License Plate: " + license +
    			"\n Minutes Parked: " + minutesParked;
    			return string;
    		}
    	}
     
    	public static class ParkingMeter//()
    	{
    		private int minutesPurchased;
     
    		public ParkingMeter(int minutesPurchased) 
    		{
    			this.minutesPurchased = minutesPurchased;
    		}
     
    		public void setMinutesPurchased(int minutesPurchased)
    		{
    			minutesPurchased = minutesPurchased;
    		}
     
    		public int getMinutesPurchased()
    		{
    			return minutesPurchased;
    		}
    	}
     
    	public static class ParkingTicket//()
    	{
    		private ParkedCar car;
    		private PoliceOfficer officer;
    		private double fine;
    		private int minutes;
    		double BASE_FINE = 25.0;
    		double HOURLY_FINE = 10.0;
     
    		public ParkingTicket(ParkedCar car, PoliceOfficer officer, double fine, int minutes) 
    		{
    			this.car = car;
    			this.officer = officer;
    			this.fine = fine;
    			this.minutes = minutes;
    		}
     
    		public ParkingTicket(ParkingTicket ticket2)
    		{
    			car = ticket2.car;
    			officer = ticket2.officer;
    			fine = ticket2.fine;
    			minutes = ticket2.minutes;
    		}
     
    		public void calculateFine()
    		{
    			if(ParkedCar.minutesParked - ParkingMeter.minutesPurchased <= 60)
    				fine = BASE_FINE;
    			else
    				fine = BASE_FINE + (HOURLY_FINE * ((ParkedCar.minutesParked - ParkingMeter.minutesPurchased) / 60 ));
    		}
     
    		public double getBASE_FINE() 
    		{
    			return BASE_FINE;
    		}
     
    		public void setBASE_FINE(double BASE_FINE) 
    		{
    			this.BASE_FINE = BASE_FINE;
    		}
     
    		public double getHOURLY_FINE() 
    		{
    			return HOURLY_FINE;
    		}
     
    		public void setHOURLY_FINE(double HOURLY_FINE) 
    		{
    			this.HOURLY_FINE = HOURLY_FINE;
    		}
     
    		public ParkedCar getCar() 
    		{
    			return car;
    		}
     
    		public void setCar(ParkedCar car) 
    		{
    			this.car = car;
    		}
     
    		public double getFine() 
    		{
    			return fine;
    		}
     
    		public void setFine(double fine) 
    		{
    			this.fine = fine;
    		}
     
    		public int getMinutes() 
    		{
    			return minutes;
    		}
     
    		public void setMinutes(int minutes) 
    		{
    			this.minutes = minutes;
    		}
     
    		public PoliceOfficer getOfficer() 
    		{
    			return officer;
    		}
     
    		public void setOfficer(PoliceOfficer officer) 
    		{
    			this.officer = officer;
    		}
     
    	}
     
    	public static class PoliceOfficer//()
    	{
    		private String name;
    		private String badgeNumber;
     
    		public PoliceOfficer(String name, String badgeNumber) 
    		{
    			this.name = name;
    			this.badgeNumber = badgeNumber;
    		}
     
    		public PoliceOfficer(PoliceOfficer officer2)
    		{
    			name = officer2.name;
    			badgeNumber = officer2.badgeNumber;
    		}
     
    		public void Patrol(ParkedCar car, ParkingMeter meter)
    		{
    			ParkingTicket();
    		}
     
    		@Override
    		public String toString()
    		{
    			String string = "Officer Data: \n Name: " + name + "\n Badge Number: " + badgeNumber +
    			"\n Minutes Illegally Parked: " + (ParkedCar.minutesParked - ParkingMeter.minutesPurchased) +
    			"Fine: $" + ParkingTicket.fine;
    			return string;
    		} 
    	[B]}[/B]
    }

    Now, we arent done, we still have some major structural issues. So, here we go:

    1st issue. The variable minutes doesnt exist. I believe you attempted to send it minutesParked. Even so, this is an ill advised way of doing this.
    public static class ParkedCar
    {
    	private String make;
    	private String model;
    	private String color;
    	private String license;
    	private int minutesParked;
     
    	public ParkedCar(String make, String model, String color, String license, int minutesParked) 
    	{
    		this.make = make;
    		this.model = model;
    		this.color = color;
    		this.license = license;
    		[B]this.minutesParked = minutes;[/B]
    	}
    ...
    }
    Here is what I would do:
    public static class ParkedCar
    {
    	private String make;
    	private String model;
    	private String color;
    	private String license;
    	private int minutesParked;
     
    	public ParkedCar(String make, String model, String color, String license, int [B]minutes[/B]) 
    	{
    		make = make;
    		model = model;
    		color = color;
    		license = license;
    		minutesParked = minutes;
    	}
    ...
    }

    I can see what your trying to do, but it is unnecessary. By saying this.variable or object.variable it means you are trying to reference a variable, which you are, but you are referencing a variable that is part of your class, so you can just say variable1 = variable2 where variable1 is a class variable and variable2 is a constructor variable.


    2nd issue. Non-static variables. The class that contains the calculateFine() method is static. Because of this, any variable you reference must also be static. But, the minutesParked and minutesPurchased methods have not been declared static.
    public void calculateFine()
    {
    	if(ParkedCar.minutesParked - ParkingMeter.minutesPurchased <= 60)
    		fine = BASE_FINE;
    	else
    		fine = BASE_FINE + (HOURLY_FINE * ((ParkedCar.minutesParked - ParkingMeter.minutesPurchased) / 60 ));
    }
    So, go to the ParkedCar and ParkingMeter classes and set these variables as static:
    public static class ParkingMeter
    {
    	private [B]static [/B]int minutesPurchased;
    ...
    }
     
    public static class ParkedCar//()
    {
    	private String make;
    	private String model;
    	private String color;
    	private String license;
    	private [B]static [/B]int minutesParked;
    ...
    }
    You also have this issue later on with the variable fine:
    public static class PoliceOfficer
    {
    ...
    	@Override
    	public String toString()
    	{
    		String string = "Officer Data: \n Name: " + name + "\n Badge Number: " + badgeNumber +
    		"\n Minutes Illegally Parked: " + (ParkedCar.minutesParked - ParkingMeter.minutesPurchased) +
    		"Fine: $" + ParkingTicket.[B]fine[/B];
    		return string;
    	} 
    }
    So we need to set the fine variable as static:
    public static class ParkingTicket
    {
    	private ParkedCar car;
    	private PoliceOfficer officer;
    	private [B]static [/B]double fine;
    	private int minutes;
    	double BASE_FINE = 25.0;
    	double HOURLY_FINE = 10.0;
    ...
    }

    3rd issue. I'm not entirely sure what you are trying to do here, and neither is the complier:
    public Patrol(ParkedCar car, ParkingMeter meter)
    {
    	ParkingTicket();
    }
    What this is saying is that you are creating the constructor for a Patrol object, which you havent made a class for. It is also saying that you are attempting to call a method "ParkingTicket()", which also doesnt exist in this class.
    I think what you are trying to do is create a ParkingTicket object. Unfortunately, you dont have a constructor for ParkingTicket that has no constructors AND you have the declare the object as "new". I am also going to assume you want this to be a void method (doesnt return anything). So here is what that would look like:
    public void Patrol(ParkedCar car, ParkingMeter meter)
    {
    	new ParkingTicket(car,this,0.0,0);
    }

    I would lastly advise removing all the unnecessary this.variable calls and setting method variables that are differently named than the class variables. This will prevent any mistaken mix-ups in variable changes.

    Here is my suggested final code:
     
    public class FileOpen
    {
    	public static void main(String args[])
    	{
    	}
     
    	public static class ParkedCar
    	{
    		private String make;
    		private String model;
    		private String color;
    		private String license;
    		private static int minutesParked;
     
    		public ParkedCar(String makev, String modelv, String colorv, String licensev, int minutes) 
    		{
    			make = makev;
    			model = modelv;
    			color = colorv;
    			license = licensev;
    			minutesParked = minutes;
    		}
     
    		public ParkedCar(ParkedCar car2)
    		{
    			make = car2.make;
    			model= car2.model;
    			color = car2.color;
    			license = car2.license;
    			minutesParked = car2.minutesParked;
    		}
     
    		public String getColor() 
    		{
    			return color;
    		}
     
    		public void setColor(String colorv) 
    		{
    			color = colorv;
    		}
     
    		public String getLicense() 
    		{
    			return license;
    		}
     
    		public void setLicense(String licensev) 
    		{
    			license = licensev;
    		}
     
    		public String getMake() 
    		{
    			return make;
    		}
     
    		public void setMake(String makev) 
    		{
    			make = makev;
    		}
     
    		public int getMinutesParked() 
    		{
    			return minutesParked;
    		}
     
    		public void setMinutes(int minutes) 
    		{
    			minutesParked = minutes;
    		}
     
    		public String getModel() 
    		{
    			return model;
    		}
     
    		public void setModel(String modelv) 
    		{
    			this.model = modelv;
    		}
     
    		@Override
    		public String toString()
    		{
    			String string = "Car Data: \n Make: " + make + "\n Model: " + model +
    			"\n Color: " + color + "\n License Plate: " + license +
    			"\n Minutes Parked: " + minutesParked;
    			return string;
    		}
    	}
     
    	public static class ParkingMeter
    	{
    		private static int minutesPurchased;
     
    		public ParkingMeter(int minutesPurchasedv) 
    		{
    			minutesPurchased = minutesPurchasedv;
    		}
     
    		public void setMinutesPurchased(int minutesPurchasedv)
    		{
    			minutesPurchased = minutesPurchasedv;
    		}
     
    		public int getMinutesPurchased()
    		{
    			return minutesPurchased;
    		}
    	}
     
    	public static class ParkingTicket
    	{
    		private ParkedCar car;
    		private PoliceOfficer officer;
    		private static double fine;
    		private int minutes;
    		double BASE_FINE = 25.0;
    		double HOURLY_FINE = 10.0;
     
    		public ParkingTicket(ParkedCar carv, PoliceOfficer officerv, double finev, int minutesv) 
    		{
    			car = carv;
    			officer = officerv;
    			fine = finev;
    			minutes = minutesv;
    		}
     
    		public ParkingTicket(ParkingTicket ticket2)
    		{
    			car = ticket2.car;
    			officer = ticket2.officer;
    			fine = ticket2.fine;
    			minutes = ticket2.minutes;
    		}
     
    		public void calculateFine()
    		{
    			if(ParkedCar.minutesParked - ParkingMeter.minutesPurchased <= 60)
    				fine = BASE_FINE;
    			else
    				fine = BASE_FINE + (HOURLY_FINE * ((ParkedCar.minutesParked - ParkingMeter.minutesPurchased) / 60 ));
    		}
     
    		public double getBASE_FINE() 
    		{
    			return BASE_FINE;
    		}
     
    		public void setBASE_FINE(double BASE_FINEv) 
    		{
    			BASE_FINE = BASE_FINEv;
    		}
     
    		public double getHOURLY_FINE() 
    		{
    			return HOURLY_FINE;
    		}
     
    		public void setHOURLY_FINE(double HOURLY_FINEv) 
    		{
    			HOURLY_FINE = HOURLY_FINEv;
    		}
     
    		public ParkedCar getCar() 
    		{
    			return car;
    		}
     
    		public void setCar(ParkedCar carv) 
    		{
    			car = carv;
    		}
     
    		public double getFine() 
    		{
    			return fine;
    		}
     
    		public void setFine(double finev) 
    		{
    			fine = finev;
    		}
     
    		public int getMinutes() 
    		{
    			return minutes;
    		}
     
    		public void setMinutes(int minutesv) 
    		{
    			minutes = minutesv;
    		}
     
    		public PoliceOfficer getOfficer() 
    		{
    			return officer;
    		}
     
    		public void setOfficer(PoliceOfficer officerv) 
    		{
    			officer = officerv;
    		}
     
    	}
     
    	public static class PoliceOfficer
    	{
    		private String name;
    		private String badgeNumber;
     
    		public PoliceOfficer(String namev, String badgeNumberv) 
    		{
    			name = namev;
    			badgeNumber = badgeNumberv;
    		}
     
    		public PoliceOfficer(PoliceOfficer officer2)
    		{
    			name = officer2.name;
    			badgeNumber = officer2.badgeNumber;
    		}
     
    		public void Patrol(ParkedCar carv, ParkingMeter meterv)
    		{
    			new ParkingTicket(carv,this,0.0,0);
    		}
     
    		@Override
    		public String toString()
    		{
    			String string = "Officer Data: \n Name: " + name + "\n Badge Number: " + badgeNumber +
    			"\n Minutes Illegally Parked: " + (ParkedCar.minutesParked - ParkingMeter.minutesPurchased) +
    			"Fine: $" + ParkingTicket.fine;
    			return string;
    		} 
    	}
    }

  8. #8
    Junior Member
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many Errors

    A great thanks to everyone! Thank you for taking time out to help a n00b!

    I apologize if I didn't seem to "get it", but I'm just a beginner and hoping to become proficient in this language, as well as many others.

  9. #9
    Junior Member
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many Errors

    I also want to add that I was using a UML and maybe I was not reading it properly, but under certain classes it said "setters and getters omitted".

    I took that as being they omitted them from printing for the sake of brevity in the UML, so I was just hitting ALT + Insert to put in the setters and getters for each class that stated they were omitted.

    Once again, thank you all so much for your help!

  10. #10
    Junior Member
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many Errors

    Sorry to bug you guys again. I need the patrol (ParkingTicket object) to return a value.

    I have fixed everything else you all have helped me with and this seems to be the one loose string. Below is my main class and method and the ParkingTicket object that was created.


     
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            ParkedCar car = new ParkedCar("Ford", "1968", "Blue", "BR459", 125);
     
            ParkingMeter meter = new ParkingMeter(60);
     
            PoliceOfficer officer = new PoliceOfficer("John McClane", "8986");
     
            ParkingTicket ticket = officer.patrol(car, meter);
     
            if(ticket != null)
                System.out.println(ticket);
            else
                System.out.println("No Ticket is Needed!");
        }


    I feel I may be on the right track, but all I am doing is taking the calculateFine() method and applying to here as well.

    For further guidance, this is what it states for the Police Officer class (which the ParkingTicket/patrol object is at)

    Police Officer Class:This class should simulate a cop inspecting parked cars. The responsibilities of the class are as follows:
    To know the cop's name and badge number.
    To examine a ParkedCar object and a ParkingMeter object, and determine whether the parking time of the car has expired.
    To issue a parking ticket -- generate a ParkingTicket object-- if the time of the car has expired.

     
    private ParkingTicket patrol(ParkedCar carv, ParkingMeter meterv){
                        double ticket;
                        if(ParkedCar.minutesParked > ParkingMeter.minutesPurchased)
                            if(ParkedCar.minutesParked - ParkingMeter.minutesPurchased <= 60)
                                ticket = 25.0;
                            else
                                ticket = 25.0 + (10.0 * ((ParkedCar.minutesParked - ParkingMeter.minutesPurchased) / 60));
                            return ticket; ;
     
    		}

    Once again, thank you for all your help!

  11. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Many Errors

    private ParkingTicket patrol(ParkedCar carv, ParkingMeter meterv)
    {
                        double ticket;
                        if(ParkedCar.minutesParked > ParkingMeter.minutesPurchased)
                            if(ParkedCar.minutesParked - ParkingMeter.minutesPurchased <= 60)
                                ticket = 25.0;
                            else
                                ticket = 25.0 + (10.0 * ((ParkedCar.minutesParked - ParkingMeter.minutesPurchased) / 60));
                            return ticket; ;
    }

    Ok, well first off, you are wanting to return a ParkingTicket but you have declared ticket to be a double. So, in order to return a ParkingTicket, we have to make a ParkingTicket. Which means, we need a ParkedCar, a PoliceOfficer, a fine, and a minutes variable in order to create a ParkingTicket. Now, I would assume what the ticket variable in this code is supposed to be is the fine variable. And I would assume the minutes variable would be minutesParked - minutesPurchased ?

    We also want to reference the variables of the objects we sent, not just the class variables. So, instead of ParkedCar.minutesParked, we want carv.minutesParked because that gets the minutesParked variable of our carv object.

    If so, here is what you want:
    private ParkingTicket patrol(ParkedCar carv, ParkingMeter meterv)
    {
                        double ticket;
                        if(carv.minutesParked > meterv.minutesPurchased)
                        {
                            if(carv.minutesParked - meterv.minutesPurchased <= 60)
                                ticket = 25.0;
                            else
                                ticket = 25.0 + (10.0 * ((carv.minutesParked - meterv.minutesPurchased) / 60));
                         }
                         return new ParkingTicket(carv,meterv,ticket, minutesParked - minutesPurchased);
    }

    You may have the change that a bit if your calculations are wrong or if I'm incorrect with my assumptions.

    Also, on a side note, when you have multiple if/else statements, it never hurts to include brackets. I'm not entirely sure, but I think the way you had it indicated the else being with the first if, where you actually wanted it with the second. By putting the brackets around your if/else statement, you are making sure that the intentions behind the if/else statements are not confused by the JVM.
    Last edited by aussiemcgr; July 16th, 2010 at 10:39 PM.

Similar Threads

  1. Getting errors
    By Nonire in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2010, 01:21 PM
  2. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 05:41 AM
  3. not spelling errors
    By britishgoose01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:59 PM
  4. Exception Errors
    By TIMBERings in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 02:13 AM
  5. Ambiguity and non-static variable reference error in java
    By jenseits in forum AWT / Java Swing
    Replies: 5
    Last Post: December 8th, 2008, 07:04 PM