Merge pull request #60 from TiagoRG/dev-tiagorg

[LSD] pratica08 part1 added (unchanged)

[POO] slides added
[POO] guide for aula11 added
[POO] aula11 ex1 added
[POO] aula11 ex2 added
[POO] sum and media added to MathTools
[POO] aula11 ex3 added
[POO] simplifications to WordPairCounter
[POO] aula11 ex4 added
This commit is contained in:
Tiago Garcia 2023-05-09 17:21:58 +01:00 committed by GitHub
commit b278435c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 1973 additions and 0 deletions

View File

@ -0,0 +1,28 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity Bin7SegDecoder is
port(binInput : in std_logic_vector(3 downto 0);
decOut_n : out std_logic_vector(6 downto 0));
end Bin7SegDecoder;
architecture Behavioral of Bin7SegDecoder is
begin
with binInput select
decOut_n <= "1111001" when "0001", --1
"0100100" when "0010", --2
"0110000" when "0011", --3
"0011001" when "0100", --4
"0010010" when "0101", --5
"0000010" when "0110", --6
"1111000" when "0111", --7
"0000000" when "1000", --8
"0010000" when "1001", --9
"0001000" when "1010", --A
"0000011" when "1011", --b
"1000110" when "1100", --C
"0100001" when "1101", --d
"0000110" when "1110", --E
"0001110" when "1111", --F
"1000000" when others; --0
end Behavioral;

View File

