You are on page 1of 3

Tasks:

1. Write a program to generate a Fibonacci series by creating fibonacci class.

The result become like 1 1 2 3 5 8 and so on

class Main {

public static void main(String[] args) {

int n = 10, firstTerm = 0, secondTerm = 1;

System.out.println("Fibonacci Series till " + n + " terms:");

for (int i = 1; i <= n; ++i) {

System.out.print(firstTerm + ", ");

// compute the next term

int nextTerm = firstTerm + secondTerm;

firstTerm = secondTerm;

secondTerm = nextTerm;

Excercise2:

) Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S)
The position of car (from imaginary zero point) The class has the following methods: • A constructor to
initialize the attributes • Turn() method to change the direction of car to one step right side (i.e. if the
direction is to E, it should be changed to S and so on) • Overload the turn() method to change the
direction to any side directly. It should accept the direction as parameter. • Move() method to change
the position of car away from zero point. It should accept the distance as parameter

Lab 3
class Car{

char name[20],direction;

int position;

public:

Car(char n[], char d, int p){

strcpy(name,n);

strcpy(direction,d);

position=p;

void turn(){

if(direction=='N')

direction='E';

else if(direction=='E')

direction='S';

else if(direction=='S')

direction='W';

else

direction='N';

void turn(char d){

strcpy(direction,d);

Lab 3
void move(int p){

position+=p;

Lab 3

You might also like