Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Java zadatak odrađen u php-u

[es] :: PHP :: Java zadatak odrađen u php-u

[ Pregleda: 3278 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

nemchus
Niš

Član broj: 325635
Poruke: 19
*.exe-net.net.



+2 Profil

icon Java zadatak odrađen u php-u16.02.2015. u 19:37 - pre 110 meseci
Je l ima neko ideju kako se navedeni zadatak za javu može odraditi u php-u?



------Define a class named TimeSpan. A TimeSpan object stores a span of time in hours and minutes (for example, the time span between 8:00am and 10:30am is 2 hours, 30 minutes). Each TimeSpan object should have the following public methods:

TimeSpan(hours, minutes)
Constructs a TimeSpan object storing the given time span of minutes and seconds.
getHours()
Returns the number of hours in this time span.
getMinutes()
Returns the number of minutes in this time span, between 0 and 59.
add(hours, minutes)
Adds the given amount of time to the span. For example, (2 hours, 15 min) + (1 hour, 45 min) = (4 hours). Assume that the parameters are valid: the hours are non-negative, and the minutes are between 0 and 59.
add(timespan)
Adds the given amount of time (stored as a time span) to the current time span.
getTotalHours()
Returns the total time in this time span as the real number of hours, such as 9.75 for (9 hours, 45 min).
toString()
Returns a string representation of the time span of hours and minutes, such as "28h46m".--------

Znam da je rešenje u javi otprilike ovakvo:
Code:
public class TimeSpan {
 private int hours;
 private int minutes;
 // Constructs a time span representing the given number of hours and minutes.
 public TimeSpan(int initialHours, int initialMinutes) {
 hours = 0;
 minutes = 0;
 add(initialHours, initialMinutes);
 }
 // Adds the given hours/minutes to this time span, wrapping hours if necessary.
 public void add(int initialHours, int initialMinutes) {
 hours += initialHours;
 minutes += initialMinutes;
 if (minutes >= 60) {
 minutes -= 60; // convert 60 min --> 1 hour
 hours++;
 }
 }
 // Adds the given hours/minutes to this time span, wrapping hours if necessary.
 public void add(TimeSpan span) {
 add(span.hours, span.minutes);
 }
 // Returns the total hours represented by this time span,
 // such as 7.75 for 7 hours, 45 minutes.
 public double getTotalHours() {
 return hours + minutes / 60.0;
 }
 // Returns a text representation of this time span, such as "7h45m".
 public String toString() {
 return hours + "h" + minutes + "m";
 }


"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

Nemanja Avramović
Engineering Manager
MENU Technologies
Beograd, Srbija

Moderator
Član broj: 32202
Poruke: 4391
*.dynamic.isp.telekom.rs.

Sajt: https://avramovic.info


+46 Profil

icon Re: Java zadatak odrađen u php-u17.02.2015. u 11:32 - pre 110 meseci
Imam ja ideju. Kol'ko platiš?

Šalu na stranu, ovaj forum NIJE mesto gde će neko da odradi zadatak umesto tebe. Ako hoćeš da neko radi nešto umesto tebe onda lepo plati tom nekome da ti odradi posao i miran si.

A prepisivanje jedne ovakve Java klase u PHP ne bi trebalo da traje duže od 10 minuta. Evo ja sam počeo, a ti završi.

Code:
public class TimeSpan {
 private $hours;
 private $minutes;
 // Constructs a time span representing the given number of hours and minutes.
 public function __construct($initialHours, $initialMinutes) {
 $this->hours = 0;
 $this->minutes = 0;
 $this->add($initialHours, $initialMinutes);
 }
 // Adds the given hours/minutes to this time span, wrapping hours if necessary.
 public function add($initialHours, $initialMinutes) {
 $this->hours += $initialHours;
 $this->minutes += $initialMinutes;

//ovaj deo koda ne radi kako treba (ni u javi ni u phpu), ako neko prosledi 150 minuta ono će 
//i dalje dodati samo jedan sat i oduzeti 60 minuta umesto da doda 2 sata i oduzme 120 minuta

 if ($this->minutes >= 60) { 
 $this->minutes -= 60; // convert 60 min --> 1 hour
 $this->hours++;
 }
 }
 //... 

Laravel Srbija.

[NE PRUŽAM PODRŠKU ZA PHP PREKO PRIVATNIH PORUKA!]
 
Odgovor na temu

nemchus
Niš

Član broj: 325635
Poruke: 19
*.exe-net.net.



+2 Profil

icon Re: Java zadatak odrađen u php-u17.02.2015. u 11:57 - pre 110 meseci
Slažem se da ovo ne treba da bude mesto gde će neko nekome da odradi posao za dzabe. Mene je u ovom konkretnom slučaju zanima logika koja se krije iza ovog zadatka ;) Realno, neću se ja hleba najesti što će neko ovo da reši umesto mene pošto sam početnik koji vežba ;) U svakom slučaju, hvala ti puno ;) Imaću na umu da ne postavljam ovako koncipirana pitanja.
"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

nemchus
Niš

Član broj: 325635
Poruke: 19
*.exe-net.net.



+2 Profil

icon Re: Java zadatak odrađen u php-u17.02.2015. u 13:19 - pre 110 meseci
Mene je malo zbunio i ovaj aspekt zadatka -
add(timespan)
Adds the given amount of time (stored as a time span) to the current time span.
getTotalHours()
Returns the total time in this time span as the real number of hours, such as 9.75 for (9 hours, 45 min).

Meni završava posao kada odradim samo ovu funkciju:
Code:
 public function toString () {
     echo $this->hours."h";
     echo $this->minutes."m";
 }

ako otprilike ovako ide ubacivanje podataka i pozivanje na funkcije:
Code:

echo "Enter Hours:";
$h=trim(fgets(STDIN)); 
echo "Enter Minutes:";
$m=trim(fgets(STDIN)); 
$obj = new TimeSpan($h,$m);
echo "Enter Hours to add:";
$h=trim(fgets(STDIN)); 
echo "Enter Minutes to add:";
$m=trim(fgets(STDIN)); 
$obj->add($h,$m);
echo $obj->toString();
  exit;

"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

Nemanja Avramović
Engineering Manager
MENU Technologies
Beograd, Srbija

Moderator
Član broj: 32202
Poruke: 4391
*.dynamic.isp.telekom.rs.

Sajt: https://avramovic.info


+46 Profil

icon Re: Java zadatak odrađen u php-u17.02.2015. u 18:01 - pre 110 meseci
Code:
 public function add($span) {
 $this->add($span->hours, $span->minutes);
 }


Naravno, u nekom ozbiljnijem poslu bi proverio da li je $span instanca klase "TimeSpan", ali radiće i ovako (sve dok prosleđuješ objekat klase TimeSpan).

Što se koda za testiranje tiče, ako ćeš preko konzole da pokrećeš to će da radi. U suprotnom (ako ideš preko browsera) moraš da imaš formular i da kupiš vrednosti iz $_POST.

btw, toString() definiši kao public function __toString() i onda možeš da radiš samo echo $obj; (da bi dobio isti efekat kao u Javi, da možeš da "ispisuješ objekat").
Laravel Srbija.

[NE PRUŽAM PODRŠKU ZA PHP PREKO PRIVATNIH PORUKA!]
 
Odgovor na temu

[es] :: PHP :: Java zadatak odrađen u php-u

[ Pregleda: 3278 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.