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

Thread: what is the way to Calculate

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is the way to Calculate

    public class NewClass4 {
    public static void main(String[] args) {
    int a = 1;
    int b = 2;
    int c = 3;
    a += 5;
    b *= 4;
    c += a * b;
    c %= 6;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
    Last edited by ohad; April 3rd, 2013 at 09:47 AM. Reason: What is the way to Calculate (C)how the result are 3?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is the way to Calculate

    Do you have any specific questions about the code?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: what is the way to Calculate

    How does c = 3?

    For clarity, rewrite the code without using shorthand (augmented operators).
    		int a = 1;
    		int b = 2;
    		int c = 3;
    		a = a + 5;
    		b = b * 4;
    		c = c + a * b;
    		c = c % 6;
    		System.out.println("a = " + a);
    		System.out.println("b = " + b);
    		System.out.println("c = " + c);
    Then trace values of all the variables on some paper.

    When you get to c = c + a * b; note that multiplication, division, and remainder operators are applied first.
    This means a*b is evaluated first and then added to c.

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the way to Calculate

    ok but how i do it a*b is 2 and then how do i do c=c&6

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: what is the way to Calculate

    The % sign means divide the two numbers and give us only the remainder.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the way to Calculate

    so what the result of c = c + a * b;

    --- Update ---

    what the result of c = c + a * b;

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is the way to Calculate

    Do you have a PC and a java compiler? Write a small program with that statement in it, compile it and execute it.
    Make sure the values of the variables are not the same and are greater than 1.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: what is the way to Calculate

    hi

    well if we trace the code then
    a = 1+5 =6;
    b = 2*4 = 8;
    c = 3 + 48 =51;
    c = 51%6 = 3 (since dividing 51 by 6 gives remainder 3)

Similar Threads

  1. Calculate method problem.....
    By DavidXCode in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2012, 10:04 PM
  2. How to calculate GPA
    By leonne in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2012, 02:37 PM
  3. Need some ideas on how to calculate this
    By derekxec in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 06:50 PM
  4. Cannot calculate. Please help me....
    By safarina02 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2011, 01:11 PM
  5. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2009, 11:41 PM