Making the result of a sum round to the nearest whole number in Java

rowanB

Junior Member
Nov 4, 2004
14
0
0
This just rounds down for me rather than to the nearest whole number. I think this is because I'm using integer values rather than doubles, but when I changed int to double I got some null exception error when I ran the program. What am I doing wrong?

public class Module
{
private String moduleCode;
private String moduleTitle;
private int semester1;
private int semester2;
private int courseworkWeighting;

public Module(String code, String title, int sem1, int sem2, int courseworkWeight)
{
moduleCode = code;
moduleTitle = title;
semester1 = sem1;
semester2 = sem2;
courseworkWeighting = courseworkWeight;
}

public int getcourseworkWeighting()

{
return courseworkWeighting;

}

}



public class ModuleRecord
{
private Module mod;
private int examMark;
private int courseMark;
private int modResult;

public ModuleRecord(Module m, int eMark, int cMark)
{
mod = m;
examMark = eMark;
courseMark = cMark;

}

public int moduleMark()
{
int courseworkWeighting = mod.getcourseworkWeighting();
return java.lang.Math.round(((courseMark * courseworkWeighting) + (examMark * (100 - courseworkWeighting)))/100);
}

}
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
An int / int = an int.
Just like an int - int = int, int + int = int, int * int = int.

ints have no fractional components by definition, so 2 / 3 = 0, 5/10 = 0, 999999 / 10000000 = 0
5 / 2 = 2, 100 / 3 = 33, 5 / 10 = 0, etc.

This means if you want a decimal result, you can't divide an int by an int, instead you need a double / int, an int / double, or double / double.

So 5.0 / 10 = 0.5, instead of 5 / 10 = 0.

(note a float also works wherever a double works)
 

rowanB

Junior Member
Nov 4, 2004
14
0
0
sorry, i've seen other people ask questions here, I assumed that's what the forum was for.
 

rowanB

Junior Member
Nov 4, 2004
14
0
0
Originally posted by: Kilrsat
An int / int = an int.
Just like an int - int = int, int + int = int, int * int = int.

ints have no fractional components by definition, so 2 / 3 = 0, 5/10 = 0, 999999 / 10000000 = 0
5 / 2 = 2, 100 / 3 = 33, 5 / 10 = 0, etc.

This means if you want a decimal result, you can't divide an int by an int, instead you need a double / int, an int / double, or double / double.

So 5.0 / 10 = 0.5, instead of 5 / 10 = 0.

(note a float also works wherever a double works)

Thanks a lot.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Zugzwang152
WE DO NOT WANT TO DO YOUR HOMEWORK FOR YOU TROLL.

Take it easy, ass. He's obviously done his work and couldn't figure something out about how the language worked. He's perfectly justified in asking why he can't round up. If we told everyone who was working on programming for school to ah heck off we'd loose half the programming traffic in this forum and we'd never get it split into it's own forum.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: rowanB
Originally posted by: notfred
Add 0.5, round down. It's not that fvcking hard.

I want the program to do it.

Don't expect people to do your homework for you, asking a legitimate question like you did.

How do I round to the nearest whole number is perfectly acceptable. But when you ask for somebody to write your own program for you, you will get answers and responses like the above.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
Originally posted by: kamper
Originally posted by: Zugzwang152
WE DO NOT WANT TO DO YOUR HOMEWORK FOR YOU TROLL.

Take it easy, ass. He's obviously done his work and couldn't figure something out about how the language worked. He's perfectly justified in asking why he can't round up. If we told everyone who was working on programming for school to ah heck off we'd loose half the programming traffic in this forum and we'd never get it split into it's own forum.

normally i don't mind, but he has posted 3 different threads asking questions on obviously the same assignment. this forum IS for programming help, and asking a single question for homework is usually okay, but this troll is asking us to do it all for him basically. why doesnt he just copy-paste the instructions and then let the push-over ATSFrs do it for him?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Since it's all for the same assignment, just keep it all in one thread. No need for a billion threads about basically the same thing. Then people won't call you a troll. ;)
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
He hasn't even mentioned that this is a school assignment. Given some of the relatively advanced object oriented techniques he's using in comparison with the fairly noob questions he's asking I'd bet this isn't for a beginners programming course.