@ -0,0 +1,878 @@
/*
WARNING: Do NOT edit the input and output ports in this file in a text
editor if you plan to continue editing the block that represents it in
the Block Editor! File corruption is VERY likely to occur.
*/
/*
Copyright (C) 1991-2015 Altera Corporation. All rights reserved.
Your use of Altera Corporation's design tools, logic functions
and other software and tools, and its AMPP partner logic
functions, and any output files from any of the foregoing
(including device programming or simulation files), and any
associated documentation or information are expressly subject
to the terms and conditions of the Altera Program License
Subscription Agreement, the Altera Quartus Prime License Agreement,
the Altera MegaCore Function License Agreement, or other
applicable license agreement, including, without limitation,
that your use is for the sole purpose of programming logic
devices manufactured by Altera and sold by Altera or its
authorized distributors. Please refer to the applicable
agreement for further details.
*/
(header "graphic" (version "1.4"))
(pin
(input)
(rect -48 264 128 280)
(text "INPUT" (rect 133 0 174 11)(font "Arial" (font_size 6)))
(text "CLOCK_50" (rect 9 0 75 14)(font "Arial" ))
(pt 176 8)
(drawing
(line (pt 92 12)(pt 117 12))
(line (pt 92 4)(pt 117 4))
(line (pt 121 8)(pt 176 8))
(line (pt 92 12)(pt 92 4))
(line (pt 117 4)(pt 121 8))
(line (pt 117 12)(pt 121 8))
)
(text "VCC" (rect 136 7 160 18)(font "Arial" (font_size 6)))
(annotation_block (location)(rect -128 280 -48 312))
)
(pin
(input)
(rect -48 560 128 576)
(text "INPUT" (rect 133 0 174 11)(font "Arial" (font_size 6)))
(text "KEY[3..0]" (rect 9 0 83 14)(font "Arial" ))
(pt 176 8)
(drawing
(line (pt 92 12)(pt 117 12))
(line (pt 92 4)(pt 117 4))
(line (pt 121 8)(pt 176 8))
(line (pt 92 12)(pt 92 4))
(line (pt 117 4)(pt 121 8))
(line (pt 117 12)(pt 121 8))
)
(text "VCC" (rect 136 7 160 18)(font "Arial" (font_size 6)))
(annotation_block (location)(rect -136 576 -48 608))
)
(pin
(output)
(rect 1352 248 1528 264)
(text "OUTPUT" (rect 1 0 50 11)(font "Arial" (font_size 6)))
(text "HEX5[6..0]" (rect 90 0 172 14)(font "Arial" ))
(pt 0 8)
(drawing
(line (pt 0 8)(pt 52 8))
(line (pt 52 4)(pt 78 4))
(line (pt 52 12)(pt 78 12))
(line (pt 52 12)(pt 52 4))
(line (pt 78 4)(pt 82 8))
(line (pt 82 8)(pt 78 12))
(line (pt 78 12)(pt 82 8))
)
(annotation_block (location)(rect 1528 264 1624 296))
)
(pin
(output)
(rect 1352 336 1528 352)
(text "OUTPUT" (rect 1 0 50 11)(font "Arial" (font_size 6)))
(text "HEX4[6..0]" (rect 90 0 172 14)(font "Arial" ))
(pt 0 8)
(drawing
(line (pt 0 8)(pt 52 8))
(line (pt 52 4)(pt 78 4))
(line (pt 52 12)(pt 78 12))
(line (pt 52 12)(pt 52 4))
(line (pt 78 4)(pt 82 8))
(line (pt 82 8)(pt 78 12))
(line (pt 78 12)(pt 82 8))
)
(annotation_block (location)(rect 1528 352 1624 384))
)
(pin
(output)
(rect 1352 424 1528 440)
(text "OUTPUT" (rect 1 0 50 11)(font "Arial" (font_size 6)))
(text "HEX3[6..0]" (rect 90 0 172 14)(font "Arial" ))
(pt 0 8)
(drawing
(line (pt 0 8)(pt 52 8))
(line (pt 52 4)(pt 78 4))
(line (pt 52 12)(pt 78 12))
(line (pt 52 12)(pt 52 4))
(line (pt 78 4)(pt 82 8))
(line (pt 82 8)(pt 78 12))
(line (pt 78 12)(pt 82 8))
)
(annotation_block (location)(rect 1528 440 1616 472))
)
(pin
(output)
(rect 1352 512 1528 528)
(text "OUTPUT" (rect 1 0 50 11)(font "Arial" (font_size 6)))
(text "HEX2[6..0]" (rect 90 0 172 14)(font "Arial" ))
(pt 0 8)
(drawing
(line (pt 0 8)(pt 52 8))
(line (pt 52 4)(pt 78 4))
(line (pt 52 12)(pt 78 12))
(line (pt 52 12)(pt 52 4))
(line (pt 78 4)(pt 82 8))
(line (pt 82 8)(pt 78 12))
(line (pt 78 12)(pt 82 8))
)
(annotation_block (location)(rect 1528 528 1624 560))
)
(pin
(output)
(rect 520 192 696 208)
(text "OUTPUT" (rect 1 0 50 11)(font "Arial" (font_size 6)))
(text "LEDG[8]" (rect 90 0 147 14)(font "Arial" ))
(pt 0 8)
(drawing
(line (pt 0 8)(pt 52 8))
(line (pt 52 4)(pt 78 4))
(line (pt 52 12)(pt 78 12))
(line (pt 52 12)(pt 52 4))
(line (pt 78 4)(pt 82 8))
(line (pt 82 8)(pt 78 12))
(line (pt 78 12)(pt 82 8))
)
(annotation_block (location)(rect 608 208 696 240))
)
(symbol
(rect 320 240 464 320)
(text "ClkDividerN" (rect 5 0 95 14)(font "Arial" ))
(text "clk_divider_50" (rect 8 64 123 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "clkIn" (rect 0 0 41 14)(font "Arial" ))
(text "clkIn" (rect 21 27 62 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32))
)
(port
(pt 144 32)
(output)
(text "clkOut" (rect 0 0 49 14)(font "Arial" ))
(text "clkOut" (rect 82 27 123 41)(font "Arial" ))
(line (pt 144 32)(pt 128 32))
)
(parameter
"divFactor"
"50"
""
(type "PARAMETER_SIGNED_DEC") )
(drawing
(rectangle (rect 16 16 128 64))
)
(annotation_block (parameter)(rect -48 192 244 234))
)
(symbol
(rect 560 440 712 552)
(text "ControlUnit" (rect 5 0 95 14)(font "Arial" ))
(text "control_unit" (rect 8 96 107 110)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "clk" (rect 0 0 24 14)(font "Arial" ))
(text "clk" (rect 21 27 45 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32))
)
(port
(pt 0 48)
(input)
(text "reset" (rect 0 0 41 14)(font "Arial" ))
(text "reset" (rect 21 43 62 57)(font "Arial" ))
(line (pt 0 48)(pt 16 48))
)
(port
(pt 0 64)
(input)
(text "statop" (rect 0 0 49 14)(font "Arial" ))
(text "statop" (rect 21 59 70 73)(font "Arial" ))
(line (pt 0 64)(pt 16 64))
)
(port
(pt 0 80)
(input)
(text "laprst" (rect 0 0 49 14)(font "Arial" ))
(text "laprst" (rect 21 75 70 89)(font "Arial" ))
(line (pt 0 80)(pt 16 80))
)
(port
(pt 152 32)
(output)
(text "cntRst" (rect 0 0 49 14)(font "Arial" ))
(text "cntRst" (rect 90 27 131 41)(font "Arial" ))
(line (pt 152 32)(pt 136 32))
)
(port
(pt 152 48)
(output)
(text "cntEnb" (rect 0 0 49 14)(font "Arial" ))
(text "cntEnb" (rect 90 43 131 57)(font "Arial" ))
(line (pt 152 48)(pt 136 48))
)
(port
(pt 152 64)
(output)
(text "regEnb" (rect 0 0 49 14)(font "Arial" ))
(text "regEnb" (rect 90 59 131 73)(font "Arial" ))
(line (pt 152 64)(pt 136 64))
)
(drawing
(rectangle (rect 16 16 136 96))
)
)
(symbol
(rect 240 416 288 448)
(text "NOT" (rect 1 0 25 11)(font "Arial" (font_size 6)))
(text "inst3" (rect 3 21 44 35)(font "Arial" ))
(port
(pt 0 16)
(input)
(text "IN" (rect 2 7 13 21)(font "Courier New" (bold))(invisible))
(text "IN" (rect 2 7 13 21)(font "Courier New" (bold))(invisible))
(line (pt 0 16)(pt 13 16))
)
(port
(pt 48 16)
(output)
(text "OUT" (rect 32 7 49 21)(font "Courier New" (bold))(invisible))
(text "OUT" (rect 32 7 46 21)(font "Courier New" (bold))(invisible))
(line (pt 39 16)(pt 48 16))
)
(drawing
(line (pt 13 25)(pt 13 7))
(line (pt 13 7)(pt 31 16))
(line (pt 13 25)(pt 31 16))
(circle (rect 31 12 39 20))
)
)
(symbol
(rect 320 320 480 400)
(text "PulseGeneratorN" (rect 5 0 128 14)(font "Arial" ))
(text "pulse_generator" (rect 8 64 131 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "clkIn" (rect 0 0 41 14)(font "Arial" ))
(text "clkIn" (rect 21 27 62 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32))
)
(port
(pt 160 32)
(output)
(text "pulseOut0" (rect 0 0 74 14)(font "Arial" ))
(text "pulseOut0" (rect 77 27 139 41)(font "Arial" ))
(line (pt 160 32)(pt 144 32))
)
(port
(pt 160 48)
(output)
(text "pulseOut1" (rect 0 0 74 14)(font "Arial" ))
(text "pulseOut1" (rect 77 43 139 57)(font "Arial" ))
(line (pt 160 48)(pt 144 48))
)
(parameter
"numberSteps"
"1000000"
""
(type "PARAMETER_SIGNED_DEC") )
(parameter
"out0CompVal"
"10000"
""
(type "PARAMETER_SIGNED_DEC") )
(parameter
"out1CompVal"
"500000"
""
(type "PARAMETER_SIGNED_DEC") )
(drawing
(rectangle (rect 16 16 144 64))
)
(annotation_block (parameter)(rect -48 328 284 412))
)
(symbol
(rect 320 440 488 520)
(text "DebounceUnit" (rect 5 0 104 14)(font "Arial" ))
(text "statop_debouncer" (rect 8 64 140 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "refClk" (rect 0 0 49 14)(font "Arial" ))
(text "refClk" (rect 21 27 70 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32))
)
(port
(pt 0 48)
(input)
(text "dirtyIn" (rect 0 0 57 14)(font "Arial" ))
(text "dirtyIn" (rect 21 43 78 57)(font "Arial" ))
(line (pt 0 48)(pt 16 48))
)
(port
(pt 168 32)
(output)
(text "pulsedOut" (rect 0 0 74 14)(font "Arial" ))
(text "pulsedOut" (rect 85 27 147 41)(font "Arial" ))
(line (pt 168 32)(pt 152 32))
)
(parameter
"kHzClkFreq"
"1000"
""
(type "PARAMETER_SIGNED_DEC") )
(parameter
"mSecMinInWidth"
"100"
""
(type "PARAMETER_SIGNED_DEC") )
(parameter
"inPolarity"
"'0'"
""
(type "PARAMETER_ENUM") )
(parameter
"outPolarity"
"'1'"
""
(type "PARAMETER_ENUM") )
(drawing
(rectangle (rect 16 16 152 64))
)
(annotation_block (parameter)(rect 496 600 838 705))
)
(symbol
(rect 600 288 776 400)
(text "CntBCDUp4" (rect 5 0 79 14)(font "Arial" ))
(text "bcd_counter" (rect 8 96 98 110)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "reset" (rect 0 0 41 14)(font "Arial" ))
(text "reset" (rect 21 27 62 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32))
)
(port
(pt 0 48)
(input)
(text "clk" (rect 0 0 24 14)(font "Arial" ))
(text "clk" (rect 21 43 45 57)(font "Arial" ))
(line (pt 0 48)(pt 16 48))
)
(port
(pt 0 64)
(input)
(text "enable1" (rect 0 0 57 14)(font "Arial" ))
(text "enable1" (rect 21 59 78 73)(font "Arial" ))
(line (pt 0 64)(pt 16 64))
)
(port
(pt 0 80)
(input)
(text "enable2" (rect 0 0 57 14)(font "Arial" ))
(text "enable2" (rect 21 75 78 89)(font "Arial" ))
(line (pt 0 80)(pt 16 80))
)
(port
(pt 176 32)
(output)
(text "count[15..0]" (rect 0 0 99 14)(font "Arial" ))
(text "count[15..0]" (rect 72 27 155 41)(font "Arial" ))
(line (pt 176 32)(pt 160 32)(line_width 3))
)
(drawing
(rectangle (rect 16 16 160 96))
)
)
(symbol
(rect 824 224 1048 368)
(text "RegN" (rect 5 0 38 14)(font "Arial" ))
(text "freeze_register" (rect 8 128 131 142)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "asyncReset" (rect 0 0 82 14)(font "Arial" ))
(text "asyncReset" (rect 21 27 103 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32))
)
(port
(pt 0 48)
(input)
(text "clk" (rect 0 0 24 14)(font "Arial" ))
(text "clk" (rect 21 43 45 57)(font "Arial" ))
(line (pt 0 48)(pt 16 48))
)
(port
(pt 0 64)
(input)
(text "enable" (rect 0 0 49 14)(font "Arial" ))
(text "enable" (rect 21 59 70 73)(font "Arial" ))
(line (pt 0 64)(pt 16 64))
)
(port
(pt 0 80)
(input)
(text "syncReset" (rect 0 0 74 14)(font "Arial" ))
(text "syncReset" (rect 21 75 95 89)(font "Arial" ))
(line (pt 0 80)(pt 16 80))
)
(port
(pt 0 96)
(input)
(text "dataIn[size-1..0]" (rect 0 0 140 14)(font "Arial" ))
(text "dataIn[size-1..0]" (rect 21 91 161 105)(font "Arial" ))
(line (pt 0 96)(pt 16 96)(line_width 3))
)
(port
(pt 224 32)
(output)
(text "dataOut[size-1..0]" (rect 0 0 148 14)(font "Arial" ))
(text "dataOut[size-1..0]" (rect 78 27 203 41)(font "Arial" ))
(line (pt 224 32)(pt 208 32)(line_width 3))
)
(parameter
"size"
"16"
""
(type "PARAMETER_SIGNED_DEC") )
(drawing
(rectangle (rect 16 16 208 128))
)
(annotation_block (parameter)(rect 792 176 1084 218))
)
(symbol
(rect 1144 224 1352 304)
(text "Bin7SegDecoder" (rect 5 0 120 14)(font "Arial" ))
(text "disp_3_decoder" (rect 8 64 123 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "binInput[3..0]" (rect 0 0 115 14)(font "Arial" ))
(text "binInput[3..0]" (rect 21 27 136 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32)(line_width 3))
)
(port
(pt 208 32)
(output)
(text "decOut_n[6..0]" (rect 0 0 115 14)(font "Arial" ))
(text "decOut_n[6..0]" (rect 90 27 187 41)(font "Arial" ))
(line (pt 208 32)(pt 192 32)(line_width 3))
)
(drawing
(rectangle (rect 16 16 192 64))
)
)
(symbol
(rect 1144 312 1352 392)
(text "Bin7SegDecoder" (rect 5 0 120 14)(font "Arial" ))
(text "disp_2_decoder" (rect 8 64 123 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "binInput[3..0]" (rect 0 0 115 14)(font "Arial" ))
(text "binInput[3..0]" (rect 21 27 136 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32)(line_width 3))
)
(port
(pt 208 32)
(output)
(text "decOut_n[6..0]" (rect 0 0 115 14)(font "Arial" ))
(text "decOut_n[6..0]" (rect 90 27 187 41)(font "Arial" ))
(line (pt 208 32)(pt 192 32)(line_width 3))
)
(drawing
(rectangle (rect 16 16 192 64))
)
)
(symbol
(rect 1144 400 1352 480)
(text "Bin7SegDecoder" (rect 5 0 120 14)(font "Arial" ))
(text "disp_1_decoder" (rect 8 64 123 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "binInput[3..0]" (rect 0 0 115 14)(font "Arial" ))
(text "binInput[3..0]" (rect 21 27 136 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32)(line_width 3))
)
(port
(pt 208 32)
(output)
(text "decOut_n[6..0]" (rect 0 0 115 14)(font "Arial" ))
(text "decOut_n[6..0]" (rect 90 27 187 41)(font "Arial" ))
(line (pt 208 32)(pt 192 32)(line_width 3))
)
(drawing
(rectangle (rect 16 16 192 64))
)
)
(symbol
(rect 1144 488 1352 568)
(text "Bin7SegDecoder" (rect 5 0 120 14)(font "Arial" ))
(text "disp_0_decoder" (rect 8 64 123 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "binInput[3..0]" (rect 0 0 115 14)(font "Arial" ))
(text "binInput[3..0]" (rect 21 27 136 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32)(line_width 3))
)
(port
(pt 208 32)
(output)
(text "decOut_n[6..0]" (rect 0 0 115 14)(font "Arial" ))
(text "decOut_n[6..0]" (rect 90 27 187 41)(font "Arial" ))
(line (pt 208 32)(pt 192 32)(line_width 3))
)
(drawing
(rectangle (rect 16 16 192 64))
)
)
(symbol
(rect 320 520 488 600)
(text "DebounceUnit" (rect 5 0 104 14)(font "Arial" ))
(text "laprst_debouncer" (rect 8 64 140 78)(font "Arial" ))
(port
(pt 0 32)
(input)
(text "refClk" (rect 0 0 49 14)(font "Arial" ))
(text "refClk" (rect 21 27 70 41)(font "Arial" ))
(line (pt 0 32)(pt 16 32))
)
(port
(pt 0 48)
(input)
(text "dirtyIn" (rect 0 0 57 14)(font "Arial" ))
(text "dirtyIn" (rect 21 43 78 57)(font "Arial" ))
(line (pt 0 48)(pt 16 48))
)
(port
(pt 168 32)
(output)
(text "pulsedOut" (rect 0 0 74 14)(font "Arial" ))
(text "pulsedOut" (rect 85 27 147 41)(font "Arial" ))
(line (pt 168 32)(pt 152 32))
)
(parameter
"kHzClkFreq"
"1000"
""
(type "PARAMETER_SIGNED_DEC") )
(parameter
"mSecMinInWidth"
"100"
""
(type "PARAMETER_SIGNED_DEC") )
(parameter
"inPolarity"
"'0'"
""
(type "PARAMETER_ENUM") )
(parameter
"outPolarity"
"'1'"
""
(type "PARAMETER_ENUM") )
(drawing
(rectangle (rect 16 16 152 64))
)
(annotation_block (parameter)(rect -48 600 294 705))
)
(symbol
(rect 776 336 808 368)
(text "GND" (rect 8 16 32 27)(font "Arial" (font_size 6)))
(text "gnd_inst_1" (rect 3 21 85 35)(font "Arial" )(invisible))
(port
(pt 16 0)
(output)
(text "1" (rect 18 0 23 14)(font "Courier New" (bold))(invisible))
(text "1" (rect 18 0 23 14)(font "Courier New" (bold))(invisible))
(line (pt 16 8)(pt 16 0))
)
(drawing
(line (pt 8 8)(pt 16 16))
(line (pt 16 16)(pt 24 8))
(line (pt 8 8)(pt 24 8))
)
)
(connector
(pt 504 504)
(pt 560 504)
)
(connector
(pt 712 488)
(pt 752 488)
)
(connector
(pt 712 472)
(pt 736 472)
)
(connector
(pt 560 520)
(pt 504 520)
)
(connector
(pt 504 520)
(pt 504 552)
)
(connector
(pt 488 552)
(pt 504 552)
)
(connector
(pt 320 552)
(pt 304 552)
)
(connector
(pt 504 504)
(pt 504 472)
)
(connector
(pt 504 472)
(pt 488 472)
)
(connector
(pt 304 472)
(pt 304 552)
)
(connector
(pt 304 472)
(pt 320 472)
)
(connector
(text "regEnb" (rect 778 488 827 502)(font "Arial" ))
(pt 712 504)
(pt 808 504)
)
(connector
(pt 304 352)
(pt 320 352)
)
(connector
(pt 304 352)
(pt 304 416)
)
(connector
(pt 304 416)
(pt 304 472)
)
(connector
(pt 128 568)
(pt 176 568)
(bus)
)
(connector
(text "KEY[1]" (rect 186 552 235 566)(font "Arial" ))
(pt 320 568)
(pt 176 568)
)
(connector
(text "KEY[0]" (rect 186 472 235 486)(font "Arial" ))
(pt 320 488)
(pt 176 488)
)
(connector
(pt 736 472)
(pt 736 424)
)
(connector
(pt 752 488)
(pt 752 408)
)
(connector
(text "enb100Hz" (rect 554 336 620 350)(font "Arial" ))
(pt 480 352)
(pt 600 352)
)
(connector
(pt 752 408)
(pt 568 408)
)
(connector
(text "cntEnb" (rect 570 352 619 366)(font "Arial" ))
(pt 600 368)
(pt 568 368)
)
(connector
(pt 568 408)
(pt 568 368)
)
(connector
(pt 736 424)
(pt 552 424)
)
(connector
(text "cntRst" (rect 570 304 619 318)(font "Arial" ))
(pt 600 320)
(pt 552 320)
)
(connector
(pt 552 424)
(pt 552 320)
)
(connector
(text "REGOut[11..8]" (rect 1066 328 1173 342)(font "Arial" ))
(pt 1144 344)
(pt 1064 344)
(bus)
)
(connector
(text "REGOut[7..4]" (rect 1066 416 1165 430)(font "Arial" ))
(pt 1144 432)
(pt 1064 432)
(bus)
)
(connector
(text "REGOut[3..0]" (rect 1066 504 1165 518)(font "Arial" ))
(pt 1144 520)
(pt 1064 520)
(bus)
)
(connector
(pt 808 504)
(pt 808 288)
)
(connector
(pt 808 288)
(pt 824 288)
)
(connector
(pt 824 304)
(pt 792 304)
)
(connector
(pt 1064 256)
(pt 1064 344)
(bus)
)
(connector
(pt 1064 344)
(pt 1064 432)
(bus)
)
(connector
(text "RegOut[15..0]" (rect 1040 443 1054 550)(font "Arial" )(vertical))
(pt 1064 432)
(pt 1064 520)
(bus)
)
(connector
(pt 1064 520)
(pt 1064 544)
(bus)
)
(connector
(pt 1048 256)
(pt 1064 256)
(bus)
)
(connector
(text "REGOut[15..12]" (rect 1072 240 1187 254)(font "Arial" ))
(pt 1064 256)
(pt 1144 256)
(bus)
)
(connector
(pt 776 320)
(pt 824 320)
(bus)
)
(connector
(pt 792 256)
(pt 824 256)
)
(connector
(pt 792 256)
(pt 792 304)
)
(connector
(pt 792 304)
(pt 792 336)
)
(connector
(pt 128 272)
(pt 320 272)
)
(connector
(pt 176 432)
(pt 176 488)
(bus)
)
(connector
(pt 176 488)
(pt 176 568)
(bus)
)
(connector
(text "KEY[3]" (rect 184 416 233 430)(font "Arial" ))
(pt 176 432)
(pt 240 432)
)
(connector
(pt 288 432)
(pt 520 432)
)
(connector
(pt 304 416)
(pt 536 416)
)
(connector
(pt 536 336)
(pt 600 336)
)
(connector
(text "clk1MHz" (rect 474 256 531 270)(font "Arial" ))
(pt 464 272)
(pt 536 272)
)
(connector
(pt 536 272)
(pt 824 272)
)
(connector
(pt 536 272)
(pt 536 336)
)
(connector
(pt 480 368)
(pt 520 368)
)
(connector
(pt 536 336)
(pt 536 416)
)
(connector
(pt 536 416)
(pt 536 472)
)
(connector
(pt 536 472)
(pt 560 472)
)
(connector
(pt 520 488)
(pt 520 432)
)
(connector
(pt 520 488)
(pt 560 488)
)
(connector
(pt 520 368)
(pt 520 200)
)
(junction (pt 304 472))
(junction (pt 304 416))
(junction (pt 176 568))
(junction (pt 176 488))
(junction (pt 1064 344))
(junction (pt 1064 432))
(junction (pt 1064 520))
(junction (pt 1064 256))
(junction (pt 792 304))
(junction (pt 536 416))
(junction (pt 536 336))
(junction (pt 536 272))

