Jump to content

(Basic)Convert int to double

- - - - -

  • Please log in to reply
8 replies to this topic

#1
lemy

lemy

    Newbie

  • Members
  • PipPip
  • 14 posts
Guys help a newbie here :P,
My problem is: how to convert integer into double in an arguments or in a method? is that possible?.
here is my code :
 public class MultimediaComputer extends Computer{

    protected int cdrom;

    protected double no;

    


    public MultimediaComputer(int pm,int cd,int cm) {

         super(cd,pm);

         cdrom = cm;

        Cdromspeed(cm); 

 

       }

    

    

    

    public int Cdromspeed(int cm){

        

        return cm;

    }

 

       public void display(){

        System.out.println("Multimedia Computer");

        System.out.println("Processor mode:" + processormode);

        System.out.println("Clock Speed:" + clockspeed);

        System.out.println("CD-ROM speed:" + cdrom);

        

    

    }

} 

then the main:


public class Main{

    public static void main (String  []args){

    Computer e = new Computer(486,166);

    

     // here is my problem the argument accept an integer but not double

    //then i want to put if-else that if the value is >1000 it becomes double or else an integer

    MultimediaComputer o = new MultimediaComputer(1.2,800,48);

    

    

    

    e.display();

    o.display();

    

  

}

}

is that possible or i need to create a new method for conversion?

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Your question sounds conflicting, which argument do you wish to do this checking on, and why must it be a double if over 1000? Can you show an example of what you wish for it to do?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
lemy

lemy

    Newbie

  • Members
  • PipPip
  • 14 posts
i need the following output :
Multimedia Computer
Processor Mode: 1.2 <- the problem, the parameters declared are (int,int,int) i need to convert pm(processor mode) to double; to input the value 1.2;
Clock Speed:800
CD-ROM Speed: 48

i invoke the processormode to Computer.class

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I'd create 2 constructors:
public MultimediaComputer(int pm,int cd,int cm) {
  super(cd,pm);
  cdrom = cm;
  Cdromspeed(cm);  
}

public MultimediaComputer(double pm, int cd, int cm){
  this((int) pm*1000, cd, cm);
}
I do think it's a bit weird.
I mean you're the programmer, you can make sure that every time you call it, it's in Megahertz, and not in Giga so you don't need the double constructor.

#5
lemy

lemy

    Newbie

  • Members
  • PipPip
  • 14 posts
oh i see it is possible to create multiple constructor thanks for the help anyway how can I ouput it into double?

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
From what I can gather, you wish to accept either an integer or a double. Wim DC's code can do this, as it is two functions that either handle one or the other.

Both can convert it in to a double and return the double to the parent class, or be converted to a string to be displayed with print or println (it should convert it automatically for you)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
lemy

lemy

    Newbie

  • Members
  • PipPip
  • 14 posts

Alexander said:

From what I can gather, you wish to accept either an integer or a double. Wim DC's code can do this, as it is two functions that either handle one or the other.

Both can convert it in to a double and return the double to the parent class, or be converted to a string to be displayed with print or println (it should convert it automatically for you)

you hook it sir, but it still return an integer or whole no. if i input 1.8 it returns to 1. :cursing:
i still having lack or unknown syntax here like Wim DC's code I don't know the "this" keyword he used.

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
With "this" I just call the other constructor, I change the double into an int propery, so I can use the first constructor with the 3 parameters now.

The fact 1.8 becomes 1 is because if you cast a double (=1.8) to an int, Java just dumps the decimals when converting from double to int.
So, my previous code was a bit wrong, because Java first seems to cast, and then does the math. To solve: add brackets.
this( (int) (pm*1000), cd, cm);


#9
lemy

lemy

    Newbie

  • Members
  • PipPip
  • 14 posts

wim DC said:

With "this" I just call the other constructor, I change the double into an int propery, so I can use the first constructor with the 3 parameters now.

The fact 1.8 becomes 1 is because if you cast a double (=1.8) to an int, Java just dumps the decimals when converting from double to int.
So, my previous code was a bit wrong, because Java first seems to cast, and then does the math. To solve: add brackets.

this( (int) (pm*1000), cd, cm);


oh the "this" keyword is very useful, Alright sir Wim DC and Alexander it works thanks you! .




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users