|
hi guys i have a question, im doing a very simple thread application that increments a value everytime a thread runs, i've got 4 threads running at the same time, but everytime all threads increment the count var, its allways 1 it doesn't change on each thread execution i use synchronized to allow 1 thread increments the var at a time but after all threads die count var outputs same value,
|
|
|
|
|
Please show your code, we cannot guess what may be happening.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
My guess is that you made the counter an instance variable in the thread, so they all have their own copy. But you haven't posted enough to be sure.
|
|
|
|
|
BobJanova wrote: made the counter an instance variable in the thread
That was my guess too!
5+
Cheers!
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Hello Everyone
I'm trying to convert this java code to C#, could someone please help me on this because I am very new into programming.
Here is the Java code...
import java.awt.Component;
import javax.swing.JOptionPane;
public class ConfirmDeleteDialog {
public static final int YES = JOptionPane.YES_OPTION;
public static final int NO = JOptionPane.NO_OPTION;
public static final int CANCEL = JOptionPane.CANCEL_OPTION;
static final int OK = JOptionPane.OK_OPTION;
static final int CLOSED = JOptionPane.CLOSED_OPTION;
public static int showMessage(Component parent, String message, String title) {
return JOptionPane.showConfirmDialog(parent, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}
}
thanks in advance
lapeci
|
|
|
|
|
That seems to be a custom message box. Hence in C# (if you are using Windows Forms), you could use MessageBox.Show as a replacement for ConfirmDeleteDialog.showMessage . The return value in C# is a DialogResult .
|
|
|
|
|
Hi Bernhard Hiller
Thanks for replaying to my java question. I'm using WPF Window Form (not the Windows Forms). Is there anyway you could help me to convert that java code I'm very new to programming and I don't have a clue how to do it honestly...
Kind Regards
lapeci
|
|
|
|
|
What are you trying to do?
|
|
|
|
|
If you are new to programming then why are you concerned about trying to convert Java to C# when creating a WPF program? Forget Java and spend some time studying how to do things properly in WPF. There are lots of useful samples in the WPF articles section[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
There is no simple equivalent of this in C# as it refers to Java specific components. It would be better if you started with Windows forms[^] in C# and build your program from scratch.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
It can almost be completely converted to C#:
using System.Windows.Forms;
public class ConfirmDeleteDialog
{
public static readonly int YES = DialogResult.Yes;
public static readonly int NO = DialogResult.No;
public static readonly int CANCEL = DialogResult.Cancel;
internal static readonly int OK = DialogResult.OK;
internal static readonly int CLOSED = JOptionPane.CLOSED_OPTION;
public static int showMessage(Component parent, string message, string title)
{
return MessageBox.Show(parent, message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}
}
Dave Doknjas
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
|
|
|
|
|
need coding of tower of hanoi game in java(netbeans) .......!!!
|
|
|
|
|
Then please go and search[^] for it. Do not expect other people to do your work for you.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Java(netbeans) does not exist.
There is Java SE[^], which you probably meant, and there is a IDE called Netbeans[^](beside of Eclipse[^] ).
The Tower of Hanoi[^] is a common puzzle. You will find an animation on the right side of the wikipedia page - check that one out and find a code to describe it.
EDIT The text is also not bad - pretty good described what's going on.
|
|
|
|
|
|
class Matrix{
public static void main(String []args){
int ArrayMatrix [] [] = {
{1,0,0,0,0},
{0,1,0,0,0},
{0,0,1,0,0},
{0,0,0,1,0},
{0,0,0,0,1},
};
int l = ArrayMatrix.length;
int i,j;
boolean a=true;
boolean b=false;
for( i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
System.out.print(ArrayMatrix[i][j]+" ");
}
System.out.println("");
}
System.out.println("\n Array is Unit Matrix");
}
}
Asad Ali Jogi
modified 12-Nov-12 1:43am.
|
|
|
|
|
I assume you do not get it to work.
As this looks much like a homework, I'll give you some tips but no solution.
read them, follow them and you'll find the right way:
- variable names always start with lower case
always. ALWAYS. Please rename "ArrayMatrix" to "iArrayMatrix". Copy it and set up a second Array for testing.
You can comment them in/out by using Strg+7 in any IDE.
- use clear names
"i" and "j" are common counters. But how about "iRow" and "iCol" ? Those give you much more control on what is happening.
- delete "l"
No. One does not put the length of an Array in a variable. Simply NO.
- I think this homework is about recursion. Read about that on the web. That would need some methods - is this topic already introduced in class? can we use some?
- one boolean "bIsMatrix" should be enough. give it a init value "false" and use the loops to set it to true when you find a value "1" in the diagonal. Set it "false" if not. Read "bIsMatrix" after the loops to present result.
|
|
|
|
|
That's not the complete rule for an identity matrix. The complete rule is that the matrix contains only 1s on the leading diagonal.
Don't hand this in, your teacher won't like the code style, but ...
boolean isIdentity(int[][] m) {
boolean isIdentity = true;
for(int i = 0; i < m.length; i++) for(j = 0; j < m[i].length; j++)
isIdentity &= m[i][j] == (i == j ? 1 : 0);
return isIdentity;
}
... I need to get my crazy coding instincts out in a controlled environment now and then
|
|
|
|
|
I want to show ImageIcon by adding to Label, but it has error, help me?
<pre lang="java">import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
class CatchTheRat extends JFrame
{
// The Rat
JLabel lb;
// Move it randomly!
Random r;
public CatchTheRat(int k)
{
// Set frame properties
setTitle("Catch The Rat");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
// Set the background (just for a good look)
setContentPane(new JLabel(new ImageIcon("background.jpg")));
// Set layout to the content pane
getContentPane().setLayout(new FlowLayout());
// Create the rat
lb=new JLabel(new ImageIcon("rat.jpg"));
// Add the rat
getContentPane().add(lb);
// Create Random object
r=new Random();
// Create a timer and call it for every k seconds
new Timer(k,new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
// Move the rat randomly, subtract 75, so that the rat should not meet the edges
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
// Add mouselistener, notify when user clicks it!
lb.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
// Create a beep sound when clicked to notify
Toolkit.getDefaultToolkit().beep();
// Also print it!
System.out.println("Caught!");
}
});
// Maximize the frame
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String args[])
{
// Create Scanner object
Scanner s=new Scanner(System.in);
// Let the user enter his capability of catching the rat!
System.out.println("Enter the speed");
// Read the input
int k=s.nextInt();
// Create the frame and send the value of k
new CatchTheRat(k);
}
}
</pre>
|
|
|
|
|
- Please fix your formatting.
- Which line has the error?
- What error?
Note, we cannot see your screen or guess what problems you are seeing.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
It's called OOP for a reason. Please, the constructor never executes code. Define some methods for that.
You are adding the JLabel to the ContentPane - which you first set with a JLabel. Adding a JLabel to a JLabel does not work.
Also please just refer to the JFrame by adding to "this".
Suggestion:
1. OOP that.
2. Set up a JPanel, that forms the playground for the rat.
You probably want a second Panel for the Game controls - which are now to be entered in the console before.
3. Use a null-Layout for the game-Panel. You want to move the rat by pixel / absolute position, not by relative position to some other control.
FlowLayout is also default, does not need to be initalized.
import javax.swing.*;
class CatchTheRat extends JFrame {
JLabel lb;
Random r;
public CatchTheRat(int k) {
createGUI(k);
}
private void createGUI(int k) {
setTitle("Catch The Rat");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(800,600));
this.add(createContent(k));
setVisible(true);
}
private JPanel createContent(int k) {
JPanel oPanel = new JPanel();
lb = new JLabel(new ImageIcon("rat.jpg"));
oPanel.add(lb);
r = new Random();
lb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
Toolkit.getDefaultToolkit().beep();
System.out.println("Caught!");
}
});
return oPanel;
}
public static void main(String args[]) {
new CatchTheRat(5);
}
}
|
|
|
|
|
Thanks for the suggestion. But that worked in my system. We can add JLabel to another JLabel because JComponent itself is a container and we can add any component to any component. There is no limitation. The add method does it.
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|
Technically - yes. Until someone wants to place place a text in the JLabel or plays around.
So it's pretty much safer to use a JPanel, that is known as a container.
|
|
|
|
|
Ok. By the way, could i also know why it is not recommended to write code in the constructor and only in a separate method and then call that method?
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|