View File

@ -0,0 +1,41 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
entity ClkDividerN is
generic(divFactor : positive := 2);
port(clkIn : in std_logic;
clkOut : out std_logic);
end ClkDividerN;
architecture Behavioral of ClkDividerN is
subtype TCounter is natural range 0 to (divFactor - 1);
signal s_divCounter : TCounter;
begin
assert(divFactor >= 2);
count_proc : process(clkIn)
begin
if (rising_edge(clkIn)) then
if (s_divCounter >= divFactor - 1) then
s_divCounter <= 0;
else
s_divCounter <= s_divCounter + 1;
end if;
end if;
end process;
out_proc : process(clkIn)
begin
if (rising_edge(clkIn)) then
if (s_divCounter >= (divFactor / 2 - 1)) then
clkOut <= '1';
else
clkOut <= '0';
end if;
end if;
end process;
end Behavioral;

View File

@ -0,0 +1,49 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
entity CntBCDUp4 is
port(reset : in std_logic;
clk : in std_logic;
enable1 : in std_logic;
enable2 : in std_logic;
count : out std_logic_vector(15 downto 0));
end CntBCDUp4;
architecture Behavioral of CntBCDUp4 is
signal s_count : unsigned(15 downto 0);
begin
count_proc : process(clk)
begin
if (rising_edge(clk)) then
if (reset = '1') then
s_count <= (others => '0');
elsif ((enable1 = '1') and (enable2 = '1')) then
if (s_count(3 downto 0) = X"9") then
s_count(3 downto 0) <= X"0";
if (s_count(7 downto 4) = X"9") then
s_count(7 downto 4) <= X"0";
if (s_count(11 downto 8) = X"9") then
s_count(11 downto 8) <= X"0";
if (s_count(15 downto 12) = X"9") then
s_count(15 downto 12) <= X"0";
else
s_count(15 downto 12) <= s_count(15 downto 12) + 1;
end if;
else
s_count(11 downto 8) <= s_count(11 downto 8) + 1;
end if;
else
s_count(7 downto 4) <= s_count(7 downto 4) + 1;
end if;
else
s_count(3 downto 0) <= s_count(3 downto 0) + 1;
end if;
end if;
end if;
end process;
count <= std_logic_vector(s_count);
end Behavioral;

