समस्या
हमें एक जावास्क्रिप्ट क्लास, Projectile लिखनी है, जिसे प्रारंभ करते समय 3 आर्गुमेंट लेने हैं:
- प्रारंभिक ऊंचाई (0 ≤ h0 < 200)
- प्रारंभिक वेग (0 < v0 < 200)
- प्रोजेक्टाइल के छोड़े जाने पर कोण (0° < a < 90°, डिग्री में मापा गया)।
हमें Projectile क्लास के लिए निम्न विधि लिखनी है:
- एक horiz विधि, जो एक आर्गुमेंट t भी लेती है और क्षैतिज दूरी की गणना करती है जो प्रोजेक्टाइल ने तय की है। [double लेता है, double लौटाता है]
उदाहरण
इस क्लास का कोड होगा:
class Projectile{ constructor(h, u, ang){ this.h = h; this.u = u; this.ang = ang; }; }; Projectile.prototype.horiz = function(t){ const dist = 2 * Math.cos(this.ang) * t; return dist; }; const p = new Projectile(5, 2, 45); const horizontal = p.horiz(.2); console.log(horizontal);आउटपुट
और आउटपुट होगा:
0.2101287955270919