How To Test And Finish ToString And Equals Method In Code
Jan 19, 2014
Write a class encapsulating the concept of a course grade, assuming a course grade has the following attributes: a course name and a letter grade. Include a constructor, the accessor and mutator, and methods toString and equals.Write a client class to test all the methods in your class.
how to test and finish the toString and equals method in this code ?
package labmodule7num57;
import java.util.*;
public class LabModule7Num57 {
// Constructors//
private String name;
private String letterGrade;
public LabModule7Num57 (String name,String letterGrade) {
I have a JFrame with a button called "Start". The code behind the "Start" button triggers a function (startwork() ) that traverses through log files and then inserts rows into my JTable. If I call the startwork() function from the Start button action performed event, then when it inserts the rows to the JTable it is not immediately shown...
The code has to finish before the JTable is refreshed. However, if I run the startwork() function as soon as the JFrame is displayed (from the main() function) then I can see the Jtable being populated dynamically as it is inserting the rows.
How to allow the user to click on the START button and yet behave correctly to where the JTable is updated properly so that I can see the rows being inserted as it progresses.
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){ if(O instanceof Geocache){ Geocache j=(Geocache) O; if (this.equals(j)) //I know this is wrong... but I can't figure it out return true; }
else return false; }
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
So I'm working on a project and noticed that my toString() method won't work. This is just an example of the type of code that I have in my real project. THIS IS MY MAIN CLASS
XML Code:
package trialanderror; import java.util.Scanner; public class TrialAndError { public static void main(String[] args) { Scanner keys = new Scanner(System.in); String name; String phonenumber;
My current assignment involves me outputting these 2 classes. Yet I'm not really sure in what manner I should go about doing this. I have tried creating a separate class and outputting my toString methods there but for some reason I am getting an error. .
The error message is thus;
Exception in thread "main" java.lang.NullPointerException at Vehicle.toString(Vehicle.java:91) at Run.main(Run.java:17) Process completed.
Write TestCabAppointment,java class where you will instantiate new CabAppointment objects and read data from RandomAccessFile and create CabAppointment objects and save them in RandomAccessFile You may use FixedLengthStringIO,java class, ICabAppointmentRecord.java interface. Complete the ReadWriteRandomAccessFile.java
I have to create a class that has two fields. One called length and the other width. I have to make a method that returns the tract area. Similarly, I also have to make a method that indicates whether two objects have the same fields. Here is the code that I have assembled...so far
// create private fields to hold width and length private double width; private double length;
[Code].....
My problem is encountered when writing that equals method
I get an error saying HTML Code: cannot invoke equals(double) on the primitive type double. Meanwhile, I do see, to realize that when I change my fields to capital "Double." The problem disappears; however, in my class I have never dealt with a situation where I have to use capital d in double. In fact, I don't even know what's the difference between Double and double. I do know what double is but not the other one..
For a few days I've been reading about the importance of overriding the equals method. How overriding it actually determines or checks the values stored in the variable. I realize that you can check the values stored in the primitive datatypes with "==", and when you don't override the equals method it acts the same way, right? When used with a reference datatype, "==" or the default equals() method only compares, or sees, if the variable is pointing to the same instance of a class. For some reason, in the examples, what is taking place to actually check the values stored inside the variables.
Here is part of an example (I've added comments for things that are confusing me):
@Override public boolean equals(Object obj) { //So we use Object here instead of the class type // we're overriding this equals method for? Is this so that we can use it to check different types? (overloading?) if (obj == this) { return true;
//Isn't this checking to see if the calling object is the same as the object we're passing to it? Why doesn't this return false? } if (obj == null || obj.getClass() != this.getClass()) { return false; }
//How exactly do we check the values stored in each object though? }
How do i print override the toString for WebBrowser as i would like to print out the object bc. Tested the program and it is fine if i put it in the main method rather than the WebBrowser constructor.
import java.util.*; class ListNode <E> { /* data attributes */ private E element; private ListNode <E> next; /* constructors */ public ListNode(E item) { this(item, null);
So I have to write all the methods for a LinkedListQueue. I've got isEmpty, enqueue and dequeue working correctly (I think) but I'm having trouble with the toString method. I tried to do it recursively and it works if there is only one element in the list, but with multiple elements it throws a StackOverflowerror exception. I've tried it multiple different ways, but I can't seem to figure out how to print it out with out clearing everything. We haven't been taught StringBuilder or .append yet, which I saw a lot of as I was looking for solutions, so I can't use those.
public class LinkedQueue<T> { protected LLNode<T> front; // reference to the front of this queue protected LLNode<T> rear; // reference to the rear of this queue private T info; public LinkedQueue() { front = null; rear = null;
[Code] ....
and this is the ITD used with it, for some reason it has the "empty the queue" function as a choice but we weren't assigned that function, so just ignore it.
import java.util.Scanner; public class ITDLinkedQueue { public static void displayMenu() { System.out.println("(1) display menu"); System.out.println("(2) check isEmpty"); System.out.println("(3) enqueue"); System.out.println("(4) dequeue");
I am attempting to override the equals method from the Object class which checks if two variables point towards the same object. I want the method to check if if the argument being passed in(an object) has the same data(instance variables) as the object that's calling this method. A NullPointerException is being thrown; Here is the code.
Exception in thread "main" java.lang.NullPointerException at javaapplication5.Product.equals(Product.java:42) at javaapplication5.Product.main(Product.java:24) Java Result: 1
I need to debug the equals method implementation of a class I've made, but I cannot for the life of me get Netbeans' debugger to step into it. I can step into other methods from the class (most of which implement the methods in an interface) that are called in the main method (just like the equals method). I've tried...
-Disabling all the step filters -Clearing the Netbeans cache -Moving the call to the equals method out of the if statement it's in and just calling it as its own statement -placing breakpoints within the equals method as well as on the call to the method -placing a method breakpoint on the overridden equals method in addition to the other locations -Using the shift-F7 version of the step into command
I'm using Netbeans 8.0.1 (I don't know if this is the latest version, but the last time I tried to update everything died and I had to completely remove NB and reinstall it) and JDK 8u05 (I think).
Our goal is to write a pretty simple program, one that takes the 12 digit UPC code entered by a user and to not only spit it back out in a format with dashes using toString, and also returns the first digit, a 2 more groups of digits numbering 2-6 and 7-11, and finally display the 12th digit. It then performs an equation to check the last digit and make sure the UPC code is correct.
However, being so new to java (I only learned visual basic before), with this I was introduced to two new concepts that for some reason I simply cannot grasp for the life of me: Using and calling the toString method, and calling on methods that are created in a completely different class file.
The first section of code is my UPC class, which is meant to contain all my methods as well as the toString to be called on:
public class UPC { // Instance variables private int itemType; // digit 1 private int manufacturer; // digits 2,3,4,5,6 private int product; // digits 7,8,9,10,11 private int checkDigit; // digit 12
How would I create a equals method to compare strings in a class that I'm creating. I need to create the method in my class, and then call it in a driver. How would I do this?
I need making the toString() method return a String rather than display a message to the screen. Also, I'm not supposed to call the toString method in my demo class to test it, so what should I do instead?
public class cupDispenser { String location; int noOfCups; cupDispenser(String location,int cups) { this.location=location; this.noOfCups=cups; } public String getlocation()
I have following Cipher Code and test for it. But I am getting following exception.
Java Code:
java.lang.IllegalStateException: Cipher not initialized at javax.crypto.Cipher.checkCipherState(Cipher.java:1672) at javax.crypto.Cipher.doFinal(Cipher.java:2079) at com.anjib.util.CipherUtil.encrypt(CipherUtil.java:67) at com.anjib.util.CipherTest.testEncryptDecrypt(CipherTest.java:23) mh_sh_highlight_all('java'); Java Code: public class CipherUtil { private static Logger log = Logger.getLogger(CipherUtil.class);
I have been creating a Java program to track inventory using an array. I need to add a search function to it to take a term, input through a text field, and locate a matching instance within the array. How can I code a JButton to grab test input from a JTextField and search for it within the array then display the corresponding information? I also need to add buttons to add and delete items as well.
I'm having trouble setting up a test for my combination method.Here's the method (It basically finds the most amount of characters that both substrings share and combines the two using the number of shared characters as a breaking point):
public static String combination(String str1, String str2, int overlap) { assert str1 != null : "Violation of: str1 is not null"; assert str2 != null : "Violation of: str2 is not null"; assert 0 <= overlap && overlap <= str1.length() && overlap <= str2.length() && str1.regionMatches(str1.length() - overlap, str2, 0, overlap) : "" + "Violation of: OVERLAPS(str1, str2, overlap)";
I had to write a class called Thermometer, that has one instance variable (an integer) for the temperature in Fahrenheit. I had to include the following methods
-a constructor that initializes the temperature to 60
-there is a method to change the temperature
-there is a method to display the temperature
-there is a method to reset the teperature to 60
Here is the code for that.
public class Thermometer { private int temp; private int thermometer; public Thermometer() { thermometer = 60;
[code]....
Now I get to the issue. I have to write a test class called thermometer to test the thermometer class. I need to test each method while displaying the temperature after it. My professor said I should use the invoke method but didn't go into much more detail than that.