View File

@ -0,0 +1,209 @@
/*
WARNING: Do NOT edit the input and output ports in this file in a text
editor if you plan to continue editing the block that represents it in
the Block Editor! File corruption is VERY likely to occur.
Copyright (C) 1991-2015 Altera Corporation. All rights reserved.
Your use of Altera Corporation's design tools, logic functions
and other software and tools, and its AMPP partner logic
functions, and any output files from any of the foregoing
(including device programming or simulation files), and any
associated documentation or information are expressly subject
to the terms and conditions of the Altera Program License
Subscription Agreement, the Altera Quartus Prime License Agreement,
the Altera MegaCore Function License Agreement, or other
applicable license agreement, including, without limitation,
that your use is for the sole purpose of programming logic
devices manufactured by Altera and sold by Altera or its
authorized distributors. Please refer to the applicable
agreement for further details.
Generated by Quartus Prime Version 15.1.1 Build 189 12/02/2015 SJ Lite Edition
Created on Mon Apr 11 16:06:33 2016
*/
VERSION = "3.0";
HEADER
(
GENERAL{
RMODE = "S";
RA_LEVEL = "H";
HOPT = "VHDL";
}
SPORT{
NAME = "clk";
PTYPE = "CI";
REG = "N";
OUTS = "N";
}
SPORT{
NAME = "reset";
PTYPE = "RI";
REG = "N";
OUTS = "N";
}
SPORT{
NAME = "statop";
PTYPE = "OI";
REG = "N";
OUTS = "N";
}
SPORT{
NAME = "laprst";
PTYPE = "OI";
REG = "N";
OUTS = "N";
}
SPORT{
NAME = "cntRst";
PTYPE = "OU";
REG = "N";
OUTS = "C";
}
SPORT{
NAME = "cntEnb";
PTYPE = "OU";
REG = "N";
OUTS = "C";
}
SPORT{
NAME = "regEnb";
PTYPE = "OU";
REG = "N";
OUTS = "C";
}
STATE{
NAME = "CLEARED";
STYPE = "RE";
PT = (40,80);
OUTP = "cntRst";
OUT_VALUE = "1";
COND = "";
OUTP = "cntEnb";
OUT_VALUE = "1";
COND = "";
OUTP = "regEnb";
OUT_VALUE = "1";
COND = "";
}
STATE{
NAME = "STARTED";
STYPE = "NR";
PT = (200,80);
OUTP = "cntRst";
OUT_VALUE = "0";
COND = "";
OUTP = "cntEnb";
OUT_VALUE = "1";
COND = "";
OUTP = "regEnb";
OUT_VALUE = "1";
COND = "";
}
STATE{
NAME = "STOPPED";
STYPE = "NR";
PT = (360,80);
OUTP = "cntRst";
OUT_VALUE = "0";
COND = "";
OUTP = "cntEnb";
OUT_VALUE = "0";
COND = "";
OUTP = "regEnb";
OUT_VALUE = "1";
COND = "";
}
STATE{
NAME = "LAPVIEW";
STYPE = "NR";
PT = (520,80);
OUTP = "cntRst";
OUT_VALUE = "0";
COND = "";
OUTP = "cntEnb";
OUT_VALUE = "1";
COND = "";
OUTP = "regEnb";
OUT_VALUE = "0";
COND = "";
}
TRANS{
SSTATE = "CLEARED";
DSTATE = "STARTED";
EQ = "statop";
PT = (64,79);
PT = (64,39);
PT = (224,39);
PT = (224,79);
PT = (128,32);
}
TRANS{
SSTATE = "STARTED";
DSTATE = "LAPVIEW";
EQ = "laprst";
PT = (224,79);
PT = (224,-1);
PT = (544,-1);
PT = (544,79);
PT = (368,0);
}
TRANS{
SSTATE = "STARTED";
DSTATE = "STOPPED";
EQ = "(~laprst) & statop";
PT = (224,79);
PT = (224,39);
PT = (384,39);
PT = (384,79);
PT = (352,40);
}
TRANS{
SSTATE = "LAPVIEW";
DSTATE = "STARTED";
EQ = "laprst";
PT = (544,129);
PT = (544,209);
PT = (224,209);
PT = (224,129);
PT = (440,160);
}
TRANS{
SSTATE = "STOPPED";
DSTATE = "STARTED";
EQ = "statop";
PT = (384,129);
PT = (384,169);
PT = (224,169);
PT = (224,129);
PT = (288,136);
}
TRANS{
SSTATE = "STOPPED";
DSTATE = "CLEARED";
EQ = "(~statop) & laprst";
PT = (384,129);
PT = (384,209);
PT = (64,209);
PT = (64,129);
PT = (120,160);
}
)
END

