Wed_15_Jan_Assigment

Run Settings
LanguageJavaScript
Language Version
Run Command
// Question 1 // Create a BankAccount class with the following : // - A constructor that accept account holder's name, account number and balance // - A deposit method that increases the balance // - A withdraw method that decreases the balance, but only if there are sufficient funds // - A getBalance method that returns the current balance console.log("Happy Coding🎇\n"); class BankAccount{ constructor(accountName, accountNumber, balance){ this.accountName = accountName; this.accountNumber = accountNumber; this.balance = balance; } deposit(amount){ if(amount>0){ this.balance += amount; const maskedAccount = this.accountNumber.slice(-3).padStart(this.accountNumber.length, 'X'); console.log(`Kiasi cha TZS ${amount} kimewekwa kikamlifu kwenye akaunti yako ${maskedAccount}. \nSalio lako jipya ni ${this.balance} \n`); }else{ console.log("You can not deposit negative amount\n"); } } withdraw(amount){ if(amount>0){ if(amount<this.balance){ this.balance -= amount; const maskedAccount = this.accountNumber.slice(-3).padStart(this.accountNumber.length, 'X'); console.log(`Umetoa kikamilifu TZS ${amount} kwenye Akaunti yako ${maskedAccount}.\n Salio lako jipya ni TZS ${this.balance} \n`); }else{ console.log("Insufficient balance to withdraw\n") } }else{ console.log("You can not deposit negative amount\n"); } } getBalance(){ return this.balance; } } const bry = new BankAccount("Bryson Mmari", "45234565798", 10000) bry.deposit(5000) bry.withdraw(3000) console.log(`Salio la Akaunti yako ni TZS ${bry.getBalance()}\n`); // Question 2 // Create a Person class with properties for name and age, and a method greet() that prints a greeting. // Then create a Student class that extends Person, adding a course property and a method study() that // prints what the student is studying. class Person { constructor(name, age){ this.name = name; this.age = age; } greet(){ console.log(`Hi!, I am ${this.age} years old, and My name is ${this.name}\n`); } } class Student extends Person{ constructor(name, age, course){ super(name, age); this.course = course; } study(){ console.log (`A ${this.course} student.\n`); } } const bryson = new Student ('Bryson Mmari', '24', 'Computer Ethics'); bryson.greet(); bryson.study(); // #Question3 // Create a Car class with: // - A private property _speed. // - Methods accelerate and brake to increase or decrease speed. // - A getSpeed method to retrieve the current speed. // - Ensure the speed doesn’t go below 0 or above 200. class Car { #speed = 0; accelarate(){ if(this.#speed<200){ this.#speed+=20; }else{ console.log("You exceed the speed limit of 200Km/h\n"); } } brake(){ if(this.#speed>0){ this.#speed-=20; }else{ console.log("You can not drive under minimum speed limit of 0Km/h\n"); } } getSpeed(){ return this.#speed; } } const ist = new Car(); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h`); ist.accelarate(); console.log(`Your current speed is ${ist.getSpeed()}Km/h\n`); ist.accelarate(); ist.brake(); console.log(`\nYou reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h`); ist.brake(); console.log(`You reduce speed to ${ist.getSpeed()}Km/h\n`); ist.brake(); console.log("Happy Ending, Deal Done😀😀\n");
Editor Settings
Theme
Key bindings
Full width
Lines