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

# Thread: Newbie, Computing Student Help

1. ## 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. ## 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

3. ## Re: Newbie, Computing Student Help

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