0% found this document useful (0 votes)
68 views4 pages

OOP Exercises For Slot 456

This document contains instructions for 5 exercises involving object-oriented programming in C#: 1. Create a Dice class with a roll method to generate random numbers, and a program to allow guessing the dice roll. 2. Create a Fraction class to perform arithmetic on fractions, with methods for input, addition, subtraction, multiplication, division, and displaying fractions as decimals. 3. Define a Song class to store song data, read songs from input, and output names by type. 4. Create classes with given components. 5. Create a PriceCalculator class to calculate hotel prices based on price per day, number of days, season, and discount type defined as enums.

Uploaded by

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

OOP Exercises For Slot 456

This document contains instructions for 5 exercises involving object-oriented programming in C#: 1. Create a Dice class with a roll method to generate random numbers, and a program to allow guessing the dice roll. 2. Create a Fraction class to perform arithmetic on fractions, with methods for input, addition, subtraction, multiplication, division, and displaying fractions as decimals. 3. Define a Song class to store song data, read songs from input, and output names by type. 4. Create classes with given components. 5. Create a PriceCalculator class to calculate hotel prices based on price per day, number of days, season, and discount type defined as enums.

Uploaded by

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

Exercise 1: Dice

Use object-oriented programming method to create a C# program with the following requirements:
 Users enter a number of sides of Dice
 The program will generate a random number from 1 to the number of sides
 Users will input a guessing number and get the results of the guessing.
What classes should we create?
- Class: Dice
o Fields: slides
o Methods: roll (generate a random number from 1 to sides)
- A class to run the program

Exercise 2: Make a Fraction class in C#


Create a class called Fraction to perform arithmetic operations with fractional numbers.
Write a program to test this class:
Use integer values to represent the attributes of the class – the numerator and the denominator.
Create public methods to perform the following:
 Input two fractions. The result is normalized.
 Add two fractions. The result is normalized.
 Subtract two fractions. The result is normalized.
 Multiply two fractions. The result is normalized.
 Divide two fractions. The result is normalized.
 Display a fraction as follows: (A/B). A is the numerator, B is the denominator
 Display a fraction on the screen as a decimal number.

Exercise 3
Define a class Song, which holds the following information about songs: Type List, Name and Time.
On the first line you will receive the number of songs - N.
On the next N-lines you will be receiving data in the following format:
"{typeList}_{name}_{time}".
On the last line you will receive Type List / "all". Print only the Names of the songs which are from
that Type List / All songs.

Examples
Input Output
3 DownTown
favourite_DownTown_3:14 Kiss
favourite_Kiss_4:16 Smooth Criminal
favourite_Smooth Criminal_4:01
favourite
4 Andalouse
favourite_DownTown_3:14
listenLater_Andalouse_3:24
favourite_In To The Night_3:58
favourite_Live It Up_3:48
listenLater
2 Replay
like_Replay_3:15 Photoshop
ban_Photoshop_3:48
all

Exercise 4: Create classes with the following components

Exercise 5: Hotel Reservation


 Create a class PriceCalculator that calculates the total price
of a holiday, by given price per day, number of days, the season
and a discount type
 The discount type and season
should be enums
 The price multipliers will be:
 1x for Autumn, 2x for Spring, etc.
 The discount types will be:
 None - 0%
 SecondVisit - 10%
 VIP - 20%

Solution of Exercise 2
Define class Song with properties: Type List, Name and Time.

Read the input lines, make collection and store the data.

Finally read your last line – Type List and print


the result.

You might also like