0% found this document useful (0 votes)
18 views6 pages

Net 25 & 26 CopyConstructor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Net 25 & 26 CopyConstructor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Create an class Mobile class should have the following properties:

Instance Variables:

MobileBrand:string static
CameraPixels:int static
RAMSize:int static
ROMSize:int static
BatteryCapacity:string static

Implement constructor for the Mobile class:


A constructor that allows you to set the MobileBrand, CameraPixels, RAMSize,
ROMSize and BatteryCapacity.

Name:DisplayInfo
ReturnType:string
AccessModifier:public
return the data of the Mobile

Create another class MobileDetails

create object for Mobile class pass the values by using constructor and also call
the DisplayInfo method.
User has to enter the values.
-----------------------------------------------------------------------------------
--------------------------------

ANS.....
using System;

namespace test
{
public class Mobile
{
static string MobileBrand;
static int CameraPixels;
static int RAMSize;
static int ROMSize;
static string BatteryCapacity;

public Mobile(string mb, int cp, int rams, int roms, string bc)
{
MobileBrand = mb;
CameraPixels = cp;
RAMSize = rams;
ROMSize = roms;
BatteryCapacity = bc;
}
public string display()
{
return $"\nMobile Brand is: {MobileBrand}\n" + $"CameraPixels is:
{CameraPixels}\n" + $"RAMSize is: {RAMSize}\n" + $"ROMSize is: {ROMSize}\n" +
$"BatteryCapacity: {BatteryCapacity}\n ";
}
}

public class MobileDetails


{
public static void Main(string[] args)
{
string MobileBrand;
int CameraPixels;
int RAMSize;
int ROMSize;
string BatteryCapacity;

Console.Write("Enter Mobile Brand: ");


MobileBrand = Console.ReadLine();

Console.Write("Enter Camera Pixels: ");


CameraPixels = int.Parse(Console.ReadLine());

Console.Write("Enter RAMSize: ");


RAMSize = int.Parse(Console.ReadLine());

Console.Write("Enter ROMSize: ");


ROMSize = int.Parse(Console.ReadLine());

Console.Write("Enter Battery Capacity: ");


BatteryCapacity = Console.ReadLine();

Mobile obj = new Mobile(MobileBrand, CameraPixels, RAMSize, ROMSize,


BatteryCapacity);

Console.Write(obj.display());
Console.ReadLine();

}
}
}

-----------------------------------------------------------------------------------
----------------------------------------

2.Create an class Employee should have the following properties:

Instance Variables:

id:int
name:string
salary:double

Implement constructor for the Employee class:


A parameterized constructor that allows you to set the id,name and salary.
Methods:
Methodname:tax
returntype:double
Accessmodifier:public
write code to calculate the tax if salary is grater than 40000 5% tax and salary is
grater than 1lac 15% tax and return the tax.

MethodName:getDetails
returntype:string
accessmodifier:public
return all the data of employee in string format.
Create another class EmployeeDetails

create object for Employee class pass the values by using constructor and also call
the tax method after that display all the data of employee from getDetails method.
User has to enter the values.
-----------------------------------------------------------------------------------
----------------------------------------

ANS.....

using System;

namespace test
{
class Employee
{
int id;
string name;
double salary;

public Employee(int id, string name, double salary)


{
this.id = id;
this.name = name;
this.salary = salary;
}

public double tax(double salary)


{
if (salary > 40000)
return salary * 0.05;
else if (salary > 100000)
return salary * 0.1;
else
return 0;
}
public string getdetails()
{
return $"\nid is: {id}" + $"\nname is: {name}" + $"\nsalary is:
{salary}";
}
}

public class Employee2


{
public static void Main(string[] args)
{
int id;
string name;
double salary;

Console.Write("Enter id: ");


id = int.Parse(Console.ReadLine());

Console.Write("Enter Name: ");


name = Console.ReadLine();

Console.Write("Enter Salary: ");


salary = double.Parse(Console.ReadLine());
Employee emp = new Employee(id, name, salary);

Console.WriteLine(emp.getdetails());

double tax = emp.tax(salary);

Console.WriteLine("Tax to be paid: "+ tax);

Console.ReadLine();
}

}
}

===================================================================================
==============================

3.Create an class Sim should have the following properties:

Instance Variables:

SimNumber:long
SimType:string

Implement two constructors for the Mobile class:


1. A parameterized constructor that allows you to set the
SimNumber and SimType
2. A Copy constructor copy the data to copy constructor:

Name:DisplayInfo
Methods:
Name:DisplayInfo
ReturnType:string
AccessModifier:public
return the data of the Sim

If you change the sim type for example before simtype is airtel and changed to jio
update the simtype

MethodName:UpdateSimtype
Prameters:NewSimType (string)
Returntype:void
AccessModifier:public
update the simtype.

Create another class SimDetails

create object for Sim class pass the values by using constructor and also call the
UpdateSimtype method.
Display the siminformation using copy constructor and also display details after
update
copy constructor details don't has to change it should be having old details
User has to enter the values.

#include <stdio.h>
#include <stdlib.h>

void decrypt(int* code, int codeSize, int k, int* result) {


int n = codeSize;

if (k == 0) {
for (int i = 0; i < n; i++) {
result[i] = 0;
}
return;
}

if (k > 0) {
int wsum = 0;
for (int i = 1; i <= k; i++) {
wsum += code[i];
}
result[0] = wsum;

for (int l = 1, r = k + 1; l < n; r++, l++) {


wsum += -code[l] + code[r % n];
result[l] = wsum;
}
return;
}

k = -k;
int wsum = 0;
for (int i = n - k; i < n; i++) {
wsum += code[i];
}
result[0] = wsum;

for (int r = 0, l = n - k; r < n - 1; r++, l++) {


wsum += -code[l % n] + code[r];
result[r + 1] = wsum;
}
}

int main() {
int n, k;

printf("Enter the size of the array: ");


scanf("%d", &n);

int *code = (int *)malloc(n * sizeof(int));


int *result = (int *)malloc(n * sizeof(int));

printf("Enter the elements of the array: ");


for (int i = 0; i < n; i++) {
scanf("%d", &code[i]);
}

printf("Enter the value of k: ");


scanf("%d", &k);

decrypt(code, n, k, result);

printf("Resulting array after decryption: ");


for (int i = 0; i < n; i++) {
printf("%d ", result[i]);
}
printf("\n");

free(code);
free(result);

return 0;
}

You might also like