Set and Get Methods

use in seperate class

Page 1 of 1

11 Replies - 3892 Views - Last Post: 07 October 2008 - 01:34 PM Rate Topic: -----

#1 NeekWorld  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Set and Get Methods

Post icon  Posted 06 October 2008 - 03:59 PM

ok im getting an error when i try to create an instance of my ticket class. i have searched in all my books and on the net and can find no info on how to get the fine from the set method to the output.
here is what i have on the ticket class.

program simulates a officer issuing/not issuing a parking ticket to a parked car

 class Ticket{
	ParkedCar car = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 125);//also declared in main
	//officer = new PoliceOfficer("Joe Friday", "4788");// also declared in main
	ParkingMeter meter = new ParkingMeter(60);//also declared in main
	double fine;
	int minutes;
	double BASE_FINE = 25.0;
	double HOURLY_FINE = 10.0;
	int temp;//for use with finding min left to charge for

	//ticket constructor
	Ticket(double newFine){
			fine = newFine;
	}
	//set fines
	public void setFines(int m, double newFine){
		minPurchased = m;
		newFine = 0.0;
		if (car.minParked > meter.minPurchased){
			newFine = BASE_FINE;
			temp = (m - 60);
			}
			while (temp > 60){
				newFine += HOURLY_FINE;
				temp = temp - 60;
			}
		//else{
		//	System.out.println("No sitations given");
		//}
		}
	
	public double getFines(){
		return newFine;
	}
}


and this is the code i am trying to use to create the instance.

Ticket ticket = new Ticket(newFine);


i know there are a few lines i have commented out i will need them later i was just trying to get some output so i could test it.
i don't really want to have to include all the arguments when i create the officer, car, or parking meter but i am unsure how to pull that duplicate info from the main method, i have tried but i get a compiler error. if anyone knows how i can do that as well.

for clarification the last argument (125) in the car is the total minutes parked, and the (60) in the parking meter is the minutes purchased.

Is This A Good Question/Topic? 0
  • +

Replies To: Set and Get Methods

#2 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: Set and Get Methods

Posted 06 October 2008 - 04:05 PM

Can you please include the classes ParkedCar and ParkingMeter?
Was This Post Helpful? 0
  • +
  • -

#3 NeekWorld  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Set and Get Methods

Posted 06 October 2008 - 04:26 PM

View Poststauffski, on 6 Oct, 2008 - 04:05 PM, said:

Can you please include the classes ParkedCar and ParkingMeter?


ok wasnt sure if you wanted all of the other code too. :)

class ParkedCar{
	//variables
	String make = "";
	String model = "";
	String color = "";
	String licenseNumber = "";
	int minParked;
	ParkedCar(String newMake, String newModel, String newColor, String newLicenseNumber, int newMinutesParked){
			make = newMake;
		model = newModel;
			color = newColor;
		licenseNumber = newLicenseNumber;
		minParked = newMinutesParked;
		}

}

class ParkingMeter{
	int minPurchased;
	//parking meter constructor
	ParkingMeter(int newMin){
			minPurchased = newMin;
	}
	//set min using random number??
	public void setMinPurchased(int m){
		//m = 10 + (int)(Math.random() * 120);//attemting to randomize min car is parked
		minPurchased = m;
		
	}
	public int getMinPurchased(){
		return minPurchased;
	}
} 


these two classes output the info i need. the only one i was really having a problem with was the ticket class.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Set and Get Methods

