Posts

Showing posts from April, 2023

inputReader.js (***REVISE nice question)

Image
  /** * Input reader -- Classes Practice Problems Write an InputReader class that can read n strings/integers/floats. InputReader should have 3 methods: readStrings, readIntegers, readFloats. Each of these methods should take n as parameter and read n number of inputs, print them, one on each line, prepended with the index number of inputs taken, starting from 0, like indexes in an array. The index and the element should be space separated. For floats, the output should be rounded to 2 decimal places only. Inputs are handled by the template. You need to implement the methods readStrings, readIntegers, readFloats inside the InputReader class. Input First line contains an integer n, 0 < n <= 100. Second line contains a string which indicates the type of the array: string, integer, float. This is followed by n lines. Each line contains one element which could be string/integer/float based on the second line. Output The output should contain n lines. Each line contains...

robotMovement.js

Image
  /** * Robot Movement -- Classes Practice Problems You have recently designed a robot and you are trying to write code to make it move over a 2-D plane. Please initialize your robot to always start at the origin (0,0). Design a class which has 4 methods moveUp -- Takes one step along the +ve Y axis. moveDown -- Takes one step along the -ve Y axis. moveRight -- Takes one step along the +ve X axis. moveLeft -- Takes one step along the -ve X axis. The directions to move will be provided to you as follows up -- Move up down -- Move down right -- Move right left -- Move left Your class should have an instance variable location which contains the present location of your robot. Print the location of your robot after taking m steps. Input First line contains an integer which specifies how many steps to take which is m followed by m lines each specifying the direction to move. Ex.. up which means take 1 step in up direction. Output Two lines specifying the final location of the Robo...

rectangle.js

Image
  /** * area and perimeter of rectangle -- Classes Practice Problems Design a class which has 2 methods. One which computes the Area of the Rectangle. The other computes the Perimeter of the Rectangle. You should be able to pass the length l and width w while creating the object for the class. For all infeasible values of length l and width w, print area and perimeter as 0 Your class should be named as Rectangle. Method to get area should be named as rectangle_area. Method to get perimeter should be named as rectangle_perimeter. Input First line contains an integer for the length l Second line contains an integer for the width w Output Two lines containing integers. First line containing the Area of the Rectangle Second line containing the Perimeter of the Rectangle Example Input: 3 2 Output: 6 10 First line is 3 representing the length. Second line is 2 representing the width. Area is 3*2 which is 6 as represented in the first line of the output. Perimeter is 2*(3 + 2) whic...

movie.js

Image
  /** * Print Movie Description -- Classes Practice Problems Write a Movie class for which you can create movie objects. The objects should have the following variables: integer length_in_minutes, integer num_characters, string language. Each object should also have a run method which prints out: "This is a movie with characters and is minutes long." Input First line is a string, denoting the language of the movie Second line is an integer, denoting the number of characters Third line is an integer, denoting the length in minutes Output The output should be the return statement of run: "This is a movie with characters and is minutes long." Example Input: French 4 200 Output: This is a French movie with 4 characters and is 200 minutes long. First line is French indicating the movie's language is French Second line is 4, indicating that the movie has 4 characters Third line is 200, indicating that the movie is 200 minutes long */ let fs = require ( ...

circle.js

Image
  /** * Area and Perimeter of the Circle - Classes Practice Problems Design a Circle class which has 2 methods. One which calculates area of the circle and one which calculates circumference of the circle. Please use the pi value as 3.14. For any infeasible radius r, please return the area and circumference as 0 Your class should be named Circle. Method to get area should be named getArea. Method to get circumference should be named getCircumference. Input One line containing an integer denoting the radius. Output 2 lines containing integers. First line containing the area of the circle. Second line containing the circumference of the circle. If the output is a float number, return the ceil of it. Example Input: 5 Output: 79 32 First line in input is radius which is 5. Area is 5*5*3.14 which is ceil(78.5) = 79, which is the first line of the output. Circumference is 2*3.14*5 which is ceil(31.40) = 32, which is the second line of the output. */ let fs = require ( "fs" ...

Private (#) attributes/ properties inside a function

Image
 

All about spread operator

Image
 1) 2) 3) 4)

Object literals and anonymous function

Image
 

creditCard.js / Polymorphism / Method Overriding is introduced

Image
 1. // This is the Parent class , the children classes will inherit it's attributes and methods class CCAccount { constructor ( n , e , ph , UA ) { this .name = n ; this .email = e ; this .phoneNumber = ph ; this .usedAmount = UA ; } getPendingBill () { // this method will return all the used up amount return this .usedAmount } isTransactionAllowed ( amount ) { // this method will check if the customer is exceeding the credit limit if ( this .usedAmount + amount > 200000 ) { console. log ( "Limit Exceede" ); } else { this .usedAmount += amount ; console. log ( "Transaction Success" ); } } } // GoldCCAccount class will have the same properties and methods as CCAccount but the limit is 5 lakhs class GoldCCAccount extends CCAccount { constructor ( n , e , ph , UA ) { ...

flight.js ( nice but tricky question to practice and revise)

Image
  /** * Flight We are building a simulation for birds. One part of it is to design a class called Flight, which deals with the flying of birds. This class has upTime and downTime as the properties. The class should also have a method named calculateFlight, which will return the calculated flying time. You need to complete this method. Here, upTime denotes the time at which a given bird starts flying, and downTime is the time at which the bird lands somewhere. You don't need to worry about input/output and object of the class. The given template will take care of it. Also, it is given the bird will fly in the morning, and will land before night of the same day. The input will contain the upTime and downTime, in 24 hr notation as hh:mm (h is hour, and m is min). You need to calculate the flying time of the given bird (in minutes), as output. Input First line contains upTime in the given notation. Second line contains downTime in the given notation. Output One Integer denoti...

car.js

Image
  /** * Car Define a class named Car, which should have the name and model as the properties. You don't need to worry about input/output and object of the class. The given template will take care of it. The input will contain the name and model. Their default values should be Audi and A4 respectively. Input First line contains an integer, either 1 or -1. If the first line is 1, then: One line containing two space separated values, denoting name and model respectively. Output Print name and model in newline each. Example Input1: 1 Ford C4 Output1: Ford C4 Input2: -1 Output2: Audi A4 Explanation Example 1 First line is 1, which denotes space separated values as input to the class. Ford C4 denotes the values for name and model respectively and we print name and model in new line each. Example 2 First line is -1, which denotes we need to print the default values line by line. */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx...