Java (or possibly just maths) help please!

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Hey guys,

I've been tasked to write a simple java applet to work out the width of 'golden rectangle' after the user inputs the height.

All the basics are done, but i'm having a problem outputting the results, i'm assuming there is a problem with a way i've coded the forumla.

The maths behind 'Golden Rectangles' is;

Say the height is 1cm, then the width is going to be 1.618cm as it follows the golden ratio (1:phi obviously). The formula is 1:1+square root of 5 all divided by 2. explained coherently here

Okay, so my program compiles, but outputs 0.0 whatever I enter. Here is my code:

Code:
import java.awt.*;
import javax.swing.*;

public class GoldenRatio extends JApplet {

double doheight;


public void init()
{
String height; // height of the rectangle entered by the user
double doheight; // height int converted to a double


// read in the height of the rectangle from the user
height =
JOptionPane.showInputDialog(
"Enter the height of the rectangle" );

// convert height from tpye String to type Double
doheight = Double.parseDouble( height );
}

public void paint(Graphics g)
{
// draw the results with g.drawString
g.drawRect( 15, 10, 270, 20 );
g.drawString( "The width is " + doheight*(doheight*(Math.sqrt(5)+1)/2), 25, 25 );
}
}

Do i need to import anything else to use math.sqrt?

Thanks in advance!

inb4 olololol n00b etc.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
It does need to be an applet unfortunately, wouldn't mind seeing the code anyway though if you think it might be relevant :)
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
sorted, do nae worry, i was way off.
 

Santa's little helper

Fledgling Freddie
Joined
Dec 22, 2003
Messages
352
package ninjaPackage;

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

public class Ninja extends JApplet {

Double calc;
String ninja;
String height; // height of the rectangle entered by the user
Double doheight; // height int converted to a double
public void init()
{



// read in the height of the rectangle from the user
height = JOptionPane.showInputDialog("Enter the height of the rectangle" );

// convert height from tpye String to type Double
doheight = Double.parseDouble( height );

}

public void paint(Graphics g)
{
// draw the results with g.drawString
g.drawRect( 15, 10, 270, 20 );
calc = doheight*(doheight*(Math.sqrt(5)+1)/2);
ninja = "The width is " + calc;


g.drawString(ninja, 25, 25);

}
}
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Thanks mate.

Any idea how to parse a double to an int? :p

tried

'int x Integer.parseInt(y)'

but it doesn't work!
 

Santa's little helper

Fledgling Freddie
Joined
Dec 22, 2003
Messages
352
Thanks mate.

Any idea how to parse a double to an int? :p

tried

'int x Integer.parseInt(y)'

but it doesn't work!

Actually I'm 10 times worse to java than you :) But I'm a pretty good bug hunter (10 years of programming).

A quick google:

int i;
double ld;

ld = 10.0;
i = ld; /*convert double to int*/
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Thanks again, tried a few different methods but can't get anything to work. I think the problem lies in the fact the integers i need are stored and not returned so i'm getting confused.

Will keep cracking on!
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
Ao:

Double value;

value = (int)5;

Or something like printf...(int)result;

Had to use it a while back.. Lemme look for code. (or I'll write some).. sec
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Basically i need to convert 'calc' and 'doheight' (from your code) into ints, but i can't work out how to do it and i've been googling for hours!

When i tried your method it stated 'lack of precision' as the error.

using other methods such as Double.intValue(x) it says it can't find the variable double.intvalue.

GAH!

edit - the whole aim of this change is to print the actual rectangle, which can't be done using doubles, hence the conversion.
 

Jeremiah

Fledgling Freddie
Joined
Aug 10, 2004
Messages
1,131
You should just be able to do "calc.intValue()" and "doheight.intValue()" to convert them into ints.


For example, int x = calc.intValue()... at least, according to the java docs thats all you should need... =)
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
I'm refering to the double to int conversion bit only.

Can't get that to work using any method.

Where do you put it in?
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
You should just be able to do "calc.intValue()" and "doheight.intValue()" to convert them into ints.


For example, int x = calc.intValue()... at least, according to the java docs thats all you should need... =)

Aye tried that one, error i got was 'double can not be dereferenced.'

Full code here:

Code:
public class GoldenRatio extends JApplet {

double width;
double doheight;
int recwidth;
int recheight;

public void init()
{
String height; // height of the rectangle entered by the user
double doheight; // height int converted to a double
double width;




// read in the height of the rectangle from the user
height =
JOptionPane.showInputDialog(
"Enter the height of the rectangle" );

// convert height from tpye String to type Double
doheight = Double.parseDouble(height);


width = doheight*(Math.sqrt(5)+1)/2;

int recwidth = width.intValue();
int recheight = doheight.intValue();
}


public void paint(Graphics g)
{
// draw the results with g.drawString
g.drawRect( 15, 10, recwidth, recheight );

}
}
 

Jeremiah

Fledgling Freddie
Joined
Aug 10, 2004
Messages
1,131
Are you getting confused between the Double object, and the double primitive type? =)

If you've got a double (lets call it y), you can convert it by doing

int x = new Double(y).intValue() ;

Looking at your code, you are calling a method on the primitive double, which doesn't have any methods =) So you'll need to convert it back to a Double object before calling intValue() ;
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Are you getting confused between the Double object, and the double primitive type? =)

If you've got a double (lets call it y), you can convert it by doing

int x = new Double(y).intValue() ;

Looking at your code, you are calling a method on the primitive double, which doesn't have any methods =) So you'll need to convert it back to a Double object before calling intValue() ;

No idea what any of that means, but thanks :p

GOOGLE!
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
import java.awt.*;
import javax.swing.*;

/**
 * Class GoldenRatio - write a description of the class here
 * 
 * @author (Gareth Sweet gs216) 
 * @version (1)
 */
public class GoldenRatio extends JApplet {

double width;
double doheight;
int recwidth;
int recheight;

public void init()
{
String height; // height of the rectangle entered by the user
double doheight; // height int converted to a double
double width;






// read in the height of the rectangle from the user
height =
JOptionPane.showInputDialog(
"Enter the height of the rectangle" );

// convert height from tpye String to type Double
doheight = Double.parseDouble(height);


width = doheight*(Math.sqrt(5)+1)/2;

double width2 = new Double(width);
double doheight2 = new Double(doheight);


int recwidth = width2.intValue();
int recheight = doheight2.intValue();
}


public void paint(Graphics g)
{
// draw the results with g.drawString
g.drawRect( 15, 10, recwidth, recheight );

}
}

Double still can't be dereferenced
 

Jeremiah

Fledgling Freddie
Joined
Aug 10, 2004
Messages
1,131
Try changing:

double width2 = new Double(width);
double doheight2 = new Double(doheight);

to

Double width2 = new Double(width);
Double doheight2 = new Double(doheight);
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
its great when the fiddly-ness of a language completely obfuscates whats actually going on. ie two fucking lines of maths.
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
its great when the fiddly-ness of a language completely obfuscates whats actually going on. ie two fucking lines of maths.

This is why they added auto-boxing to Java in 1.5 ;)

The problem is in these lines:
Code:
double width2 = new Double(width);
double doheight2 = new Double(doheight);


int recwidth = width2.intValue();
int recheight = doheight2.intValue();
adding "int" infront of recwidth & recheight makes the local variables in the scope of the init() method, and not change the recwidth & recheight global variables.

Replace that with:
Code:
recwidth = (int) width;
recheight = (int) doheight;

You may get a warning about loss of precision.
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
@Jingle: Pretty much what I said? ;) But you did it showing code examples :(
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Thanks for the help guys, was due in thursday night so unfortunately i had to hand it in a bit incomplete. Everythign compiled but didnt work as it should, so i should get some marks at least :p

At least i know now for future reference :)
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
this has been a freddyshouse homework club production.
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
impressive stuart, 4th hit on google for swing tutorials.
 

Milkshake

Loyal Freddie
Joined
Dec 22, 2003
Messages
496
Aye, I get about 3000 hits a month. Just need to update it a bit when I have time, looks a bit dated :S
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
stick some casino adverts on there, you'll make a mint.
 

Milkshake

Loyal Freddie
Joined
Dec 22, 2003
Messages
496
It's on a Uni box and is a Uni project, so they'd probably own the rights unless I specifically apply for them. I'd have to look into it...thought it was nice having a site like that WITHOUT adverts.
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
was a joke :p (but if you do feel like doing it BETFAIR have a great affiliates team /pimp)
 

Users who are viewing this thread

Top Bottom