Sei sulla pagina 1di 16

Group 5: Trần Minh Quang

Phạm Thế Việt Hoàng


Trần Bình Nguyên
LECTURE 1

1. What are the differences between procedural programming and object-oriented programming?

There are primarily two methods of programming in use today: procedural and object oriented. The
earliest programming languages were procedural, meaning a program was made of one or more
procedures. A procedure is a set of programming statements that, together, perform a specific task. The
statements might gather input from the user, manipulate data stored in the computer’s memory, and
perform calculations or any other operation necessary to complete the procedure’s task.

Procedures typically operate on data items that are separate from the procedures. In a procedural
program, the data items are commonly passed from one procedure to another. As you might imagine, the
focus of procedural programming is on the creation of procedures that operate on the program’s data.
The separation of data and the code that operates on the data often leads to problems, however. For
example, the data is stored in a particular format, which consists of variables and more complex
structures that are created from variables. The procedures that operate on the data must be designed with
that format in mind. But what happens if the format of the data is altered? Quite often, a program’s
specifications change, resulting in a redesigned data format. When the structure of the data changes, the
code that operates on the data must also be changed to accept the new format. This results in added work
for programmers and a greater opportunity for bugs to appear in the code.

This has helped influence the shift from procedural programming to object-oriented programming
(OOP). Whereas procedural programming is centered on creating procedures, object-oriented
programming is centered on creating objects. An object is a software entity that contains data and
procedures. The data contained in an object is known as the object’s attributes. The procedures, or
behaviors, that an object performs are known as the object’s methods. The object is, conceptually, a self-
contained unit consisting of data (attributes) and procedures (methods).

OOP addresses the problem of code/data separation through encapsulation and data hiding.
Encapsulation refers to the combining of data and code into a single object. Data hiding refers to an
object’s ability to hide its data from code that is outside the object. Only the object’s methods may then
directly access and make changes to the object’s data. An object typically hides its data, but allows
outside code to access the methods that operate on the data.

2. Find an example in the real world where the Object Technology is applied with success.

a. Real-Time System Design


Real time systems inherit complexities that makes difficult to build them. Object-oriented techniques
make it easier to handle those complexities. These techniques present ways of dealing with these
complexities by providing an integrated framework which includes schedulable analysis and
behavioral specifications.

b. Simulation and Modelling System


It’s difficult to model complex systems due to the varying specification of variables. These are
prevalent in medicine and in other areas of natural science, such as ecology, zoology, and agronomic
systems. Simulating complex systems requires modelling and understanding interactions explicitly.
Object-oriented Programming provides an alternative approach for simplifying these complex
modelling systems.

c. Hypertext and Hypermedia


OOP also helps in laying out a framework for Hypertext. Basically, hypertext is similar to regular
text as it can be stored, searched, and edited easily. The only difference is that hypertext is text with
pointers to other text as well.

Hypermedia, on the other hand, is a superset of hypertext. Documents having hypermedia, not only
contain links to other pieces of text and information, but also to numerous other forms of media,
ranging from images to sound.

d. Neural Networking and Parallel Programming


It addresses the problem of prediction and approximation of complex time-varying systems. Firstly,
the entire time-varying process is split into several time intervals or slots. Then, neural networks are
developed in a particular time interval to disperse the load of various networks. OOP simplifies the
entire process by simplifying the approximation and prediction ability of networks.

e. Office Automation Systems


These include formal as well as informal electronic systems primarily concerned with information
sharing and communication to and from people inside as well as outside the organization. Some
examples are:

Email
Word processing
Web calendars
Desktop publishing
#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

const int ROW = 5,


COL = 3,
SIZE = 4;

struct PlayerInfo
{
string name;
int color;
int point;
};
void showMenu();
int getChoice();
void generateBoard(int [][COL], string [][COL]);
void showBoard(int [][COL], string [][COL]);
bool isDuplicated(int [][COL], int, int, int);
void changeBoard(int [][COL], string [][COL]);
void playBoard(PlayerInfo [], int [][COL], string [][COL]);
void showResult(PlayerInfo []);
int main()
{
int boardNum[ROW][COL];
string boardName[ROW][COL];
int choice;
PlayerInfo player[SIZE];

generateBoard(boardNum, boardName);
do
{
system("cls");
showMenu();
choice = getChoice();
if (choice != 5)
{
switch (choice)
{
case 1: showBoard(boardNum, boardName);
break;
case 2: changeBoard(boardNum, boardName);
break;
case 3: playBoard(player, boardNum, boardName);
break;
case 4: showResult(player);
}
}
} while (choice != 5);
return 0;
}

