Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,337 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,666 people online right now. Registration is fast and FREE... Join Now!




help me In the code convert from decimal to Binary

 

help me In the code convert from decimal to Binary

sahmalhob

6 Jun, 2009 - 05:36 AM
Post #1

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 5

Help me in this project

Please have the delivery date approached and the 50 mark of 100

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

�� Write a Java program to develop an advance calculator with many
numbering systems. The program will support some services to allow the
user to convert from one system to another and make any arithmetic
operations with any system. Look at the following graph which demonstrates
the idea:

Number
String numb;
Void printInFormat( )

Binary
boolean check_B( )
int convertToDecimal( )
int SumTwoBinary(Binary cool.gif
int SubTwoBinary(Binary cool.gif
int MultiTwoBinary(Binary cool.gif
int DivTwoBinary(Binary cool.gif

Decimal
boolean check_D( )
int convertToBinary( )
int convertToOctal( )
int SumTwoDec(Decimal d)
int SubTwoDec(Decimal d)
int MultiTwoDec(Decimal d)
int DivTwoDec(Decimal d)

Octal
boolean check_Oc( )
int convertToDecimal( )
int SumTwoOctal(Octal c)
int SubTwoOctal(Octal c)
int MultiTwoOctal(Octal c)
int DivTwoOctal(Octal c)

��Your program should do the following:
1. Your program will deal only with integer part of any number.
2. Write classes to simulate the above graph with described fields and
methods for each class:
• Number class is a super class consisting of a number itself and method to
write a number in specific format. For example, if numb = "123", the output will
be printed as "The result is ***123".
• Binary, Decimal and Octal classes have some methods that describe
specific system. For example, the " convertToDecimal( ) " method in binary
class will convert the binary number to decimal and return it as an integer. The
methods of summation, subtraction, multiplication and division will take two
binary operators and return binary number. Each class will deal with its type.
• The methods "check" will check if your number is as expected that if your
object is binary, the input number must be in binary format that consists only from
0 or 1 and if it is not, the number will be returned to enter again. Each class will
deal with its type.
Faculty of EIT Java Project
Number
String numb;
Void printInFormat( )
Binary
boolean check_B( )
int convertToDecimal( )
int SumTwoBinary(Binary cool.gif
int SubTwoBinary(Binary cool.gif
int MultiTwoBinary(Binary cool.gif
int DivTwoBinary(Binary cool.gif
Decimal
boolean check_D( )
int convertToBinary( )
int convertToOctal( )
int SumTwoDec(Decimal d)
int SubTwoDec(Decimal d)
int MultiTwoDec(Decimal d)
int DivTwoDec(Decimal d)
Octal
boolean check_Oc( )
int convertToDecimal( )
int SumTwoOctal(Octal c)
int SubTwoOctal(Octal c)
int MultiTwoOctal(Octal c)
int DivTwoOctal(Octal c)
2
3. In main class, you must develop a scientific calculator as the following
output:
Welcome to scientific calculator….enjoyed with it.
Scientific calculator's Main Menu……….
************************************************************
1. Decimal 2. Binary 3. Octal 4. Conversions 5. Quit
************************************************************
CHOOSE YOUR SYSTEM: 2
A) Add two binary
S) Subtract two binary
M) Multiple two binary
D) Divide two binary
cool.gif Back to main menu
Enter your formula:
## A 101 11
The result is ***1000
Enter your formula:
## B
Finished section…………….
************************************************************
1. Decimal 2. Binary 3. Octal 4. Conversions 5. Quit
************************************************************
CHOOSE YOUR SYSTEM: 4
This section is designed to convert from one system to another…..
1-Decimal to Binary 3-Decimal to Octal 5-Binary to Octal
2-Binary to Decimal 4-Octal to Decimal 6-Octal to Decimal
************************************************************
7-Back to Main
**************
ENTER YOUR CHOICE: 1
Write number to convert: 98
The result is ***1100010
ENTER YOUR CHOICE: 5
Write number to convert: 123
Be attention!!!!! Your number must be in binary format.
ENTER YOUR CHOICE: 5
Write number to convert: 11
The result is ***3
ENTER YOUR CHOICE: 7
Finished section…………….
************************************************************
1. Decimal 2. Binary 3. Octal 4. Conversions 5. Quit
************************************************************
CHOOSE YOUR SYSTEM: 5
Thank you for using our system…..
3
4. Finally, print an error message if any of your choice is incorrect and try
again.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


User is offlineProfile CardPM
+Quote Post


mostyfriedman

RE: Help Me In The Code Convert From Decimal To Binary

6 Jun, 2009 - 05:58 AM
Post #2

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 3,144



Thanked: 355 times
Dream Kudos: 600
Expert In: Learning

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

sahmalhob

RE: Help Me In The Code Convert From Decimal To Binary

6 Jun, 2009 - 07:33 AM
Post #3

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 5

I worked part
This code, super
CODE

import java.util.Scanner;
public class Number{

private String numb;

public Number(){
    
}
public Number(String n){
    numb=n;
}
public void setNumber(String n){
    
    numb=n;
}
public String getNumber(){
    return numb;
}

}
    


This inherits from the code
CODE

import java.util.Scanner;

public class Binary extends Number {
    private  int out;
    public Binary(){
        
    }
    public Binary(String n1){
        super(n1);
    }
    public boolean check_B() {

        boolean check = true;
        String input=super.getNumber();
        for(int i=0; i < input.length(); i++){
               if(input.charAt(i) == '0' || input.charAt(i) == '1'){
                   continue;
               }
               else {
                  check = false;
                  break;
               }
        }
        return check;
    }
    public int convertToDecimal(){
        Scanner input = new Scanner(System.in);
        int binValue=0;
        int i;
        boolean done=false;


        do{

        if(check_B()){
            
        int length=super.getNumber().length();
        int po=length-1;
      
       for (int counter = 0; counter<length; counter++) {
           
            if (super.getNumber().charAt(counter) == '1') {
          
          binValue = binValue +(int) (Math.pow(2.0, po));
             }
            po--;
           }
          done = true;
              }
              else {            
                System.out.println(super.getNumber() + " is not a valid binary number.");
                System.out.print("Enter a valid binary number:");
                super.setNumber(input.next());                
            }
                   }while(!done);
           out=binValue;
    return binValue;
   }
  
   public int sum(Binary b){
        Scanner input = new Scanner(System.in);

             
        int b3=0,b4=0,ss=0;
        String s,sum="0";
        boolean done=false;

       b3=b.convertToDecimal();

       b4=this.convertToDecimal();
        ss=(b3+b4);
        return ss;
     }
     public void print(){
         System.out.println("Your number " + super.getNumber());
        System.out.println("The decimal value of " + super.getNumber() + " is: " + out);
     }
    
   }

This also inherit from the
This also inherit from it, but where I want to repair your mistakes

CODE

import java.util.*;
public class Decimal extends Number{
        public Decimal(){
        
    }
    public Decimal(String n1){
        super(n1);
    }
        Binary d = new Binary();
     public boolean check_D() {

        boolean check = true;
        for(int i=0; i < super.getNumber().length(); i++){
               if(super.getNumber().charAt(i) == '0' || super.getNumber().charAt(i) == '1'|| super.getNumber().charAt(i) == '2'|| super.getNumber().charAt(i) == '3'|| super.getNumber().charAt(i) == '4'|| super.getNumber().charAt(i) == '5'|| super.getNumber().charAt(i) == '6'|| super.getNumber().charAt(i) == '7'|| super.getNumber().charAt(i) == '8'|| super.getNumber().charAt(i) == '9'){
                   continue;
               }
               else {
                  check = false;
                  break;
               }
        }
        return check;
    }
   public int convertToBinary(){
            Binary d = new Binary("11");
            d.check_B();
            d.convertToDecimal();
            int out;
            Scanner input = new Scanner(System.in);
        int decValue=0;
        int i;
        boolean done=false;

        
        do{

        if(check_D()){
            
        int dec=super.getNumber().length();
        int mod;

      decValue = (dec/2);
      mod = (dec%2);
      decValue = (mod/2);

      }
      }while(!done);
           out=decValue;
    return decValue;
   }
   public static void main(String args[]){
       Decimal d = new Decimal("11");
       d.check_D();
       System.out.println(d.convertToBinary());
   }  
  
}

This is the code for testing, but it is also incomplete because I could be finished for the rest

CODE

import java.util.Scanner;
public class testProject{

public static void main(String args[]){
               Scanner input = new Scanner(System.in);
                 Binary b = new Binary();
System.out.println("\tWelcome to scientific calculator...enjoyed with it.\n");
System.out.println("\t   Scientific calculator's Main Menu..........");
System.out.print(& #34;****************************************************************************
****");
    
System.out.println("1.Decimal \t 2.Binary \t 3.Octal \t 4.Conversions \t 5.Quit ");    

System.out.println(& #34;****************************************************************************
****");
         int x=0,z=0;
         char y='0';
         x=input.nextInt();
         switch(x){
             case 1:
             
             break;
             case 2:
             System.out.println("A)Add two binary");
             System.out.println("S)Add two binary");
             System.out.println("M)Add two binary");
             System.out.println("D)Add two binary");
             System.out.println("B)Add two binary");
       y=input.next().charAt(0);
             switch(y){
        case 'A':
        String b1,b2;
         b1=input.next();
         b2=input.next();
         Binary bb=new Binary(b1);
         b.setNumber(b2);
    System.out.println(b.sum(bb));
        break;
        case 'S':
             System.out.println("empty");
        break;
        case 'M':
             System.out.println("empty");
        break;
        case 'D':
             System.out.println("empty");
        break;
        case 'B':
             System.out.println("empty");
        break;
        }
            break;
            case 3:
             System.out.println("empty");
             break;
             case 4:
             System.out.println("\tThis section is designed to convert from one system to onther....\n");
System.out.println("1-Decimal to Binary \t 3-Decimal to Octal \t 5-Binary to Octal ");
System.out.println("2-Binary to Decimal \t 4-Octal to Decimal \t 6-Octal to Binary");
System.out.println(& #34;****************************************************************************
****");
System.out.println("\t\t\t\t7-Back to Main");
System.out.println("\t\t\t\t**************");
             switch(z){
        case 1:
        String b3;
         b3=input.next();
         Binary bb=new Binary(b3);

    System.out.println(b.sum(bb));
        break;
        case 2:
             System.out.println("empty");
        break;
        case 3:
             System.out.println("empty");
        break;
        case 4:
             System.out.println("empty");
        break;
        case 5:
             System.out.println("empty");
        break;
        case 6:
             System.out.println("empty");
        break;
        }
             break;
             case 5:
             System.out.println("Thank you for using our system.....");
             break;
         }

   }
   }
    


I hope you understood that you had the draft, and any point of understanding what I'm ready for spelled

User is offlineProfile CardPM
+Quote Post

sahmalhob

RE: Help Me In The Code Convert From Decimal To Binary

7 Jun, 2009 - 02:24 AM
Post #4

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 5

This code conversion from decimal-to-binary, but where I want to repair your mistakes


I want to amend this section

CODE

public int convertToBinary(){
            Binary d = new Binary("11");
            d.check_B();
            d.convertToDecimal();
            int out;
            Scanner input = new Scanner(System.in);
        int decValue=0;
        int i;
        boolean done=false;

        
        do{

        if(check_D()){
            
        int dec=super.getNumber().length();
        int mod;

      decValue = (dec/2);
      mod = (dec%2);
      decValue = (mod/2);

      }
      }while(!done);
           out=decValue;
    return decValue;
   }


This post has been edited by sahmalhob: 7 Jun, 2009 - 02:31 AM
User is offlineProfile CardPM
+Quote Post

janotte

RE: Help Me In The Code Convert From Decimal To Binary

7 Jun, 2009 - 02:38 AM
Post #5

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,157



Thanked: 152 times
Expert In: C/C++

My Contributions

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.


User is offlineProfile CardPM
+Quote Post

sahmalhob

RE: Help Me In The Code Convert From Decimal To Binary

7 Jun, 2009 - 02:58 AM
Post #6

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 5

This code, super

CODE

import java.util.Scanner;
public class Number{

private String numb;

public Number(){
    
}
public Number(String n){
    numb=n;
}
public void setNumber(String n){
    
    numb=n;
}
public String getNumber(){
    return numb;
}

}
    


This method requires you to modify the
Is the conversion from a decimal to binary

I will test you to the originator of this method

This code, sub

CODE

import java.util.*;
public class Decimal extends Number{
        public Decimal(){
        
    }
    public Decimal(String n1){
        super(n1);
    }
        Binary d = new Binary();
     public boolean check_D() {

        boolean check = true;
        for(int i=0; i < super.getNumber().length(); i++){
               if(super.getNumber().charAt(i) == '0' || super.getNumber().charAt(i) == '1'|| super.getNumber().charAt(i) == '2'|| super.getNumber().charAt(i) == '3'|| super.getNumber().charAt(i) == '4'|| super.getNumber().charAt(i) == '5'|| super.getNumber().charAt(i) == '6'|| super.getNumber().charAt(i) == '7'|| super.getNumber().charAt(i) == '8'|| super.getNumber().charAt(i) == '9'){
                   continue;
               }
               else {
                  check = false;
                  break;
               }
        }
        return check;
    }
   public int convertToBinary(){
            Binary d = new Binary("11");
            d.check_B();
            d.convertToDecimal();
            int out;
            Scanner input = new Scanner(System.in);
        int decValue=0;
        int i;
        boolean done=false;

        
        do{

        if(check_D()){
            
        int dec=super.getNumber().length();
        int mod;

      decValue = (dec/2);
      mod = (dec%2);
      decValue = (mod/2);

      }
      }while(!done);
           out=decValue;
    return decValue;
   }
   public static void main(String args[]){
       Decimal d = new Decimal("11");
       d.check_D();
       System.out.println(d.convertToBinary());
   }  
  
}

I want to be modified and transferred from the decimal-to-binary

User is offlineProfile CardPM
+Quote Post

sahmalhob

RE: Help Me In The Code Convert From Decimal To Binary

7 Jun, 2009 - 07:27 PM
Post #7

New D.I.C Head
*

Joined: 6 Jun, 2009
Posts: 5

This prevents code transferred from the decimal to Binary, which I want the amendment made by

CODE

import java.util.*;
public class dec{
    
    public static void main(String args[]){
       System.out.println(convertToBinary("7"));
   }
  
     public static boolean isDecimal(String input) {

        boolean check = true;
        for(int i=0; i < input.length(); i++){
               if(input.charAt(i) == '0' || input.charAt(i) == '1'|| input.charAt(i) == '2'|| input.charAt(i) == '3'|| input.charAt(i) == '4'|| input.charAt(i) == '5'|| input.charAt(i) == '6'|| input.charAt(i) == '7'|| input.charAt(i) == '8'|| input.charAt(i) == '9'){
                   continue;
               }
               else {
                  check = false;
                  break;
               }
        }
        return check;
    }
     public static int convertToBinary(String d){
        Scanner input = new Scanner(System.in);
        int decValue=0,mod=0;
        int i;
        i=Integer.parseInt(d);
        decValue=i/2;
        mod=decValue%2;
        return mod;
        
   }
  
}


In this code two ways, first to examine the number of decimal input is it or not, and not any error, and this is
CODE

public static boolean isDecimal(String input) {

        boolean check = true;
        for(int i=0; i < input.length(); i++){
               if(input.charAt(i) == '0' || input.charAt(i) == '1'|| input.charAt(i) == '2'|| input.charAt(i) == '3'|| input.charAt(i) == '4'|| input.charAt(i) == '5'|| input.charAt(i) == '6'|| input.charAt(i) == '7'|| input.charAt(i) == '8'|| input.charAt(i) == '9'){
                   continue;
               }
               else {
                  check = false;
                  break;
               }
        }
        return check;
    }



The next code finded error

The second method to enter the number through the String, and the conversion of decimal number system to Binary, I do not want to enter the number in the integer but I want to enter through the string, as I worked
CODE

public static int convertToBinary(String d){
        Scanner input = new Scanner(System.in);
        int decValue=0,mod=0;
        int i;
        i=Integer.parseInt(d);
        decValue=i/2;
        mod=decValue%2;
        return mod;
        
   }


This post has been edited by sahmalhob: 7 Jun, 2009 - 07:54 PM
User is offlineProfile CardPM
+Quote Post

Dantheman

RE: Help Me In The Code Convert From Decimal To Binary

7 Jun, 2009 - 07:36 PM
Post #8

D.I.C Regular
***

Joined: 27 May, 2009
Posts: 445



Thanked: 25 times
My Contributions
Lol, why is this marked as "Advanced"? This is "Beginner" at most.

You're not converting anything with your algorithm. This is how you get the first binary digit:

CODE

int digit = number % 2;  <--- First digit

Now, do the same to the rest of the number until you process all of its digits.

int rest_of_the_number = number / 2;


I'll leave the rest for you to figure out.


User is offlineProfile CardPM
+Quote Post

pbl

RE: Help Me In The Code Convert From Decimal To Binary

7 Jun, 2009 - 08:24 PM
Post #9

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1124 times
Dream Kudos: 450
My Contributions
Converting to binary is easy
init a String to ""
while the number to convert is > 0
prefix the String by number modulo 2
divide the number by 2

CODE

String convertToBin(int n) {
   String str = "";
   while(n > 0) {
      str = (n % 2) + str;
      n /= 2;
   }
   return str;
}

User is offlineProfile CardPM
+Quote Post

pbl

RE: Help Me In The Code Convert From Decimal To Binary

7 Jun, 2009 - 09:30 PM
Post #10

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1124 times
Dream Kudos: 450
My Contributions
Topics merged
Please avoid double postings mad.gif
User is offlineProfile CardPM
+Quote Post

Lodetx

RE: Help Me In The Code Convert From Decimal To Binary

8 Jun, 2009 - 08:42 AM
Post #11

New D.I.C Head
*

Joined: 8 Jun, 2009
Posts: 3

Hi Amroo,

Maybe You Should Do Your Assignments By Yourself, Not Asking For It And Pretend That You R The One In front Of Your Friends.

Hope To Find Out Your Answers .. Bye
User is offlineProfile CardPM
+Quote Post

Lodetx

RE: Help Me In The Code Convert From Decimal To Binary

8 Jun, 2009 - 08:44 AM
Post #12

New D.I.C Head
*

Joined: 8 Jun, 2009
Posts: 3

Hi Amroo,

Maybe You Should Do Your Assignments By Yourself, Not Asking For It And Pretend That You R The One In front Of Your Friends.

Hope To Find Out Your Answers .. Bye
User is offlineProfile CardPM
+Quote Post

computerfox

RE: Help Me In The Code Convert From Decimal To Binary

8 Jun, 2009 - 08:45 AM
Post #13

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
CODE

void yourAnswer(){
while(0==0){
cout<<"So what seems to be the problem...\n";
cout<<"We here at DIC don't do your work for you...\n";
cout<<"We only help with code presented...\n";
cout<<"Please edit your post with your code and specific question...\n";
cout<<"Thank you...\n";
}
return;
}


A+++++++++++++++++ worthy code biggrin.gif
User is offlineProfile CardPM
+Quote Post

computerfox

RE: Help Me In The Code Convert From Decimal To Binary

8 Jun, 2009 - 08:58 AM
Post #14

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
@lodetx hey dude, if you're gonna be hanging around in the help threads, help, don't bump. also there is nothing wrong with those posts, but make sure they aren't just trying to get code. if they are actually trying don't be a jerk. just some friendly advise
User is offlineProfile CardPM
+Quote Post

pbl

RE: Help Me In The Code Convert From Decimal To Binary

8 Jun, 2009 - 04:54 PM
Post #15

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1124 times
Dream Kudos: 450
My Contributions
Merged again... you are playing with our patience and the time we spent answering all your duplicated posts mad.gif

QUOTE(sahmalhob @ 6 Jun, 2009 - 05:36 AM) *

Help me in this project

Please have the delivery date approached and the 50 mark of 100

Bad planning on your part does not mean an emergency on our part
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 05:10PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month