#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Number System calculator

edited December 2016

Hi all, I need help on how to go about my programming assignment about designing a number system calculator. It's a very important part of my CA. Any help would be appreciated. I'm new to programming... Below is the detail of the assignment. Thanks.

Design your own Number system calculator. It must read in a Decimal value and convert the value to a Hexadecimal, Octal and Binary. The application must work and show the objectives listed below

Objectives: 1. Design a GUI (Graphical User Interface)
2. Declare a local and global variable and initialize them. 3. Use a for loop. 4. Implement if else statement. 5. Declare an Array and access each element of the Array. 6. Write a function, call and return a value from the function. 7. Preform a calculation. 8. Fully comment all code. 9. Use the mouse functions 10. Use the keyboard functions.

Session 1:  Design GUI on paper.  Write code to implement GUI (Graphical User Interface). Session 2:  Take data in from the GUI using the Keyboard and/or the Mouse. Session 3:  Calculate from  DEC ‐> BIN  DEC ‐> HEX  DEC ‐> OCT (Create a function) Session 4:  Finish up final code.  Comment all code.

Tagged:

So, did you do anything yet? For example, did you "Design GUI on paper"? That seems like a crucial first step, and it isn't a part of your homework that people can just do for you.

• Thanks for your response. Yes I did, and this is how far I have gone...

void setup() {//returns no value size(300,300);//sets the window size to width 500 and height 500 //rect(0,50,35,35);//draws a square of width 50 and height 50 rect(0,170,75,35); } void draw() { rect(0,5,200,40);

for (int i = 50; i < 140; i = i+40) { fill(250,250,230); rect(0,i,35,35); //fill(0); //text((i/50)/50, 10, i+10); } fill(0); text("7", 10, 75); text("4", 10, 115); text("1", 10, 150); text("0", 30, 190);

for (int x = 50; x < 140; x = x+40) { fill(250,250,230); rect(40,x,35,35); }

for (int y = 50; y < 140; y = y+40) { rect(80,y,35,35); }

}

Format your code - select code, ctrl + o to indent, leave a line above and below.

• ``````void setup() {
size(300,300);
rect(0,5,200,40);
}

void draw() {

for (int i = 50; i < 140; i = i+40) {
fill(250,250,230);
rect(0,i,35,35);
}

for (int x = 50; x < 140; x = x+40) {
fill(250,250,230);
rect(40,x,35,35);
}

for (int y = 50; y < 140; y = y+40) {
rect(80,y,35,35);
}

rect(0,170,75,35);
rect(120,50,80,35);
rect(120,90,80,35);
rect(120,130,80,35);
rect(80,170,35,35);
rect(120,170,80,35);

fill(0);
text("7", 12, 75);
text("8", 52, 75);
text("9", 90, 75);
text("4", 12, 115);
text("5", 52, 115);
text("6", 92, 115);
text("1", 12, 150);
text("2", 52, 150);
text("3", 92, 150);
text("0", 30, 190);
text("C", 92, 190);
text("BIN", 145, 74);
text("HEX", 145, 112);
text("OCT", 145, 150);
text("=", 155, 190);

}

void mousePressed () {
int count=0, selection=0, input1, input2;

if ((mouseX>0) && (mouseX<35) && (mouseY<80) && (mouseY>50)) {
count = 7;
}
if ((mouseX>40) && (mouseX<75) && (mouseY<80) && (mouseY>50)) {
count = 8;
}
if ((mouseX>80) && (mouseX<115) && (mouseY<80) && (mouseY>50)) {
count = 9;
}
if ((mouseX>0) && (mouseX<35) && (mouseY<125) && (mouseY>90)) {
count = 4;
}
if ((mouseX>40) && (mouseX<75) && (mouseY<125) && (mouseY>90)) {
count = 5;
}
if ((mouseX>80) && (mouseX<115) && (mouseY<125) && (mouseY>90)) {
count = 6;
}
if ((mouseX>0) && (mouseX<35) && (mouseY<170) && (mouseY>130)) {
count = 1;
}
if ((mouseX>40) && (mouseX<75) && (mouseY<170) && (mouseY>130)) {
count = 2;
}
if ((mouseX>80) && (mouseX<115) && (mouseY<170) && (mouseY>130)) {
count = 3;
}

}
``````
• Thanks a lot Jeremydouglass and Lord_of_the_Galaxy. This is how far I have gone. I don't know where to go from here. Your help will be appreciated.

• Hey, try using a button class. Here is a demo.
For reference, https://processing.org/tutorials/objects/

Kf

``````Button b1;
String onClickStr="";
int counter=0;

void setup() {
size(600, 400);
rectMode(CORNER);
textAlign(CENTER, CENTER);
b1=new Button("Tap me!", width*0.2, 200, width*0.6, 50);
}

void draw() {
background(92);
b1.Draw();

if (b1.MouseIsOver()==true) {
text("On button", width/2.0, height*0.75);
} else {
text("Away from button", width/2.0, height*0.75);
}

if (counter>0) {
text(onClickStr, width/2.0, height*0.9);
counter--;
}
}

void mouseReleased() {
if (b1.MouseIsOver()==true) {
onClickStr="Clicked on Button!!!";
} else {
onClickStr="Clicked outside of button";
}
counter=60;  //Display message for about 2 secs
}

class Button {
String label;
float x;
float y;
float w;
float h;

// constructor
Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
label = labelB;
x = xpos;
y = ypos;
w = widthB;
h = heightB;
}

void Draw() {
fill(218);
stroke(141);
rect(x, y, w, h, 10);
textAlign(CENTER, CENTER);
fill(0);
text(label, x + (w / 2), y + (h / 2));
}

boolean MouseIsOver() {
if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
return true;
}
return false;
}
}
``````
• edited December 2016

Better still, replace line 58 of @kfrajer's code wit this - `rect(x, y, w, h, 0.35*h);`.

• Thanks to all who showed concern