comp110l intro to algorithms & programming lab

63
COMP110L ©2016-21 Jeff Drobman ©2016-19 Jeff Drobman Dr Jeff Drobman Dr Jeff Software drjeffsoftware.com Intro to Algorithms & Programming LAB Part 1 email [email protected] website COMP110

Upload: others

Post on 13-Apr-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff Drobman©2016-19 Jeff Drobman

Dr Jeff DrobmanDr Jeff Softwaredrjeffsoftware.com

Intro to Algorithms & Programming

LABPart 1

email [email protected]

website

COMP110

Page 2: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanIndex

vLabs à slide 3vLab 1 à slide 4vLab 2 à slide 18vLab 3 à slide 34vProject 1 à slide 53

Page 3: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab Programs1. Hello World (I/O)2. Temperature conversion (IF-THEN, numerics, formatted output)3. Guess Secret Name (Input, IF-THEN, loops)4. Palindromes/Anagrams (strings, methods)5. Homonyms (strings, methods, arrays, files)6. Prime numbers (algorithms, loops, methods, arrays, files)7. Cryptography/blockchains (algorithms, methods)8. Tic-Tac-Toe (arrays, methods, formatted output, Classes)9. Bowling League (arrays, files, methods, stats, Classes)10. Calendar (algorithms, formatted output, Date/Time)11. Games (arrays, random numbers) à Project12. Probability (factorials-> recursion)

Page 4: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab

LAB 1

Page 5: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 1: Hello WorldRqts– OUTPUT:1) “Hello World!”2) Input Name3) “Hello Name”

PROCESS

DEBUGGING/TESTING

INPUT:Name

as intended?

correct program?

OUTPUT:1) console2) GUI

1) Output ”Hello World!”2) Input <name>3) Output “Hello <name>”

Page 6: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanComparison: “Hello World”#include <stdio.h>int main (void) {printf(“Hello world!\n”;

}

#include <iostream>int main () {std::cout << “Hello world!\n”;

}

public class helloWorld {public static void main (String[] args) {system.out.println (“Hello world!”);

} }

//myfile.jsConsole.log(“Hello world!”);

C

C++

Java

Javascript

Lab 1

Print “Hello world!”Python

Page 7: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanComparison: “Hello World”

Basic

VB

C#

note: line numbers!

OOP + GUI

OOP + console

DOS script (for console)

Page 8: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanComparison: “Hello World”

Assembly

PHPØ all console

Page 9: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanHello World – ConsoleJava

javax.swing.JOptionPane.showMessageDialog

header

main

consoleout

Ø Console Lab 1

Page 10: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanHello World – ConsoleLab 1zyLabs

Page 11: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanHello World – ConsoleLab 1zyLabs

I OP

Ø I supply “starter” source code here

Ø You supply YOUR source code here

Page 12: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanzyLab 1 Tests

Page 13: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanGUI: “Hello World”

//add ref to GUI classimport javax.swing.JOptionPane;//main codepublic class helloWorld {public static void main (String[] args) {JOptionPane.showMessageDialog(null, “Hello world!”);

}}

Java

javax.swing.JOptionPane.showMessageDialog

import javax.swing.*;-OR-

Ø GUI

Lab 1

Page 14: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanHello World – Combined

Macversion

Ø Console

Ø GUI

Lab 1

Page 15: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanHello World + InputØ Console Lab 1

Page 16: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanHello World – PlusØ Console Lab 1

v Math extra

Page 17: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 1 FormRequirements1. Print “Hello World” on both console and GUI box2. Input (console) your name3. Print “Hello <your name>” on both console and GUI box

Inputs

Outputs

Ø ConsoleØ GUI

Lab 1

Page 18: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab

LAB 2

Page 19: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 2: Temp ConvRqts– INPUT:(see Input)

PROCESS (source code)

DEBUGGING/TESTING

as intended?

correct program?

1) Input2) Conversions3) Output results

OUTPUT:1) new tempsà in 4 dif formats

Ø GUI Ø Console

INPUT:1) Use givens2) user tempØ GUI

