COMP110L
©2016-19 Jeff Drobman©2016-19 Jeff Drobman
Dr Jeff DrobmanDr Jeff Softwaredrjeffsoftware.com
Intro to Algorithms & Programming
LABPart 1
email [email protected]
website
COMP110
COMP110L
©2016-19 Jeff DrobmanIndex
vLabs à slide 3vLab 1 à slide 4vLab 2 à slide 18vLab 3 à slide 34vProject 1 à slide 53
COMP110L
©2016-19 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)
COMP110L
©2016-19 Jeff DrobmanLab
LAB 1
COMP110L
©2016-19 Jeff DrobmanLab 1: Hello WorldRqts– OUTPUT:1) “Hello World”2) “Hello Name”
PROCESS
DEBUGGING/TESTING
INPUT:Name
as intended?
correct program?
OUTPUT:1) console2) GUI
1) Output ”HW”2) Input <name>3) Output “Hello <name>”
First due Past due
v Challenge Activity
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanComparison: “Hello World”
Basic
VB
C#
note: line numbers!
OOP + GUI
OOP + console
DOS script (for console)
COMP110L
©2016-19 Jeff DrobmanComparison: “Hello World”
Assembly
PHPØ all console
COMP110L
©2016-19 Jeff DrobmanHello World – ConsoleJava
javax.swing.JOptionPane.showMessageDialog
header
main
consoleout
Ø Console Lab 1
COMP110L
©2016-19 Jeff DrobmanHello World – ConsoleLab 1zyLabs
COMP110L
©2016-19 Jeff DrobmanHello World – ConsoleLab 1zyLabs
I OP
Ø I supply “starter” source code here
Ø You supply YOUR source code here
COMP110L
©2016-19 Jeff DrobmanzyLab 1 Tests
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanHello World – Combined
Macversion
Ø Console
Ø GUI
Lab 1
COMP110L
©2016-19 Jeff DrobmanHello World + InputØ Console Lab 1
COMP110L
©2016-19 Jeff DrobmanHello World – PlusØ Console Lab 1
v Math extra
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanLab
LAB 2
COMP110L
©2016-19 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
COMP110L
©2016-19 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
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanPart 2: GUI Input of C
console output
Lab 2
COMP110L
©2016-19 Jeff DrobmanGUI Input Extended
can combine
check type
Lab 2
COMP110L
©2016-19 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
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanFormatted Precision123.45
(int) (x * 100) / 100.0
(int) (x * 1000) / 1000.0
123.456
Lab 2
COMP110L
©2016-19 Jeff DrobmanFormatted Output
String.format(%10.2f, variable) Sec 10.10.7p. 390
Lab 2
Ø GUI
Ø Console
COMP110L
©2016-19 Jeff DrobmanFormatted OutputLab 2
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanPreferred Output
extra
extra
fix this
fix
Lab 2Ø Console
Ø Inputs
COMP110L
©2016-19 Jeff DrobmanCode – Error/FixLab 2
fix
COMP110L
©2016-19 Jeff DrobmanCode – Complete
call methods
convert types
Lab 2Ø Console
COMP110L
©2016-19 Jeff DrobmanExample GUI OutputLab 2
String.format(%10.2f, variable)Ø Add GUI
COMP110L
©2016-19 Jeff DrobmanLab
LAB 3
COMP110L
©2016-19 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: unlimited—jGRASP
v Next player select from GUI Box
I/O:1) admin: console2) user: GUI
*Part 2
COMP110L
©2016-19 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
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanExample Requirements3 guesses
Unlimited guesses
Lab 3
?
COMP110L
©2016-19 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
COMP110L
©2016-19 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)
COMP110L
©2016-19 Jeff DrobmanStop vs. Break
vBreak Loopq Either For or While loopsq break;q continue;
vStopq Program reaches endq System.exit(0);
Lab 3
COMP110L
©2016-19 Jeff DrobmanControl ConstructsLab 3
init test adjustment
FLAG
true
false
v3 cases1) Win2) Lose (part 1 only)3) Neither à continue
COMP110L
©2016-19 Jeff DrobmanKey Variables
“Flags”
Global Strings
Inits Lab 3
Counts
COMP110L
©2016-19 Jeff DrobmanEnter Guesses: LoopPart 1
Lab 3
Part 2
zyLab
jGRASP
COMP110L
©2016-19 Jeff DrobmanHandle GuessCheck Win/Lose
Ø String.equalsIgnoreCase
Lab 3
COMP110L
©2016-19 Jeff DrobmanI/O: GuessesMac version Lab 3 Part 1
Ø GUI only on jGRASP! Ø Replace with Console input on zyLab
COMP110L
©2016-19 Jeff DrobmanEnter Player NameLab 3 Part 2
COMP110L
©2016-19 Jeff DrobmanLab 3 OutputsMac versionCheck input Lab 3 Part 2– extra
COMP110L
©2016-19 Jeff DrobmanCheck for Valid InputPart 2
OK for part 2?check input
Lab 3
COMP110L
©2016-19 Jeff DrobmanI/O: AskMac version“Input” boxes
“Confirm” box
Part 2Lab 3
COMP110L
©2016-19 Jeff DrobmanAsk Continue?Switch Ø continuing in “WHILE” loop
end “WHILE” loop
Part 2Lab 3Enumerative version: all cases
COMP110L
©2016-19 Jeff DrobmanOther ExamplesWin PC version
“Confirm” box
“Input” box
“Console” input
Part 2Lab 3
COMP110L
©2016-19 Jeff DrobmanLab
Project 1
COMP110L
©2016-19 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
COMP110L
©2016-19 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
COMP110L
©2016-19 Jeff DrobmanProject 1Hennessy & Patterson
COMP110L
©2016-19 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)
COMP110L
©2016-19 Jeff DrobmanExample Thermostats
COMP110L
©2016-19 Jeff DrobmanSample Code: GlobalsØGlobal (static) variables
Float/Double?
(Data Fields)
Float/Double?
Ø Better
COMP110L
©2016-19 Jeff DrobmanSample Code: I/O
ØDon’t use thisq Use Case-Switchq Use array indexing
Rotate modes:Cool àHeat àAuto àOff à
Confirm
COMP110L
©2016-19 Jeff DrobmanSample CodeOption
COMP110L
©2016-19 Jeff DrobmanSample MenusProject 1
v or just toggle F <-> C
Ø GUI “options” buttons
COMP110L
©2016-19 Jeff DrobmanThermostats
v Uses thermo-expanding coil
v Hysteresis