Results 1 to 10 of 10
  1. #1
    Brillyx is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Question I Need Help With A Java Assignment

    I am in a Computer science class for Java 1. I am still very new to the concept and I am doing my best to learn the material. Although I am stumped on my first homework assignment.

    Keep in mind I'm not asking anyone to do the homework for me, I am simply asking for advice on how to complete it, so please don't abandon the thread if you think I am merely being lazy and asking people to complete my work for me.

    I am getting stuck on trying to string a line that would tell you what the time is. The way my professor said it has to be done is for the line to read, "The time is now" and then the current time.

    I already coded the first line of my homework successfully, through reading on Java's website and a helpful tutorial site on how to code date and time in java, I was able to create a code that prints out the current time.

    So, with that being done, I figured I could just write a simple text line that said "The time is now" and then just reattach the string that would give me the current time. However, I can't seem to fit the pieces together without getting an error, and there must be a simpler way to do it than what I'm trying, since my professor told us that in order to complete the second line, you must use the first String using the substring method.

    So I think I was on the right track, I just don't know how to code it exactly so I can get the right output.

    Can anyone help me, please? I'm an honest noob to Java.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: I Need Help With A Java Assignment

    Hi Brillyx,

    It would be useful to see the code so far and an output of the error you are receiving.

    Regards.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: I Need Help With A Java Assignment

    Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Brillyx is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Re: I Need Help With A Java Assignment

    I figured out how to do the first part, but I am totally lost on the second part. I have no idea where to start with this...





    "Part 2. Write a program called PayBack that calculates the total amount of money required to repay a loan with compound interest. There is a simple formula for this: p(1+r/n)^nt

    • p = the principal amount (the initial loan)

    • r = the annual rate of interest (as a decimal)

    • t = the period of the loan in years

    • n = the number of times the interest is compounded per year

    The required I/O format is shown in the following execution snapshot (user input shown in bold):

    Enter principal:25000
    Enter annual rate of interest:0.035
    Enter period of loan in years:10
    How many times is interest compounded yearly?:12

    Total to be repaid:$35458.62

    As you can see, your program will perform console I/O (do not use dialog boxes). You will need to declare a variable of type double to store the principle and the amount to be repaid. Use int variables for the other values. The output should be formatted exactly as shown. Note especially that the amount owed is displayed with a dollar sign and exactly two digits after the decimal point. When you calculate the result as a double value (using the appropriate static method in the java.lang.Math class), there will be more than two digits after the decimal point. There are two ways you can deal with this:

    1. Use the DecimalFormat class from the Java library. You will have to read the documentation to figure out how to use it.

    2. Convert the double value to a String (using the appropriate static method in the String class). Then use String methods to find the position of the decimal point and extract a substring from the beginning of the string to the second character after the decimal point.

    A solution can be written with 14-18 lines of code."

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: I Need Help With A Java Assignment

    make a list of the steps the program must do and work on them one at a time.

    If you have any specific questions, ask them.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: I Need Help With A Java Assignment

    To get you started, look at how you are going to accept and hold user input. Asking for the input should be easy given the fact you are already outputting the current time.

    The statement below is a little misleading.
    You will need to declare a variable of type double to store the principle and the amount to be repaid. Use int variables for the other values.
    This suggests the annual rate of interest should be an int and the principal is a double. The example though seems to suggest this is the other way around. You might want to double check this.

    Regards.

  7. #7
    Brillyx is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Re: I Need Help With A Java Assignment

    Okay so I coded this program:

    /**
    *
    *Author Graham
    *
    *This program calculates the total amount of money required to repay a loan with compound interest.
    */

    package payback;
    import java.util.Scanner;
    import java.text.DecimalFormat;

    class payback {

    public static void main(String[] args ) {

    double r, answer;
    int t, n, p;

    DecimalFormat format = new DecimalFormat("#.##");

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter principal: ");
    p = scan.nextInt();

    System.out.println("Enter annual rate of interest: ");
    r = scan.nextDouble();

    System.out.println("Enter period of loan in years: ");
    t = scan.nextInt();

    System.out.println("How many times is interest compounded yearly? ");
    n = scan.nextInt();

    answer = Math.pow(1 + (r / n), (n * t)) * p;

    System.out.println("$" + format.format(answer));

    }
    }



    It WORKS on Netbeans! However, in order for this assignment to be complete we need it to work on Command Prompt, and when I run it on Command Prompt, all that happens is I get the error:

    Exception in thread "main" java.lang.NoClassDefFoundError: Payback (wrong name:payback/payback)

    I have no clue what I'm doing wrong. The program works in Netbeans. I have the class specified in my environment variables to my jdk folder. I can clearly see the java and class files in the directory I have the command prompt set to. What gives?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: I Need Help With A Java Assignment

    Exception in thread "main" java.lang.NoClassDefFoundError: Payback (wrong name:payback/payback)
    Several things wrong. Java is case sensitive. The classname in the code is payback, not Payback.
    The class is in a package: payback.
    The full name of the class must be used with the java command: java payback.payback
    The class file must be in the payback folder
    and the java command must be issued in the folder that contains the payback folder
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Brillyx is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Re: I Need Help With A Java Assignment

    Quote Originally Posted by Norm View Post
    Several things wrong. Java is case sensitive. The classname in the code is payback, not Payback.
    The class is in a package: payback.
    The full name of the class must be used with the java command: java payback.payback
    The class file must be in the payback folder
    and the java command must be issued in the folder that contains the payback folder
    Ah, okay yeah I see what went wrong, I got rid of package: payback, and I also ran the program with a lowercase payback. Everything works as I wanted it to, once again Norm you've saved me, thank you!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: I Need Help With A Java Assignment

    Gald you got it to work. Sometime as an exercise add back the package statement and move to the right folder and see if you can get that to work
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Java Assignment
    By lance215 in forum New To Java
    Replies: 1
    Last Post: 06-15-2012, 12:11 AM
  2. java assignment
    By omgonoes in forum New To Java
    Replies: 13
    Last Post: 04-23-2011, 02:29 AM
  3. need help with a java assignment.
    By Z-slasher in forum New To Java
    Replies: 7
    Last Post: 04-17-2011, 11:05 PM
  4. Java assignment.
    By jmsjames in forum New To Java
    Replies: 14
    Last Post: 12-07-2010, 03:18 AM
  5. Help me please (Java assignment)
    By cris_carriaga in forum Java Applets
    Replies: 4
    Last Post: 10-06-2010, 04:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •