Professional Documents
Culture Documents
Untitled
Untitled
Create an instance of
this class and print out its properties.
// class Person{
// constructor(name, age){
// this.name = name;
// this.age = age;
// }
// }
// Create a class Rectangle that has properties width and height. Add a method
getArea() that calculates the area of the rectangle. Create an instance of this
class and call the getArea() method.
// class Rectangle{
// constructor(width,height){
// this.wid = width;
// this.hgt = height;
// }
// getValues(){
// console.log("the area of rectangle is : " + this.wid * this.hgt)
// }
// }
//Create a class Animal that has properties name and species. Add a method speak()
that prints out a message saying "The {name} is a {species} and says {sound}".
Create an instance of this class and call the speak() method.
// class Animal{
// constructor(animal_name, animal_species,sound){
// this.name = animal_name;
// this.species = animal_species;
// this.sound = sound
// }
// speak(){
// console.log("The " + this.name + " is a " + this.species + " and it " +
this.sound)
// }
// }
//Create a class Employee that has properties name, position, and salary. Add a
method raiseSalary(percent) that increases the salary by the given percentage.
Create an instance of this class and call the raiseSalary() method with a
percentage of your choice.
// class Employee{
// constructor(name, position, salary){
// this.name = name;
// this.position = position;
// this.salary = salary;
// }
// raiseSalary(percent){
// this.salary = (this.salary * percent/100)
// console.log("The salary is raised by :" + this.salary)
// }
// }
// employee1.raiseSalary(7);
//Create a class Car that has properties make, model, and year. Add a method
getAge() that calculates the age of the car based on the current year. Create an
instance of this class and call the getAge() method.
// class Car {
// constructor(make, modal, year){
// this.company = make;
// this.modal = modal;
// this.year = year
// }
// getAge(){
// const currentYear = new Date().getFullYear();
// const carAge = currentYear - this.year;
// return ` car is ${carAge} year old`
// }
// }
// class Myage {
// constructor(birthYear,name){
// this.age = birthYear;
// this.name = name;
// }
// getAge(){
// const currentYear = new Date().getFullYear();
// return currentYear - this.age;
// // console.log(this.name + " is " + this.age + " year old")
// }
// }
// class BankAccount{
// constructor(bal, interestRate){
// this.balance = bal;
// this.interest = interestRate;
// }
// deposite(amount){
// this.balance = this.balance - (this.balance * amount/100);
// }
// withdraw(amt){
// console.log(this.balance)
// }
// }
//Create a class Book that has properties title, author, and isbn. Add a method
getInfo() that prints out a message saying "The book {title} by {author} has an
ISBN of {isbn}". Create an instance of this class and call the getInfo() method.
// class Book{
// constructor(title, author, price){
// this.title = title;
// this.author = author;
// this.price = price;
// }
// getInfo(){
// console.log("The Book " + this.title + " by " + this.author + " has
price over " + "$" + this.price)
// }
// }
//Create a class Shape that has properties color and type. Add a method draw() that
prints out a message saying "Drawing a {color} {type}". Create a subclass Circle
that has a property radius. Override the draw() method in the Circle class to print
out a message saying "Drawing a {color} circle with a radius of {radius}". Create
an instance of the Circle class and call the draw() method.
// class Shape{
// constructor(color, type){
// this.color = color;
// this.type = type;
// }
// draw(){
// console.log("Drawing a " + this.color + " " + this.type)
// }
// }
// }
//Create a class Player that has properties name, score, and level. Add a method
increaseScore(points) that increases the score by the given number of points. Add a
method increaseLevel() that increases the level by 1. Create an instance of this
class and call the increaseScore() and increaseLevel() methods with values of your
choice.
// class Player{
// constructor(name, score, level){
// this.name = name;
// this.score = score;
// this.level = level;
// }
// increseScore(points){
// this.score += points;
// }
// increaseLevel(){
// this.level++;
// }
// }
// Russell.increaseScore(50);
// console.log(Russell.score)
// Russell.increaseLevel();
// console.log(Russell.level)
//Create a class Movie that has properties title, director, year, and rating. Add a
method getRating() that returns the rating of the movie as a string ("PG", "PG-13",
"R", etc.). Create an instance of this class and call the getRating() method.
// class Movie{
// constructor(title, director, year, rating){
// this.title = title;
// this.director = director;
// this.year = year;
// this.rating = rating;
// }
// getRating(){
// console.log(`The Movie ${this.title} is created by ${this.director} in
the year of ${this.year} it has ${this.rating} rating`)
// }
// }
//
-----------------------------------------------------------------------------------
----
// // 1.create a class that will show the difference between constructor methods
and the instance method in the class.
// class Demo{
// name:string;
// age:number;
// id:number;
// //constructor method
// constructor(name:string,age:number,id:number){
// this.name = name;
// this.age = age;
// this.id = id;
// }
// // instance method
// setDetails(n:string,a:number,id:number){
// this.name = n;
// this.age = a;
// this.id = id;
// }
// getDetails(){
// console.log(this.name + " " + this.age + " " + this.id)
// }
// }
// class BankAccount{
// accountNumber:number;
// accountHolder:string;
// balance:number;
// constructor(ac:number,ah:string,bal:number){
// this.accountNumber = ac;
// this.accountHolder = ah;
// this.balance = bal;
// }
// deposit(amount:number){
// this.balance += amount;
// console.log(`Deposited ${amount} Your New Balance is ${this.balance}`)
// }
// withdraw(amount:number){
// if(this.balance >= amount){
// this.balance -= amount;
// console.log(`Withdrawan ${amount} Your New Balance is $
{this.balance}`)
// } else {
// console.log("Insufficient Balance")
// }
// }
// }
// //3.Create a Vehicle class with make, model, and year properties. Add a method
start to the class that logs a message indicating that the vehicle has started.
// class Vehicle{
// make:string;
// model:string;
// year:number;
// constructor(company:string,id:string,year:number){
// this.make = company;
// this.model = id;
// this.year = year;
// }
// startEngine(){
// console.log(`${this.model} made by ${this.make} in the year of $
{this.year} one of the best racing car`)
// }
// }
// class Student{
// name:string;
// age:number;
// gender:string;
// grades:number[]
// constructor(name:string,age:number,gender:string,grades:number[]){
// this.name = name;
// this.age = age;
// this.gender = gender;
// this.grades = grades;
// }
// getAverageGrades(){
// let total = this.grades.reduce((sum,grade)=> sum + grade,0)
// let average = total/this.grades.length;
// console.log(Math.floor(average))
// }
// }
// //5.Create a Book class with title, author, and price properties. Add a method
discount to the class that applies a discount to the book's price.
// class Book{
// title:string;
// price:number;
// constructor(title:string, price:number, ){
// this.title = title;
// this.price = price
// }
// discount(percentage:number){
// let discountAmt = (this.price * percentage)/ 100;
// this.price -= discountAmt;
// console.log("The discount is " + discountAmt)
// }
// }
// // 6.create class define two properties extend the class and use parent
properties in child class
// class Parent{
// name:string;
// role:string;
// constructor(name:string,role:string){
// this.name = name;
// this.role = role;
// }
// }
// class Child extends Parent{
// age:number;
// id:number;
// constructor(_name:string,_role:string,age:number,id:number){
// super(_name,_role)
// this.age = age;
// this.id = id;
// }
// }
// constructor(name:string,age:number,gender:string,role:string){
// this.name = name;
// this.age = age;
// this.gender = gender;
// this.role = role;
// }
// getDetails(){
// console.log(`my name is ${this.name} my age is ${this.age} and I am an $
{this.role}`)
// }
// }
// // 8.Create a class called BankAccount with properties balance and owner. Add
methods deposit and withdraw that update the balance accordingly.
// class BankAccount{
// accountNumber:number;
// accountHolder:string;
// balance:number;
// constructor(ac:number,ah:string,bal:number){
// this.accountNumber = ac;
// this.accountHolder = ah;
// this.balance = bal;
// }
// deposit(amount:number){
// this.balance += amount;
// console.log(`Deposited ${amount} New Balance is ${this.balance}`)
// }
// withdraw(amount:number){
// if(this.balance >= amount){
// this.balance -= amount;
// console.log(`withdrawn ${amount} New Balance is ${this.balance}`)
// }else{
// console.log("Insufficient Balance")
// }
// }
// }
// myAccount.deposit(5000);
// myAccount.withdraw(500) // Balance deposited
// // myAccount.withdraw(1000); // Balance withdrawn
// // myAccount.withdraw(100); // Insufficient Balance
// // 9.create an bank class and add the account holders details and get the all
details for creating new bank account.
// class Bank{
// bankname:string;
// branch:string;
// accountType:string;
// accountnumber:number;
// accountHolder:string;
// constructor(bn:string,brc:string,at:string,ac:number,user:string){
// this.bankname = bn
// this.branch = brc;
// this.accountType = at;
// this.accountnumber = ac;
// this.accountHolder = user;
// }
// getDetails(){
// console.log(`Bank Name : ${this.bankname}`)
// console.log(`Account Holder: ${this.accountHolder}`)
// console.log(`Account Number: ${this.accountnumber}`)
// console.log(`Account Type : ${this.accountType}`)
// console.log(`Branch : ${this.branch}`)
// }
// }
// // 10. Create a class Person that has properties name and age. Create an
instance of this class and print out its properties.
// class Sample{
// readonly name:string;
// private id:number;
// protected loacation :string;
// constructor(name:string,id:number,loc:string){
// this.name = name;
// this.id = id;
// this.loacation = loc;
// }
// getDetails(){
// console.log(`My name is ${this.name} My id is ${this.id} I live in $
{this.loacation}`)
// }
// }
// class Demo extends Sample{
// getDetails1(){
// console.log(this.loacation)
// }
// }
// const newObj = new Sample("Prashant",10,"Udgir")
// newObj.getDetails();
// newObj.name = "tony"
// newObj.id = 12;
// newObj.getDetails1()
//---------------------------------------------------------------------------------
------------------------
//1. Create a class called car with properties make, model, and year and add them
in a start() method.
//Create a class called SportsCar that inherits from Car and adds a property
horsepower.
//Override the start method to include the car's horsepower in the message.
// class Car {
// make:string;
// model:string;
// year:number;
// constructor(company:string,id:string,year:number){
// this.make = company;
// this.model = id;
// this.year = year;
// }
// start(){
// console.log(`${this.model} made by ${this.make} in the year of $
{this.year} one of the best racing car`)
// }
// }
// class SportsCar extends Car{
// horsePower:number;
// constructor(hp:number,comp:string,id:string,yr:number){
// super(comp,id,yr)
// this.horsePower = hp;
// }
// //overriding start()method to add horsePower property in it
// start(){
// console.log(`${this.model} made by ${this.make} in the year of $
{this.year} and have ${this.horsePower} the best racing car`)
// }
// }
/* 2.Create a class called Book with properties title, author, and pages. Add a
method read that logs a message to the console
indicating that the book has been read. */
// class Book{
// title:string;
// author:string;
// pages:number;
// constructor(title:string,author:string,pages:number){
// this.title= title;
// this.author = author;
// this.pages = pages;
// }
// read(){
// console.log(`${this.title} ${this.author} ${this.pages}`)
// }
// }
// class Book{
// title:string;
// author:string;
// pages:number;
// constructor(title:string,author:string,pages:number){
// this.title = title;
// this.author = author;
// this.pages = pages;
// }
// read(){
// console.log(`${this.title} ${this.author} ${this.pages}`)
// }
// }
// class Ebook extends Book{
// format:string;
// constructor(t:string,a:string,p:number,format:string){
// super(t,a,p)
// this.format = format;
// }
// //overridden method
// read(){
// console.log(`${this.title} ${this.author} ${this.pages} ${this.format}`)
// }
// }
// class Shape{
// x:number;
// y:number;
// constructor(x:number,y:number){
// this.x = x;
// this.y = y;
// }
// draw(){
// console.log(`${this.x} ${this.y} Shpae has been drawn`)
// }
// }
// class Shape{
// x:number;
// y:number;
// constructor(x:number,y:number){
// this.x = x;
// this.y = y;
// }
// draw(){
// console.log(`${this.x} ${this.y} Shape has been drawn`)
// }
// }
// class Circle extends Shape{
// radius:number;
// constructor(x:number,y:number,r:number){
// super(x,y)
// this.radius = r;
// }
// // overriding the draw method
// draw(){
// console.log(`${this.x} ${this.y} ${this.radius} Circle has been drawn`)
// }
// }
// const circle = new Circle(13,23,12)
// circle.draw();
// class Person{
// name:string;
// age:number;
// constructor(n:string,a:number){
// this.name = n;
// this.age = a;
// }
// speak(){
// console.log(`My name is ${this.name} My age is ${this.age}`)
// }
// }
// const person1 = new Person("Tony",21)
// person1.speak();
// class Person{
// name:string;
// age:number;
// constructor(n:string,a:number){
// this.name = n;
// this.age = a
// }
// speak(){
// console.log(`My name is ${this.name} My age is ${this.age}`)
// }
// }
// class Teacher extends Person{
// subject:string;
// constructor(n:string,a:number,sub:string){
// super(n,a);
// this.subject = sub;
// }
// speak(){
// console.log(`I am a Teacher My name is ${this.name} My age is $
{this.age} My subject is ${this.subject}`)
// }
// }
/*8.Create a class called Todo with properties description, dueDate, and completed.
Add methods markCompleted and markIncomplete that update the completed property
accordingly. */
// class Todo{
// description:string;
// dueDate:string;
// completed:boolean;
// constructor(des:string,date:string){
// this.description = des;
// this.dueDate = date;
// this.completed = false;
// }
// markCompleted(){
// this.completed = true;
// console.log(`Congratulations!`)
// }
// markUncomplete(){
// this.completed = false;
// console.log(`Dont Give Up, Try your best...`)
// }
// }
// class Todo{
// description:string;
// dueDate:string;
// completed:boolean;
// constructor(des:string,date:string){
// this.description = des;
// this.dueDate = date;
// this.completed = false;
// }
// markCompleted(){
// this.completed = true;
// console.log(`Congratulations!`)
// }
// markUncomplete(){
// this.completed = false;
// console.log(`Dont Give Up, Try your best...`)
// }
// }
// class PriorityTodo extends Todo{
// priority:string;
// constructor(des:string,date:string,pr:string){
// super(des,date);
// this.priority = pr;
// }
// markCompleted(){
// super.markCompleted()
// console.log(`Marked '${this.description}' (${this.priority} priority) as
completed.`)
// }
// }