View File

@ -0,0 +1,81 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity ControlUnit is
port(reset : in std_logic;
clk : in std_logic;
statop : in std_logic;
laprst : in std_logic;
cntRst : out std_logic;
cntEnb : out std_logic;
regEnb : out std_logic);
end ControlUnit;
architecture Behavioral of ControlUnit is
type TState is (CLEARED, STARTED, STOPPED, LAPVIEW);
signal s_currentState, s_nextState : TState;
begin
sync_proc : process(clk)
begin
if (rising_edge(clk)) then
if (reset = '1') then
s_currentState <= CLEARED;
else
s_currentState <= s_nextState;
end if;
end if;
end process;
comb_proc : process(s_currentState, statop, laprst)
begin
case (s_currentState) is
when CLEARED =>
cntRst <= '1';
cntEnb <= '1';
regEnb <= '1';
if (statop = '1') then
s_nextState <= STARTED;
else
s_nextState <= CLEARED;
end if;
when STARTED =>
cntRst <= '0';
cntEnb <= '1';
regEnb <= '1';
if (laprst = '1') then
s_nextState <= LAPVIEW;
elsif (statop = '1') then
s_nextState <= STOPPED;
else
s_nextState <= STARTED;
end if;
when STOPPED =>
cntRst <= '0';
cntEnb <= '0';
regEnb <= '1';
if (statop = '1') then
s_nextState <= STARTED;
elsif (laprst = '1') then
s_nextState <= CLEARED;
else
s_nextState <= STOPPED;
end if;
when LAPVIEW =>
cntRst <= '0';
cntEnb <= '1';
regEnb <= '0';
if (laprst = '1') then
s_nextState <= STARTED;
else
s_nextState <= LAPVIEW;
end if;
end case;
end process;
end Behavioral;

