[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Java help needed

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 21
Thread images: 2

File: java.png (39KB, 518x518px) Image search: [Google]
java.png
39KB, 518x518px
Heya /g/! I got a Java exam incoming and i have tried to do the next task for 5 hours now. So: how do i create a subroutine to compare 2 java dates? It needs to return either 1, 0 or -1. I have to us compareTo and both dates have to be parameters.
>>
>>59023527
public function suchCock (date Uno, date Dos) {
if Uno > Dos return 1
else if Dos > Uno return -1
else return 0
}


Usually dates are converted to UNIX time stamps, pretty sure Java would have a date object so look that up. Simple function above, obviously not syntactically correct but you get the jist. Easy as balls.
>>
hmm.. it says that i got to add a static method compareTo(date1,date2) to Dates-class. I have tried like you did, couldn't make it work.
>>
File: 4adfbea4.gif (1MB, 400x225px) Image search: [Google]
4adfbea4.gif
1MB, 400x225px
>>59023527
Literally read the Java API.

https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#compareTo-java.util.Date-
>>
The problem is that in our exercises we don't use Date, we got our own object called Pvm, which is should compare. It's days, months and years are in private ints, they are not taken from java's date.
>>
>>59023791
Can you modify the class that the object "Pvm" belongs to?
>>
>>59024382

yes, i can modify it. Still having the problem.
>>
>>59023791

here's an inelegant brute force attempt (everything you've heard about Java's verbosity is true).

int compareTo(Pvm date1, Pvm date2) {
if(date1.getYear() != date2.getYear()) {
return date1.getYear().compareTo(date2.getYear());
} else if(date1.getMonth() != date2.getMonth()) {
return date1.getMonth().compareTo(date2.getMonth());
} else {
return date1.getDay().compareTo(date2.getDay());
}


I don't remember if an int returned by a method can get autoboxed to Integer, so my calling compareTo directly off the get methods may not work. You may have to slip in a couple casts to Integer to get this code to work.
>>
>>59024922
actually, calling that method compareTo is probably a bad idea--as compareTo will generally be expected to be an instance method that takes one argument (i.e. date1.compareTo(date2)) As written, it'd probably be better to make it a static method named to imply it compares two Pvm's. Something like...

static int comparePvmDates(...) {...}
>>
Yeah we got this kind of code to get the date, month and a year from the object:

public int getPv() {
return pv;
}

now my code looks like this:

public static int comparePvm(Pvm date1, Pvm date2) {
if(date1.getVv() != date2.getVv()) {
return date1.getVv().compareTo(date2.getVv());
} else if(date1.getKk() != date2.getKk()) {
return date1.getKk().compareTo(date2.getKk());
} else {
return date1.getPv().compareTo(date2.getPv());
}

it says "Cannot invoke compareTo(int) on the primitive type int" on all the return statements.
>>
>>59025095
>Cannot invoke compareTo(int) on the primitive type int"

You'll have to cast your int primatives to Integer objects before calling .compareTo on them.
>>
>>59025095
I assume that it's erroring out on:

date1.getVv()
date1.getKk()
date1.getPv()


You either needs to parse them as Integers (not ints) or create Integer objects.

Integer I = new Integer(date1.getVv());
I.compareTo(date2.getVv())


It gets messy.
>>
>>59025095
>Pvm
>pv
>getKk()

I hope you never get a job writing software professionally--I'd hate to get stuck maintaining this inscrutable code.

If your professors said that names for identifiers don't matter, they lied to you. You need to get in the habit now of naming things so it's clear exactly what your code is doing. If you have to write comments to explain what's going on, you've already fucked up.
>>
>>59025187
This anon does have a point. Pvm, pv and Kk are pretty bad variable names. I'm not sure what they refer to.
>>
>>59023527
you make a database call and do and do a select firstdate - secondate
>>
Thanks guys, i managed to do it. It got really messy and awful, but it worked.

>>59025187

These are finnish names for days, months and years. I know what you mean, these came for me in our excersises, I did not come up with them.
>>
If this is all your exam asks for then you need to seriously consider if your class is for retards.
>>
>>59025414

well it's just a small part of it.
>>
>>59025414
It's probably for a very early and introductory class, calm down aspie.
>>
A
Thread
About
Code
And
None
Of
You
Faggots
Use
A
Code
Tag
Kill
Yourself
>>
When I was programming java I spent more time fighting the API then the programming language itself, it's fucking terrible.
Thread posts: 21
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.