void showMenu()
{
cout << "\t---Chao mung toi gameshow Hanh Khach Cuoi Cung---\n\n";
cout << "1. Hien ra bang choi\n";
cout << "2. Thay doi vi tri mau\n";
cout << "3. Bat dau phan choi\n";
cout << "4. Ket qua\n";
cout << "5. Thoat chuong trinh\n\n";
}

int getChoice()
{
int choice;

cout << "Bam de chon: ";


cin >> choice;
while (choice < 1 || choice > 5)
{
cout << "Yeu cau chon trong khoang 1 - 5: ";
cin >> choice;
}
cout << endl;
return choice;
}

void generateBoard(int boardNum[][COL], string boardName[][COL])


{
int ranNum;
srand(time(0));

for (int i = 0; i < ROW; i++)


{
for (int j = 0; j < COL; j++)
{
boardNum[i][j] = (rand() % (2 - 0 + 1)) + 0;
while (!isDuplicated(boardNum, boardNum[i][j], i, j))
{
boardNum[i][j] = (rand() % (2 - 0 + 1)) + 0;
}
if (boardNum[i][j] == 0)
boardName[i][j] = "Xanh";
else if (boardNum[i][j] == 1)
boardName[i][j] = "Vang";
else if (boardNum[i][j] == 2)
boardName[i][j] = "Do";
}
}
}

bool isDuplicated(int boardNum[][COL], int num, int row, int col)


{
for (int i = 0; i < col; i++)
if (boardNum[row][i] == num) return false;

return true;
}

void showBoard(int boardNum[][COL], string boardName[][COL])


{
for (int i = 0; i < 21; i++)
cout << "_";
cout << endl;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
cout << " " << left << setw(4) << boardName[i][j] << " |";
}
cout << endl;
for (int i = 0; i < 21; i++)
{
if (i == 6 || i == 13 || i == 20)
cout << "|";
else
cout << "_";
}
cout << endl;
}
cout << "\nBam enter de ve menu ";
cin.get();
cin.get();
}

void changeBoard(int boardNum[][COL], string boardName[][COL])


{
int row, col, color;
string swap;

cout << "Nhap hang muon doi: ";


cin >> row;
while (row < 1 || row > 5)
{
cout << "Yeu cau nhap hang trong khoang 1 - 5: ";
cin >> row;
}
cout << "Nhap cot muon doi: ";
cin >> col;
while (col < 1 || col > 3)
{
cout << "Yeu cau nhap cot trong khoang 1 - 3: ";
cin >> col;
}
cout << "Ban muon doi thanh mau gi? (0 - Xanh, 1 - Vang, 2 - Do): ";
cin >> color;
while (color == boardNum[row - 1][col - 1] || color < 0 || color > 2)
{
if (color == boardNum[row - 1][col - 1])
{
cout << "Mau nay da trung voi mau cua o! Yeu cau nhap lai: ";
cin >> color;
}
else if (color < 0 || color > 2)
{
cout << "Yeu cau nhap lai trong khoang 0 - 2: ";
cin >> color;
}
}
for (int i = 0; i < COL; i++)
if (boardNum[row - 1][i] == color)
{
swap = boardName[row - 1][col - 1];
boardName[row - 1][col - 1] = boardName[row - 1][i];
boardName[row - 1][i] = swap;
}
cout << "\nBang choi sau khi thay doi\n";
for (int i = 0; i < 21; i++)
cout << "_";
cout << endl;
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
cout << " " << left << setw(4) << boardName[i][j] << " |";
}
cout << endl;
for (int i = 0; i < 21; i++)
{
if (i == 6 || i == 13 || i == 20)
cout << "|";
else
cout << "_";
}
cout << endl;
}
cout << "\nBam enter de ve menu ";
cin.get();
cin.get();
}

void playBoard(PlayerInfo player[], int boardNum[][COL], string boardName[][COL])


{
cin.ignore();
int playerPick;

for (int i = 0; i < SIZE; i++)


{
cout << "Dien ten nguoi choi " << i + 1 << ": ";
getline(cin, (player + i)->name);
int length = (player + i)->name.length();
for (int j = 0; j < length; j++)
if ((player + i)->name[j] == ' ' && isalpha((player + i)->name[j + 1]))
{
(player + i)->name[j + 1] = toupper((player + i)->name[j + 1]);
(player + i)->name[0] = toupper((player + i)->name[0]);
}
}
cout << endl;
for (int i = 0; i < SIZE; i++)
{
cout << "Nguoi choi " << i + 1 << " chon mau gi? (0 - Xanh, 1 - Vang, 2 - Do): ";
cin >> (player + i)->color;
while ((player + i)->color < 0 || (player + i)->color > 2)
{
cout << "Yeu cau nhap lai trong khoang 0 - 2: ";
cin >> (player + i)->color;
}
(player + i)->point = 0;
int count = 0;
cout << "Nguoi choi chon o nao de dat chan len hang " << count + 1 << ": ";
cin >> playerPick;
while (boardNum[count][playerPick - 1] == (player + i)->color)
{
(player + i)->point++;
count++;
if (count == 5)
break;
cout << "Nguoi choi chon o nao de dat chan len hang " << count + 1 << ": ";
cin >> playerPick;
}
cout << endl;
}
cout << "Bam enter de ve menu ";
cin.get();
cin.get();
}

void showResult(PlayerInfo player[])


{
int max, min, maxPos = 0, minPos = 0;

max = min = player->point;


for (int i = 1; i < SIZE; i++)
{
if ((player + i)->point > max)
maxPos = i;
else if ((player + i)->point < min)
minPos = i;
}
for (int i = 0; i < 58; i++)
cout << "_";
cout << endl;
cout << left << setw(12) << "Luot choi | " << setw(23) << "Nguoi choi " << setw(15)
<< "| Mau lua chon | " << "Diem |\n";
for (int i = 0; i < 58; i++)
{
if (i == 10 || i == 35 || i == 50 || i == 57)
cout << "|";
else
cout << "_";
}
cout << endl;
for (int i = 0; i < SIZE; i++)
{
string color;
if ((player + i)->color == 0)
color = "Xanh";
else if ((player + i)->color == 1)
color = "Vang";
else
color = "Do";
cout << left << setw(10) << i + 1 << "| " << setw(22) << (player + i)->name << " | "
<< setw(13) << color << "| " << setw(5) << (player + i)->point << "|" << endl;
for (int i = 0; i < 58; i++)
{
if (i == 10 || i == 35 || i == 50 || i == 57)
cout << "|";
else
cout << "_";
}
cout << endl;
}
cout << "\nNguoi choi co so diem cao nhat la " << (player + maxPos)->name << endl;
cout << "Nguoi choi co so diem thap nhat la " << (player + minPos)->name << endl;
cout << "\nBam enter de ve menu ";
cin.get();
cin.get();
}

LECTURE 2

Programming challenges (Java)

2.17 Ingredient Adjuster


A cookie recipe calls for the following ingredients:
• 1.5 cups of sugar
• 1 cup of butter
• 2.75 cups of flour

The recipe produces 48 cookies with these amounts of the ingredients. Write a program
that asks the user how many cookies he or she wants to make, and then displays the number of cups of each
ingredient needed for the specified number of cookies.

import java.util.Scanner;

public class Homework


{
public static void main(String[] args)
{
final double SUGAR = 1.5;
final double BUTTER = 1;
final double FLOUR = 2.75;

double sugar,
butter,
flour;

int cookie;

System.out.print("How many cookies do you want to make? ");


Scanner keyboard = new Scanner(System.in);
cookie = keyboard.nextInt();

sugar = (SUGAR / 48) * cookie;


butter = (BUTTER / 48) * cookie;
flour = (FLOUR / 48) * cookie;

System.out.println("The recipe to make " + cookie


+ " cookies is as follow");
System.out.printf("- %.2f cups of sugar\n", sugar);
System.out.printf("- %.2f cups of butter\n", butter);
System.out.printf("- %.2f cups of flour\n", flour);
}
}

2.18 Word Game


Write a program that plays a word game with the user. The program should ask the user to
enter the following:
• His or her name
• His or her age
• The name of a city
• The name of a college
• A profession
• A type of animal
• A pet’s name

After the user has entered these items, the program should display the following story,
inserting the user’s input into the appropriate locations:
There once was a person named NAME who lived in CITY. At the age of AGE,
NAME went to college at COLLEGE. NAME graduated and went to work as a
PROFESSION. Then, NAME adopted a(n) ANIMAL named PETNAME. They both lived
happily ever after!

import java.util.Scanner;

public class Homework


{
public static void main(String[] args)
{
String name,
city,
college,
profession,
animal,
petName;
int age;

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter your name: ");


name = keyboard.nextLine();
System.out.print("Enter your age: ");
age = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter the name of a city: ");
city = keyboard.nextLine();
System.out.print("Enter the name of a college: ");
college = keyboard.nextLine();
System.out.print("Enter a profession: ");
profession = keyboard.nextLine();
System.out.print("Enter a type of animal: ");
animal = keyboard.nextLine();
System.out.print("Enter a pet's name: ");
petName = keyboard.nextLine();

System.out.println("\nThere once was a person named " + name


+ " who lived in " + city + ". At the age of " + age + ",");
System.out.println(name + " went to college at " + college + ". "
+ name + " graduated and went to work as a");
System.out.println(profession + ". Then, " + name + "adopted a(n) "
+ animal + " named " + petName + ". They both lived");
System.out.println("happily ever after!");
}
}

2.19 Stock Transaction Program


Last month Joe purchased some stock in Acme Software, Inc. Here are the details of
the purchase:
• The number of shares that Joe purchased was 1,000.
• When Joe purchased the stock, he paid $32.87 per share.
• Joe paid his stockbroker a commission that amounted to 2% of the amount he paid
for the stock.
Two weeks later Joe sold the stock. Here are the details of the sale:
• The number of shares that Joe sold was 1,000.
• He sold the stock for $33.92 per share.
• He paid his stockbroker another commission that amounted to 2% of the amount he
received for the stock.

Write a program that displays the following information:


• The amount of money Joe paid for the stock.
• The amount of commission Joe paid his broker when he bought the stock.
• The amount that Joe sold the stock for.
• The amount of commission Joe paid his broker when he sold the stock.
• Display the amount of profit that Joe made after selling his stock and paying the two
commissions to his broker. (If the amount of profit that your program displays is a
negative number, then Joe lost money on the transaction.)

public class Homework


{
public static void main(String[] args)
{
System.out.println("Joe paid $" + (32.87 * 1000) + " for the stock.");
System.out.println("Joe paid his broker $" + (32.87 * 1000 * 0.02) +
" for the commission when he bought the stock.");
System.out.println("Joe sold the stock for $" + (1000 * 33.92) + ".");
System.out.println("Joe paid his broker $" + (33.92 * 1000 * 0.02) +
" for the commission when he sold the stock.");
double profit = (1000 * 33.92) - (32.87 * 1000) -
((33.92 * 1000 * 0.02) + (32.87 * 1000 * 0.02));
if (profit > 0)
System.out.printf("In total, Joe made $%.2f " +
" after selling his stock and paying the two commissions to his broker.\n", profit);
else
{
profit = Math.abs(profit);
System.out.printf("In total, Joe lost $%.2f" +
" after selling his stock and paying the two commissions to his broker.\n", profit);
}
}
}

LECTURE 3

Programming challenges (Java)


3.7 Sorted Names
Write a program that asks the user to enter three names, and then displays the names sorted
in ascending order. For example, if the user entered “Charlie”, “Leslie”, and “Andy”, the
program would display:

Andy
Charlie
Leslie

import java.util.Scanner;
public class Homework
{
public static void main(String[] args)
{
String name1,
name2,
name3;

Scanner keyboard = new Scanner(System.in);


System.out.print("Enter name 1: ");
name1 = keyboard.nextLine();
System.out.print("Enter name 2: ");
name2 = keyboard.nextLine();
System.out.print("Enter name 3: ");
name3 = keyboard.nextLine();
System.out.println("Here are the names in ascending order:");
if (name1.compareToIgnoreCase(name2) < 0 && name1.compareToIgnoreCase(name3) < 0)
{
System.out.println(name1);
if (name2.compareToIgnoreCase(name3) < 0)
{
System.out.println(name2);
System.out.println(name3);
}
else
{
System.out.println(name3);
System.out.println(name2);
}
}
else if (name2.compareToIgnoreCase(name1) < 0 && name2.compareToIgnoreCase(name3) < 0)
{
System.out.println(name2);
if (name1.compareToIgnoreCase(name3) < 0)
{
System.out.println(name1);
System.out.println(name3);
}
else
{
System.out.println(name3);
System.out.println(name1);
}
}
else
{
System.out.println(name3);
if (name1.compareToIgnoreCase(name2) < 0)
{
System.out.println(name1);
System.out.println(name2);
}
else
{
System.out.println(name2);
System.out.println(name1);
}
}
}
}

3.8 Software Sales


A software company sells a package that retails for $99. Quantity discounts are given
according to the following table:

Quantity Discount

10–19 20%
20–49 30%
50–99 40%
100 or more 50%

Write a program that asks the user to enter the number of packages purchased. The program should then
display the amount of the discount (if any) and the total amount of the
purchase after the discount.

import java.util.Scanner;
public class Homework
{
public static void main(String[] args)
{
final int RETAIL = 99;
double total;
int packages;

Scanner keyboard = new Scanner(System.in);


System.out.print("Enter the number of packages purchased: ");
packages = keyboard.nextInt();
if (packages < 10)
System.out.println("You don't have any discount, total $"
+ (99 * packages) + ".");
else if (packages < 20)
{
total = RETAIL * packages * 0.8;
System.out.printf("Your discount is 20 percent, total $%.2f\n", total);
}
else if (packages < 50)
{
total = RETAIL * packages * 0.7;
System.out.printf("Your discount is 30 percent, total $%.2f\n", total);
}
else if (packages < 100)
{
total = RETAIL * packages * 0.6;
System.out.printf("Your discount is 40 percent, total $%.2f\n", total);
}
else
{
total = RETAIL * packages * 0.5;
System.out.printf("Your discount is 50 percent, total $%.2f\n", total);
}
}
}

3.9 Shipping Charges


The Fast Freight Shipping Company charges the following rates:

Weight of Package Rate per 500 Miles Shipped

2 pounds or less $1.10


Over 2 pounds but not more than 6 pounds $2.20
Over 6 pounds but not more than 10 pounds $3.70
Over 10 pounds $3.80

The shipping charges per 500 miles are not prorated. For example, if a 2-pound package is
shipped 550 miles, the charges would be $2.20. Write a program that asks the user to enter
the weight of a package and then displays the shipping charges.

import java.util.Scanner;
public class Homework
{
public static void main(String[] args)
{
final double RATE1 = 1.10;
final double RATE2 = 2.20;
final double RATE3 = 3.70;
final double RATE4 = 3.80;
double miles,
weights,
charges,
ratio;

Scanner keyboard = new Scanner(System.in);


System.out.print("Enter weights of the packages (in pounds): ");
weights = keyboard.nextDouble();
System.out.print("Enter miles ship: ");
miles = keyboard.nextDouble();
ratio = Math.ceil(miles / 500);
if (weights <= 2)
{
charges = RATE1 * ratio;
System.out.printf("The shipping charges is $%.2f\n", charges);
}
else if (weights > 2 && weights <= 6)
{
charges = RATE2 * ratio;
System.out.printf("The shipping charges is $%.2f\n", charges);
}
else if (weights > 6 && weights <= 10)
{
charges = RATE3 * ratio;
System.out.printf("The shipping charges is $%.2f\n", charges);
}
else
{
charges = RATE4 * ratio;
System.out.printf("The shipping charges is $%.2f\n", charges);
}
}
}

Potrebbero piacerti anche