mirror of
https://github.com/danbulant/introductionToProgramming
synced 2026-06-21 15:51:54 +00:00
week 4 + 5
This commit is contained in:
parent
4bf15294e7
commit
a475669d50
15 changed files with 4352 additions and 37 deletions
9
.envrc
Normal file
9
.envrc
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# the shebang is ignored, but nice for editors
|
||||||
|
|
||||||
|
if type -P lorri &>/dev/null; then
|
||||||
|
eval "$(lorri direnv)"
|
||||||
|
else
|
||||||
|
echo 'while direnv evaluated .envrc, could not find the command "lorri" [https://github.com/nix-community/lorri]'
|
||||||
|
use nix
|
||||||
|
fi
|
||||||
1881
common/Draw.java
Normal file
1881
common/Draw.java
Normal file
File diff suppressed because it is too large
Load diff
2260
common/StdDraw.java
Normal file
2260
common/StdDraw.java
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -6,8 +6,12 @@
|
||||||
#doc
|
#doc
|
||||||
]
|
]
|
||||||
|
|
||||||
#let embedClass(name: str) = {
|
#let embedClass(name: str, label: none) = {
|
||||||
|
show figure: set block(width: 100%)
|
||||||
show figure: set align(left)
|
show figure: set align(left)
|
||||||
show figure.caption: set align(center)
|
show figure.caption: set align(center)
|
||||||
figure(caption: name, raw(read("../" + name + ".java"), lang:"java", block: true))
|
[
|
||||||
|
#figure(caption: name, kind: "Class", supplement: [Class], raw(read("../" + name + ".java"), lang:"java", block: true))
|
||||||
|
#label
|
||||||
|
]
|
||||||
}
|
}
|
||||||
11
shell.nix
Normal file
11
shell.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
pkgs ? import <nixpkgs> { },
|
||||||
|
}:
|
||||||
|
pkgs.mkShell rec {
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
jdk
|
||||||
|
];
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
jre
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -67,6 +67,7 @@ public class Card {
|
||||||
var top = "";
|
var top = "";
|
||||||
top += value;
|
top += value;
|
||||||
if(this.value > 10) {
|
if(this.value > 10) {
|
||||||
|
// face cards are empty
|
||||||
top += " ";
|
top += " ";
|
||||||
} else {
|
} else {
|
||||||
// and for numbered cards, show the suit characters
|
// and for numbered cards, show the suit characters
|
||||||
|
|
@ -92,7 +93,7 @@ public class Card {
|
||||||
// instead of using 3 rows we have a gap in the 3rd row
|
// instead of using 3 rows we have a gap in the 3rd row
|
||||||
output += " ";
|
output += " ";
|
||||||
output += this.value >= 6 ? suit : " ";
|
output += this.value >= 6 ? suit : " ";
|
||||||
// odd or 10
|
// odd or 10 have a symbol in the middle
|
||||||
output += this.value % 2 == 1 || this.value == 10 ? suit : " ";
|
output += this.value % 2 == 1 || this.value == 10 ? suit : " ";
|
||||||
output += this.value >= 6 ? suit : " ";
|
output += this.value >= 6 ? suit : " ";
|
||||||
output += " ";
|
output += " ";
|
||||||
|
|
@ -113,7 +114,8 @@ public class Card {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shows cards next to each other (left to right)
|
// Shows cards next to each other (left to right)
|
||||||
// assumes that sprintCard returns the same width for each card (and each row of a card)
|
// for visuals, assumes that sprintCard returns the same width for each card (and each row of a card)
|
||||||
|
// for correctness, assumes that sprintCard always returns 4 rows (is asserted)
|
||||||
public static String sprintCards(Card[] cards) {
|
public static String sprintCards(Card[] cards) {
|
||||||
String[] output = { "", "", "", "" };
|
String[] output = { "", "", "", "" };
|
||||||
|
|
||||||
|
|
@ -121,6 +123,7 @@ public class Card {
|
||||||
// save the row into relevant output
|
// save the row into relevant output
|
||||||
for(var i = 0; i < cards.length; i++) {
|
for(var i = 0; i < cards.length; i++) {
|
||||||
var cardstr = cards[i].sprintCard().split("\n");
|
var cardstr = cards[i].sprintCard().split("\n");
|
||||||
|
assert output.length == cardstr.length;
|
||||||
for(var x = 0; x < cardstr.length; x++) {
|
for(var x = 0; x < cardstr.length; x++) {
|
||||||
output[x] += cardstr[x] + " ";
|
output[x] += cardstr[x] + " ";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,24 @@
|
||||||
public class Transpose {
|
public class Transpose {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[][] input = {
|
||||||
|
{99, 85, 98},
|
||||||
|
{98, 57, 78},
|
||||||
|
{92, 77, 76},
|
||||||
|
};
|
||||||
|
|
||||||
|
for(var i = 0; i < input.length; i++) {
|
||||||
|
for(var j = i; j < input.length; j++) {
|
||||||
|
var og = input[i][j];
|
||||||
|
input[i][j] = input[j][i];
|
||||||
|
input[j][i] = og;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(var i = 0; i < input.length; i++) {
|
||||||
|
for(var j = 0; j < input.length; j++) {
|
||||||
|
System.out.print(input[i][j] + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,32 +9,50 @@
|
||||||
Write a program `Deal` that takes an integer command-line argument `n` and
|
Write a program `Deal` that takes an integer command-line argument `n` and
|
||||||
prints `n` poker hands (five cards each) from a shuffled deck, separated by blank lines.
|
prints `n` poker hands (five cards each) from a shuffled deck, separated by blank lines.
|
||||||
|
|
||||||
|
A second class Card (@card) is used for visuals.
|
||||||
|
|
||||||
|
Example run for 2 hands (`java Deal.java 2`):
|
||||||
|
|
||||||
|
```
|
||||||
|
J J K K ⒑♥ ♥⒑ 9♦ ♦9 7♠ ♠7
|
||||||
|
♥ ♥ ♠ ♠ ♥♥♥ ♦♦♦ ♠♠♠
|
||||||
|
♥ ♥ ♠ ♠ ♥♥♥ ♦ ♦
|
||||||
|
J J K K ⒑♥ ♥⒑ 9♦ ♦9 7♠ ♠7
|
||||||
|
|
||||||
|
3 ♥ 3 4♠ ♠4 4♦ ♦4 7♦ ♦7 6♦ ♦6
|
||||||
|
♥ ♦♦♦ ♦ ♦
|
||||||
|
|
||||||
|
3 ♥ 3 4♠ ♠4 4♦ ♦4 7♦ ♦7 6♦ ♦6
|
||||||
|
```
|
||||||
|
|
||||||
#embedClass(name: "Deal")
|
#embedClass(name: "Deal")
|
||||||
|
|
||||||
#embedClass(name: "Card")
|
#embedClass(name: "Card", label: <card>)
|
||||||
|
|
||||||
== Exercise 1.4.14
|
== Exercise 1.4.14
|
||||||
|
|
||||||
Write a code fragment to print the transposition (rows and columns exchanged) of a square two-dimensional array.
|
Write a code fragment to print the transposition (rows and columns exchanged) of a square two-dimensional array.
|
||||||
For the example spreadsheet array in the text, you code would print the following:
|
|
||||||
|
|
||||||
```
|
#place(
|
||||||
99 98 92 94 99 90 76 92 97 89
|
right,
|
||||||
85 57 77 32 34 46 59 66 71 29
|
figure(
|
||||||
98 78 76 11 22 54 88 89 24 38
|
table(
|
||||||
```
|
columns: 2,
|
||||||
|
[Example input],
|
||||||
|
[Output],
|
||||||
|
```
|
||||||
|
99 85 98
|
||||||
|
98 57 78
|
||||||
|
92 77 76
|
||||||
|
```,
|
||||||
|
```
|
||||||
|
99 98 92
|
||||||
|
85 57 77
|
||||||
|
98 78 76
|
||||||
|
```
|
||||||
|
),
|
||||||
|
caption: [Example input and output matrix]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
Input:
|
#embedClass(name: "Transpose")
|
||||||
|
|
||||||
```
|
|
||||||
99 85 98
|
|
||||||
98 57 78
|
|
||||||
92 77 76
|
|
||||||
94 32 11
|
|
||||||
99 34 22
|
|
||||||
90 46 54
|
|
||||||
76 59 88
|
|
||||||
92 66 89
|
|
||||||
97 71 24
|
|
||||||
89 29 38
|
|
||||||
```
|
|
||||||
|
|
@ -1,10 +1,3 @@
|
||||||
99 85 98
|
99 85 98
|
||||||
98 57 78
|
98 57 78
|
||||||
92 77 76
|
92 77 76
|
||||||
94 32 11
|
|
||||||
99 34 22
|
|
||||||
90 46 54
|
|
||||||
76 59 88
|
|
||||||
92 66 89
|
|
||||||
97 71 24
|
|
||||||
89 29 38
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
99 98 92 94 99 90 76 92 97 89
|
99 98 92
|
||||||
85 57 77 32 34 46 59 66 71 29
|
85 57 77
|
||||||
98 78 76 11 22 54 88 89 24 38
|
98 78 76
|
||||||
38
week5/CircleLines.java
Normal file
38
week5/CircleLines.java
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
package week5;
|
||||||
|
|
||||||
|
import common.StdDraw;
|
||||||
|
|
||||||
|
public class CircleLines {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n = Integer.parseInt(args[0]);
|
||||||
|
double p = Double.parseDouble(args[1]);
|
||||||
|
assert p >= 0. && p <= 1.;
|
||||||
|
StdDraw.setXscale(-1.1, 1.1);
|
||||||
|
StdDraw.setYscale(-1.1, 1.1);
|
||||||
|
StdDraw.clear();
|
||||||
|
double[][] points = new double[n][2];
|
||||||
|
|
||||||
|
for(var i = 0; i < n; i++) {
|
||||||
|
// explicitly convert to double
|
||||||
|
double fi = i;
|
||||||
|
// divide by total to get 0-1
|
||||||
|
fi /= n;
|
||||||
|
// and convert to radians
|
||||||
|
fi *= 2*Math.PI;
|
||||||
|
// save calculations
|
||||||
|
points[i][0] = Math.sin(fi);
|
||||||
|
points[i][1] = Math.cos(fi);
|
||||||
|
StdDraw.filledCircle(points[i][0], points[i][1], 0.01);
|
||||||
|
}
|
||||||
|
|
||||||
|
StdDraw.setPenColor(StdDraw.GRAY);
|
||||||
|
|
||||||
|
for(var i = 0; i < n; i++) {
|
||||||
|
for(var x = i; x < n; x++) {
|
||||||
|
if(Math.random() > p) continue;
|
||||||
|
|
||||||
|
StdDraw.line(points[i][0], points[i][1], points[x][0], points[x][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
week5/Circles.java
Normal file
27
week5/Circles.java
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package week5;
|
||||||
|
|
||||||
|
import common.StdDraw;
|
||||||
|
|
||||||
|
public class Circles {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var circleCount = Integer.parseInt(args[0]);
|
||||||
|
var pBlack = Double.parseDouble(args[1]);
|
||||||
|
var minRadius = Double.parseDouble(args[2]);
|
||||||
|
var maxRadius = Double.parseDouble(args[3]);
|
||||||
|
|
||||||
|
StdDraw.setScale();
|
||||||
|
StdDraw.clear();
|
||||||
|
|
||||||
|
for(var i = 0; i < circleCount; i++) {
|
||||||
|
if(Math.random() < pBlack) {
|
||||||
|
StdDraw.setPenColor(StdDraw.BLACK);
|
||||||
|
} else {
|
||||||
|
StdDraw.setPenColor(StdDraw.WHITE);
|
||||||
|
}
|
||||||
|
var radii = Math.random() * (maxRadius - minRadius) + minRadius;
|
||||||
|
var posX = Math.random();
|
||||||
|
var posY = Math.random();
|
||||||
|
StdDraw.filledCircle(posX, posY, radii);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
week5/MissingInt.java
Normal file
19
week5/MissingInt.java
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package week5;
|
||||||
|
|
||||||
|
import common.StdIn;
|
||||||
|
|
||||||
|
public class MissingInt {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n = Integer.parseInt(args[0]);
|
||||||
|
boolean[] nums = new boolean[n];
|
||||||
|
for(var i = 0; i < n - 1; i++) {
|
||||||
|
int integer = StdIn.readInt() - 1;
|
||||||
|
nums[integer] = true;
|
||||||
|
}
|
||||||
|
for(var i = 0; i < nums.length; i++) {
|
||||||
|
if(!nums[i]) {
|
||||||
|
System.err.println(i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
week5/common
Symbolic link
1
week5/common
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
../common
|
||||||
30
week5/doc.typ
Normal file
30
week5/doc.typ
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
#import "./common/common.typ" : *
|
||||||
|
|
||||||
|
#show: template
|
||||||
|
|
||||||
|
= Week 5
|
||||||
|
|
||||||
|
== Exercise 1.5.7
|
||||||
|
|
||||||
|
Write a program that takes an integer command-line argument $n$, reads in
|
||||||
|
$n-1$ distinct integers between 1 and $n$, and determines the missing value.
|
||||||
|
|
||||||
|
#embedClass(name: "MissingInt")
|
||||||
|
|
||||||
|
== Exercise 1.5.19
|
||||||
|
|
||||||
|
Write a program that takes as command-line arguments an integer $n$ and
|
||||||
|
a floating-point number $p$ (between $0$ and $1$), plots $n$ equally spaced points on the
|
||||||
|
circumference of a circle, and then, with probability $p$ for each pair of points, draws
|
||||||
|
a gray line connecting them.
|
||||||
|
|
||||||
|
#embedClass(name: "CircleLines")
|
||||||
|
|
||||||
|
== Exercise 1.5.26
|
||||||
|
|
||||||
|
Write a program Circles that draws filled circles of random radii at random
|
||||||
|
positions in the unit square, producing images like those below. Your program
|
||||||
|
should take four command-line arguments: the number of circles, the probability
|
||||||
|
that each circle is black, the minimum radius, and the maximum radius.
|
||||||
|
|
||||||
|
#embedClass(name: "Circles")
|
||||||
Loading…
Reference in a new issue