Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

I need help with some Java questions

Hi, so my Java "project" (really homework) is listed below, if anyone can give me some pointers, that'd be really helpful. I posted this up a month ago, but I am coming back to it now since I just took the Java Basics course and read some more of the textbook.

Here is the outline

package coins;

/* Import the Scanner class and DecimalFormat class */

 // Your code here

public class Coins{

 public static void main( String [] args ){

 /* Declare three variable identifiers of type int to represent nickels, dimes and quarters */

       // Your code here

 /* Instantiate an object of type Scanner (see example 3.9 in text)*/

      // Your code here

 /* prompt the user for the number of nickels, dimes and quarters and input the values to your declared variable identifiers using the appropriate input method on the scanner class object (see example 3.9 in text) */

      // Your code here

 /* Enter formula(s) to calculate total dollar amount of coins and assign the calculated value to a variable identifier of type double */

      // Your code here

 /* Use the DecimalFormat class to output your result in currency format (see example  3.7 in text) */

      // Your code here
 }

}

And this is what I have:

package coins;

/** *

  • @author LBH */ import java.text.NumberFormat; import java.text.DecimalFormat; import java.util.Scanner;

public class Coins {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    int nickel, dime, quarter;

    Scanner scan = new Scanner(System.in);

    System.out.print("How many nickels?  ");
    nickel = scan.nextInt();

    System.out.print("How many dimes?  ");
    dime = scan.nextInt();

    System.out.print("How many quarters?  ");
    quarter = scan.nextInt();

    double total = ((nickel*.05) + (dime*.10) + (quarter*.25));

    DecimalFormat dollarAmount = new DecimalFormat("$0.00");
    System.out.println("You have " + dollarAmount.format(total));

}

}

IF anyone can check this out and let me know if I followed the steps correctly or not. I tested it and it works fine, but I want to make sure this is okay.

1 Answer

If you are looking for the dollar amount, I would suggest doing almost exactly what you did, except I would change it to the following:

double total = (nickel*.05) + (dime*.1) + (quarter*.25)

Since a quarter is only 25% of a dollar, multiplying the values in the way would give you the proper dollar amount, if I am understanding the question correctly. From there, it would be simply

System.out.println(total);

Or, if you need to output it in currency form, then you can say something like

NumberFormat formatting = NumberFormat.getCurrencyInstance();
System.out.println(formatting.format(total));

Hope this helps! Good luck!

ACtually, it's funny because right before you posted this, I had figured out that I needed to put .05, .10 and .25 instead of 5, 10, and 25.

Thanks for getting back to this.