Something about implementing Comparator interface isn't very clear to me: overriding the compare method.
Like here for example:
//This sorts a list of objects holding information based on age: the name and the age of the person
public class Person {
String name;
int age;
public Person (String name, int age)
{
this.name = name;
this.age = age;
[Code] ....
What exactly is happening behind the scenes? I don't understand mostly the part where it returns a 0, a 1, or a -1. After it returns one of those values, what really happens next?
For the displaying of the list, is the method toString() being accessed to output the list in the System.out.println statement?
public class Someone { String name; int age; ArrayList <Someone> listarr = new ArrayList <Someone>(); public Someone(String name1, int age1) { name = name1; age = age1;
[code]...
1. In the compare method, what happens when it returns one of the 0, -1, or 1? How does returning such values contribute to the sorting process? How does it take in information (like I know it does through the parameters, but how exactly)?
I have one interface with three(more than one) method declaration. In the subclass that implements it I want to define only one method not all three not even blank definition of them.Is there any keyword or method for that. How to do it? Is it possible to do it? In GUI we use adapter classes to achieve it. What for console application?
The 2 minute drill from page 69 SCJP kathy and bert book, says regarding Interfaces, that - "A legal nonabstract implementing class must not declare any new checked exceptions for an implementation method."
When I try the below given code in eclipse , it does not throw any errors . (Here I have tried to throw NullPointerException from testFunc whereas the interface function throws IllegalStateExc)
package abstracttesting; public class StaticCheck implements check{ public void testFunc() throws NullPointerException{ // TODO Auto-generated method stub } public static void main(String[] args) { // TODO Auto-generated method stub } } interface check{ void testFunc() throws IllegalStateException; }
So we have an assignment regarding a linked list implementation of a given list interface.
In my list interface, the method contains(T anEntry) is defined.
In the LList implementation, contains is already implemented as part of getting the core methods in.
Now I am being tasked with the following:
Provide a second implementation of the method contains2(T anEntry) that calls a private recursive method
Private boolean contains (T anEntry, Node startNode) that returns whether the list that starts at startNode contains the entry anEntry.
I've written the private recursive method already. That's not an issue (at least not right now).
But what I don't understand is how startNode is supposed to be populated when this private contains method is called from the public contains2 method? contains2 only takes one parameter: anEntry. the private method takes two parameters: anEntry and startNode. How am i supposed to provide startNode when I am calling contains2?
I am trying to sort an ArrayList of objects with the comparator as I want to sort based on a certain value for each object. I understand I would need to override compareTo() in the objects class, is there any way I can get around also needing to override for all subclasses of the object?
I am asked to create a code that if a user enters 1 it will use the object natural comparison form ('default') as written in CompareTo method.But if he chooses to enter something else then another comparison is used.Maybe I just need to use 2 diff comparators? but then what;s the point of defining something as 'default'....
If an array has been sorted using a comparator then why is it necessary to pass on that comparator to the binaryserach method. What I want to know is that how come the presence of a comparator reference affect the way the algorithm works?
I have a java code that should sort an array of names based on the last name. e.g jane a, jane b, jane z, jane d should be jane a, jane b, jane d, jane z. I have the following code but for some reasons, the s1 in the comparator method is always null.
public class ShuffleName { public static void sortNames(String[] names){ Arrays.sort( names, new Comparator<String>() { public int compare( String s1, String s2 ) { String s1last = s1.split("s+")[1]; String s2last = s2.split("s+")[1]; return s1last.compareTo(s2last);
Which of the following classes uses Comparable and Comparator?
QueueTreeSetStackPriorityQueue
In the above question, what does 'uses' mean? Does it mean do above classes implement Comparable and Comparator?
I know that in order to compare any two elements stored in one of the above classes, we need to make the elements' class to implement one of these - either Comparable or Comparator.
So I built this comparator exactly the same way I built my others that are working.But the comparator for UserComparator is not being found for some reason.I will post my usercomparator class and JSP page.
The initial output of the texfile is this. NO ARRAYLIST OR COMPARATOR IS ALLOWED:
Steve Jobs 9 f 91 Bill Gates 6 m 90 James Gosling 3 m 100 James Gosling 3 f 100 Dennis Ritchie 5 m 94 Steve Jobs 9 m 95 Dennis Ritchie 5 f 100 Jeff Dean 7 m 100 Bill Gates 6 f 96 Jeff Dean 7 f 100 Sergey Brin 27 f 97 Sergey Brin 22 m 98
The collateExams method collates/sorts exam objects starting with the first 'm' (midterm) of the first object and immediately followed by the same person's 'f'(final). Only a SINGLE loop construct is allowed. The output from collateExams() should be the one below but my code is not working, i.e. collateExams method is not working. The output from collateExams() should be
Bill Gates 6 m 90 Bill Gates 6 f 96 James Gosling 3 m 100 James Gosling 3 f 100 Dennis Ritchie 5 m 94 Dennis Ritchie 5 f 100 Steve Jobs 9 m 95 Steve Jobs 9 f 91 Jeff Dean 7 m 100 Jeff Dean 7 f 100 Sergey Brin 22 m 98 Sergey Brin 27 f 97
I am getting [b]NullExceptions[/b] at
r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
package com.Lists; public class EmployeeOffice implements EmpInterface { private double salary; private String name; private String postion; private double hoursWorked;
[Code] .....
So if i wanna sort this Generic class using comaparator what do i do... I cant find an answer to this... I wanna sort them on the basis of salary what to do ...
1. In jsp 1 , i have 3 checkboxes(chkbox1,chkbox2,chkbox3) , if i select check boxes (chkbox1, chkbox2), and click on submit, corresponding two text fields (chkbox1,chkbox2) will have to appear in the next jsp i.e., jsp 2.
2. In jsp 1 , i have 3 checkboxes(chkbox1,chkbox2,chkbox3) , if i select check boxes (chkbox2, chkbox3), and click on submit, corresponding two text fields(chkbox2,chkbox3) will have to appear in the next jsp i.e., jsp 2.
Like this, which ever checkbox i select, corresponding text fields should appear in the subsequent jsp.
In the above program even if i comment out the Hashcode method , i believe it is still taking the memory address values from the native hashcode method of Object class. but the equals override implentation says that i have two insertions which are same . So as per my logic it should not allow the duplicate element to enter.but its not so ...the duplicate element is well inserted without hashcode .
How to implement GridLayout. In my applet, I want to make a grid of 2 rows and 2 columns. In each grid I want to add a Label and a TextField. I want the background to be red.
So my code would be?
import java.awt.*; import java.applet.*; import java.awt.event.*; public class GridLayoutApplet extends Applet implements ActionListener{ // construct components Label fNameLabel = new Label("First Name"); TextField fNameField = new TextField(20);
[Code] .....
I have read about panels and frames but, it is all confusing to me. How can you add a label and a TextField to one square of the grid?
Not necessarily Java related, more a general programming issue I have come a long and I am currently using java to mock up a project that will eventually get moved over to C++.
I am trying to implement FFT Convolution into a java project and have things partially working, I do not know if it is a coding issue, logic issue or if I simply know nothing of FFT and convolution.
For starters I got an FFT library that I do have working, if I create a small array pass it into the forward FFT, do a little bit of bit manipulation (converting a complex array of size n*2 into a re[] and im[] of size n), run the inverse FFT and do the same bit manipulation again I get back where I started.
However if I take two arrays, a Dirac Delta function (an array with a 1 followed by zeros for the rest of the array) and a stepping kernel (ex. {1, 2, 3, 4 etc}), I would expect to get the forward FFT of both arrays, bit manipulation, multiply them together, Inverse FFT, and bit manipulate again and the result would be the kernel. I have not had such luck.
This is the gist of the program:
// bit manipulation, converts complex array to two re and im arrays public void bitTwiddle(double [] real, double [] real2, double [] imaginary){
for (int idx = 0; idx < (N * 2); idx++){ realCopy[idx] = real [idx];
[Code] ....
This is the result:
Quote ===================================================================== Dirac Delta In Time Domain: Real:1.000.000.000.00 Imag:0.000.000.000.00 ===================================================================== Dirac Delta Frequency Domain Real:0.500.500.500.50 Imag:0.000.000.000.00 ===================================================================== Kernel In Time Domain Real:1.002.003.004.00 Imag:0.000.000.000.00 ===================================================================== Kernel In Frequency Domain Real:5.00-1.00-1.00-1.00 Imag:0.00-1.000.001.00 ===================================================================== Result: Dirac Delta x Kernel Real:2.50-0.50-0.50-0.50 Imag:0.00-0.000.000.00 ===================================================================== Result In Time Domain Real:0.501.501.501.50 Imag:0.000.000.00-0.00
The following codes shows an implementation of an enqueue function of a FIFO immutable queue, and the output result shows the String "c".
I don't understand why since I expected it should be null.
The head and the tail of an ImmutableQueue Object are two separate queue, and each time I call the enqueue function, it just return a new object with a new tail, however, the head is not modified except the first two times I call the function.
Therefore, I expected head.next.next should be a null element, but the result is not like that.
public class ImmutableQueue<E> { private int size =0; public Queue<E> head; public Queue<E> tail; public ImmutableQueue(){} private ImmutableQueue(Queue<E> hd, Queue<E> tl){ head=hd; tail=tl;
I want to implement new locking mechanism similar to like how threads locking or synchronized does the operation. Any inputs to implement out own locking mechanism ?