This project I had steps which I will place below and below the steps I
will place my project (the class and driver).
What I am not understanding is What does a constructor do in laymans
terms??
also is my copy method correct and steps V. and VI. I just need help doing
those all together.
I know that this alot but I went to my instructor and I was there for 5
hours and she was not much help.
Please Help Me!!
Thank You In Advance
STEPS:
I. Update your class definition to contain Constructors (at least 3):
a) one that includes all the instance variables (except results) as
parameters;
b) one that includes only two of the instance variables (except results)
as parameters; and
c) one that includes zero parameters ? this last one should replace the
input method.
II. Update your class definition to contain an equals method that
checks if one object has the same features (all the same values for the
instance variables, except result) as another object.
III. Update your class definition to contain a copy method that copies
all the features (all the same values for the instance variables, except
result) from one object to another object. You will want to use this
method when you want more features the same than are different. Use this
method to create objects to test your equals method for all possibilities:
a) all the same features; b) all the same except the first feature; c) all
the same except the second feature; d) all the same except the third
feature; e) etc., until all possibilities are checked. Then, use your
mutator methods to change one of the values.
IV. Update your class definition to use the DecimalFormat class in your
output method so that all numbers have meaningful output:
a) money formatted with $, commas and decimal to 2 places representing
cents [Examples: $4,567.89 and $6.00];
b) decimal results to one, two, or three decimal places rather
than six or ten [Example: GPA = 2.345];
V. Write a new main method in the driver (or demo) program to test ALL
the constructors and new methods you create. If you have three (or four)
features to test in your equals method, you will need to create five (six)
objects to test all the possible options. [You may use constant arguments
to test your constructors except the one with the zero parameter?use
input.]
VI. In the driver program, use your mutator methods to complete the
assignment of all necessary values to an object before calling the
calculate method for each. Then, print out a table showing the results
for all the objects (7, 8, 9 or more) you created.
THE PROJECT:
DRIVER:
* This program calculates the fine of a speeding ticket
* given based on Parkvilles' ticket system.
* It also changes data and return data values
* checks for equality to see if there is more than one
* driver in system with same name.
* ****************************************************/
public class TicketDriver
{
public static void main(String[] args)
{
String dName;
String highestName = " ";
int dSpeed = 0 ;
int total = 0;
int cSpeedLimit = 0;
char cResponse = 'y';
double highestTicket = 0;
double fine = 0;
//Create an object, enter data, process, and display pay information
Ticket ticket1 = new Ticket();
ticket1.ticketInfo();
ticket1.fine();
ticket1.schoolZone();
Ticket ticket2 = new Ticket();
ticket2.ticketInfo();
ticket2.fine();
ticket2.schoolZone();
Ticket ticket3 = new Ticket();
ticket3.ticketInfo();
ticket3.fine();
ticket3.schoolZone();
Ticket ticket4 = new Ticket();
ticket4.ticketInfo();
ticket4.fine();
ticket4.schoolZone();
//Print
ticket1.tableHeading();
ticket1.tableData();
ticket2.tableData();
ticket3.tableData();
ticket4.tableData();
//**************************************************************************************************
//Test the constructors
ticket1.setName("Jack Black");
ticket1.setSpeed(85);
ticket1.setSpeedLimit(25);
ticket1.setResponse('y');
ticket2.setName("James Brown");
ticket2.setSpeed(75);
ticket2.setSpeedLimit(55);
ticket2.setResponse('n');
// Test the equals method
if (ticket1.equals(ticket2))&&
(ticket1.equals(ticket3) &&
(ticket1.equals(ticket3) &&
(ticket1.equals(ticket4)
{
System.out.println("Drivers have identical information.");
}
else
{
System.out.println("Drivers have identical information.");
}
//***********************************************************************************************************
//Test the set methods
//Car Driver 1
System.out.print("You entered the drivers name as: " +
ticket1.getName());
System.out.println();
System.out.print("Please enter the correct name:");
dName = Input.readLine();
ticket1.setName(dName);
System.out.print("You entered the drivers speed as: " +
ticket1.getSpeed());
System.out.println();
System.out.print("Please enter the correct speed:");
dSpeed = Input.readInt();
ticket1.setSpeed(dSpeed);
System.out.print("You entered the speed limit as: " +
ticket1.getSpeedLimit());
System.out.println();
System.out.print("Please enter the correct speed limit:");
cSpeedLimit = Input.readInt();
ticket1.setSpeedLimit(cSpeedLimit);
System.out.print("You entered " + ticket1.getResponse() + " as the
response to driving in a school zone");
System.out.println();
System.out.print("Please enter the correct response:");
cResponse = Input.readChar();
ticket1.setResponse(cResponse);
//Print
ticket1.tableHeading();
ticket1.tableData();
ticket2.tableData();
ticket3.tableData();
ticket4.tableData();
System.out.print("You entered the drivers speed for driver #2 as: " +
ticket2.getSpeed());
System.out.println();
System.out.print("Please enter the correct speed:");
dSpeed = Input.readInt();
ticket2.setSpeed(dSpeed);
System.out.print("You entered the speed limit for driver #3 as: " +
ticket3.getSpeedLimit());
System.out.println();
System.out.print("Please enter the correct speed limit:");
cSpeedLimit = Input.readInt();
ticket3.setSpeedLimit(cSpeedLimit);
System.out.print("You entered the response for driving in a school zone
for driver #4 as " + ticket4.getResponse());
System.out.println();
System.out.print("Please enter the correct response:");
cResponse = Input.readChar();
ticket4.setResponse(cResponse);
//Print
ticket1.tableHeading();
ticket1.tableData();
ticket2.tableData();
ticket3.tableData();
ticket4.tableData();
//*************************************************************************************************************
//Find the highest ticket paid and the person
fine = ticket1.getFine();
{
if (fine>highestTicket)
{
highestTicket = fine;
highestName = ticket1.getName();
}
fine = ticket2.getFine();
if (fine>highestTicket)
{
highestTicket = fine;
highestName = ticket1.getName();
}
fine = ticket3.getFine();
if (fine>highestTicket)
{
highestTicket = fine;
highestName = ticket1.getName();
}
fine = ticket4.getFine();
if (fine>highestTicket)
{
highestTicket = fine;
highestName = ticket1.getName();
}
System.out.print(highestName + " has the highest ticket value of $"
+highestTicket);
System.out.println();
}
}
}
//************************************************************************************************************
//}//end main
//}//end class TicketDriver
CLASS:
* This class inputs the speeders info, calculates and
* outputs.
*
********************************************************************************************/
import java.text.DecimalFormat;
public class Ticket
{
public String name; //Input driver's name
private int speed; //Input driver's speed
private int speedLimit; //Input of speed limit
private double fine; //Calculates fine Amount
private char response; //Input of whether driving in school zone
//********************************************************************************************
public Ticket(String name, int speed, int speedLimit, char response)
{
this.name = name;
this.speed = speed;
this.speedLimit = speedLimit;
this.response = response;
} // end Ticket constructor
//*********************************************************************************************
public Ticket (String name, int speedLimit)
{
this.name = name;
this.speedLimit = speedLimit;
}//end Ticket constructor
//********************************************************************************************
public Ticket()
{
System.out.print("Enter driver's name: ");
name = Input.readLine ();
System.out.print("Enter driver's speed: ");
speed = Input.readInt ();
while (speed > 100)
{
System.out.println("Number of hours illegal");
System.out.print("Please enter correct number of hours:");
speed = Input.readInt();
}
System.out.print("Was driver in school zone during school hours? Y or N:
");
response = Input.readChar ();
System.out.print("Enter speed limit: ");
speedLimit = Input.readInt ();
}//end Drivers Information
//********************************************************************************************
//Make Copy
public Ticket makeCopy()
{
Ticket ticket = new Ticket();
ticket.name = this.name;
ticket.speed = this.speed;
ticket.speedLimit = this.speedLimit;
ticket.response = this.response;
return ticket;
} // end makeCopy
//*********************************************************************************************
//This method checks to see if any of the car drivers have the same
information
public boolean equals (Ticket otherTicket)
{
if ((this.name.equalsIgnoreCase(otherTicket.name)) &&
(this.speed == (otherTicket.speed))&&
(this.speedLimit == (otherTicket.speedLimit)) &&
(Character.toUpperCase(this.response)==
(Character.toUpperCase(otherTicket.response))
{
return true;
}
else
{
return false;
}
}//end equals
//***********************************************************************************************
//This method calculates the fine
public void fine()
{
int overSpeed;
overSpeed = speed - speedLimit;
if(overSpeed > 10)
{
fine = overSpeed * 10;
}
else
{
fine = overSpeed * 6;
}
}// end Calculate Fine
//*********************************************************************************************
//This method is the input validation
public void schoolZone ()
{
if(response == 'y' || response == 'Y')
{
fine = fine * 2;
}
//else
//{
// System.out.println("Invalid Entry");
// System.out.println("You entered" + response +"Please enter a Y for
Yes or N for No: ");
//}
}//end school zone
//********************************************************************************************
//This method prints the table heading
public void tableHeading ()
{
System.out.println("Name" + '\t'+'\t' + "Speed" + '\t' + "SpeedLimit" +
'\t' + "SchoolZone" +
'\t' + "Fine");
}// end tableheading
//********************************************************************************************
//This method prints the output
public void tableData ()
{
DecimalFormat currencyFormat = new DecimalFormat ("$, ##0.00");
System.out.println(name + '\t'+" " + speed + '\t' + speedLimit + '\t'
+ '\t' + response + '\t' +
'\t' + currencyFormat.format(fine));
}// end tableData
//*******************************************************************************************
//This method returns the name of the driver
public String getName()
{
return name;
} //end getName
//******************************************************************************************
//This method returns the speed
public int getSpeed()
{
return speed;
} //end getSpeed
//******************************************************************************************
//This method returns the speed limit
public int getSpeedLimit()
{
return speedLimit;
} // end getSpeedLimit
//******************************************************************************************
//This method returns the response
public char getResponse()
{
return response;
} // end getresponse
//******************************************************************************************
//This method returns the fine
public double getFine()
{
return fine;
} // end getfine
//******************************************************************************************
//This method changes the name
public void setName(String dName)
{
name = dName;
} //end setName
//********************************************************************************************
//This method changes the speed
public void setSpeed(int dSpeed)
{
speed = dSpeed;
} //end setSpeed
//*******************************************************************************************
//This method changes the speed limit
public void setSpeedLimit(int cSpeedLimit)
{
speedLimit = cSpeedLimit;
} //end setspeedLimit
//********************************************************************************************
//This method changes the response
public void setResponse(char cResponse)
{
response = cResponse;
} //end setresponse
//*********************************************************************************************
// This method changes all of the variables
public void setALL(String dName, int dSpeed, int cSpeedLimit, char
cResponse)
{
name = dName;
speed = dSpeed;
speedLimit = cSpeedLimit;
response = cResponse;
}
}//end class Ticket
//**********************************************************************************************
klynn47@comcast.net - 09 Dec 2004 03:45 GMT
The main task of a constructor is to initialize the instance variables
of the
class definition when an instance is created. If the instance variables
are not
explicitly initialized in a constructor, then they are assigned default
values. Numeric
primitives are 0, booleans are false.
Your driver method must be your main method. It is the first method
that will be executed
when the application is run. You will make the calls to your
constructors from your main
method.
Does this help?
CappaKia - 09 Dec 2004 14:00 GMT
Yes that helps explain what constructors.
So can you help me with my program If not then that is fine, I have an
appointment with a tutor Friday???
CappaKia - 10 Dec 2004 00:17 GMT
I am still open to help. If you can offer any.