PDA

View Full Version : Help java "converting numbers"


shak
03-10-2009, 11:52 AM
hi guys am trying to crack this code and do the following tasks, its been a month and i haven't been able to crack, i wonder if someone can help me out.

i have some code i just need some one to help me with this. please help me..

program should do the following;

1. The program has a text entry field and two buttons, “Convert” and “Reset”.

2. From its initial state, the user enters a decimal number in the text field, and presses “Convert”. The program then performs the conversion and prints the result. How you do this is up to you:

l For a "basic" mark, you might print the binary version as a simple string.

l In order to qualify for a higher mark (60%+), however, you must print the binary string out in graphical form (i.e. the 1s and 0s are represented not as characters but as graphical objects - these may either be simple drawn objects, or imported images).

3. However the result is printed, the user must then press “Reset” to return the program to its initial state, ready for another number conversion.

4. For a first-class mark, you should modify the program so that it also offers binary to decimal conversion (you might need another button).

5. For any submission, you must in addition supply

1. Stepwise-refinement of pseudo-code based design work for the program (this should be completed before the code is written).
2. Suitable test cases (including external conditions and valid and invalid equivalence classes) which will ensure that the code to the specification above.


my code (this is what ive managed to do so far)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class binary extends JPanel implements ActionListener
{

JPanel board; // Panel for buttons
JPanel canvas; // Panel for drawing on
JButton convert;
JTextField decimalstring;

public binary()
{

super(true); // Call constructor of parent

// Standard layout (flow)
setLayout(new FlowLayout());

// Set up two panels, control board and canvas
board = new JPanel(true);
canvas = new JPanel(true);
board.setPreferredSize(new Dimension(100, 100));
canvas.setPreferredSize(new Dimension(300, 300));
board.setBorder(BorderFactory.createLineBorder(Color.black));
canvas.setBorder(BorderFactory.createLineBorder(Color.blue));


// Create buttons and attach listeners
convert = new JButton("Convert");
convert.addActionListener(this);

decimalstring = new JTextField(8);

add(canvas); add(board);

// Add button to board panel
board.add(convert);
board.add(decimalstring);

}

public void actionPerformed(ActionEvent e)
{
// Called when a button is pressed
// Set current working colour, depending on button

Graphics g = canvas.getGraphics();

}

public static void main(String[] args)
{
// Create a Blob entity
binary b = new binary();

// Set up outer frame, and its exit behaviour
JFrame frame = new JFrame("Decimal to binary");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Set the main content frame to be the Blob,
// size the frame (pack) and make it visible
frame.setContentPane(b);
frame.pack();
frame.setVisible(true);

}
}



contact me
shakz.m@hotmail.co.uk

shak
03-10-2009, 11:53 AM
Can anyone help me? Plzzzzzzzzzzzzzzzzzzzzz

Fou-Lu
03-10-2009, 11:59 AM
First, you have to give this more than 1 minute before reasking for help.
Second, all you've done is posted you're homework assignment and haven't indicated what you're specific problem is. Perhaps you should check on the API, the Integer wrapper class should have a toBinaryString method on it. Otherwise you can do this with bitmasking.


Just noticed this was supposed to be for a double. Search the net for how Java handles the double primitive and use bitmasking to extract the bits for the sign, exponent and mantissa values. Float has a method to extract raw bits, but I haven't used it before so I don't know how it works.

drbobcherry
03-10-2009, 07:25 PM
Shak, i dont think Nick Costen will be too pleased that you are trying to get other people to do your assignment....
All you needed to do was go into the help tutorials that were readily available.
And people thought i couldnt possibly know and see everything....

Old Pedant
03-11-2009, 06:47 AM
LOL! Finally! I think this is the first time I've seen a person asking for homework help get "caught."

Fou-Lu: Not sure it has to be double. Spec says "decimal number". Doesn't say anything about decimal number with a decimal point. I think he is just misusing the term "decimal number" when he means "a text string with characters represting a number in base 10 notation."