rowanB, could you tell us what this code is for?
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
Originally posted by: BingBongWongFooey
Since it's all for the same assignment, just keep it all in one thread. No need for a billion threads about basically the same thing. Then people won't call you a troll. ;)

i'd still call him a troll, but i would offer some useful help rather than just crapping all over it like i'm doing now. ;)
 

rowanB

Junior Member
Nov 4, 2004
14
0
0
Originally posted by: MCrusty
Originally posted by: rowanB
Originally posted by: notfred
Add 0.5, round down. It's not that fvcking hard.

I want the program to do it.

Don't expect people to do your homework for you, asking a legitimate question like you did.

How do I round to the nearest whole number is perfectly acceptable. But when you ask for somebody to write your own program for you, you will get answers and responses like the above.

When have I asked someone to write the program for me? His answer seemed to be taking the piss. I realise that mathematically, adding 0.5 and rounding down is rounding to the nearest number, but I'm interested in how this can be done using Java, hence my reply 'I want the program to do it'. I'm not asking anyone to write the whole program out for me, I was hoping that it wouldn't require such a lengthy answer.
 

rowanB

Junior Member
Nov 4, 2004
14
0
0
Originally posted by: Zugzwang152
Originally posted by: kamper
Originally posted by: Zugzwang152
WE DO NOT WANT TO DO YOUR HOMEWORK FOR YOU TROLL.

Take it easy, ass. He's obviously done his work and couldn't figure something out about how the language worked. He's perfectly justified in asking why he can't round up. If we told everyone who was working on programming for school to ah heck off we'd loose half the programming traffic in this forum and we'd never get it split into it's own forum.

normally i don't mind, but he has posted 3 different threads asking questions on obviously the same assignment. this forum IS for programming help, and asking a single question for homework is usually okay, but this troll is asking us to do it all for him basically. why doesnt he just copy-paste the instructions and then let the push-over ATSFrs do it for him?

OK, I should keep these questions to the same thread. They are separate issues, and even if they are for the same assignment they cover different areas, but if this forum can't afford the bandwidth, then I will keep questions to the same thread. I'm not asking anyone to write the code for me, I just want to be put in the right direction. I'm not certainly not asking anyone to do my whole assignment for me. It's not a small project, and I just need a bit of extra teaching in the areas I'm stuck with. Sometimes this is what you need with computing science. The best way of learning the subject is through working with other people.
 

rowanB

Junior Member
Nov 4, 2004
14
0
0
Originally posted by: BingBongWongFooey
Since it's all for the same assignment, just keep it all in one thread. No need for a billion threads about basically the same thing. Then people won't call you a troll. ;)

I'll keep it to the same thread if that's what everyone wants, but they are separate topics whether for the same assignment or not. One's on arrays, one's on rounding to the nearest whole number, and another's on using a strings from one class in another.
 

VenomXTF

Senior member
May 3, 2004
341
15
81
Is it really that hard to figure out how to add .5 and round down?

Double d = 1.6;
d + .5; (would equal 2.1)
int i = d; (guess what i equals.... that's right 2 and you just rounded 1.6 to 2)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: VenomXTF
Is it really that hard to figure out how to add .5 and round down?

Double d = 1.6;
d + .5; (would equal 2.1)
int i = d; (guess what i equals.... that's right 2 and you just rounded 1.6 to 2)

I don't know but I had never realized it until someone mentioned it to me. Then again, I had never really tried to come up with a way to do it.
 

VenomXTF

Senior member
May 3, 2004
341
15
81
I never really thought about it either until he asked. I'm pretty sure there are math classes in Java that do rounding for you already.
 

VenomXTF

Senior member
May 3, 2004
341
15
81
I never really thought about it either until he asked. I'm pretty sure there are math classes in Java that do rounding for you already.