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

Java homework help!

Hi Everyone,

I posted something similar to this about a month back when I didn't know much about java. I still don't know much, but after reading some recommended books from Treehouse members and going through some of the basic java courses from Treehouse, I've gotten a better feel for it.

With that said, I have gotten through some of my homework. I was able to get through about 3 segments of code on my own, but I still have two left to get through, but I'm stuck.

This is what I have so far:

package coins;

import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Coins {

    public static void main(String[] args) {
        int nickel, dime, quarter;
        nickel = 5;
        dime = 10;
        quarter = 25;

        Scanner scan = new Scanner(System.in);

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

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

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


        /* 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

    }

}

I need help with calculating the int that's been input by the user with the int's nickel, dime, and quarter that I've specified.

If anyone can help me step by step through only the next code ( /* Enter formula(s) to calculate total dollar amount of coins and assign the calculated value to a variable identifier of type double */), that would be awesome.

Thanks in advance!

Also, can anyone check to see if I wrote my existing code correctly?

4 Answers

I'd love to help, but you haven't given me much to help on. Try to work out a bit of it, then I can help you with what it is you're having trouble with, and what it is you simply don't get at all. Working from just the instructions, I'd feel like I'm just doing your homework.

Hey Ricardo,

I posted something similar to this about a month back when I didn't know much about java. I still don't know much, but after reading some recommended books from Treehouse members and going through some of the basic java courses from Treehouse, I've gotten a better feel for it.

With that said, I have gotten through some of my homework. I was able to get through about 3 segments of code on my own, but I still have two left to get through, but I'm stuck.

This is what I have so far:

package coins;

import java.text.NumberFormat; import java.text.DecimalFormat; import java.util.Scanner;

public class Coins {

public static void main(String[] args) {
    int nickel, dime, quarter;
    nickel = 5;
    dime = 10;
    quarter = 25;

    Scanner scan = new Scanner(System.in);

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

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

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


    /* 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

}

}

I need help with calculating the int that's been input by the user with the int's nickel, dime, and quarter that I've specified.

If you can help me step by step through only the next code ( /* Enter formula(s) to calculate total dollar amount of coins and assign the calculated value to a variable identifier of type double */), that would be awesome. I'm having trouble finding the formulas to do this.

Thanks in advance!

Also, can you check to see if I wrote my existing code correctly?

I came up with this for the formula (calculating user input int to the int's that I've specified), can you let me know if this is correct?

System.out.print("You have " + ((nickel*ni) + (dime*di) + (quarter*qu)) + " cents");

It's probably wrong, but I'm hoping I'm on the right trail?

Thank you! I was looking at the examples in the book and I was getting stumped. You writing out the first line really helped me see what I needed to do.

I'll be sure to email or post back on this thread if I have any more questions.

I second Ricardo's response. It's just the template we see and no input from your end.

What I suggest is that try and complete each section //Your code here one by one. The template is very self explanatory in itself. If you get stuck just message here or send me a direct email and i'd be happy to help.

Well to get you started here is your first part

package coins;

     /* Import the Scanner class and DecimalFormat class */

import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Coins{
      public static void main( String [] args ){
         // rest of your code
     }
}

@ Gunjeet Hattar, can I ask why you added NumberFormat? I thought it was only asking for decimal format and scanner. Also, why did you use java.text for decimal format, but java.util for scanner? Also, what exactly is Scanner?

For the declaring the nickels, dimes, and quarters, I wrote this out. Is it correct? It seems super easy, no brain power wasted to you, but I'm literally just starting out, so please bear with me.

public class Coins { public static void main(String[] args) {

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

        int nickel = .05;
        int dime = .1;
        int quarter = .25;

Thanks in advance, this is really a lot of help. I'm so happy to find people that are actually willing to help.

So far this is what I have. I'm having trouble with the third line of code here.

public class Coins { public static void main(String[] args) {

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

        int nickel = .05;
        int dime = .1;
        int quarter = .25;

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

        Scanner scan = new Scanner(System.in);

    /* 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) */

        System.out.print("")
        String nickel = scan.nextLine();
        System.out.println("You entered [" + message + "]");

What do I add after System.out.print, and system.out.printIn? I know that System.out.print does, but I just don't know how and where to input the values and whatnot.

Hello!

Well you can remove the decimal format if you want. It will be fine. No specific reason, just that decimal format is a specialised form of Number format.

java libraries work in packages, and thus it is required to call the top level package first i.e. java , then next sub package i.e. text and finally the class itself i.e. ** Decimal Format**
Similarly for Scanner. All classes are stored in different packages to avoid clashes in namespace that is why we use java.util and java.text respectively to call the classes within these packages. Read more about packages here

The Scanner class is a class in java.util, which allows the user to read values of various types. So it basically accepts input from sources like keyboard etc.

Coming back to your implementation, is that a decimal point in from the int numbers? If yes then this in incorrect, integer numbers do not store decimal values. We have float for that.

I'd suggest reading some more on primitive data types. It will help you understand better

Print is used to display things on the console. So it will be pretty much used for

1) adding information to user to input text
2) Display the result

Thank you very much for your help. I'm going to go over some of the basic java stuff with my professor in a few days (it's an online class, so I rarely see him, only at office hours).

So when I put int nickel= .05, should I have written float nickel = .05 instead?

I saw online, as you explained, that float is used when a value has a decimal, such as a GPA, or in my case .05 (5 cents). My book did not explain that to me. I actually had to look it up.

I can't wait until Treehouse comes out with the java course.

You are welcome. Yes, it will be helpful for you to go through the basics.

Let me clear this point for you.
So in your assignment is says ** Declare three variable identifiers of type int to represent ... **

Pay attention to the word int short for integer. Integer numbers are whole numbers and represent both positive and negative numbers. If you come to think about it when we talk of currency we usually speak in whole numbers like 30 dollars and 10 cents and never 0.10 cents 2.75 dollars.

So what you should have written instead is int nickle = 5; without the decimal number.

If there was a decimal number then yes it would have been float nickle = 0.05f; where f is used to represent a float value

The java courses are due in November, you still have enough time to brush up on some basic java skills yourself. Will be helpful.