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 3 of 3

Thread: Math operations not working..

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Math operations not working..

    Hey, I'm having a bit of trouble with math operators which should work but don't.

    class Assignment10
    {
    	//constructor with 8 parameters to initialize the variables
    	Assignment10(int markQuiz1, int markQuiz2, int markQuiz3, int markQuiz4, int markQuiz5, int markMidterm, int markFinExam, String studentName)
    	{
    		//Calculates the grade percentages and then finds out the final grade for the class.
     
     
    		double markQuizesFinal = (((markQuiz1 + markQuiz2 + markQuiz3 + markQuiz4 + markQuiz5) / 50) * 100);
    		double markFinal_D = 5.7; //Temp testing value, don't pay any attention to this
    		double decimalPlace = (markFinal_D % 1);
     
    		if (decimalPlace >= 0.51) // Determines if the final mark should be rounded up or down and then rounds it up or down to get a more precise mark.
    		{
    			markFinal_D = (markFinal_D - decimalPlace);
    			markFinal_D = (markFinal_D + 1);
    		}
    		else
    			markFinal_D = (markFinal_D - decimalPlace);
     
    		int markFinal_I = 0; //Initilizes markFinal_I and sets it to 0 so the program will compile properly.
    		markFinal_I = (int)markFinal_D; //Casts the double markFinal_D to the interger variable markFinal_I
     
    		//Prints the final grade in both numerical and letter format.
    		System.out.print(""+studentName+"'s final Grade is:");
    		System.out.print(" "+markFinal_I+" ");
    		System.out.print(" "+markQuizesFinal+" ");
     
    		if (markFinal_I >= 90.0) //Determines and sets the letter grade for the student depending on their mark.
    			System.out.print("A");
    		else if (markFinal_I >= 80.0 && markFinal_I < 90.0)
    			System.out.print("B");
    		else if (markFinal_I >= 70.0 && markFinal_I < 80.0)
    			System.out.print("C");
    		else if (markFinal_I >= 60.0 && markFinal_I < 70.0)
    			System.out.print("D");
    		else if (markFinal_I < 60.0)
    			System.out.print("F");
     
    		System.out.println("");
    	}
    }	
     
    	// Test Class
     
    	class Assignment10Tester
    	{
    		public static void main (String[] args)
    		{
    			Assignment10 bob = new Assignment10(3, 4, 6, 2, 7, 70, 54, "bob");
    			Assignment10 celestia = new Assignment10(9, 8, 7, 6, 5, 97, 85, "celestia");
    		}
    	}

    The line that has the 'broken?' math operators in line number 9. I've done the same operation in a calculator and it does work but for some reason it's not working here.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Math operations not working..

    You're using all ints, so all calculations are going to be ints. That means that any decimals are going to be dropped. You might want to use floats or doubles instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Math operations not working..

    Edit: Problem solved, thanks a ton!!!
    Last edited by tyeeeee1; October 13th, 2012 at 12:45 PM.

Similar Threads

  1. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  2. RPN Help Please! (Order of Operations)
    By yuvalb in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 30th, 2011, 10:53 AM
  3. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  4. Help with OS operations!
    By benglish in forum Java Theory & Questions
    Replies: 8
    Last Post: April 1st, 2011, 06:32 AM
  5. Operations with lists
    By datreta in forum Collections and Generics
    Replies: 8
    Last Post: October 29th, 2010, 08:54 AM