Need Help with Final Project which involves an “Equals” Method, a “copy” method, and a Decimal Format Class

From: CappaKia (HowaboutNO_at_spam.com)
Date: 12/09/04


Date: Wed, 08 Dec 2004 18:23:51 -0500

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

//**********************************************************************************************



Relevant Pages

  • Need Help getting kinks out
    ... public Ticket(String name, int speed, int speedLimit, char response) ... } // end Ticket constructor ... public void schoolZone ... //This method changes the speed ...
    (comp.lang.java.help)
  • Re: Washington State Patrol Media Release
    ... the jackass that damn near killed me and totaled my bike last year didn't get a ticket for driving with a cell phone because the cop didn't see him do it. ... the cop can't write a ticket for something he didn't witness. ... I added that *they* would be required to identify the driver in court, which 99% of the time they couldn't do/wouldn't do. ...
    (rec.motorcycles.harley)
  • Re: Michael Waltrip Charged After Vehicle Accident Saturday (another kook theory)
    ... I am a retired cop. ... couldn't give someone a ticket once (after they hit me head on and ... the driver never looked before changing lanes and as she ... Witnesses got the name of the tow truck off the door, ...
    (rec.autos.sport.nascar)
  • Re: Michael Waltrip Charged After Vehicle Accident Saturday (another kook theory)
    ... I will add to it they can charge anyone, at any time, for any ... couldn't give someone a ticket once (after they hit me head on and ... the driver never looked before changing lanes and as she ... The tow truck driver was ...
    (rec.autos.sport.nascar)
  • Re: Washington State Patrol Media Release
    ... the jackass that damn near killed me and totaled my bike last year didn't get a ticket for driving with a cell phone because the cop didn't see him do it. ... the cop can't write a ticket for something he didn't witness. ... I added that *they* would be required to identify the driver in court, which 99% of the time they couldn't do/wouldn't do. ...
    (rec.motorcycles.harley)