View File

@ -0,0 +1,64 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
entity DebounceUnit is
generic(kHzClkFreq : positive := 50000;
mSecMinInWidth : positive := 100;
inPolarity : std_logic := '1';
outPolarity : std_logic := '1');
port(refClk : in std_logic;
dirtyIn : in std_logic;
pulsedOut : out std_logic);
end DebounceUnit;
architecture Behavioral of DebounceUnit is
constant MIN_IN_WIDTH_CYCLES : positive := mSecMinInWidth * kHzClkFreq;
subtype TCounter is natural range 0 to MIN_IN_WIDTH_CYCLES;
signal s_debounceCnt : TCounter := 0;
signal s_dirtyIn, s_previousIn, s_pulsedOut : std_logic;
begin
in_sync_proc : process(refClk)
begin
if (rising_edge(refClk)) then
if (inPolarity = '1') then
s_dirtyIn <= dirtyIn;
else
s_dirtyIn <= not dirtyIn;
end if;
s_previousIn <= s_dirtyIn;
end if;
end process;
count_proc : process(refClk)
begin
if (rising_edge(refClk)) then
if ((s_dirtyIn = '0') or
(s_debounceCnt > MIN_IN_WIDTH_CYCLES)) then
s_debounceCnt <= 0;
s_pulsedOut <= '0';
elsif (s_dirtyIn = '1') then
if (s_previousIn = '0') then
s_debounceCnt <= MIN_IN_WIDTH_CYCLES;
s_pulsedOut <= '0';
else
if (s_debounceCnt >= 1) then
s_debounceCnt <= s_debounceCnt - 1;
end if;
if (s_debounceCnt = 1) then
s_pulsedOut <= '1';
else
s_pulsedOut <= '0';
end if;
end if;
end if;
end if;
end process;
pulsedOut <= s_pulsedOut when (outPolarity = '1') else
not s_pulsedOut;
end Behavioral;

View File

@ -0,0 +1,38 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
entity PulseGeneratorN is
generic(numberSteps : positive := 8;
out0CompVal : natural := 2;
out1CompVal : natural := 4);
port(clkIn : in std_logic;
pulseOut0 : out std_logic;
pulseOut1 : out std_logic);
end PulseGeneratorN;
architecture Behavioral of PulseGeneratorN is
subtype TCounter is natural range 0 to (numberSteps - 1);
signal s_counter : TCounter;
begin
count_proc : process(clkIn)
begin
if (rising_edge(clkIn)) then
if (s_counter >= (numberSteps - 1)) then
s_counter <= 0;
else
s_counter <= s_counter + 1;
end if;
end if;
end process;
pulseOut0 <= '1' when ((s_counter rem out0CompVal) = 0) else
'0';
pulseOut1 <= '1' when (s_counter < out1CompVal) else
'0';
end Behavioral;

View File

@ -0,0 +1,30 @@
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity RegN is
generic(size : positive := 8);
port(asyncReset : in std_logic;
clk : in std_logic;
enable : in std_logic;
syncReset : in std_logic;
dataIn : in std_logic_vector((size - 1) downto 0);
dataOut : out std_logic_vector((size - 1) downto 0));
end RegN;
architecture Behavioral of RegN is
begin
reg_proc : process(asyncReset, clk)
begin
if (asyncReset = '1') then
dataOut <= (others => '0');
elsif (rising_edge(clk)) then
if (enable = '1') then
if (syncReset = '1') then
dataOut <= (others => '0');
else
dataOut <= dataIn;
end if;
end if;
end if;
end process;
end Behavioral;

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,47 @@
package aula11.ex1;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class WordPairCounter {
public static void main(String[] args) {
TreeMap<String, HashMap<String, Integer>> wordPairs = new TreeMap<>() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (String word1 : this.keySet()) {
sb.append(word1).append("={");
for (String word2 : this.get(word1).keySet()) {
sb.append(word2).append("=").append(this.get(word1).get(word2)).append(", ");
}
sb.delete(sb.length()-2, sb.length()).append("}\n");
}
return sb.toString();
}
};
String text = "";
Path path = Paths.get((new Scanner(System.in)).nextLine());
try {
text = Files.readString(path);
} catch (IOException e) {
System.out.printf("Certifique-se que o ficheiro \"%s\" está na raiz da pasta do projeto", path);
System.exit(1);
}
Object[] words = Arrays.stream(text.split("[\\s.,:';?!\\-*{}=+&/()\\[\\]”“\"]+")).filter(word -> word.length() > 2).map(String::toLowerCase).toArray();
for (int i = 1; i < words.length-1; i++) {
String word1 = (String) words[i-1];
String word2 = (String) words[i];
HashMap<String, Integer> word1Pair = wordPairs.getOrDefault(word1, new HashMap<>());
wordPairs.put(word1, word1Pair);
word1Pair.put(word2, word1Pair.getOrDefault(word2, 0) + 1);
}
System.out.println(wordPairs);
}
}

