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

JAVA

    Sponsored Links

    Next

  • 1. Stop InputBox prompt loop?
    Hi, I have a program that prompts a user to input a String value. The program then does stuff with this String then prompts for another String. However I want the loop/program to halt when the user enters "stop!". I have attempted this with: while(inputWord != "stop!") { inputWord = inputBox.getString("Please type in a word: "); //stuff } .... but it doesn't work. Any ideas how I can sort this?
  • 2. JSpinner + JOptionPane problem, spinner keeps spinning
    Hello, I've written a little application in java which contains a JTextfield and a JSpinner. On the JTextfield I've added an inputverifier. When the users fills in a wrong value in the textfield, then a JOptionPane shows up with a warning. Now I've the following problem. When I fill in a wrong value in the JTextfield and next I push on the JSpinner's up or down button, then the JOptionPane apears to show the warning, but the JSpinner keeps spinning, instead of just spinning one value up or down it spins till it has reached the max or min value. Does anyone know how I can solve this annoying problem? grtz, Hans
  • 3. perl to java
    Hi, I am in the process of trying to convert an existing perl program to java. Being a very rusty newbie programmer I've discovered I didn't know what to do with environment variables and taking them into a java program. So what I work in is linux and I set a few environment variable...ex: KW_ON=quick and a couple of paths. Can I take environment variables into a java program? Is so how? Pardon my ignorance! :-)
  • 4. java app as windows service
    Hello all, I have a program in java that I want to run as a service. Or have an installer install it as a windows service. Any ideas? Oh yeah.... one more thing. This is more of a dos question but I will ask anyways. :) I have a batch file that starts the program. I want it to start it no matter where the program is located. Any ideas on where I can look? Here is what I have now. @echo off set BIN_DIR=%~d0%~p0 set JAVA_HOME=%BIN_DIR%\..\jre\windows set CLASSPATH=%JAVA_HOME%\lib\rt.jar set CLASSPATH=%CLASSPATH%;%BIN_DIR%\..\conf set CLASSPATH=%CLASSPATH%;%BIN_DIR%\..\lib\aocode-public.src.jar set CLASSPATH=%CLASSPATH%;%BIN_DIR%\..\lib\jcert.jar set CLASSPATH=%CLASSPATH%;%BIN_DIR%\..\lib\jnet.jar set CLASSPATH=%CLASSPATH%;%BIN_DIR%\..\lib\jsse.jar set CLASSPATH=%CLASSPATH%;%BIN_DIR%\..\..\src %JAVA_HOME%\bin\javaw -Xmx256M something.something.something.something.something.myClassFile %BIN_DIR%\.. It works fine if it is on the root of my windows box.... but if I move it... it will not start the program. :( Thanks!
  • 5. In J2EE 1.4, I get "Could not find main class" ....help..
    Hi, All. I download "j2eesdk-1_4-dr-windows-eval.exe" from SUN site. I installed it on my WinXP(sp1) machine. And then I clicked "Start->Sun Microsystems->J2EE 1.4 SDK->Start Default Domain". But I get the following error message after about 3 seconds. --------------------------------------------------- Java Virtual Machine Launcher X Could not find main class. Program will exit --------------------------------------------------- What should I do? Thank you in advanced. -Jeff

Re: Need Help with Final Project which involves an "Equals" Method, a "copy" method, and a Decimal Format Class

Postby klynn47 » Fri, 10 Dec 2004 12:45:15 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?


Similar Threads:

1.Need Help with Final Project which involves an 揈quals?Method, a 揷opy?method, and a Decimal Format Class

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 parameterse
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 (fin>>highestTicket)
  {
   highestTicket = fine;
   highestName = ticket1.getName();
  }

  fine = ticket2.getFine();

  if (fin>>highestTicket)
  {
   highestTicket = fine;
   highestName = ticket1.getName();

  }

  fine = ticket3.getFine();

  if (fin>>highestTicket)
  {
     highestTicket = fine;
     highestName = ticket1.getName();

  }

  fine = ticket4.getFine();
    if (fin>>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

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























2.Need Help with Final Project which involves an

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???

3.clone method call non-private, non-final methods

I saw it in the Effective Java book, though understanding still eludes
me (I suspect it goes on par with the constructor's contract). I don't
see implications whereby clone calls some public method to copy parts
of the object, resulting in the disaster or the end of the world sort
of a thing. Can someone illustrate that?


Thanks

4.Error in the equal method of generic Pair class

puzzlecracker < XXXX@XXXXX.COM > writes:
>Pair<T, U> other = getClass().cast(o);
>This line gives a cast error during compilation: Pair<T, U> other =
>getClass().cast(o);

  Type parameters are for compile-time type information.
  There is no such static type information available here.
  Therefore, do not use type parameters here.
  Use inal Pair other =( Pair )other

5.Error in the equal method of generic Pair class

public class Pair <T, U>
{

	@Override
	public boolean equals( Object o )
	{
		if ( this == o )
		{
			return true;
		}
		if ( o == null || !(getClass().isInstance( o )) )
		{
			return false;
		}
		Pair<T, U> other = getClass().cast(o);
		return (first == null? other.first == null :
first.equals( other.first ))
		&& (second == null? other.second == null :
second.equals( other.second ));
	}

}

This line gives a cast error during compilation: Pair<T, U> other =
getClass().cast(o);

Why can't we use a traditional  Pair<T, U> other =(Pair<T, U> ) o; ?


Thanks

6. do I need to override the equals() method?

7. Two equals method for Class and Collection.contains?

8. Error in the equal method of generic Pair class



Return to JAVA

 

Who is online

Users browsing this forum: No registered users and 3 guest