Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › More Java help (Sorry guys :-()
New Posts  All Forums:Forum Nav:

More Java help (Sorry guys :-()

post #1 of 12
Thread Starter 
Let me explain a little, I am taking a online Java course with ZERO direction from the teacher and there isn't much for examples when going through the chapter, so I'm a little lost once I get to the assignments, sorry if I'm spamming. Starting to panic since these assignments are due in about 2 hours and I don't wanna be late.

That being said, I'm not sure how to figure this out:
Quote:
(i) Create a new folder called 'assign32' under C:\Java\Lesson3. Copy HelloWorld.java from Lesson 1 into it, and rename the file to "GDPPerCapita.java". Replace the word "HelloWorld" with "GDPPerCapita". Replace the System.out line with instructions to declare variables, and display output as described below:
(ii) Declare in 'int' type variable called 'gdp' and initialize it with a value of 175000
(iii) Declare a 'int' type variable called 'population' initialized to 4000.
(iv) Declare a 'double' type variable called 'gdpPerCapita', and assign it the result of GDP per capita (= GDP/Population).
(v) Declare an 'int' type variable called 'gdpPerCapitaDollars' and assign it only the "dollars" part of the value from 'gdpPerCapita'. So if the 'gdpPerCapita' = 43.75, then 'gdpPerCapitaDollars' should contain 43. [Hint: Use an appropriate cast on 'gdpPerCapita'].
(vi) Declare an 'int' type variable called 'gdpPerCapitaCents' and assign it only the "cents" part of the value from 'gdpPerCapita'. So if the 'gdpPerCapita' = 43.75, then 'gdpPerCapitaCents' should contain 75. [Note: Do NOT "hard-code" its value as "75". Instead, compute that value from an arithmetic expression using 'gdpPerCapitaDollars' and 'gdpPerCapita'].

(vii) Using System.out.println lines, display the following:
• The label: "GDP: $" concatenated with the value of 'gdp' variable.
• The label: "Population: " concatenated with the value of 'population' variable.
• The label: "GDP Per Capita: $" concatenated with the value of 'gdpPerCapita' variable.
• The label: "GDP Per Capita: " concatenated with the value of 'gdpPerCapitaDollars' variable, concatenated with the label " Dollars and ", concatenated with the value of 'gdpPerCapitaCents' variable, finally concatenated with the label: " Cents".
(viii) A sample output session is shown below:

Not exactly sure how to go about doing that. redface.gif

This is what I have:
Code:
class GDPPerCapita
{
    public static void main(String[] args)
    {
                int gdp = 175000;
                int population = 4000;
                double gdpPerCapita = (175000/4000); 
                int gdpPerCapitaDollars = 
                int gdpPerCapitaCents = 
        
        System.out.println("Hai Gais");
    }
}
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #2 of 12
Quote:
Originally Posted by Krunk_Kracker View Post

Let me explain a little, I am taking a online Java course with ZERO direction from the teacher and there isn't much for examples when going through the chapter, so I'm a little lost once I get to the assignments, sorry if I'm spamming. Starting to panic since these assignments are due in about 2 hours and I don't wanna be late.
That being said, I'm not sure how to figure this out:
Quote:
(i) Create a new folder called 'assign32' under C:\Java\Lesson3. Copy HelloWorld.java from Lesson 1 into it, and rename the file to "GDPPerCapita.java". Replace the word "HelloWorld" with "GDPPerCapita". Replace the System.out line with instructions to declare variables, and display output as described below:
(ii) Declare in 'int' type variable called 'gdp' and initialize it with a value of 175000
(iii) Declare a 'int' type variable called 'population' initialized to 4000.
(iv) Declare a 'double' type variable called 'gdpPerCapita', and assign it the result of GDP per capita (= GDP/Population).
(v) Declare an 'int' type variable called 'gdpPerCapitaDollars' and assign it only the "dollars" part of the value from 'gdpPerCapita'. So if the 'gdpPerCapita' = 43.75, then 'gdpPerCapitaDollars' should contain 43. [Hint: Use an appropriate cast on 'gdpPerCapita'].
(vi) Declare an 'int' type variable called 'gdpPerCapitaCents' and assign it only the "cents" part of the value from 'gdpPerCapita'. So if the 'gdpPerCapita' = 43.75, then 'gdpPerCapitaCents' should contain 75. [Note: Do NOT "hard-code" its value as "75". Instead, compute that value from an arithmetic expression using 'gdpPerCapitaDollars' and 'gdpPerCapita'].

(vii) Using System.out.println lines, display the following:
• The label: "GDP: $" concatenated with the value of 'gdp' variable.
• The label: "Population: " concatenated with the value of 'population' variable.
• The label: "GDP Per Capita: $" concatenated with the value of 'gdpPerCapita' variable.
• The label: "GDP Per Capita: " concatenated with the value of 'gdpPerCapitaDollars' variable, concatenated with the label " Dollars and ", concatenated with the value of 'gdpPerCapitaCents' variable, finally concatenated with the label: " Cents".
(viii) A sample output session is shown below:
Not exactly sure how to go about doing that. redface.gif
This is what I have:
Code:
class GDPPerCapita
{
    public static void main(String[] args)
    {
                int gdp = 175000;
                int population = 4000;
                double gdpPerCapita = (175000/4000); 
                int gdpPerCapitaDollars = 
                int gdpPerCapitaCents = 
        
        System.out.println("Hai Gais");
    }
}

Ok, so for the gdpPerCaptia, you should do your arithmatic with the actual variables:
Code:
double gdpPerCapita = gdp / population;

Then, to get gdpPerCapitaDollars, you can just cast it as an int, which will shave off the decimal:
Code:
int gdpPerCapitaDollars = (int) gdpPerCapita;

Then you should be able to figure out how to get gdpPerCaptiaCents. smile.gif (Hint: If you have the dd.cc and dd, how do you get .cc?)
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #3 of 12
Thread Starter 
Quote:
Originally Posted by Seventh Badger View Post

Ok, so for the gdpPerCaptia, you should do your arithmatic with the actual variables:
Code:
double gdpPerCapita = gdp / population;
Then, to get gdpPerCapitaDollars, you can just cast it as an int, which will shave off the decimal:
Code:
int gdpPerCapitaDollars = (int) gdpPerCapita;
Then you should be able to figure out how to get gdpPerCaptiaCents. smile.gif (Hint: If you have the dd.cc and dd, how do you get .cc?)

Not following your hint....lol
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #4 of 12
Quote:
Originally Posted by Krunk_Kracker View Post

Not following your hint....lol

Haha I suppose it was a bit cryptic tongue.gif. So if you have 43.75 for gdpPerCapita and 43 for gdpPerCapticaDollars, then to find the cents, you would do 43.75 - 43 = .75. If you want this as an integer, just multiply it by 100 and cast it as an int like before. I'm terrible at teaching stuff lol, so here's the code:
Code:
int gdpPerCapitaCents = (int) ((gdpPerCapita - gdpPerCapitaDollars) * 100);
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #5 of 12
Thread Starter 
doh.gif

I see now.

Seeing the code is good for me...it usually clicks what needs needs clicking in my brain, haha. Thanks!

Hopefully I can get this crap to print correctly.
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #6 of 12
Excellent! Glad I could help. biggrin.gif I'll keep this page up in case you need any more help. thumb.gif
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #7 of 12
Thread Starter 
I've gotten it mostly...my only problem is this:
Code:
class GDPPerCapita
{
    public static void main(String[] args)
    {
                int gdp = 175000;
                int population = 4000;
                double gdpPerCapita = (gdp / population);
                int gdpPerCapitaDollars = gdpPerCapita;
                int gdpPerCapitaCents = (int) ((gdpPerCapita - gdpPerCapitaDollars) * 100);
        
        System.out.println("GDP: $" + gdp);
                System.out.println("Population: " + population);
                System.out.println("GDP per Capita: $" + gdpPerCapita);
                System.out.println("GDP Per Capita: " + gdpPerCapitaDollars + " and " + gdpPerCapitaCents);
    }
}

I'm having an compute problem. If I keep gdpPerCapita as a double, it won't compile, I get this error:
Quote:
C:\Users\jstroud\Dropbox\Java\Lesson 3\assign32>javac GDPPerCapita.java
GDPPerCapita.java:8: error: possible loss of precision
int gdpPerCapitaDollars = gdpPerCapita;
^
required: int
found: double
1 error

C:\Users\jstroud\Dropbox\Java\Lesson 3\assign32>

But if I change it to be an integer, it compiles and runs, but with an incorrect output of course.

Thoughts on this? I'm not seeing where I'm going wrong.
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #8 of 12
Does it work if you do
Code:
int gdpPerCapitaDollars = (int) gdpPerCapita;
?
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #9 of 12
Thread Starter 
Yeah, I changed that, and now the double works, but my output is still incorrect....

It's outputting:

GDP: $175000
Population: 4000
GDP per Capita: $43.0
GDP Per Capita: 43 and 0

It should be

GDP: $175000
Population: 4000
GDP per Capita: $43.0
GDP Per Capita: 43 and 75

:-/
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #10 of 12
Ah, I see the problem. When calculating the gdpPerCapita, you need to cast your variables as doubles, like so:
Code:
double gdpPerCapita = (double) gdp / (double) population;

That should work smile.gif
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › More Java help (Sorry guys :-()