View File

@ -0,0 +1,57 @@
package aula11.ex2;
import utils.MathTools;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Gradebook implements IGradeCalculator {
private final LinkedList<Student> students = new LinkedList<>();
public void load(String filename) {
LinkedList<String> lines = new LinkedList<>();
Path path = Paths.get(filename);
try {
lines = new LinkedList<>(Files.readAllLines(path));
} catch (IOException e) {
System.out.printf("Certifique-se que o ficheiro \"%s\" está na raiz da pasta do projeto", path);
System.exit(1);
}
for (String line : lines) {
String[] data = line.split("\\|");
String name = data[0];
LinkedList<Double> grades = new LinkedList<>(Arrays.stream(data).toList().subList(1, data.length).stream().map(Double::parseDouble).toList());
students.add(new Student(name, grades));
}
}
public void addStudent(Student newStudent) {
students.add(newStudent);
}
public void removeStudent(String name) {
students.removeIf(student -> student.getName().equals(name));
}
public Student getStudent(String name) {
return students.stream().filter(student -> student.getName().equals(name)).findFirst().orElse(null);
}
public double calculateAverageGrade(String name) {
return calculate(getStudent(name).getGrades());
}
@Override
public double calculate(LinkedList<Double> grades) {
return MathTools.media(grades);
}
public void printAllStudents() {
for (Student student : students)
System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), calculate(student.getGrades()));
}
}

View File

@ -0,0 +1,32 @@
package aula11.ex2;
import java.util.LinkedList;
import java.util.List;
public class GradebookTester {
public static void main(String[] args) {
// Create a new Gradebook instance
Gradebook gradebook = new Gradebook();
// Load the student data from a text file
gradebook.load("datafiles/aula11/ex2/alunos.txt");
// Add a new student to the collection
Student newStudent = new Student("Johny May", new LinkedList<>(List.of(10.0, 15.0, 19.0)));
gradebook.addStudent(newStudent);
// Remove a student from the collection
gradebook.removeStudent("Jane Smith");
// Retrieve a student from the collection
Student student = gradebook.getStudent("John Doe");
// Calculate the average grade for a specific student
double averageGrade = gradebook.calculateAverageGrade("John Doe");
System.out.println("Average grade for John Doe: " + averageGrade);
// Print a summary of all students in the collection
gradebook.printAllStudents();
}
}

View File

@ -0,0 +1,7 @@
package aula11.ex2;
import java.util.LinkedList;
public interface IGradeCalculator {
double calculate(LinkedList<Double> grades);
}

View File

@ -0,0 +1,30 @@
package aula11.ex2;
import java.util.LinkedList;
public class Student {
private String name;
private LinkedList<Double> grades;
public Student(String name, LinkedList<Double> grades) {
this.name = name;
this.grades = grades;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LinkedList<Double> getGrades() {
return grades;
}
public void setGrades(LinkedList<Double> grades) {
this.grades = grades;
}
}

View File

@ -0,0 +1,29 @@
package aula11.ex3;
import java.util.LinkedList;
public class Customer {
private int customerId;
private LinkedList<Double> meterReadings;
public Customer(int customerId, LinkedList<Double> meterReadings) {
this.customerId = customerId;
this.meterReadings = meterReadings;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public LinkedList<Double> getMeterReadings() {
return meterReadings;
}
public void setMeterReadings(LinkedList<Double> meterReadings) {
this.meterReadings = meterReadings;
}
}

View File

@ -0,0 +1,60 @@
package aula11.ex3;
import utils.MathTools;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
public class EnergyUsageReport {
private final LinkedList<Customer> customers = new LinkedList<Customer>();
public void load(String file) {
LinkedList<String> lines = new LinkedList<>();
Path path = Paths.get(file);
try {
lines = new LinkedList<>(Files.readAllLines(path));
} catch (IOException e) {
System.out.printf("Certifique-se que o ficheiro \"%s\" está na raiz da pasta do projeto", path);
System.exit(1);
}
for (String line : lines) {
String[] data = line.split("\\|");
int customerId = Integer.parseInt(data[0]);
LinkedList<Double> meterReadings = new LinkedList<>(Arrays.stream(data).toList().subList(1, data.length).stream().map(Double::parseDouble).toList());
customers.add(new Customer(customerId, meterReadings));
}
}
public void addCustomer(Customer newCustomer) {
customers.add(newCustomer);
}
public void removeCustomer(int id) {
customers.removeIf(customer -> customer.getCustomerId() == id);
}
public Customer getCustomer(int id) {
return customers.stream().filter(customer -> customer.getCustomerId() == id).findFirst().orElse(null);
}
public double calculateTotalUsage(int id) {
return MathTools.sum(getCustomer(id).getMeterReadings());
}
public void generateReport(String path) {
for (Customer customer : customers) {
try {
Files.writeString(Path.of(path), String.format("Customer ID: %d%nTotal usage: %.1f%n%n", customer.getCustomerId(), calculateTotalUsage(customer.getCustomerId())), (new File(path)).exists() ? java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

View File

@ -0,0 +1,34 @@
package aula11.ex3;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class EnergyUsageReportTester {
public static void main(String[] args) throws IOException {
// Create an instance of the EnergyUsageReport class
EnergyUsageReport energyReport = new EnergyUsageReport();
// Load the customer data from a text file using the load() method
energyReport.load("datafiles/aula11/ex3/clients.txt");
// Add one or more customers to the collection using the addCustomer() method
Customer newCustomer = new Customer(999, new LinkedList<>(List.of(1500.0, 2000.0, 2500.0, 3000.0)));
energyReport.addCustomer(newCustomer);
// Remove one or more customers from the collection using the removeCustomer() method
energyReport.removeCustomer(1015);
// Retrieve a customer from the collection using the getCustomer() method
Customer retrievedCustomer = energyReport.getCustomer(1025);
// Calculate the total energy usage for a specific customer using the calculateTotalUsage() method
double totalEnergyUsage = energyReport.calculateTotalUsage(1003);
System.out.println("Total energy usage for customer 1003: " + totalEnergyUsage);
// Generate a report of all customers and their total energy usage using the generateReport() method
energyReport.generateReport("datafiles/aula11/ex3/energy_report.txt");
}
}

View File

@ -0,0 +1,19 @@
package aula11.ex4;
import java.util.LinkedList;
import java.util.Objects;
public record Company(String code, String name, LinkedList<Flight> flights) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Company company)) return false;
return Objects.equals(code, company.code) && Objects.equals(name, company.name) && Objects.equals(flights, company.flights);
}
@Override
public int hashCode() {
return Objects.hash(code, name, flights);
}
}

View File

@ -0,0 +1,17 @@
package aula11.ex4;
import java.util.Objects;
public record Flight(String code, String origin, Time departure, Time delay, Company company) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Flight flight)) return false;
return Objects.equals(code, flight.code) && Objects.equals(origin, flight.origin) && Objects.equals(departure, flight.departure) && Objects.equals(delay, flight.delay);
}
@Override
public int hashCode() {
return Objects.hash(code, origin, departure, delay);
}
}

