0% found this document useful (0 votes)
2 views

EXERCISE-8 MSD LAB PROGRAMS

The document outlines two exercises in TypeScript focusing on classes, access modifiers, and namespaces. Exercise 8(a) involves creating a Product class with various properties and access modifiers, and a Gadget class that extends it, while Exercise 8(b) requires creating a namespace called ProductUtility to encapsulate the Product class and demonstrate its usage in another file. The document includes source code examples and instructions for compiling and running the TypeScript files.

Uploaded by

tmsubhashvaleti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EXERCISE-8 MSD LAB PROGRAMS

The document outlines two exercises in TypeScript focusing on classes, access modifiers, and namespaces. Exercise 8(a) involves creating a Product class with various properties and access modifiers, and a Gadget class that extends it, while Exercise 8(b) requires creating a namespace called ProductUtility to encapsulate the Product class and demonstrate its usage in another file. The document includes source code examples and instructions for compiling and running the TypeScript files.

Uploaded by

tmsubhashvaleti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercise -8 MSD LAB PROGRAMS

Exercise -8 (Typescript – Classes, Access modifiers, Namespaces)


a). Write a Typescript program to create a Product class with 4 properties namely productId,
productName, productPrice, productCategory with private, public, static, and protected
access modifiers and accessing them through Gadget class and its methods.
b). Write a Typescript program to create a namespace called ProductUtility and place the
Product class definition in it. Import the Product class inside productlist file and use it.

Exercise – 8(a) (Typescript – Classes, Access modifiers)


AIM:
Write a Typescript program to create a Product class with 4 properties namely
productId, productName, productPrice, productCategory with private, public, static, and
protected access modifiers and accessing them through Gadget class and its methods.

SOURCE CODE: modifiers.ts

class Product // declaring a Product class with access modifiers


{
private productId: number;
public productName: string;
protected productCategory: string;
static productPrice = 650;
// declaration of constructor with 3 parameters
constructor(productId: number, productName , productCategory)
{
this.productId = productId;
this.productName = productName;
this.productCategory = productCategory;
}
getProductId() // method to display product id details
{
console.log('Product Id : ' + this.productId);
}
}
// declaring a Gadget class extending the properties from Product class
class Gadget extends Product {
// method to display productCategory property
getProduct(): void {
console.log('Product category : ' + this.productCategory);
// static property product price directly access with Product class name
console.log('Product Price : $' + Product.productPrice);

}
}
// Gadget Class object creation
const g1: Gadget = new Gadget(180, 'SmartPhone', 'Mobile');
// invoking getProduct method with the help of Gadget object
g1.getProduct();
// invoking getProductId method with the help of Gadget object
g1.getProductId();
// product name property access with Gadget object
console.log('Product Name : ' + g1.productName);

OUTPUT: 8(a)
Exercise – 8(b) (Typescript – Namespaces)
AIM:
Write a Typescript program to create a namespace called ProductUtility and place the
Product class definition in it. Import the Product class inside productlist file and use it.

Description:
Namespace is basically has collection of classes, interfaces, variables, functions together in one
file. A Namespace is used to group functions, classes, or interfaces under a common name. The
content of namespaces is hidden by default unless they are exported. Use nested namespaces if
required. The function or any construct which is not exported cannot be accessible outside the
namespace. To import the namespace and use it, make use of the triple slash reference tag.

Procedure Steps:
Step 1: Create a file namespace_demo.ts
Step 2: Create another file namespace_import.ts
 The file in which the namespace is declared and the file which uses the namespace to be
compiled together. It is preferable to group the output together in a single file. You have
an option to do that by using the
--outFile keyword.
 To import the namespace and use it, make use of the triple slash reference tag.
Step 3: Open Node.js command prompt, change directory to the folder in which Namespace file
resides and run the below command from the command line:
D:/MSD LAB/Typescript> tsc --outFile namespace.js namespace_demo.ts
namespace_import.ts
Step 4: Run the namespace.js file from the command line :
D:/MSD LAB/Typescript> node namespace.js
SOURCE CODE: namespace_demo.ts
namespace ProductUtility {
export class Product
{
productId: number;
productName:string;
quantity: number;
price: number;
constructor( productId: number, productName:string, quantity: number, price: number)
{
this.productId =productId;
this.productName =productName;
this.quantity=quantity;
this.price = price;
console.log("Product Name : "+productName);
}
CalculateAmount(){
var totalPrice= this.price * this.quantity;
return totalPrice;
return this.productId, this.quantity, this.price;
}
}
}

namespace_import.ts

// Accessing Namespace
/// <reference path="./namespace_demo.ts" />
let paymentAmount = new ProductUtility.Product(180,"Apple", 2, 60500);
console.log("Product Id :"+paymentAmount.productId);
console.log("Product Quantity : "+paymentAmount.quantity);
console.log("Product Price : "+paymentAmount.price);
console.log("Amount to be paid:"+paymentAmount.CalculateAmount());

 In order to use namespace components at other places, first we need to include


the namespace using the triple slash reference syntax /// <reference path=”path
to namespace file”/>. After including the namespace file using the reference
tag, we can access all the functionalities using the namespace.
 You can compile both namespace_demo.ts namespace_import.ts files into one
namespace.js file as shown below:
tsc --outFile namespace.js namespace_demo.ts namespace_import.ts
OUTPUT: 8(b)

You might also like