Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Remainder issue

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Remainder issue

    The actual computations aren't working properly when it comes to the remainder. Specifically the variable termLeft. Any suggestions?

    package graduationplanner;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class GraduationPlanner {
     
        public static void main(String[] args) {
           Scanner in = new Scanner(System.in);
           final int MINCU = 12;
           final int TUITIONCOST = 2890; //stores value for tuition cost per term
           final int TERMMONTHS = 6; //stores value for number of months in a term
           int sum = 0;
     
           System.out.println("Welcome to the Western Governor's University Graduation Planner! \nWe will calculate the number of terms you have left until graduation, \nalong with the amount of tuition you have left to pay. \nWe will also provide the number of months until graduation! \nLet's get started by asking a few questions.");
     
           System.out.println("How many CU's are you planning on taking per term?"); //Prompts user for the number of CU's per term
           		int perTerm = in.nextInt(); //storing user input
           		if (perTerm < MINCU){ //verification on positive input by user
           			System.out.println("12 CU's are required per term. Try a higher number!");
           			System.out.println("How many CU's are required for your degree?");
           			perTerm = in.nextInt();} //stores corrected number
           		//System.out.println(perTerm);
     
           ArrayList<Integer> leftCU = new ArrayList<>();{
           	    System.out.println("Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done.");
           	    leftCU.add(in.nextInt());
           	    while (in.hasNextInt()){
           	    	leftCU.add(in.nextInt());}
           	    for(int i=0; i < leftCU.size(); i++){
           	       	sum = sum + leftCU.get(i);}
           	    }
           	    //System.out.println(leftCU);
           	    //System.out.println(sum);
     
           System.out.println("The number of CU's left are: "+ leftCU);
           System.out.println("The sum of the number of CU's left are: " + sum);
     
           int tempLeft = sum/perTerm; //Calculates number of terms left
           int termLeft = 0;
           if ((tempLeft % 4) == 0){
        	   termLeft = tempLeft;}
           else {termLeft = tempLeft + 1;}
           int totalTuition = termLeft * TUITIONCOST; //Calculates the amount of tuition left to pay
           int monthsLeft = termLeft * TERMMONTHS; //Calculates the number of months left
     
           System.out.println("Number of terms left is " + sum + " divided by " + perTerm + " equals " + termLeft);
           System.out.println("Total amount of tuition left is " + termLeft + " times " + TUITIONCOST + " equals " + totalTuition);
           System.out.println("Number of Months left is " + termLeft + " times " + TERMMONTHS + " equals " + monthsLeft);
     
           System.out.println("+----------------------------------------+");
           System.out.println("|Number of Terms left is: " + termLeft + "|");
           System.out.println("|Total amount of Tuition left is: " + totalTuition + "|");
           System.out.println("|Number of Months left is: " + monthsLeft + "|");
           System.out.println("+----------------------------------------+");
           System.out.println("Thank you for using the WGU Graduation Planner");
           }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Remainder issue

    Rather than require us to run your code, determine the inputs etc., and find logic errors, could you PLEASE, PLEASE, PLEEEEEEEZE, provide a sample run, describe what inputs you used, what output is expected, and what output was received?

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Remainder issue

    Quote Originally Posted by GregBrannon View Post
    Rather than require us to run your code, determine the inputs etc., and find logic errors, could you PLEASE, PLEASE, PLEEEEEEEZE, provide a sample run, describe what inputs you used, what output is expected, and what output was received?
    My apologies - here's the output:

    Welcome to the Western Governor's University Graduation Planner!
    We will calculate the number of terms you have left until graduation,
    along with the amount of tuition you have left to pay.
    We will also provide the number of months until graduation!
    Let's get started by asking a few questions.
    How many CU's are you planning on taking per term?
    12
    Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done.
    10
    10
    10
    10
    10
    q
    The number of CU's left are: [10, 10, 10, 10, 10]
    The sum of the number of CU's left are: 50
    Number of terms left is 50 divided by 12 equals 4
    Total amount of tuition left is 4 times 2890 equals 11560
    Number of Months left is 4 times 6 equals 24
    +----------------------------------------+
    |Number of Terms left is: 4|
    |Total amount of Tuition left is: 11560|
    |Number of Months left is: 24|
    +----------------------------------------+
    Thank you for using the WGU Graduation Planner.

    The 4 should actually increase to 5 because of the If, Else statement, but it's not.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Remainder issue

    The 4 should actually increase to 5
    What is the reason that the 4 should be incremented to 5?
    What line of code should do that?
    If the values of the variables don't make the code execute as you want, what values should the variables have?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Remainder issue

    Quote Originally Posted by Norm View Post
    What is the reason that the 4 should be incremented to 5?
    What line of code should do that?
    If the values of the variables don't make the code execute as you want, what values should the variables have?
    Here is the "calculation" portion of the code:

           int tempLeft = sum/perTerm; //Calculates number of terms left
           int termLeft = 0;
           if ((tempLeft % 4) == 0){
        	   termLeft = tempLeft;}
           else {termLeft = tempLeft + 1;}
           int totalTuition = termLeft * TUITIONCOST; //Calculates the amount of tuition left to pay
           int monthsLeft = termLeft * TERMMONTHS; //Calculates the number of months left

    I put in enough for a total of 50 for sum and 12 for term. That should leave a remainder and thus increase the 4 that 50/12 gives to a 5. I know using int as the data type drops the decimal, but the if, else statement is supposed to account for that. What am I missing?

    Thanks for helping!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Remainder issue

    That should leave a remainder
    What variable has that value for a remainder?
    What expression is not computing the value that you want?
    If you can not tell, add a println() statement after each expression that prints out the value computed.

    if, else statement is supposed to account for that.
    How? What value does the variable have when the if statement is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    mrivera85 (July 4th, 2014)

Similar Threads

  1. remainder issue
    By nepperso in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 14th, 2013, 11:58 AM
  2. [SOLVED] Math Remainder Issue
    By bzhagar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 12th, 2012, 07:41 PM
  3. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  4. Remainder Assignment Operator Help
    By jsam0123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2011, 06:04 PM
  5. Chinese Remainder therom
    By Joy123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 16th, 2011, 07:11 AM