Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Static Method - Did I do this right?
New Posts  All Forums:Forum Nav:

Java Static Method - Did I do this right? - Page 2

post #11 of 17
To me, the way that I read it, you did the second half to part 1, and you kind of did the bonus already. It looks like you are missing, as was said earlier, to get the total sum from the user BEFORE you get how many of each coin. so you have some good structure so I don't feel like you are just begging for an answer. So I will supply some pseudo code:

Ask user: Please enter sum of all coins
input = userInput
if the input is greater than 99 or less than 0.
return error to user
exit program
else
ask user to enter number of each type of coin.
add all values together
check to see if all added together equals users original sum input.
if it does
Tell him good job
else
ask for coins again
if wrong again
exit program

A key thing in the description to me that the professor might look for is, "prompt the user ONE more time". So while a loop would work, I feel it is overkill if this is exactly what the professor is looking for. Also the pseudo code I gave will work in the order I gave, but I would highly suggest looking for the repetition in the code and moving that into it's own static function (Hint: bonus question).

It looks as though you have a good start to the program. Just go back and reread the problem a couple of times to make sure you really understand what is supposed to be happening. Good luck.
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
post #12 of 17
if I would have known i could have recieved programming help from this site i would have asked!
post #13 of 17
The key word is help. I won't give any kind of answer unless the asker puts some effort into the problem. To many people post a homework question on here and expect to get any ready answer. (Sorry to take away from this post)
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
post #14 of 17
1. Some instructors want to see a lot of pertinent comments (documentation) so make sure you comment each variable and block - what it is and what is its purpose... and maybe expected outcome.

2. initialize (numberA = 0) variables when you declare them or you risk the variables to keep last value/pointer when you re-run the code.

3. USERS MAKE MISTAKES! You do not want garbage in. Allways check user input to fit whatver is needed - usually integers or literals from ASCII code; e.g. get the length of user input string and check each element (from 0 to N, use a counter) to be either a digit or a letter. For ex, if you ask for SSN make sure it has exacly 9 digits and that each is 0 to 9.
4. Give user precise instructions as much as you can on what and how to input data; e.g. "Enter your birthdate - mm/dd/yyy: "

In your particular case, you may notice, for example, that since 99 is max (I assume it's pennies), 4 quarters will go beyond that; either hint the user "Quarts - up to 3" or go straight to "Error!" if Quarts_input >3... or loop N times asking user to enter a digit between 0 and 3.
Rampage 3 Formula
(23 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 950 Asus Rampage lll Formula EVGA GTX980Ti ACX2.0 12 GB G-Skill 
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 x 3 RAID 0 Seagate ST31000524NS x 2 Intel 520 Memorex 
CoolingOSMonitorMonitor
Corsair H70 Win7U 64-bit Planar PX2611W Planar PX2611W 
KeyboardPowerCaseMouse
Alps Wave Seasonic Platinum 1000W CM Cosmos S Logitech M510 
AudioAudioAudio
SoundBblaster X-FI Titanium HD Sennheiser PC350 Swan M-200 
  hide details  
Reply
Rampage 3 Formula
(23 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 950 Asus Rampage lll Formula EVGA GTX980Ti ACX2.0 12 GB G-Skill 
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 x 3 RAID 0 Seagate ST31000524NS x 2 Intel 520 Memorex 
CoolingOSMonitorMonitor
Corsair H70 Win7U 64-bit Planar PX2611W Planar PX2611W 
KeyboardPowerCaseMouse
Alps Wave Seasonic Platinum 1000W CM Cosmos S Logitech M510 
AudioAudioAudio
SoundBblaster X-FI Titanium HD Sennheiser PC350 Swan M-200 
  hide details  
Reply
post #15 of 17
Thread Starter 
What is this error?
Code:
import java.util.Scanner;

public class CountingChange {
        
        public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                int pennyCount, nickelCount, dimeCount, quarterCount,
                    penny, nickel, dime, quarter, value;
                
                System.out.print("Enter an integer between 0 and 99: ");
                value = input.nextInt();
                
                if (value > 99 || < 0) { // Error the operator || is undefined etc....??
                        System.out.println("Error, invalid integer");
                }
                        
                // Receive input from static method
                pennyCount   = getInput("Pennies ");
                nickelCount  = getInput("Nickels ");
                dimeCount    = getInput("Dimes   ");
                quarterCount = getInput("Quarters");
                penny        = 1 * pennyCount;
                nickel       = 5 * nickelCount;
                dime         = 10 * dimeCount;
                quarter      = 25 * quarterCount;
                
                
                if (penny + nickel + dime + quarter == value) {
                        System.out.print("Yes");
                }
                else {
                        System.out.print("No");
                }
                
        }
        
        public static int getInput(String coins) {
                Scanner input = new Scanner(System.in);
                
                System.out.print("Enter number of " + coins + ": ");
                return input.nextInt();
        }

}
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 #16 of 17
You should try and put the error in you descriptive text. Not as a comment in the code. Actually you should do both.
You have if (value > 99 || < 0)
it should be if (value > 99 || value < 0)
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
post #17 of 17
System.out.print("Enter an integer between 0 and 99: ");
value = input.nextInt();

if (value > 99 || < 0) { // Error the operator || is undefined etc....??
System.out.println("Error, invalid integer");
}

**************
In case of user error, you need to go back to "Enter an integer...."
Rampage 3 Formula
(23 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 950 Asus Rampage lll Formula EVGA GTX980Ti ACX2.0 12 GB G-Skill 
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 x 3 RAID 0 Seagate ST31000524NS x 2 Intel 520 Memorex 
CoolingOSMonitorMonitor
Corsair H70 Win7U 64-bit Planar PX2611W Planar PX2611W 
KeyboardPowerCaseMouse
Alps Wave Seasonic Platinum 1000W CM Cosmos S Logitech M510 
AudioAudioAudio
SoundBblaster X-FI Titanium HD Sennheiser PC350 Swan M-200 
  hide details  
Reply
Rampage 3 Formula
(23 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 950 Asus Rampage lll Formula EVGA GTX980Ti ACX2.0 12 GB G-Skill 
Hard DriveHard DriveHard DriveOptical Drive
Intel 320 x 3 RAID 0 Seagate ST31000524NS x 2 Intel 520 Memorex 
CoolingOSMonitorMonitor
Corsair H70 Win7U 64-bit Planar PX2611W Planar PX2611W 
KeyboardPowerCaseMouse
Alps Wave Seasonic Platinum 1000W CM Cosmos S Logitech M510 
AudioAudioAudio
SoundBblaster X-FI Titanium HD Sennheiser PC350 Swan M-200 
  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 › Java Static Method - Did I do this right?