View File

@ -0,0 +1,138 @@
package aula11.ex4;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class FlightManager {
private final LinkedList<Company> companies = new LinkedList<>();
private final LinkedList<Flight> flights = new LinkedList<>();
private String table;
private String delaysTable;
private String flightsNTable;
public void loadCompanies(String filename) {
Scanner input;
try {
input = new Scanner(new FileReader(filename));
input.nextLine();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while(input.hasNext()){
String line = input.nextLine();
String[] fields = line.split("\t");
if(fields.length != 2)
throw new RuntimeException("Invalid file format");
this.companies.add(new Company(fields[0], fields[1], new LinkedList<>()));
}
}
public void loadFlights(String filename) {
Scanner input;
try {
input = new Scanner(new FileReader(filename));
input.nextLine();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while (input.hasNext()) {
String line = input.nextLine();
String[] fields = line.split("\t");
if (fields.length != 3 && fields.length != 4)
throw new RuntimeException("Invalid file format");
String[] timeStr = fields[0].split(":");
Time time = new Time(Integer.parseInt(timeStr[0]), Integer.parseInt(timeStr[1]));
String code = fields[1];
Company company = this.companies.stream().filter(c -> c.code().equals(code.substring(0, 2))).findFirst().orElse(null);
String origin = fields[2];
String[] delayStr;
try {
delayStr = fields[3].split(":");
} catch (IndexOutOfBoundsException e) {
delayStr = new String[]{"0", "0"};
}
Time delay = new Time(Integer.parseInt(delayStr[0]), Integer.parseInt(delayStr[1]));
Flight flight = new Flight(code, origin, time, delay, company);
this.flights.add(flight);
Objects.requireNonNull(company).flights().add(flight);
}
}
public void buildTable() {
StringBuilder table = new StringBuilder().append("Depart. Flight Company Origin Delay Obs\n");
for (Flight flight : flights)
table.append(String.format("%-10s%-10s%-25s%-25s%-8s%s%n", flight.departure(), flight.code(), flight.company().name(), flight.origin(), flight.delay(), flight.delay().isZero() ? "" : "New departure: " + flight.departure().addTime(flight.delay())));
this.table = table.toString();
}
public String getTable() {
return table;
}
public void buildDelaysTable() {
StringBuilder table = new StringBuilder().append("Company Avg. Delay\n");
LinkedHashMap<String, Integer> delays = new LinkedHashMap<>();
for (Company company : companies) {
Time totalTime = new Time(0, 0);
int nDelays = 0;
for (Flight flight : company.flights()) {
if (!flight.delay().isZero()) {
totalTime.addTime(flight.delay());
nDelays++;
}
}
if (nDelays > 0)
delays.put(company.name(), Time.timeToMinsInt(totalTime) / nDelays);
}
delays = (LinkedHashMap<String, Integer>) sortByValue(delays);
for (String compName : delays.keySet())
table.append(String.format("%-25s%s%n", compName, Time.minsIntToTime(delays.get(compName))));
this.delaysTable = table.toString();
}
public String getDelaysTable() {
return delaysTable;
}
public void buildFlightsNTable() {
StringBuilder table = new StringBuilder().append("Origin n Flights\n");
LinkedHashMap<String, Integer> flightsFromOrigin = new LinkedHashMap<>();
for (Flight flight : flights)
flightsFromOrigin.put(flight.origin(), flightsFromOrigin.getOrDefault(flight.origin(), 0) + 1);
flightsFromOrigin = (LinkedHashMap<String, Integer>) sortByValue(flightsFromOrigin);
for (String origin : flightsFromOrigin.keySet())
table.append(String.format("%-25s%s%n", origin, flightsFromOrigin.get(origin)));
this.flightsNTable = table.toString();
}
public String getFlightsNTable() {
return flightsNTable;
}
public void storeTable(String filename) {
Path path = Path.of(filename);
try {
Files.writeString(path, getTable());
} catch (IOException e) {
try {
Files.createFile(path);
storeTable(filename);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap) {
List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet());
// Sorting the list based on values
list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()) == 0
? o2.getKey().compareTo(o1.getKey())
: o2.getValue().compareTo(o1.getValue()));
return list.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b, LinkedHashMap::new));
}
}

View File

@ -0,0 +1,21 @@
package aula11.ex4;
public class Main {
public static void main(String[] args) {
FlightManager fm = new FlightManager();
fm.loadCompanies("datafiles/aula11/ex4/companhias.txt");
fm.loadFlights("datafiles/aula11/ex4/voos.txt");
fm.buildTable();
System.out.println(fm.getTable());
fm.buildDelaysTable();
System.out.println(fm.getDelaysTable());
fm.buildFlightsNTable();
System.out.println(fm.getFlightsNTable());
fm.storeTable("datafiles/aula11/ex4/Infopublico.txt");
}
}

View File

@ -0,0 +1,52 @@
package aula11.ex4;
public class Time {
private int hour;
private int minute;
public Time(int hour, int minute) {
if (hour < 0 || hour > 23 || minute < 0 || minute > 59)
throw new IllegalArgumentException("Invalid time");
this.hour = hour;
this.minute = minute;
}
public Time addTime(Time time) {
int newHour = hour + time.hour();
int newMinute = minute + time.minute();
if (newMinute >= 60) {
newHour++;
newMinute -= 60;
}
if (newHour >= 24)
newHour -= 24;
this.hour = newHour;
this.minute = newMinute;
return new Time(newHour, newMinute);
}
public boolean isZero() {
return hour == 0 && minute == 0;
}
@Override
public String toString() {
return String.format("%02d:%02d", hour, minute);
}
public static int timeToMinsInt(Time time) {
return time.hour() * 60 + time.minute();
}
public static Time minsIntToTime(int mins) {
return new Time(mins/60, mins%60);
}
public int hour() {
return hour;
}
public int minute() {
return minute;
}
}

View File

@ -1,5 +1,6 @@
package utils; package utils;
import java.util.List;
import java.util.Random; import java.util.Random;
public class MathTools { public class MathTools {
@ -20,4 +21,15 @@ public class MathTools {
return false; return false;
return true; return true;
} }
public static double sum(List<Double> list) {
double sum = 0;
for (double n : list)
sum += n;
return sum;
}
public static double media(List<Double> list) {
return sum(list) / list.size();
}
} }