Assignment #3 - Kennesaw State University



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!

import java.util.Random;

import java.util.Scanner;

class Main {

public static void main(String[] args) {

String userInput = "";

Scanner scan = new Scanner (System.in);

while (!userInput.equals("cookie")) {

System.out.print ("Gimme a cookie: ");

userInput = scan.nextLine();

}

System.out.println ("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!

class Main {

public static void main(String[] args) {

for (int i = 1; i 0)&&(monsterHP > 0)) {

System.out.println ("====== ROUND "+round+++" ======");

// Player attack

int playerAttackValue = generator.nextInt(weaponDamage);

System.out.println ("Hero attacks for: " + playerAttackValue);

monsterHP -= playerAttackValue;

System.out.println ("Monster has " + monsterHP + " HP left");

if (monsterHP ................
................

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

Google Online Preview   Download