Write a recursive method that converts a decimal number into a binary number as a string. The method header is as follows:
public static String convertDecimalToBinary(int value)
Hello
I'm building an application with distributed parts.
Meaning, while one part (writer) maybe inserting, updating information to a database, the other part (reader) is reading off and acting on that information.
Now, i wish to trigger an action event in the reader and reload information from the DB whenever i insert something from the writer.
Is there a simple way about this?
Would this be a good idea? :
// READER
while(true) {
connect();
// reload info from DB
executeQuery("select * from foo");
disconnect();
}
public static MySingleton getInstance() {
if (_instance==null) {
synchronized (MySingleton.class) {
_instance = new MySingleton();
}
}
return _instance;
}
1.is there a flaw with the above implementation of the getInstance method?
2.What is the difference between the two implementations.?
public static synchronized MySingleton getInstance() {
if (_instance==null) {
_instance = new MySingleton();
}
return _instance;
}
I have seen a lot of answers on the singleton pattern in stackoverflow but the question I have posted is to know mainly difference of 'synchronize' at method and block level in this particular case.
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
There's a class I'm working with that has a display() function that prints some information to the screen. I am not allowed to change it. Is there a way to "catch" the string it prints to the screen externally?
I need to generate n percentages (integers between 0 and 100) such that the sum of all n numbers adds up to 100.
If I just do nextInt() n times, each time ensuring that the parameter is 100 minus the previously accumulated sum, then my percentages are biased (i.e. the first generated number will usually be largest etc.). How do I do this in an unbiased way?
How do you handle clean up when the program receives a kill signal?
For instance, there is an application I connect to that wants any third party app (my app) to send a finish command. What is the best say to send that finish command when my app has been destroyed with a kill -9?
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 all,
in the case of an IM client.
i have made 2 separate threads to handle sending packets (by std io) and receiving packets.
the question is how to make these 2 threads run simultaneously so that i can keep prompting for input while at the same time be ready to receive packets at any time?
i have already tried setting a timer but the data is always lost receiving.
I need to do a search in a map of maps and return the keys this element belong.
I think this implementation is very slow, can you help me to optimize it?.
I need to use TreeSet and I can't use contains because they use compareTo, and equals/compareTo pair are implemented in an incompatible way and I can't change that.
(sorry my bad english)
Map m = new TreeSet();
public String getKeys(Element element) {
for(Entry e : m.entrySet()) {
mapSubKey = e.getValue();
for(Entry e2 : mapSubKey.entrySet()) {
setElements = e2.getValue();
for(Element elem : setElements)
if(elem.equals(element)) return "Key: " + e.getKey() + " SubKey: " + e2.getKey();
}
}
}
I have one Map that contains some names and numbers
Map<String,Integer> abc = new TreeMap<String,Integer>();
It works fine. I can put some values in it but when I call it in different class it gives me wrong order. For example:
I putted
abc.put("a",1);
abc.put("b",5);
abc.put("c",3);
some time it returns the order (b,a,c) and some time (a,c,b).
What is wrong with it? Is there any step that I am missing when I call this map?
Writing generated PDF (ByteArrayOutputStream) in a Servlet to PrintWriter.
I am desperately looking for a way to write a generated PDF file to the response PrintWriter.
Since a Filter up the hierarchy chain has already called response.getWriter() I can't get response.getOutputStream().
I do have a ByteArrayOutputStream where I generated the PDF into. Now all I need is a way to output the content of this ByteArrayOutputStream to the PrintWriter. If anyone could give me a helping hand would be very much appreciated!
I am making a program that lets a user input a chemical for example C9H11N02. When they enter that I want to split it up into pieces so I can have it like C9, H11, N, 02. When I have it like this I want to make changes to it so I can make it C10H12N203 and then put it back together. This is what I have done so far. using the regular expression I have used I can extract the integer value, but how would I go about get C10, H11 etc..?
System.out.println("Enter Data");
Scanner k = new Scanner( System.in );
String input = k.nextLine();
String reg = "\\s\\s\\s";
String [] data;
data = input.split( reg );
int m = Integer.parseInt( data[0] );
int n = Integer.parseInt( data[1] );
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.
This is a jFrame to connect to the database and this is in the button connect. My issue is in the passwordField NetBeans make me do a char[], but my .getConnection not let me insert the char[] ERROR: "no suitable method found for getConnection(String,String,char[])". So I will change to String right? So when I change and run the jFrame said access denied. when I start doing the System.out.println(l) " Give me the right answer" Like this: "Alex". But when I do the System.out.println(password) "Give me the Array spaces and not the value"
Like this: jdbc:mysql://localhost/home inventory root [C@5be5ab68 <--- Array space . What I doing wrong?
try {
Class.forName("org.gjt.mm.mysql.Driver"); //Load the driver
String host = "jdbc:mysql://"+tServerHost.getText()+"/"+tSchema.getText();
String uName = tUsername.getText();
char[] l = pPassword.getPassword();
System.out.println(l);
String password= l.toString();
System.out.println(host+uName+l);
con = DriverManager.getConnection(host, uName, password);
System.out.println(host+uName+password);
} catch (SQLException | ClassNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
I want to create and fill/stroke a path that consists of an outer boundary which is a square of side d and an inner boundary that is the outline of any of the capital letters.
How can I do this? (challenges = creating a mask from a font, and figuring out the right size/position to use)
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 have 1 thread who sole job is to grab DatagramPackets off of a socket and stick them in a buffer. Another thread works out of that buffer, processing the DatagramPackets. I'd like to have a pool of threads working out of that buffer.
I had thought to use a fixed thread pool to do this. To do so, do I need to create the pool, then submit enough runnables for execution to fill it up? I had hoped for a way to say "this is the thread/runnable that I want you to execute, this is how many I want running, GO!". Is there such a method of doing this? Is something other than a fixed thread pool better suited?
I have a file with data in the form timestamp, coordinate, coordinate, seperated by spaces, as here;
14:25:01.215 370.0 333.0
I need to loop through and add the coordinates only to an array. The data from the file is read in and put into as String[] called info, from split(" "). I have two problems, I think the end of the file has a extra " " which I need to lose appropriately and I also want confirmation/suggestions of my loop, at the moment I am getting sporadic out of bounds exceptions. My loop is as follows;
String[] info;
info = dataHolder.split(" ");
ArrayList<String> coOrds1 = new ArrayList<String>();
for (int counter = 0; counter < info.length; counter = counter+3)
{
coOrds1.add(info[counter+1]);
coOrds1.add(info[counter+2]);
}
Help and suggestions appreciated.
The text file is here but the class receives in a UDP packet from another class so I am unsure if this potentially adds " " at the end or not.
I use the following trick to get the array type of a specific class:
@SuppressWarnings("unchecked")
public static <T> Class<T[]> getArrayType(Class<T> componentType) {
String arrayClassName = "[L" + componentType.getName() + ";";
try {
return (Class<T[]>) Class.forName(arrayClassName);
} catch (ClassNotFoundException e) {
throw new UnexpectedException("Can't get the array type for " + componentType, e);
}
}
But, is there any more elegant way to get this?
I Have dynamically render row. WE have fields like FROM TO.
For eg: From TO
2 10,
2 3,
8 12
It cannot accept this combination row.. That means no number should be overlapping.
For eg: From TO
2 10,
0 1,
11 12
This combination is allowed.the row may also increased.
I need need to write a validation for this overlapping.
Can any 1 help to solve this problem.
Is there a difference between these ?
if(myString.equals("")){
}
if(myString.equals(null)){
}
if(myString == ""){
}
I have a string, I don't know is it empty or has some emtpy spaces I just wan't to stop it to be written in database if it is invalid(if empty or with some blank spaces).
I'm trying to make a method that takes an argument of Country.class, User.class etc, and returns argument.count().
All the possible classes that I would give to this method extend from Model and have the method count().
My code:
private static long <T> countModel(Model<T> clazz)
{
return clazz.count();
}
Called by:
renderArgs.put("countryCount", countModel(Country.class));
However this just doesn't work at all.
How do I do this, please?