OUTPUT:new temps in format:1) double2) float3) fixed-pt N.nn4) printf (“%10.2f”..

v Submit BOTH C->F & F->Cv Use Methods

v GUI extra credit

2 parts!

Ø Console

v Challenge ActivityPowers of 2

Page 20: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanStructure (Macro)

MAINmethod

FtoCmethod

CtoFmethod

MAIN Class

Minimumrequired

MAIN Class = any nameOOPStructures

Execution isby call sequence

Classes/methodsMAIN Method = “main”

Lab 2

v Place “main” methodFIRST

Page 21: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanFlow Chart

CALLFtoCCtoF

STOP

FtoC

C = f(F)

RETURNC

START

Given C & FInitial values

Output in 4 formats

Double Function

FLOW CHART

Loop

Lab 2

CtoF

F = f(C)

RETURNF

Double FunctionNew values GUI Input C

CALLCtoF

Output F in 4 formats

Repeat

v Loop extra credit

Part 1

Part 2

zyLab

Page 22: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanPart 2: GUI Input of C

console output

Lab 2

Page 23: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanGUI Input Extended

can combine

check type

Lab 2

Page 24: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanTemp Conversion

F = C * (9/5) + 32C = (F-32) * (5/9)

q(9/5) = 1.8 q(5/9) = 0.555…

àhow to represent a fixed-point number (literals vs. vars)àhow to use mixed types in expressionsàhow to truncate (and round) extra digitsàhow to use formatted output

exact valuerepeating decimal

v What you should learn

Lab 2

§ Double > Float > Long > Int > Short > Byte

Page 25: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab: Type ConversionsvJava truncates Integers

Ø to Round add 0.5vExpressions

Ø mixed types resolve to highest precision operand

vCastingq Implicit

q Explicit

5.0/9 à 0.55555/9 à 0

5/9 à 05/9 + 0.5 à 1.0555 à 1

int i = 1.23 à 1int i = 1.23e+12 à errorfloat x = 1.23 à 1.23byte x = 128 à error

float f = 1.23 à 1.23e0int i = f + 1.23 à error?int i = (int) f à 1int i = (int) 1.23e+12 à errorlong i = (int) 1.23e+12 à 1,230,000,000,000

Lab 2

§ Double > Float > Long > Int > Short > Byte

Page 26: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanFormatted Precision123.45

(int) (x * 100) / 100.0

(int) (x * 1000) / 1000.0

123.456

Lab 2

Page 27: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanFormatted Output

String.format(%10.2f, variable) Sec 10.10.7p. 390

Lab 2

Ø GUI

Ø Console

Page 28: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanFormatted OutputLab 2

Page 29: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanMethods in Java

public static double FtoC(double ftemp) {double ctemp = (ftemp-32) *5/9.0;return ctemp;

}

vSignaturevCallvParameter passing

Ø By ValuevReturn

Ø Sub -> voidØ Function -> value

public static void main(String[ ], args) {<statements>if ($F2C) tempC = FtoC(fahr);else tempF = CtoF(celc);

}

public static double CtoF(double ctemp) {double ftemp = ctemp *9/5.0 + 32;return ftemp;

}

Fahr ó CelcCh 6

Ø CALL methods

Page 30: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanPreferred Output

extra

extra

fix this

fix

Lab 2Ø Console

Ø Inputs

Page 31: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanCode – Error/FixLab 2

fix

Page 32: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanCode – Complete

call methods

convert types

Lab 2Ø Console

Page 33: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanExample GUI OutputLab 2

String.format(%10.2f, variable)Ø Add GUI

Page 34: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab

LAB 3

Page 35: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 3: Secret NameRqts– INPUT:(see Input)

PROCESS (source code)

DEBUGGING/TESTING

as intended?

correct program?

Ø Input secret nameØ Player select name*Loop (<=3 or unlimited)1) Input guess2) Output results3) Exit loop if WIN4) Ask “Play again?”*

OUTPUT:(see Output)

1) console2) GUI

Ø GUI

INPUT:1) admin: secret name2) user: select player3) user: guesses

OUTPUT:1) correct–Win2) not–# guesses3) 0 guesses–Lose

Part 1: 3 guesses-–ZyLabPart 2: play again?—jGRASP

v Next player select from GUI Box

I/O:1) admin: either2) user: GUI

*Part 2

+8 points

Page 36: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanStructure (Macro)

MAINmethod

Guess Handlermethod

MAIN Class

Minimumrequired

MAIN Class = any nameOOPStructures

Execution isby call sequence

Classes/methodsMAIN Method = “main”

Lab 3

v Place “main” methodFIRST

Ø Optional – extra credit

Page 37: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanStructure (Loops)

MAINLoop

MAIN MethodLoopStructures

Lab 3

InnerLoop

3 guesses

Play again?

Reset secret name?Reset game

Done

Page 38: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 3 Form View

make thisdisappear update

Lab 3

Enter your guessENTER buttons

Text boxes

Adminarea

button

List box

clear after wrong guess

Lab 3

Page 39: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 3: Guess Secret Name

CALLTEST

Correct?

incr: guess++

STOP

Y

N

TEST

guess>NUM? GOTO A

N

Y

Correct Guess? N

Clear Win flag

SET Win flagY

RETURN

START

input:secret name

input:user guess

console

GUI

output:“Correct-WIn”

STOP

init: NUM=3, guess=1

Aoutput:#guesses left

output:“You Lose”

Win flag

RETURN

Boolean Function

FLOW CHART

testguesses left

FORLoop

Lab 3 3 guessesWIN or LOSE

Page 40: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 3: Guess Secret Name

CALLTEST

Correct?

STOP

Y

N

TEST

Cont? GOTO A

N

Y

Correct Guess? N

Clear Win flag

SET Win flagY

RETURN

START

input:secret name

input:user guess

console

GUI

output:“Correct-WIn”

STOP

A

output:“Good-bye”

Win flag

RETURN

Boolean Function

FLOW CHART

input:user guess again?

continuousLoop

While (run)

Lab 3 Unlimited guessesWIN or Quit (no LOSE)

Page 41: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanStop vs. Break

vBreak Loopq Either For or While loopsq break;q continue;

vStopq Program reaches endq System.exit(0);

Lab 3

Page 42: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanControl ConstructsLab 3

init test adjustment

FLAG

true

false

v3 cases1) Win2) Lose (part 1 only)3) Neither à continue

Page 43: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanKey Variables

“Flags”

Global Strings

Inits Lab 3

Counts

Page 44: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanEnter Guesses: Loops

Part 1

Lab 3

Part 2

zyLab

jGRASP +8 points

Page 45: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanHandle GuessCheck Win/Lose

Ø String.equalsIgnoreCase

Lab 3

Page 46: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanI/O: GuessesMac version Lab 3

Ø GUI only on jGRASP! Ø Replace with Console input on zyLab

Page 47: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanEnter Player NameLab 3 Part 2

Mac version

Page 48: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab 3 Extra OutputsMac versionCheck input Lab 3 Part 2– extra

Page 49: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanCheck for Valid InputPart 2

OK for part 2?check input

Lab 3

+3 points

Page 50: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanI/O: Ask“Input” boxes

“Confirm” box

Part 2Lab 3

Page 51: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanAsk Continue?Switch Ø continuing in “WHILE” loop

end “WHILE” loop

Part 2Lab 3Enumerative version: all cases

Page 52: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanOther ExamplesWin PC version

“Confirm” box

“Input” box

“Console” input

Lab 3

Page 53: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanLab

Project 1

Page 54: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanProjectsvProject 1: Embedded Control

Ø Thermostat à use Temp Conversionq Others

§ TV remote§ Car transmission/acceleration§ Any other approved application

vProject 2: SimulationØ Card game à use “Shuffling”

§ Blackjack§ Poker (pick a variety)§ Thermonuclear War

q Others§ Weather à use Temp Conversion§ Stock Market à ref my app (SMM)§ US Economy (GDP, CPI, etc.)

DUE AT MIDTERM

DUE AT FINAL

v Required extrasq USER GUIDE

Ø while (true)

Ø game playing§ random numbers§ monte carlo

v Required extrasq USER GUIDEq UML

v Classes

Page 55: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanProject Form

<screen shots of all possible User Inputs>

v User Guide

<screen shots of all possible Outputs>

Description

<screen shot of SOURCE CODE> v UML

Page 56: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanProject 1Hennessy & Patterson

Page 57: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanProject 1: ThermostatvRequired Functions

Ø Control buttonsq Temperature

§ Up§ Down

q Mode§ Heat§ Cool§ Off§ Auto

q Fan§ On§ Off§ Auto

Ø Displaysq Temperatures

§ Current§ Set to (2: Cool, Heat)

q Modeq Fan status

Display

Mode

Fan

Temperatures:Current: 76FSet to: 72F

Mode: CoolFan: Auto

Ø Don’t use GUIConfirm Dialogs:1 Mode/Fan/box2 Up/Dn/CF

YES

NOCANCELC/F

Temperatures:Current: 25CSet to: 22C

Mode: CoolFan: Auto

-OR-

Ø Use Switch-Case

Ø Use GUIOption Dialogs:1 Mode2 Fan3 Up/Dn/C-F

Ø Use Console – as “Display”

v Use Methods for C<->F

Ø Add Time(extra credit)

Page 58: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanExample Thermostats

Page 59: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanSample Code: GlobalsØGlobal (static) variables

Float/Double?

(Data Fields)

Float/Double?

Ø Better

Page 60: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanSample Code: I/O

ØDon’t use thisq Use Case-Switchq Use array indexing

Rotate modes:Cool àHeat àAuto àOff à

Confirm

Page 61: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanSample CodeOption

Page 62: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanSample MenusProject 1

v or just toggle F <-> C

Ø GUI “options” buttons

Page 63: COMP110L Intro to Algorithms & Programming LAB

COMP110L

©2016-21 Jeff DrobmanThermostats

v Uses thermo-expanding coil

v Hysteresis