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: Newbie, Computing Student Help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie, Computing Student Help

    Hi, im a first year computing student after having basically done about 2 years of learning databases, web development and networking i have now started to learn Java. However its just plain difficult for me as i have trouble understanding what everything means and such.

    Anyway heres my current assignment:

    class Main
    {
    public static void main(String args[])
    {
    int exam;
    int coursework;
    double result;

    exam = 71;
    coursework = 40;

    result = (exam + coursework) / 2;

    System.out.print( "EX = ");
    System.out.print( exam );
    System.out.print( " CW = ");
    System.out.print( coursework );
    System.out.print( " Mark = ");
    System.out.println( result );
    }
    }

    Which prints : EX = 71 CW = 40 Mark = 55.0

    However i want it to print: EX = 71 CW = 40 Mark = 55.5

    I know its basic but i do need the help and anyone with links or tips on learning java/programming overall would be greatly appreciated


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Newbie, Computing Student Help

    Hey BITmixit, welcome to the JavaProgrammingForums.
    In the future could you please surround your code with tags such as the ones in my signature, as It makes it easier for everyone to read.

    The problem that you're facing here is that the computation done on (71+40)/2 is done under the assumption of integers, and only then assigned to a double variable;
    So, (71+40)/2 as an int = 55, then storing 55 in a float/double yields 55.0;

    There are two things you can do, either declare all the other variables as double, or I recommend converting the calculations to a double.
    eg: double answer = (double) //sum here
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Newbie, Computing Student Help

    Awesome that worked, thanks. Will do with the tags.

Similar Threads

  1. Standards, cloud computing
    By Loadeed in forum The Cafe
    Replies: 0
    Last Post: June 23rd, 2011, 02:44 AM
  2. Computing Arrays...
    By sarahmiller1980 in forum Collections and Generics
    Replies: 1
    Last Post: January 31st, 2011, 07:50 AM
  3. Something wrong with computing the average
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 18th, 2010, 07:51 AM
  4. project on mobile computing
    By seejay in forum Java Theory & Questions
    Replies: 2
    Last Post: May 18th, 2010, 02:31 AM
  5. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM