Assignment #3



CSE 1321L: Programming and Problem Solving I Lab

Assignment 4

Loops

Assignment Outcomes

By completing this assignment, students will be able to:

1) Design programs that leverage loops to solve problems

2) Apply concepts from earlier in the semester, including conditional statements

3) Generate and use random numbers

4) Solve problems of increasing complexity

Program 0 (Warm up): Gimme a cookie! In the early days of computing, you likely couldn’t afford your own computer. Instead, you had to sit at a terminal – which was literally just a monochrome monitor and keyboard that was connected to a big mainframe machine (hidden somewhere in the building). When you needed a break, you were supposed to log out. Not everyone did and later fell victim to the old “Gimme a cookie” prank. While the person was away, someone else would come along and write a quick program that said “Gimme a cookie: ". When the person returned, they saw this on their terminal. After repeatedly typing in random things (like passwords), they would eventually realize they needed to type the word “cookie” to regain control of their terminal. It’s quite funny, or at least it was 30 years ago. Design (pseudocode) and implement (source code) for this simple program.

Sample run 1:

Gimme a cookie: What?

Gimme a cookie: I don't understand

Gimme a cookie: Who is this?

Gimme a cookie: help?

Gimme a cookie: ls -l

Gimme a cookie: ^C

Gimme a cookie: ^D

Gimme a cookie: ESC

Gimme a cookie: cookie

Don’t leave your terminal open, fool!

Program 1: The Five Days of Finals. Let’s create a song, shall we? Using only a single for loop, a switch/case statement and in less than 20 lines of code, design (pseudocode) and implement (source code) a program that prints something similar to the “Twelve Days of Xmas” (Google it), but for the five days of final exams. You may NOT have duplicate text in your code (e.g. multiple copies of “a student programming at 3AM”. And yes, we know the first line is not grammatically correct.

Sample run 1:

On the 1 day of Final Exams my professor gave to me

a student programming at 3AM!

On the 2 day of Final Exams my professor gave to me

Two fanboys fanning, and

a student programming at 3AM!

On the 3 day of Final Exams my professor gave to me

Three resistors smoking,

Two fanboys fanning, and

a student programming at 3AM!

On the 4 day of Final Exams my professor gave to me

Four admins groaning,

Three resistors smoking,

Two fanboys fanning, and

a student programming at 3AM!

On the 5 day of Final Exams my professor gave to me

Five nerds a-geeking,

Four admins groaning,

Three resistors smoking,

Two fanboys fanning, and

a student programming at 3AM!

Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the maximum damage the player does, and then subtract that from the monster. A similar thing happens when the monster attacks the hero. The program should display rounds and the HP of the hero and monster each round. If the hero or monster dies, it should print that this happened and should NOT continue (i.e. no extra text). To learn how to create random numbers, see the appendix.

Sample run 1:

Enter the hero's initial hit points: 50

Enter the damage the hero’s weapon does per strike: 20

Enter the monster's initial hit points: 40

Enter the monster's damage per strike: 15

====== ROUND 1 ======

Hero attacks for: 10

Monster has 30 HP left

Monster attacks you for: 1

You have 49 HP left

====== ROUND 2 ======

Hero attacks for: 18

Monster has 12 HP left

Monster attacks you for: 7

You have 42 HP left

====== ROUND 3 ======

Hero attacks for: 0

Monster has 12 HP left

Monster attacks you for: 14

You have 28 HP left

====== ROUND 4 ======

Hero attacks for: 18

Monster has -6 HP left

The monster dies and you earn 5 XP

Battle ends...

Sample run 2:

Enter the hero's initial hit points: 50

Enter the damage the hero’s weapon does per strike: 10

Enter the monster's initial hit points: 40

Enter the monster's damage per strike: 20

====== ROUND 1 ======

Hero attacks for: 1

Monster has 39 HP left

Monster attacks you for: 6

You have 44 HP left

====== ROUND 2 ======

Hero attacks for: 5

Monster has 34 HP left

Monster attacks you for: 1

You have 43 HP left

====== ROUND 3 ======

Hero attacks for: 8

Monster has 26 HP left

Monster attacks you for: 8

You have 35 HP left

====== ROUND 4 ======

Hero attacks for: 4

Monster has 22 HP left

Monster attacks you for: 5

You have 30 HP left

====== ROUND 5 ======

Hero attacks for: 7

Monster has 15 HP left

Monster attacks you for: 1

You have 29 HP left

====== ROUND 6 ======

Hero attacks for: 7

Monster has 8 HP left

Monster attacks you for: 9

You have 20 HP left

====== ROUND 7 ======

Hero attacks for: 0

Monster has 8 HP left

Monster attacks you for: 14

You have 6 HP left

====== ROUND 8 ======

Hero attacks for: 4

Monster has 4 HP left

Monster attacks you for: 11

You have -5 HP left

You are killed by the monster and lose 10 gold.

Battle ends...

Program 3: What’s in Your Wallet?™. Face it, debt is bad and it can be hard to get out of. If you don’t understand why, you’re about to! This is a serious life lesson, so pay attention! A credit card has an “APR” – or Annual Percentage Rate that can be divided by 12 to get a Monthly Percentage Rate (MPR). And don’t forget that these are percentages, so you have to divide through by 100. When you purchase something, that is your loan amount (called the “principle”). Often, credit cards will let you make a “minimum payment”, which is the MPR * original loan amount. So, if you have an original loan of $1000 at 12% APR, that’s $1000 * 1%(MPR) * 0.01 = $10. Of course, you can pay more than that, and the difference goes towards knocking down the loan. So, if you paid $50 per month, $40 of it would go towards the original $1000 loan, leaving $960. Interestingly, in month two, you pay less interest and more of it goes towards the loan! Why? In our example, $960 * 1%(MPR)*0.01 = $9.60 of interest, so $40.40 goes towards the loan, leaving $919.60. Note, you’ve now paid $19.60 in interest.

Your task is to design (pseudocode) and implement (source code) a program that displays a month-by-month running total of loan payments, including how much is left on the loan, the interest paid for that month, the total interest so far, and the amount that is going towards the principle. HINT: start by making sure you’ve calculated the minimum monthly payment correctly. Then, start your loop that should reduce the loan amount per month…

Note: once you get it working, try running a standard 29.9% interest rate on $1000 making a $30 monthly payment. Learn anything? Or, trying looking at your house payment!

Sample run 1:

Enter your credit card's interest rate: 29.9

That's a monthly interest rate of: 2.4916666

How much are you about to charge? $1000

Minimum monthly payment is: 24.916664

Finally, how much will you pay per month? $100

===== MONTH 1 =====

Loan: $1000.0

Interest this month: $24.916664

Interest so far: $24.916664

Amount towards principle: $75.083336

===== MONTH 2 =====

Loan: $924.9167

Interest this month: $23.04584

Interest so far: $47.9625

Amount towards principle: $76.95416

===== MONTH 3 =====

Loan: $847.9625

Interest this month: $21.128399

Interest so far: $69.0909

Amount towards principle: $78.8716

===== MONTH 4 =====

Loan: $769.09094

Interest this month: $19.163183

Interest so far: $88.25408

Amount towards principle: $80.836815

===== MONTH 5 =====

Loan: $688.25415

Interest this month: $17.148998

Interest so far: $105.403076

Amount towards principle: $82.851

===== MONTH 6 =====

Loan: $605.40314

Interest this month: $15.084628

Interest so far: $120.4877

Amount towards principle: $84.915375

===== MONTH 7 =====

Loan: $520.4878

Interest this month: $12.968821

Interest so far: $133.45653

Amount towards principle: $87.03118

===== MONTH 8 =====

Loan: $433.4566

Interest this month: $10.800293

Interest so far: $144.25682

Amount towards principle: $89.19971

===== MONTH 9 =====

Loan: $344.2569

Interest this month: $8.577734

Interest so far: $152.83455

Amount towards principle: $91.422264

===== MONTH 10 =====

Loan: $252.83463

Interest this month: $6.2997956

Interest so far: $159.13434

Amount towards principle: $93.7002

===== MONTH 11 =====

Loan: $159.13443

Interest this month: $3.9650996

Interest so far: $163.09944

Amount towards principle: $96.0349

You make one final payment of $63.099533

Program 4: Nested Loops. Design (pseudocode) and write (source code) a program that asks the user for a number and counts from 1 to that number – listing out all numbers that could be factors for that number. It’s probably best just to look at the example below…

Sample Output

Enter a number to count up to: 10

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

1 is divisible by 1

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

2 is divisible by 1

2 is divisible by 2

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

3 is divisible by 1

3 is divisible by 3

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

4 is divisible by 1

4 is divisible by 2

4 is divisible by 4

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

5 is divisible by 1

5 is divisible by 5

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

6 is divisible by 1

6 is divisible by 2

6 is divisible by 3

6 is divisible by 6

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

7 is divisible by 1

7 is divisible by 7

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

8 is divisible by 1

8 is divisible by 2

8 is divisible by 4

8 is divisible by 8

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

9 is divisible by 1

9 is divisible by 3

9 is divisible by 9

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

10 is divisible by 1

10 is divisible by 2

10 is divisible by 5

10 is divisible by 10

Submission:

Part 1: Pseudocode:

1. Review the assignment submission requirements and grading guidelines.

2. Upload the pseudocode files (Word doc or PDF) to the assignment submission folder in D2L.

3. The files must be uploaded to D2L by the due date.

4. The Pseudocode must be complete and following the standards listed on the FYE website.

Part 2: Source Code:

1. Review the assignment submission requirements and grading guidelines.

2. Upload the source code files to the assignment submission folder in D2L.

3. The files must be uploaded to D2L by the due date.

Appendix

Random numbers: for most languages, you have a special function you can call to get a random number. Often, this returns a value between 0.0-1.0 that you then have to multiply by the range you want. For example, if you multiply a number between 0.0-1.0 by 30, you get a result between 0.0-30.0. However, because Java and C# are Object-Oriented Programming languages, there are special objects you have work with, each of which have multiple (useful) methods. Try running the programs below.

In Java, it looks like (import java.util.Random;) :

Random generator = new Random();

float randNum = generator.nextFloat();

int randNum2 = generator.nextInt(10);

System.out.println (randNum + " " + randNum2);

While in C#, it looks like:

Random generator = new Random();

int randNum = generator.Next(1, 10); // 1-10

float randNum2 = (float)generator.NextDouble()*30; // 0.0-30.0

Console.WriteLine(randNum+" "+randNum2);

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download