Assignment #1
Assignment #1
} *
} ***
} *****
} *******
*****
***
*
2. Write a class that contain the following attributes:
The name of a car
The direction of a car(E,W,N,S)
The position of a car(from imaginary zero point)
The class has the following member functions:
Initialize to attributes.
Turn function to change the direction of a car to one step right
side(e.g if the direction of a car is E, it should be changed to S and
so on)
Overload the function to change the direction to any side directly.
It should accept the direction as parameters.
Move function to change the direction of car away from zero point.
It should have the direction as parameters.
class car
{
char direction;
int position;
public
car(char d,int p)
{
strcpy(direction,d);
position=p;
}
void turn()
{
switch(direction)
{
case 'N':
direction='E';
break;
case 'E':
direction='S';
break;
case 'S':
direction='W';
break;
case 'W':
direction='N';
break;
default:
System.out.println("ERROR");
break;
}
}
void turn(char new_direction)
{
strcpy (direction,new_direction);
}
void move(int position_change)
{
position=position+position_change;
}
};
void ()
{
car c('E',0);
System.out.println (" Current Direction : ");
System.out.print"\n Current Position : ");
c.turn();
c.move(25);
System.out.println" Current Direction : ");
System.out.println"\n Current Position : ");
c.turn('N');
c.move(25);
System.out.println("Current Direction : ");
System.out.println(" Current Position : ");
}
/*
Q3. Write a class for a bank account that includes the following data
members.
Name of the depositor
Account Number
Type of account
Balance amount in the account
1. The class also contain the following member functions:
Assign initial value
Deposit function to deposit some value.
It should accept the amount as parameters.
Withdraw function to withdraw an amount after checking the
balance.
It should accept the amount as parameter.
Display function to display name and balance
*/