New Posts  All Forums:Forum Nav:

Basic Java help?

post #1 of 24
Thread Starter 
Hey everyone just looking for some help here. Never posted in this section so please go easy on me if I'm making a complete mess of this! I'm really lost on this java. I've only started recently and I've started messing around with Auxiliary methods (if that's the right word for it (basically I mean drawing information from another java file). I finally got it too compile after messing around with it loads only now whenever I try and run it it'll give me results which I know are wrong. Could someone please point me in the right direction?

This is my code for the main java file:


import static javax.swing.JOptionPane.*;
class TankApp
{
public static void main(String[] args)
{
// input variables - dimensions of tank
// Strings for input, to be converted to ints

String lengthStr, widthStr, depthStr;
lengthStr = showInputDialog("Length of tank (mm)");
widthStr = showInputDialog("Width of tank (mm)");
depthStr = showInputDialog("Depth of tank (mm)");

long length, width, depth;
length = Long.parseLong(lengthStr);
width = Long.parseLong(widthStr);
depth = Long.parseLong(depthStr);

Tank calcTank = new Tank(length, width, depth);
double volume = calcTank.calculateVolume;

// display volume of tank

showMessageDialog(null, "Volume of fish tank is " +
volume + " litres");

}
}



And this is the Auxiliary method:


class Tank
{
private long length;
private long width;
private long depth;


Tank(long l, long w, long d)
{
length = l;
width = w;
depth = d;
}

public double calculateVolume;
{


// calculate volume in cubic mm
long volCmm = length * width * depth;

// convert to volume in litres
double volLitre = volCmm / 1000000.0;

// round to the nearest litre
long volume = Math.round(volLitre);

}
}



Basically it always gives me zero for volume. What am I doing so wrong!? If you could please try being clear I'm a complete n00b. Thanks!
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 24
Are you not returning your value from the function used to calculate the response?

Code:
public double calculateVolume;
{


// calculate volume in cubic mm
long volCmm = length * width * depth;

// convert to volume in litres
double volLitre = volCmm / 1000000.0;

// round to the nearest litre
long volume = Math.round(volLitre);

}
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #3 of 24
Thread Starter 
Ahhh I knew I was missing something! Only whenever I try and add "return volume;" just before the brackets it refuses to compile saying
"Tank.java.23: return outside method
return volume;
^
1 error"

What does this mean and what am I doing it wrong? :S

Thanks
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #4 of 24
Sorry your going to have to forgive me because i mainly program in C# so i dont know the exact java syntax, but first you declared that the function will return a type double and your volume is a type long. So one of those needs to be changed, then you just return at the end of the function.

Code:
public double calculateVolume;
{


// calculate volume in cubic mm
long volCmm = length * width * depth;

// convert to volume in litres
double volLitre = volCmm / 1000000.0;

// round to the nearest litre
long volume = Math.round(volLitre);


//change long volume to a double here
<---

return volume; 

}

Edited by mykah89 - 2/23/11 at 9:06am
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #5 of 24
Thread Starter 
Ahhh thanks that seems to make sense only I'm not too sure how I'd convert the long to a double it's such a pain and my notes aren't that clear :/ would it be something like this?

Code:
"long volume = Math.round(volLitre);
//change long to double
double volumeFinal = volume;"

Or is that COMPLETELY wrong
Edited by kidshenlong - 2/23/11 at 9:16am
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #6 of 24
Quote:
Originally Posted by kidshenlong View Post
Ahhh thanks that seems to make sense only I'm not too sure how'd covert the long to a double it's such a pain and my noes aren't that clear :/ would it be something like this?

Code:
"long volume = Math.round(volLitre);
//change long to double
double volumeFinal = volume;"

Or is that COMPLETELY wrong

Thats completely wrong

If you dont need to return a double you could just change your return type to a long and then the call from the main thread to expect a long as a return type.


Code:

import static javax.swing.JOptionPane.*;
class TankApp
{
public static void main(String[] args)
{
// input variables - dimensions of tank
// Strings for input, to be converted to ints

String lengthStr, widthStr, depthStr;
lengthStr = showInputDialog("Length of tank (mm)");
widthStr = showInputDialog("Width of tank (mm)");
depthStr = showInputDialog("Depth of tank (mm)");

long length, width, depth;
length = Long.parseLong(lengthStr);
width = Long.parseLong(widthStr);
depth = Long.parseLong(depthStr);

Tank calcTank = new Tank(length, width, depth);

//change double volume to long volume
long volume = calcTank.calculateVolume;

// display volume of tank

showMessageDialog(null, "Volume of fish tank is " +
volume + " litres");

}
}


And this is the Auxiliary method:


class Tank
{
private long length;
private long width;
private long depth;


Tank(long l, long w, long d)
{
length = l;
width = w;
depth = d;
}

//change return type to long

public long calculateVolume;
{


// calculate volume in cubic mm
long volCmm = length * width * depth;

// convert to volume in litres
double volLitre = volCmm / 1000000.0;

// round to the nearest litre
long volume = Math.round(volLitre);

return volume;

}
}

Otherwise your going to have to look up how to cast volume to a double via google or hopefully someone else can chime in with the correct syntax.



Edit: After a quick google it looks like you may be able to just cast as such:

double volumeFinal = (double)volume;
Edited by mykah89 - 2/23/11 at 9:22am
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #7 of 24
Thread Starter 
Ahhh thanks. But no matter what I do it won't compile... It keeps telling me there's a problem with the return bit saying :

"Tank.java.23: return outside method
return volume;
^
1 error"

any idea on what this means this mean? :S
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #8 of 24
Quote:
Originally Posted by kidshenlong View Post
Ahhh thanks. But no matter what I do it won't compile... It keeps telling me there's a problem with the return bit saying :

"Tank.java.23: return outside method
return volume;
^
1 error"

any idea on what this means this mean? :S


Lol yea, sorry i guess i was completely overlooking this

Try rewriting the function as

Code:
public long calculateVolume()
{

//function code in here


}
Code:

public long calculateVolume;   <--------------------------- this is a method and you wrote it as a statement with the ;
{


// calculate volume in cubic mm
long volCmm = length * width * depth;

// convert to volume in litres
double volLitre = volCmm / 1000000.0;

// round to the nearest litre
long volume = Math.round(volLitre);

return volume;

}
}
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #9 of 24
Try changing the declaration to:

public long calculateVolume()

(no semi-colon)

Edit: Beaten by a mere second.

Anyway, you don't want to use the semi-colon when you are defining a method like that. Every time you use a semi-colon, you're saying "this is the end of this statement." So nothing in the brackets after that was attached to the proper method. Feel me?
Edited by Plex - 2/23/11 at 9:37am
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #10 of 24
Thread Starter 
*facepalm* Oh wow I'd done it twice it turns out. I really do have to watch out for that and stop going crazy with semi-colons. There's such a fine line in programming :/ but thanks dude it's running now. You're a complete life saver and I'll rep+ accordingly! Maybe someday I'll be able to return the favour...
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming