Am about to do a homework, and i need to store quite a lot of information (Dictionary) in a data structure of my choice. I heard people in my classroom saying hash-tables are the way to go. How come?
Hi,
I need to find out how many even values are contained in a binary tree.
this is my code.
private int countEven(BSTNode root){
if ((root == null)|| (root.value%2==1))
return 0;
return 1+ countEven(root.left) + countEven(root.right);
}
this i just coded as i do not have a way to test this out. I'm not able to test it out at the moment but need an answer so badly.
any help is deeply appreciated.
I have the following scenario :
public class A {
private int x = 5;
public void print()
{
System.out.println(x);
}
}
public class B extends A {
private int x = 10;
/*public void print()
{
System.out.println(x);
}*/
public static void main(String[] args) {
B b = new B();
b.print();
}
}
On executing the code, the output is : 5.
How to access the child class(B's) variable(x) via the parent class method?
Could this be done without overriding the print() method (i.e. uncommenting it in B)?
[This is important because on overriding we will have to rewrite the whole code for the print() method again]
I've come across Netbeans but is there any tools out there that lets you build things event driven ?
I'm looking for a feature like being able to drag and drop UI components, and add methods to buttons directly by double clicking it (kinda like visualbasic) and viewing the source.
I came across this weird (in my opinion) behavior today. Take this simple Test class:
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.run();
}
private void run() {
List<Object> list = new ArrayList<Object>();
list.add(new Object());
list.add(new Object());
method(list);
}
public void method(Object o) {
System.out.println("Object");
}
public void method(List<Object> o) {
System.out.println("List of Objects");
}
}
It behaves the way you expect, printing "List of Objects". But if you change the following three lines:
List<String> list = new ArrayList<String>();
list.add("");
list.add("");
you will get "Object" instead.
I tried this a few other ways and got the same result. Is this a bug or is it a normal behavior? And if it is normal, can someone explain why?
Thanks.
I have a string;
String allIn = "(50 > 100) AND (85< 100)";
Now I need to evaluate if the conditions inside are TRUE or FALSE, how can I do it?
In real the string will be a value from a field in my DB, where I will substitute different values and they will form a string as shown above.
I am having a problem figuring how to check a string for the same characters in a row then count that same character in a row then printing it out then giving the location of the last occorance of that character count then printing it out then moving to the next character in the string that is different then the previous character and the program is case sensitive.
So the input could be: aaaaAAAbbbddccc
How would I compress this string to: a4A3b3d2c3 ? and then decompress it?
My web service was created some time back using IBM JAX-RPC. As a part of enhancement, I need to provide some security to the existing service.
One way is to provide a handler, all the request and response will pass through that handler only. In the request I can implement some authentication rules for each and every application/user accessing it.
Other than this, What are the possible ways for securing it?
I have heard someting called wsse security for web service. Is it possible to implement it for the JAX-RPC? Or it can be implemented only for JAX-WS? Need some helpful inputs on the wsse security so that i can jump learning it.
Other than handler and wsse security, any other possible way to make a service secure?
Please help.
According to javadoc... Collections.fill() is written as below :
public static <T> void fill(List<? super T> list, T obj) {
int size = list.size();
if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
for (int i=0; i<size; i++)
list.set(i, obj);
} else {
ListIterator<? super T> itr = list.listIterator();
for (int i=0; i<size; i++) {
itr.next();
itr.set(obj);
}
}
}
Its easy to understand why they didn't use listIterator for
if (size < FILL_THRESHOLD || list instanceof RandomAccess)
condition as of RandomAccess. But whats the use of size < FILL_THRESHOLD in above?
I mean is there any significant performance benefit over using iterator for size>=FILL_THRESHOLD and not for size < FILL_THRESHOLD ?
I see the same approach for Collections.copy() also :
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
int srcSize = src.size();
if (srcSize > dest.size())
throw new IndexOutOfBoundsException("Source does not fit in dest");
if (srcSize < COPY_THRESHOLD ||
(src instanceof RandomAccess && dest instanceof RandomAccess)) {
for (int i=0; i<srcSize; i++)
dest.set(i, src.get(i));
} else {
ListIterator<? super T> di=dest.listIterator();
ListIterator<? extends T> si=src.listIterator();
for (int i=0; i<srcSize; i++) {
di.next();
di.set(si.next());
}
}
}
FYI:
private static final int FILL_THRESHOLD = 25;
private static final int COPY_THRESHOLD = 10;
We always say that method overloading is static polymorphism and overriding is runtime polymorphism. What exactly do we mean by static here? Is the call to a method resolved on compiling the code? So whats the difference between normal method call and calling a final method? Which one is linked at compile time?
I've been working on this for one hour, just can't get it.
I have a Vector2d class:
public class Vector2d
{
public double x = 0.0;
public double y = 0.0;
....
}
This vector class has a rotate() method which is causing me trouble.
The first snippet seems to make the x and y values smaller and smaller. The second one works just fine! Am I missing something simple here?
public void rotate(double n)
{
this.x = (this.x * Math.cos(n)) - (this.y * Math.sin(n));
this.y = (this.x * Math.sin(n)) + (this.y * Math.cos(n));
}
This works:
public void rotate(double n)
{
rx = (this.x * Math.cos(n)) - (this.y * Math.sin(n));
ry = (this.x * Math.sin(n)) + (this.y * Math.cos(n));
x = rx;
y = ry;
}
I just can't spot any difference there
Hello,
I'm not able to understand the following multi-dimensional code. Could someone please clarify me?
int[][] myJaggedArr = new int [][]
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
May I know how it is different from the following code?
int[][] myArr = new int [][] {
{1,3,5,7,9},
{0,2,4,6},
{11,22} };
I'm working on some basic linked list stuff, like insert, delete, go to the front or end of the list, and basically i understand the concept of all of that stuff once i have the list i guess but im having trouble setting up the list. I was wondering of you guys could tell me if im going in the right direction. (mostly just the setup) this is what i have so far:
public class List {
private int size;
private List linkedList;
List head;
List cur;
List next;
/**
* Creates an empty list.
* @pre
* @post
*/
public List(){
linkedList = new List();
this.head = null;
cur = head;
}
/**
* Delete the current element from this list. The element after the deleted element becomes the new current.
* If that's not possible, then the element before the deleted element becomes the new current.
* If that is also not possible, then you need to recognize what state the list is in and define current accordingly.
* Nothing should be done if a delete is not possible.
* @pre
* @post
*/
public void delete(){
// delete
size--;
}
/**
* Get the value of the current element. If this is not possible, throw an IllegalArgumentException.
* @pre the list is not empty
* @post
* @return value of the current element.
*/
public char get(){
return getItem(cur);
}
/**
* Go to the last element of the list. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goLast(){
while (cur.next != null){
cur = cur.next;
}
}
/**
* Advance the cursor to the next element. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goNext(){
if(cur.next != null){
cur = cur.next;}
//else do nothing
}
/**
* Retreat the cursor to the previous element. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goPrev(){
}
/**
* Go to top of the list. This is the position before the first element.
* @pre
* @post
*/
public void goTop(){
}
/**
* Go to first element of the list. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goFirst(){
}
/**
* Insert the given parameter after the current element. The newly inserted element becomes the current element.
* @pre
* @post
* @param newVal : value to insert after the current element.
*/
public void insert(char newVal){
cur.setItem(newVal);
size++;
}
/**
* Determines if this list is empty. Empty means this list has no elements.
* @pre
* @post
* @return true if the list is empty.
*/
public boolean isEmpty(){
return head == null;
}
/**
* Determines the size of the list. The size of the list is the number of elements in the list.
* @pre
* @post
* @return size which is the number of elements in the list.
*/
public int size(){
return size;
}
public class Node {
private char item;
private Node next;
public Node() {
}
public Node(char item) {
this.item = item;
}
public Node(char item, Node next) {
this.item = item;
this.next = next;
}
public char getItem() {
return this.item;
}
public void setItem(char item) {
this.item = item;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
I got the node class alright (well i think it works alright), but is it necessary to even have that class? or can i go about it without even using it (just curious).
And for example on the method get() in the list class can i not call that getItem() method from the node class because it's getting an error even though i thought that was the whole point for the node class.
bottom line i just wanna make sure im setting up the list right.
Thanks for any help guys, im new to linked list's so bear with me!
If I use a statement in my code like
int[] a = new int[42];
does it initialized the array to anything in particular? (e.g. 0) I seem to remember this is documented somewhere but I am not sure what to search for.
Coming from other web frameworks, I'm used to being able to map parts of a URL to method parameters. I know that web.xml provides a way to map an entire URL to a Servlet but is there a way to get more features out of this, such as mapping pieces of the URL to method parameters?
There´s any eclipse shortcut to stance a new object ?
For example. I would like to type:
Object zzz =
and it would complete for me this way:
Object zzz = new Object();
with void parameter of course.
I will wait answers.. thank you people.
I'm using SAX and XML reader to read XML weather info from the web and it works fine if the page exists. But if for instance the user inputs an invalid city, zip etc the XML page that gets read from is empty and the app force closes with nullpointerexception. The area that generates the error is here right at open inputstream. Any suggestions?:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = null;
try {
sp = spf.newSAXParser();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = null;
try {
xr = sp.getXMLReader();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Create a new ContentHandler and apply it to the XML-Reader*/
WeatherHandler myExampleHandler = new WeatherHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
try {
xr.parse(new InputSource(url.openStream()));
parsedWeatherDataSet =
myExampleHandler.getParsedData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parsedWeatherDataSet.toString();
the Method hasTwoTrueValues returns true if at least two values in an array of booleans are true. Provide the Big-Oh running time for all three implementations proposed.
// Version 1
public boolean has TwoTrueValues( boolean [ ] arr ) {
int count = 0;
for( int i = 0; i < arr. length; i++ )
if( arr[ i ] )
count++;
return count >= 2;
}
// Version 2
public boolean hasTwoTrueValues( boolean [ ] arr ) {
for( int i = 0; i < arr.length; i++ )
for( int j = i + 1; j < arr.length; j++ )
if( arr[ i ] && arr[ j ] )
return true;
}
// Version 3
public boolean hasTwoTrueValues( boolean [ ] arr ) {
for( int i = 0; i < arr.length; i++
if( arr[ i ] )
for( int j = i + 1; j < arr.length; j++ )
if( arr[ j ] )
return true;
return false;
}
For Version 1 I say the running time is O(n)
Version 2 I say O(n^2)
Version 3 I say O(n^2)
I am really new to this Big Oh Notation so if my answers are incorrect could you please explain and help.
I'm making a BMI calculator that doesn't seem to be working.
The math operations work if i just do something like w/h, but once i had the brackets, it returns an error.
If i change the variables w and h and use a constant number, the operation works.
Another problem is that although i'm making result a double, it seems to be rounding to the nearest int.
Could someone tell me what I'm doing wrong here?
public class ass10 {
public static void main(String[] args) {
bmi(223,100);
}
public static bmi(int w, int h){
double result;
result = (w/(h*h))*703
System.out.println(result)
}
}
Hi, I have a string like this String str = "la$le\$li$lo".
I want to split it to get the following output "la","le\$li","lo". The \$ is a $ escaped so it should be left in the output.
But when I do str.split("[^\\\\]\\$") y get "l","le\$l","lo".
From what I get my regex is matching a$ and i$ and removing then. Any idea of how to get my characters back?
Thanks
Hi I am creating a method that will take a number and print it along with its binary representation. The problems is that my method prints all 0's for any positive number, and all 1's for any negative number
private static void display( int number ){
System.out.print(number + "\t");
int mask = 1 << 31;
for(int i=1; i<=32; i++) {
if( (mask & number) != 0 )
System.out.print(1);
else
System.out.print(0);
if( (i % 4) == 0 )
System.out.print(" ");
}
}