Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with arrays in Java
New Posts  All Forums:Forum Nav:

Need help with arrays in Java - Page 3

post #21 of 23
Thread Starter 
doh.gif
Thank you.
That makes sense haha. The output is still ever so slightly off... rediculous. I suck.
almostright.PNG 1k .PNG file
Should be 27.4 and 42
Doesn't make since how it could be off by so little. I'll try and figure that part out tomorrow.
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #22 of 23
Quote:
Originally Posted by Heat Miser View Post

doh.gif
Thank you.
That makes sense haha. The output is still ever so slightly off... rediculous. I suck.
almostright.PNG 1k .PNG file
Should be 27.4 and 42
Doesn't make since how it could be off by so little. I'll try and figure that part out tomorrow.

Hmmmm.... I'm sorry I don't actually know Java so I have no clue how it handles rounding and all that. One thing is for sure though, you definitely don't suck.

Someone else who knows the language better than me can hopefully help out here.

I'm going to bang out your code in C# though and see what happens... if the result is the same I can still help biggrin.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #23 of 23
Right OK... I re-read the updated first post. Looks like they charge the full amount even for part of an hour over the min hours (my logic was calculating part of the price for part of an hour). So all we need to do is raise that difference to the ceiling then multiply out.

I did make a logic error that was screwing up your total hours though.

Here it is in C# which is so close to Java you should be able to translate it:
Code:
static void Main( string[] args )
{
        double[] cars = new double[ 5 ];
        double[] hours = new double[ 5 ];
        double totalHours = 0;
        double totalCharge = 0;

        const double minPrice = 5.00;
        const double maxPrice = 12.00;
        const double minHours = 2.0;
        const double hourIncrementPrice = 1.0;

        for ( int i = 0; i < 5; ++i )
        {
                Console.WriteLine( "Enter hours parked for car #" + ( i + 1 ) );
                if ( double.TryParse( Console.ReadLine(), out hours[ i ] ) )
                {
                        /* Always charge a minimum price */
                        cars[ i ] = minPrice;

                        if ( hours[ i ] > minHours )
                        {
                                /* 
                                 * If we go over the minimum hours then add to the price with this calculation:
                                 * - raises the difference of hours[i] and minHours to the ceiling (round to next highest whole number)
                                 * - multiplies that raised difference by the hourly increment
                                 */
                                cars[ i ] += ( Math.Ceiling( ( hours[ i ] - minHours ) ) * hourIncrementPrice );
                        }

                        if ( cars[ i ] > maxPrice )
                        {
                                /* If we go over max price, cap it there */
                                cars[ i ] = maxPrice;
                        }

                        totalHours += hours[ i ];
                        totalCharge += cars[ i ];
                }
        }

        Console.WriteLine( "Total hours: " +totalHours );
        Console.WriteLine( "Total charge: " +totalCharge );
        Console.ReadLine();
}

Ceiling is sometimes called rounding toward positive infinity. In other words, if a is positive, the presence of any fractional component causes a to be rounded to the next highest integer. If a is negative, the rounding operation causes any fractional component of a to be discarded.

The equivalent in Java is Math.ceil - http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#ceil%28double%29

I did not bother with decimal places or formatting, here is the output:
Code:
Enter hours parked for car #1
1.5
Enter hours parked for car #2
4.1
Enter hours parked for car #3
12.0
Enter hours parked for car #4
9.3
Enter hours parked for car #5
0.5
Total hours: 27.4
Total charge: 42
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with arrays in Java