PDA

View Full Version : Java wtf moment



Donut
February 25th, 2009, 10:30 PM
the title is probably misleading, but it is java related.
im in computer programming at my school, we are working with java in JCreator. pretty simple stuff so far, declaring and defining variables, doing math, and spitting it all back as a string.... ok, brutally easy.
today in class we went over declaring variables as int, and modulus division. all very easy. i understand all of it, but what i DONT understand is the practical application to it. id show you guys a script, but i didnt have time to save the damn thing to my flashdrive in class today, so the file is sitting in a folder on the desktop at school.
basically, the teacher wants us to prompt the user for a number of seconds (like 284), do some math, then spit out the time in minute:second format. i can do that with basic division, but the teacher wants us to use modulus division and take advantage of the fact that int variables drop the decimal place with division.
my question is why would you use modulus to calculate time like that, and how? ill get the sample script we did in class saved to my flash drive tomorrow and post it

E: oh the assignments are posted on my school's website. here are the specifics:


Create a TimeConversion application that prompts the user for a time in minutes and then displays the time in hours and minutes. Be sure to consider times whether the number of minutes left over is less than 10. For example, 184 minutes in hour:minute format is 3:04 (Hint: use the modulus operator). The application output should look similar to:
Enter the time in minutes: 135 The time is: 2:15
apparently im supposed to calculate burger cost too, but it has nothing to do with the modulus stuff we learned today. goddamn this guy sucks with directions
E2: im not asking anybody to do my work, im just wondering how and why a person would use modulus to do something like this
E3: seeing a pattern here. all of the assignments are taking a number, splitting it into smaller groups (time: 60 seconds = 1 minute, little league baseball teams: 13 kids per team) and calculating the left overs. obviously the colon in the time readout is done using a string in a System.out.print thing.
E4: i totally didnt edit this like 8 times in the past 5 minutes :saddowns:

flibitijibibo
February 26th, 2009, 08:01 AM
Stuff as simple as this probably won't have any practical use. I mean, really, can you think of a decent application for spitting out simple numbers/Strings? Once you get to instances and shit, assignments will probably begin to feel like something useful.

Or, if you're too impatient, I have a few assignments that could keep you busy. With what you've been taught, you could probably do our Madlibs assignment...

Limited
February 26th, 2009, 10:07 AM
I've been taught Java for 2 years and I have no idea what modular ints are, is it when you use %?

flibitijibibo
February 26th, 2009, 01:01 PM
Yeah. It's basically the other side of the int division coin: when you do /, you get the result and it cuts off any remainder; when you do %, you get the remainder and the result is dropped.

Donut
February 26th, 2009, 05:19 PM
ever notice how inexplicable programming issues just come to you in your sleep? i was trying to fall asleep last night, and it suddenly made sense.
this is NOT the script i wanted to post last night. this is one i wrote today

//Exercise 3.1: Time Conversion
import java.util.Scanner;
public class timeconversion
{
public static void main(String[] args)
{
Scanner mario = new Scanner(System.in);
int time, minutes, hours;
System.out.print("Enter the time in minutes: ");
time = mario.nextInt();
hours = time / 60;
minutes = time % 60;
System.out.println("The time is " + hours + ":" + minutes);
}
}in case anyone is wondering, i used mario in the scanner declaration (is that considered a declaration?) to stress the point to my friend that variables can be named anything.
this code here works for what i want it to do. what it does is take the time in minutes, divide by 60, which will give you the amount of hours (remainder is dropped because the variables are int) then takes the modulus of the same input value and uses that for the minutes. it works fine, but i noticed that if the amount of minutes is less than 10, it doesnt put a 0 before the readout. for example, 245 seconds -> 4:5, when what i would like it to say is 4:05. would i achieve this using an if statement to see if the variable minutes is less than 10? or is there a way to define variables that put preceding 0's in the readout?

iv had alot more programming experiencing than what my teacher has taught the class so far, so its not like im lost in the dark. iv worked with unity 3d, game maker, halo script (although can you really count this?), html (see halo), morrowind and oblivion, ut2004, and robot C. maybe more but i cant remember at the moment. what was the assignment you have flibitijibibo?

Limited
February 26th, 2009, 05:35 PM
This may not be the most effecient way, but this is how I would do it Donut.



//Exercise 3.1: Time Conversion
import java.util.Scanner;
public class test
{
public static void main(String[] args)
{
Scanner mario = new Scanner(System.in);
int time, minutes, hours;
String stringMinutes;
System.out.print("Enter the time in minutes: ");
time = mario.nextInt();
hours = time / 60;
minutes = time % 60;
stringMinutes = Integer.toString(minutes);
if (stringMinutes.length() < 2)
{
stringMinutes = "0" + stringMinutes;
}
System.out.println("The time is " + hours + ":" + stringMinutes);
}
}
Bold stuff is the bits I have added/amended.

Donut
February 26th, 2009, 06:15 PM
oh sweet. im basically teaching myself this language because this teacher doesnt go out of hs way to explain much at all. we havent even officially gone over if statements or any kind of loop yet. we havent used the string variables either, although we HAVE covered them.

Integer.toString(minutes);this one part we have not covered in class yet, but i think i understand it. thanks for the help :D

Limited
February 26th, 2009, 06:18 PM
To convert a string to int you use this command.

Integer.parseInt(*string variable name here*);

Phopojijo
February 26th, 2009, 06:30 PM
Simply put... a modulus operator returns the remainder.

In PseudoCode so you could better understand what's going on:

Minutes (Seconds / 60 with the remainder simply dropped... which is what Java does for Integers)
Seconds (Seconds % 60 which is, as stated above... (Seconds / 60)'s remainder)

So 67/60 would be 1... and 67%60 would be 7... 1 minute, 7 seconds.

Donut
February 26th, 2009, 06:39 PM
i knew what the modulus was all along but the math functions you posted were what i was looking for last night. thanks
another question: im back to using doubles for numbers so i can calculate money. how do i limit the variable to storing only 2 decimal places? or round to 2 decimal places?

kenney001
February 26th, 2009, 07:08 PM
just out of curiosity what school do you go to?

Donut
February 26th, 2009, 07:19 PM
would you call me a faggot if i said that i really dont want to disclose that information?
i live in rhode island, so like, how many schools are there? 3?

Phopojijo
February 26th, 2009, 07:20 PM
i knew what the modulus was all along but the math functions you posted were what i was looking for last night. thanks
another question: im back to using doubles for numbers so i can calculate money. how do i limit the variable to storing only 2 decimal places? or round to 2 decimal places?
Easy Truncate: Store it as integers in cents

Not-so-easy Truncate: var = Math.floor(var*100.0)/100.0; //where var is a double.
Round: var = Math.round(var * 100.0)/100.0; //where var is a double.

Basically for the last two... you're using round (rounding) or floor (truncating) acting on the one's decimal place... but you just shift it up 100-fold, then back down 100-fold to act on the 100ths place.

Donut
February 26th, 2009, 07:25 PM
ok cool. that makes sense. thanks again