Posted 06 October 2008 - 05:48 PM

	public void setFines(int m, double newFine){
		minPurchased = m;
 


minPurchased is not defined and kind of useless as setFines() is not called by anybody

	public double getFines(){
		return newFine;



newFine is not declared... you probably meant "fine" into which you store "newFine" there:
   Ticket(double newFine){
			fine = newFine;
	}


This post has been edited by pbl: 06 October 2008 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 NeekWorld  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Set and Get Methods

Posted 06 October 2008 - 06:04 PM

View Postpbl, on 6 Oct, 2008 - 05:48 PM, said:

	public void setFines(int m, double newFine){
		minPurchased = m;
 


minPurchased is not defined and kind of useless as setFines() is not called by anybody

	public double getFines(){
		return newFine;



newFine is not declared... you probably meant "fine" into which you store "newFine" there:
   Ticket(double newFine){
			fine = newFine;
	}



ok yes minPurchased is defined in the ParkingMeter class, so the Ticket class should pull it from there. and i did not include my entire program here i am trying to get the setFines to output to a dialog box. but that is not the initial problem. the error is when i try to create a Ticket instance. since there is no actual parameter that is predefined i want it to use the one generated in the setFines method.
dont get me wrong im sure there will be other errors popping up but i need to figure out why the Ticket is not generating first.

the actual error im getting is

ParkingTicket.java:27 cannot find symbol
symbol: constructor Ticket()
location: class Ticket
Ticket ticket = new Ticket();
.........................^

if im understanding the error correctly it says that there is something wrong with my constructor but it is almost exactly the same as my other constructors only missing an initial value (which i dont want). i have tried putting in the variables i used in the Ticket class inside the () but it says they are all wrong as well as giving me the same error i noted above.

This post has been edited by NeekWorld: 06 October 2008 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: Set and Get Methods

Posted 06 October 2008 - 07:43 PM

pbl is right, I would have gotten back to you sooner, sorry. There are two things wrong with
public void setFines(int m, double newFine){
        minPurchased = m;
}

//it should be
public void setFines(int m){// don't use the variable newFine in this method, so don't send it.
    meter.minPurchased(m); //you must access the class meter
}



However, the problem you are asking about... it seems like you aren't sending a double in your constructor, that's how you wrote it if you look in the ticket class.
Ticket ticket = new Ticket(double);
    //you need a double ------^



Feel free to ask any questions.

Good Luck!
Was This Post Helpful? 0
  • +
  • -

#7 NeekWorld  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Set and Get Methods

Posted 06 October 2008 - 08:00 PM

View Poststauffski, on 6 Oct, 2008 - 07:43 PM, said:

pbl is right, I would have gotten back to you sooner, sorry. There are two things wrong with
public void setFines(int m, double newFine){
        minPurchased = m;
}

//it should be
public void setFines(int m){// don't use the variable newFine in this method, so don't send it.
    meter.minPurchased(m); //you must access the class meter
}



However, the problem you are asking about... it seems like you aren't sending a double in your constructor, that's how you wrote it if you look in the ticket class.
Ticket ticket = new Ticket(double);
    //you need a double ------^



Feel free to ask any questions.

Good Luck!


ok i get what your saying about the meter.minPurchased i did have that at one time but changed it due to another compiler error.
and i have tried the
Ticket ticket = new Ticket(double);

and i get a related error to creating the ticket instance.


errors are now
ParkingTicket.java: 30: '.class' expected
Ticket ticket = new Ticket(double);
..................................................^

ParkingTicket.java: 30: ')' expected
Ticket ticket = new Ticket(double);
...................................................^

thank you

This post has been edited by NeekWorld: 06 October 2008 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: Set and Get Methods

Posted 06 October 2008 - 08:17 PM

Okay, post, all of your code. Including your main class. Don't leave out anything. I want to compile precisely what you are compiling.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Set and Get Methods

Posted 06 October 2008 - 08:30 PM

View PostNeekWorld, on 6 Oct, 2008 - 08:00 PM, said:

View Poststauffski, on 6 Oct, 2008 - 07:43 PM, said:

pbl is right, I would have gotten back to you sooner, sorry. There are two things wrong with
public void setFines(int m, double newFine){
        minPurchased = m;
}

//it should be
public void setFines(int m){// don't use the variable newFine in this method, so don't send it.
    meter.minPurchased(m); //you must access the class meter
}



However, the problem you are asking about... it seems like you aren't sending a double in your constructor, that's how you wrote it if you look in the ticket class.
Ticket ticket = new Ticket(double);
    //you need a double ------^



Feel free to ask any questions.

Good Luck!


ok i get what your saying about the meter.minPurchased i did have that at one time but changed it due to another compiler error.
and i have tried the
Ticket ticket = new Ticket(double);

and i get a related error to creating the ticket instance.


errors are now
ParkingTicket.java: 30: '.class' expected
Ticket ticket = new Ticket(double);
..................................................^

ParkingTicket.java: 30: ')' expected
Ticket ticket = new Ticket(double);
...................................................^

thank you


Your only constructor for Ticket is

	//ticket constructor
	Ticket(double newFine){
			fine = newFine;
	}



which means that to create a Ticket you have to pass it the fine as a double
Was This Post Helpful? 0
  • +
  • -

#10 NeekWorld  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Set and Get Methods

Posted 06 October 2008 - 08:47 PM

ok i have added a few things since i last included my code. there is now a random number generator for minParked and minPurchased.
here is the entire program

import javax.swing.JOptionPane;
import java.util.Random;

public class ParkingTicket{
	

	public static void main(String[] args){

	//create a car
	ParkedCar car = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM");
	car.getMinParked();

	//create officer
	PoliceOfficer officer = new PoliceOfficer("Joe Friday", "4788");

	//set meter
	ParkingMeter meter = new ParkingMeter(0);
	meter.getMinPurchased();
		
	Ticket ticket = new Ticket(double);// doesnt work yet :)
	//ticket.getFines();

	/*//let officer patrol (have not gotten to this part yet, need to be able to issue ticket first)
	ParkingTicket ticket = officer.patrol(car,meter);
	
	//loop for checking if officer issued a ticket
	if (ticket != null){
		System.out.println(ticket);
	}
	else {
		System.out.println("No sitations given");
	}*/	


	//output/display results  (this is only to check output need to uncomment loop yet)
	String output = "Make: " + car.make + "\nModel: " + car.model + "\nColor: " + car.color +
			"\nLicense Plate Number: " + car.licenseNumber + "\nMinutesParked: " + car.minParked;
	output += "\n\nOfficer: " + officer.name + "\nBadge Number: " + officer.badgeNum;
	output += "\n\nMinutes Purchased: " + meter.minPurchased;
	//output += "\n\nParking Ticket Issued: " + ticket.fine;
	JOptionPane.showMessageDialog(null, output);

	}

}

class ParkedCar{
	//variables
	String make = "";
	String model = "";
	String color = "";
	String licenseNumber = "";
	int minParked;
	int min;
	//parkedcar constructor
	ParkedCar(String newMake, String newModel, String newColor, String newLicenseNumber){
			make = newMake;
		model = newModel;
			color = newColor;
		licenseNumber = newLicenseNumber;
		}
	//set min parked using random number
	public void setMinParked(int min){
		// Generate a random min
			Random generator = new Random();
			min = generator.nextInt(10) * 15;
		minParked = min;
	}
	//return minutes car is parked
	public int getMinParked(){
		setMinParked(min);
		return minParked;
	}

}

class ParkingMeter{
	int minPurchased;
	private Random generator;
	int min;

	//parking meter constructor
	ParkingMeter(int newMin){
			minPurchased = newMin;
	}
	//set min using random number
	public void setMinPurchased(int min){
		// Generate a random min
			Random generator = new Random();
			min = generator.nextInt(10) * 15;
		minPurchased = min;
	}
	//return minutes purchased
	public int getMinPurchased(){
		setMinPurchased(min);
		return minPurchased;
	}
}

class Ticket{
	ParkedCar car = new ParkedCar();
	//officer = new PoliceOfficer();
	ParkingMeter meter = new ParkingMeter(0);
	double fine;
	int minutes;
	double BASE_FINE = 25.0;
	double HOURLY_FINE = 10.0;
	int temp;//for use with finding min left to charge for

	//ticket constructor
	Ticket(double newFine){
			fine = newFine;
	}
	
	
	//set fines
	public void setFines(int m, double newFine){
		meter.minParked(m);
		newFine = 0.0;
		if (car.minParked > meter.minPurchased){
			newFine = BASE_FINE;
			temp = (m - meter.minPurchased);
			
			while (temp > 60){
				newFine += HOURLY_FINE;
				temp = temp - 60;
			}
		}
		else{
			System.out.println("No sitation given");
		}
	}
	//return fines
	public double getFines(){
		setFines(m);
		return fine;
	}
}

class PoliceOfficer{
	String name = "";
	String badgeNum = "";
	//officer constructor
	PoliceOfficer(String newName, String newBadge){
			name = newName;
		badgeNum = newBadge;

		}

} 

Was This Post Helpful? 0
  • +
  • -

#11 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: Set and Get Methods

Posted 06 October 2008 - 10:27 PM

Ohhhhhk... There is a lot of code and a couple of inconsistencies, but it can be worked through. First I am going to remove all of your comments and put in comments where I think something is wrong. I warn you I will conclude this post with something you may not like, but it may be necessary.

Class ParkingTicket
public class ParkingTicket{
    public static void main(String[] args){

    ParkedCar car = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM");
    car.getMinParked();

    PoliceOfficer officer = new PoliceOfficer("Joe Friday", "4788");

    ParkingMeter meter = new ParkingMeter(0);
    meter.getMinPurchased();
        
    Ticket ticket = new Ticket(double);//sorry for being unclear, but when I said you needed "Ticket ticket =new Ticket(double)," I meant a double variable, not the world "double." Sorry

    String output = "Make: " + car.make + "\nModel: " + car.model + "\nColor: " + car.color +
            "\nLicense Plate Number: " + car.licenseNumber + "\nMinutesParked: " + car.minParked;
    output += "\n\nOfficer: " + officer.name + "\nBadge Number: " + officer.badgeNum;
    output += "\n\nMinutes Purchased: " + meter.minPurchased;
    JOptionPane.showMessageDialog(null, output);
    }
}



Class Ticket
class Ticket{
    ParkedCar car = new ParkedCar();//take a look at your ParkedCar constructor, the parameters do not match what you have declared here. You need four String variables.
    ParkingMeter meter = new ParkingMeter(0);
    double fine;
    int minutes;
    double BASE_FINE = 25.0;
    double HOURLY_FINE = 10.0;
    int temp;

    Ticket(double newFine){
            fine = newFine;
    }
    
    public void setFines(int m, double newFine){
        meter.minParked(m);//you call a method that does not exist. Look back into the ParkingMeter class. I believe what you are trying to call is setMinPurchased().
        newFine = 0.0;
        if (car.minParked > meter.minPurchased){
            newFine = BASE_FINE;
            temp = (m - meter.minPurchased);
            
            while (temp > 60){
                newFine += HOURLY_FINE;
                temp = temp - 60;
            }
        }
        else{
            System.out.println("No sitation given");
        }
    }

    public double getFines(){
        setFines(m);//m is not an accessible variable. Also, the parameters do not match, setFines(int, double), you try to use setFines(int)
        return fine;
    }
}



It seems like you put a lot of effort into, but that may just be the problem, it sucks, but sometimes the best thing to do is just start over. I've done it, so don't be afraid to. The second time through can end up in much better code because your understanding for what you are writing becomes, much clearer. However, if you think you can finish it up, that's fine too. But you will find the second time through, your code will be much neater.

On another note, I must ask, why are you writing this program?
Was This Post Helpful? 0
  • +
  • -

#12 NeekWorld  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Set and Get Methods

Posted 07 October 2008 - 01:34 PM

View Poststauffski, on 6 Oct, 2008 - 10:27 PM, said:

Ohhhhhk... There is a lot of code and a couple of inconsistencies, but it can be worked through. First I am going to remove all of your comments and put in comments where I think something is wrong. I warn you I will conclude this post with something you may not like, but it may be necessary.

Class ParkingTicket
public class ParkingTicket{
    public static void main(String[] args){

    ParkedCar car = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM");
    car.getMinParked();

    PoliceOfficer officer = new PoliceOfficer("Joe Friday", "4788");

    ParkingMeter meter = new ParkingMeter(0);
    meter.getMinPurchased();
        
    Ticket ticket = new Ticket(double);//sorry for being unclear, but when I said you needed "Ticket ticket =new Ticket(double)," I meant a double variable, not the world "double." Sorry

    String output = "Make: " + car.make + "\nModel: " + car.model + "\nColor: " + car.color +
            "\nLicense Plate Number: " + car.licenseNumber + "\nMinutesParked: " + car.minParked;
    output += "\n\nOfficer: " + officer.name + "\nBadge Number: " + officer.badgeNum;
    output += "\n\nMinutes Purchased: " + meter.minPurchased;
    JOptionPane.showMessageDialog(null, output);
    }
}



Class Ticket
class Ticket{
    ParkedCar car = new ParkedCar();//take a look at your ParkedCar constructor, the parameters do not match what you have declared here. You need four String variables.
    ParkingMeter meter = new ParkingMeter(0);
    double fine;
    int minutes;
    double BASE_FINE = 25.0;
    double HOURLY_FINE = 10.0;
    int temp;

    Ticket(double newFine){
            fine = newFine;
    }
    
    public void setFines(int m, double newFine){
        meter.minParked(m);//you call a method that does not exist. Look back into the ParkingMeter class. I believe what you are trying to call is setMinPurchased().
        newFine = 0.0;
        if (car.minParked > meter.minPurchased){
            newFine = BASE_FINE;
            temp = (m - meter.minPurchased);
            
            while (temp > 60){
                newFine += HOURLY_FINE;
                temp = temp - 60;
            }
        }
        else{
            System.out.println("No sitation given");
        }
    }

    public double getFines(){
        setFines(m);//m is not an accessible variable. Also, the parameters do not match, setFines(int, double), you try to use setFines(int)
        return fine;
    }
}



It seems like you put a lot of effort into, but that may just be the problem, it sucks, but sometimes the best thing to do is just start over. I've done it, so don't be afraid to. The second time through can end up in much better code because your understanding for what you are writing becomes, much clearer. However, if you think you can finish it up, that's fine too. But you will find the second time through, your code will be much neater.

On another note, I must ask, why are you writing this program?


Quote

//take a look at your ParkedCar constructor, the parameters do not match what you have declared here. You need four String variables.

as for this i know i removed to test it. i had it in earlier i was just trying to get the ticket instance to be generated.


Quote

//you call a method that does not exist. Look back into the ParkingMeter class. I believe what you are trying to call is setMinPurchased().

as for this i was only doing as you suggested and it should be getMinPurchased so i can have the minutes to generate the amount for the fine

Quote

//m is not an accessible variable. Also, the parameters do not match, setFines(int, double), you try to use setFines(int)

ok this was changed in reference to the one above. and i know it should have the variable name in there not the return type.

but this still doesnt fix the original issue i was having which was the ticket not generating at all. i get the same error as before even if i fix what you have noted back to what i had.

and as for why i am doing this it was one of the projects given to us by our teacher. and the reason there is so much code is because that is what was asked for specifically in the directions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1