Monday, May 15. 2023
Final Project Launcher
Dean Game : Press SPACE to take out your phone. Don't get caught by Mr. Grable!
Pacwoman: Eat the coins and avoid the men. Use WASD to move. Waka waka waka.
Rift Battle: Fight three opponents in this board game. Use the mouse to select a piece and move.
The Thaw: Click to move between rooms and collect items to prevent the Cold War. Can you find all the secret endings in this very historically accurate game?
Car Game: Race around the track! Use WASD to move.
Italian Bros: Use SPACE to jump and use the arrow keys to move the Italian Brother to the ending! Any resemblance to other Italian-themed platformers is purely coincidental.
Cat Jump Game: Use WASD to move the cat and SPACE to jump. Can you collect all of the cat food in this purrfect platformer?
Fun Time Game: It's raining pancakes! Use the arrow keys to move and collect as many as you can. Be sure to avoid the falling forks.
Pancake Pros: Use the WASD keys to move the Tetris pieces and SPACE to drop them. There are special pancake pieces to help you fill the gaps!
ThugSim: Can you find a way to escape the prison? Use WASD to move your character.
Crossy Gym: Move through the gym with WASD and collect all of the coins. Watch out for the treadmills!.
The Hacker: Type the sentence as quickly as you can to hack into the mainframe. Don't make any tyops and don't let the time run out!
Knock Out Kenzie: Click to see different scenes of this interactive story and pursue the love of your life.
Pokeman: Use WASD to move around the field. Enter tall grass for a chance to encounter a "Pokeman". Press C to add it to your inventory. Can you catch 'em all?/
Room Rushers: Move around the room with WASD to dodge enemy bullets. Aim with the mouse and click to shoot. Look out for that game over sound effect!
Wednesday May 3, 2023
Picture Story Template
Friday, April 28, 2023
Tile Mover Template
Thursday, April 20, 2023
Mini Projects
Period 3: SpaceFighters
class Game | |
{ | |
Alien[] enemies; | |
Projectile[] bullets; | |
Player player; | |
int points; | |
double width; | |
double height; | |
Game(double width, double height) | |
{ | |
this.width = width; | |
this.height = height; | |
bullets = null; | |
enemies = null; | |
player = new Player(200, 200); | |
points = 0; | |
} | |
void update() | |
{ | |
} | |
} |
class Player extends SpaceCraft | |
{ | |
Player(double x, double y) | |
{ | |
this.x = x; | |
this.y = y; | |
this.width = 30; | |
this.height = 30; | |
this.hp = 10; | |
this.bullets = 3; | |
} | |
void moveUp() | |
{ | |
this.y -= 3; | |
} | |
void moveDown() | |
{ | |
this.y += 3; | |
} | |
void moveLeft() | |
{ | |
this.x -= 3; | |
} | |
void moveRight() | |
{ | |
this.x += 3; | |
} | |
} |
class SpaceCraft | |
{ | |
double x; | |
double y; | |
double width; | |
double height; | |
int hp; | |
int bullets; | |
void moveUp() { | |
// Override this in the subclass | |
} | |
void moveDown() { | |
// Override this in the subclass | |
} | |
void moveLeft() { | |
// Override this in the subclass | |
} | |
void moveRight() { | |
// Override this in the subclass | |
} | |
} |
import javafx.application.Application; | |
import javafx.stage.Stage; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.Scene; | |
import javafx.animation.AnimationTimer; | |
import javafx.scene.paint.Color; | |
import javafx.scene.input.KeyEvent; | |
public class SpaceFightersApp extends Application | |
{ | |
GraphicsContext gc; | |
Game game; | |
public void start(Stage stage) | |
{ | |
Canvas canvas = new Canvas(600, 1000); | |
gc = canvas.getGraphicsContext2D(); | |
VBox vbox = new VBox(canvas); | |
Scene scene = new Scene(vbox); | |
scene.setOnKeyPressed(this::handleKeyPress); | |
stage.setScene(scene); | |
stage.show(); | |
// Set up the data for the game | |
game = new Game(600, 1000); | |
GameTimer timer = new GameTimer(); | |
timer.start(); | |
} | |
void drawPlayerShip() | |
{ | |
gc.setFill(Color.RED); | |
gc.fillOval(game.player.x, game.player.y, game.player.width, game.player.height); | |
} | |
void handleKeyPress(KeyEvent e) | |
{ | |
if (e.getText().equals("w")) | |
{ | |
game.player.moveUp(); | |
} | |
if (e.getText().equals("s")) | |
{ | |
game.player.moveDown(); | |
} | |
if (e.getText().equals("a")) | |
{ | |
game.player.moveLeft(); | |
} | |
if (e.getText().equals("d")) | |
{ | |
game.player.moveRight(); | |
} | |
} | |
class GameTimer extends AnimationTimer | |
{ | |
public void handle(long t) | |
{ | |
drawPlayerShip(); | |
} | |
} | |
} |
Friday, April 14, 2023
Lab: Experimenting with a Large Language Model
Lab Assignment: Experimenting with a Large Language Model
In this lab, you will pick three of the five short projects to try with ChatGPT.
The instructions are available in the link above. Please note: You will submit the assignment three times in total, once for each project that you choose.
Thursday, April 6, 2023
Turn in the Paint Application on Github
Accept the assignment and upload your .java
file to the repository.
Monday, April 3, 2023
Add features to the Paint Application
Get the starter code from where we left off in class:
PaintProgram.java
import javafx.application.Application; | |
import javafx.stage.Stage; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.Scene; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.paint.Color; | |
import javafx.animation.AnimationTimer; | |
import java.util.ArrayList; | |
import javafx.scene.control.Button; | |
import javafx.event.ActionEvent; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.control.ColorPicker; | |
import javafx.scene.input.MouseEvent; | |
public class InteractiveProgram3 extends Application | |
{ | |
TextField username; | |
GraphicsContext gc; | |
ColorPicker cp; | |
double mouseX; | |
double mouseY; | |
boolean drawingEnabled; | |
public void start(Stage stage) | |
{ | |
drawingEnabled = false; | |
Canvas canvas = new Canvas(500, 500); | |
gc = canvas.getGraphicsContext2D(); | |
canvas.setOnMouseMoved(this::handleMouseMove); | |
canvas.setOnMouseClicked(this::handleMouseClick); | |
Button helloButton = new Button("Say hello"); | |
helloButton.setOnAction(this::handleHelloButton); | |
username = new TextField(); | |
username.setPromptText("Enter your name."); | |
cp = new ColorPicker(); | |
HBox container = new HBox(username, cp, helloButton); | |
VBox mainLayout = new VBox(container, canvas); | |
Scene scene = new Scene(mainLayout); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
void handleHelloButton(ActionEvent e) | |
{ | |
String name = username.getText(); | |
Color c = cp.getValue(); | |
gc.setFill(c); | |
gc.fillText("Hello " + name, Math.random() * 500, Math.random() * 500); | |
} | |
void handleMouseMove(MouseEvent e) | |
{ | |
mouseX = e.getX(); | |
mouseY = e.getY(); | |
if (drawingEnabled == true) | |
{ | |
gc.fillOval(mouseX, mouseY, 20, 20); | |
} | |
} | |
void handleMouseClick(MouseEvent e) | |
{ | |
Color c = cp.getValue(); | |
gc.setFill(c); | |
gc.fillOval(mouseX, mouseY, 20, 20); | |
if (drawingEnabled == false) | |
{ | |
drawingEnabled = true; | |
} | |
else | |
{ | |
drawingEnabled = false; | |
} | |
} | |
} |
Thursday, March 30, 2023
Bouncing Ball Animation
Get the two classes here:
Animation.java and Ball.java
import javafx.application.Application; | |
import javafx.stage.Stage; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.Scene; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.paint.Color; | |
import javafx.animation.AnimationTimer; | |
public class Animation4 extends Application | |
{ | |
GraphicsContext gc; | |
Ball ball; | |
double red = 0.5; | |
double green = 0.1; | |
double blue = 0.7; | |
public void start(Stage stage) | |
{ | |
Canvas canvas = new Canvas(500, 500); | |
gc = canvas.getGraphicsContext2D(); | |
Scene scene = new Scene(new HBox(canvas)); | |
stage.setScene(scene); | |
stage.show(); | |
ball = new Ball(250, 250, -3, 2); | |
Timer timer = new Timer(); | |
timer.start(); | |
} | |
void drawBall() | |
{ | |
gc.setFill(Color.color(Math.abs(Math.sin(red)), Math.abs(Math.sin(green)), Math.abs(Math.sin(blue)))); | |
gc.fillOval(ball.x, ball.y, ball.size, ball.size); | |
red += .02; | |
green += .04; | |
blue += .05; | |
} | |
void drawFrame() | |
{ | |
gc.setFill(Color.WHITE); | |
gc.fillRect(0, 0, 500, 500); | |
drawBall(); | |
} | |
void changeSize() | |
{ | |
ball.size += 0.5; | |
ball.size %= 60; //maximum size | |
} | |
void update() | |
{ | |
//changeSize(); | |
ball.move(); | |
if (ball.x < 0 || ball.x + ball.size > 500) | |
{ | |
ball.vx *= -1; | |
} | |
if (ball.y < 0 || ball.y + ball.size > 500) | |
{ | |
ball.vy *= -1; | |
} | |
} | |
class Timer extends AnimationTimer | |
{ | |
public void handle(long t) | |
{ | |
// this code will run 60 times every second... | |
drawFrame(); | |
update(); | |
} | |
} | |
} |
class Ball | |
{ | |
double x; | |
double y; | |
double vx; | |
double vy; | |
double size = 30; | |
Ball(double x, double y, double vx, double vy) | |
{ | |
this.x = x; | |
this.y = y; | |
this.vx = vx; | |
this.vy = vy; | |
} | |
void move() | |
{ | |
x += vx; | |
y += vy; | |
} | |
} |
Wednesday, March 22, 2023
GUI Programming
Hi folks. Please continue working through the notes and challenge problems from earlier this week.
For those who are finishing up or are ready to move on, take a look at this next topic: creating graphical user interfaces in Java.
Tuesday, March 21, 2023
Continue ArrayList Notes
Hi all, please continue to work through the material posted yesterday.
Quick correction: Where it says ArrayIndexOutOfBoundsError
in the notes, it should have said ArrayIndexOutOfBoundsException
.
Feel free to send me an email if you have any questions. Thanks!
Monday, March 20, 2023
Exploring Data Structures
Hi everyone! I hope you have enjoyed your break. I will be returning next Monday and I look forward to seeing you all then.
For this week, we will learn how to create a useful data structure called an ArrayList. It can hold a collection of items like a normal array, but can also change its size as needed.
I have prepared a series of guided notes for this topic. Please follow along with these notes with BlueJ open to a new project called DataStructures. Whenever you see a section of Java code, please add this code to your project.
- Data Structures Introduction
This first slideshow does not have any Java code to add to your project. (Press spacebar to go to the next slide or use the up, down, left, and right arrows to navigate).
- Building an ArrayList from Scratch
This second slideshow has many sections of code to follow along with. Please create a class called DynamicArray in your BlueJ project and follow along as you read the slides.
- Using ArrayList Methods
This last section of notes has some code examples and then challenge problems for you to complete at the end.
Friday, March 3, 2023
Implications of the Halting Problem
So far we have seen that there is a Turing Machine (a Universal Turing Machine) that can simulate the operations of other Turing Machines. And Turing used this idea to give a surprising proof that there are some things that computers cannot do.
A Turing Machine (or computer) cannot decide if another Turing Machine (or computer program) will run forever on its given input or eventually halt.
This result stands for all times. Even if we are able to make computer hardware with as-yet unimaginable technology, this limit of computation will never be overcome.
For today's lesson, please read this description of more implications of the Halting Problem's undecidability.
Thursday, March 2, 2023
The Universal Turing Machine
Hi everyone. I forgot to present this lesson to you first, which is background knowledge to make more sense of the halting problem, which you learned about yesterday.
Today's readings establish a way for one Turing Machine to interpret another Turing Machine's instructions. As you will see, it is possible to build a "Universal" Turing Machine that can be reprogrammed with the "blueprint" of another Turing Machine. Most Turing Machines can perform only a very specific task, but the Universal Turing Machine can do anything that any Turing Machine can do. In that sense it is like a general computer.
The videos that you watched for homework rely on this idea—that one Turing Machine can simulate another one.
I find this idea extremely fascinating, and I hope that you will too!
Please read the following:
There is no homework today. Thank you.
Wednesday, March 1, 2023
The Halting Problem
Please read the following online lessons closely and make an attempt to answer the questions for yourself before moving on.
The two lessons for the day are to introduce "The Halting Problem" before we later set up Turing's ingenious proof of it's inability to be solved with Turing Machines.
The videos for lesson two are not included for today's in-class lesson. Please view them at home for homework:
- Turing & the Halting Problem - Computerphile
- Proof that Computers can't do Everything (The Halting Problem)
Tuesday, February 28, 2023
Yay! I'm a dad!
Hi class, as you might have heard by now, my baby arrived a little sooner than expected! Her name is Aletta Ruth Haney Kanim. She is named after important people in our families and is getting a doubled last name. We're calling her Allie.
Allie is in perfect health and so is Ms. Kanim. I feel amazingly blessed by that.
I will return to class the second week of quarter 4 on Monday. In the meantime, I will leave some assignments posted on the website for you guys to complete independently while you have a sub.
Here's a picture of me holding her on her birthday:

Thanks guys!
Finish up the Designing Turing Machines problem set
We have one day left to finish the Turing Machines problem set. Please have this ready to turn in by tomorrow morning. I will ask the sub to collect them and place them on my desk in the faculty office.
Please complete it for homework if you run out of time today. If you are already done, you may use this period as a study hall. Thank you!
Monday, February 13, 2023
Correction to getNearest()
method
Replace the getNearest()
method in the Organism class with the corrected code below:
Correction:
Organism getNearest(Class species) | |
{ | |
Organism nearestSoFar = null; | |
int nearestDistanceSoFar = 1000000000; | |
for (Organism org : world.organisms) | |
{ | |
if(!this.equals(org) && | |
org.isAlive && | |
org.getClass() == species && | |
this.getDistanceTo(org) < nearestDistanceSoFar) | |
{ | |
nearestDistanceSoFar = this.getDistanceTo(org); | |
nearestSoFar = org; | |
if (nearestDistanceSoFar == 0) | |
{ | |
return nearestSoFar; | |
} | |
} | |
} | |
return nearestSoFar; | |
} |
Tuesday, February 7, 2023
Ecosystem Inheritance Lab
We are beginning a lab to practice inheritance and to see how a system can be designed with a hierarchy of classes.
Download the starter code here and complete the todo items that are left as comments in the code:
Monday, January 9, 2023
Regular Expression Lab
We are beginning a lab that will exercise our ability to write regular expressions in Java. It will also review a bit of iteration and File IO
Get the starter code from Github classroom:
Tuesday, December 6. 2022
Logic Midterm Study Guide
We are reviewing for the cumulative logic midterm this week. Use this study guide as an opportunity to review the content from this semester.
Thursday, December 1, 2022
Advent of Code
Join people around the world in celebrating the holidays with fun programming challenges: Advent of Code.
Each day until Christmas, two new puzzles unlock in this advent calendar. They get progressively harder each day, but they are a good opportunity to practice using some of the skills we have been learning recently like iteration, arrays, and file io.
Wednesday, November 30, 2022
GuessTheNumber Game
Code from the first day:
import java.util.Scanner; | |
class GuessTheNumber | |
{ | |
int guesses; | |
int secretNumber; | |
Scanner s; | |
public static void main(String[] args) // This is expected by Java! | |
{ | |
while (true) // game loop | |
{ | |
GuessTheNumber game = new GuessTheNumber(); | |
game.start(); | |
game.promptGuess(); | |
game.exit(); | |
System.out.println("Type y to play again."); | |
Scanner s = new Scanner(System.in); | |
String choice = s.nextLine(); | |
s.close(); | |
if (!choice.equalsIgnoreCase("y")) | |
{ | |
System.out.println("Goodbye."); | |
return; | |
} | |
} | |
} | |
GuessTheNumber() | |
{ | |
guesses = 0; | |
secretNumber = (int) (Math.random() * 100) + 1; //between 1 and 100 | |
s = new Scanner(System.in); | |
} | |
void start() | |
{ | |
System.out.println("GUESS THE NUMBER!!!"); | |
System.out.println("Guess a number between 1 and 100."); | |
} | |
void promptGuess() | |
{ | |
int guess = 0; | |
while (guess != secretNumber) | |
{ | |
guess = s.nextInt(); | |
guesses++; | |
if (guess < secretNumber) | |
{ | |
System.out.println("Too low. Guess higher."); | |
} | |
else if (guess > secretNumber) | |
{ | |
System.out.println("Too high. Guess lower."); | |
} | |
else | |
{ | |
System.out.println("Correct! You got it in " + guesses); | |
} | |
} | |
} | |
void exit() | |
{ | |
System.out.println("Thanks for playing."); | |
s.close(); //close the scanner | |
} | |
} |
Monday, November 28, 2022
File IO Examples
Today we introduced file input and output in Java.
We covered reading text from a file with a Scanner and writing to a file with a PrintStream.
Example code:
import java.util.Scanner; | |
import java.io.File; | |
import java.io.PrintStream; | |
import java.io.FileNotFoundException; | |
class Examples | |
{ | |
static void greeting() | |
{ | |
System.out.println("Please enter your name."); | |
// A scanner can read from keyboard input at the console | |
Scanner s = new Scanner(System.in); | |
String name = s.nextLine(); | |
System.out.println("Hello " + name); | |
s.close(); | |
} | |
static void findLongWords() throws FileNotFoundException | |
{ | |
// Download a text file from gutenberg.org and place it in your BlueJ project file | |
// This scanner is set up to read text from the book we downloaded | |
File f = new File("frankenstein.txt"); | |
Scanner s = new Scanner(f); | |
// This printstream is set up to print into a new file (created automatically) called "longwords.txt" | |
File o = new File("longwords.txt"); | |
PrintStream p = new PrintStream(o); | |
while (s.hasNext()) | |
{ | |
String word = s.next(); | |
// Print into longwords.txt if the current word is long enough | |
if (word.length() >= 12) | |
{ | |
p.println(word); | |
} | |
} | |
System.out.println("Done!"); | |
s.close(); | |
p.close(); | |
} | |
static void registerUser() throws FileNotFoundException | |
{ | |
Scanner s = new Scanner(System.in); | |
System.out.println("Do you accept my very important TERMS AND CONDITIONS?"); | |
System.out.println("Type (y)es to continue"); | |
String choice = s.nextLine(); | |
if (!choice.equalsIgnoreCase("yes") && !choice.equalsIgnoreCase("y")) | |
{ | |
System.out.println("Goodbye then. :("); | |
return; | |
} | |
System.out.println("Good choice."); | |
System.out.println("Please enter your username:"); | |
String username = s.nextLine(); | |
System.out.println("Please enter your password:"); | |
String password = s.nextLine(); | |
s.close(); | |
System.out.println("Saving your information..."); | |
File f = new File("userinfo.txt"); | |
PrintStream p = new PrintStream(f); | |
p.println(username); | |
p.println(password); | |
p.close(); | |
System.out.println("Done. Thanks for registering, " + username); | |
} | |
static void changePassword() throws FileNotFoundException | |
{ | |
// Read the current username and password from userinfo.txt | |
File f = new File("userinfo.txt"); | |
Scanner s = new Scanner(f); | |
String username = s.nextLine(); | |
String password = s.nextLine(); | |
s.close(); | |
// Ask for the user's name and password to verify their identity: | |
Scanner k = new Scanner(System.in); | |
System.out.println("Please enter your username:"); | |
String usernameInput = k.nextLine(); | |
System.out.println("Please enter your password:"); | |
String passwordInput = k.nextLine(); | |
// Validate their information | |
while (!username.equals(usernameInput) || !password.equals(passwordInput)) | |
{ | |
System.out.println("Incorrect username or password."); | |
System.out.println("Please enter your username:"); | |
usernameInput = k.nextLine(); | |
System.out.println("Please enter your password:"); | |
passwordInput = k.nextLine(); | |
} | |
System.out.println("Welcome " + username); | |
System.out.println("Please enter a new password."); | |
String newPassword = k.next(); | |
k.close(); | |
// Write the information to the file | |
PrintStream p = new PrintStream(f); | |
p.println(username); | |
p.println(newPassword); | |
p.close(); | |
System.out.println("Your information has been saved."); | |
} | |
} |
Monday, November 21, 2022
Turn in Arrays Lab
Turn in instructions:
- Click the link for your class period to accept the assignment:
- When it directs you to github, you should see an empty repository.
- Click the Add Files button and select Upload files
- Drag the folder containing your project into the upload zone on github.
-
Example:
- Press commit changes when finished.
Monday, November 7. 2022
Iteration Strategies Handout
Arrays Mixed Practice
We began working in groups on this problem set to practice processing arrays using the iteration strategies.
Wednesday, November 2, 2022
Arrays and Computer Memory
The limitations of arrays make sense when we understand how they are backed by the memory of our computer. Take a look at the slides from today and the code examples that we created together.
Array Processing Code examples:
import java.util.Arrays; | |
class BasicExamples | |
{ | |
// example arrays of various datatypes | |
// static keyword means that the variable belongs to the class, not to an object | |
static int[] nums = {8, -9, 0, 1, 33, 7, 55, 7}; | |
static String[] animals = {"cow", "cat", "dog", "pig", "duck"}; | |
static boolean[] quizAnswers = {true, true, false, true, false}; | |
// create an empty array capable of holding 500 int values | |
static int[] data = new int[500]; | |
/** | |
* Prints the animals array on a single line. | |
*/ | |
static void printAnimals() | |
{ | |
// System.out.println(animals); // This prints a memory address!!! | |
System.out.println(Arrays.toString(animals)); // This prints the expected list of animals as a readable string | |
} | |
/** | |
* Prints the nums array, then prints the array in sorted order. | |
*/ | |
static void printNums() | |
{ | |
// The Arrays class has a lot of useful helper methods (like sorting). | |
System.out.println(Arrays.toString(nums)); | |
Arrays.sort(nums); | |
System.out.println(Arrays.toString(nums)); | |
} | |
/** | |
* Prints the animals array, one animal per line. | |
*/ | |
static void printAnimalsVertical() | |
{ | |
// Print an animal on each line by indexing the array with a variable | |
// for loop syntax: for (COUNTER; CONDITION; INCREMENT) | |
// COUNTER: set up a counting number. Using "i" is traditional ("index") | |
// CONDITION: continue to loop while the condition is true (same as while loop condition) | |
// INCREMENT: change the counting number to a new value (usually by incrementing/decrementing) | |
for (int i = 0; i < animals.length; i++) | |
{ | |
System.out.println(animals[i]); | |
} | |
} | |
/** | |
* Prints the animals array backwards. | |
*/ | |
static void printAnimalsBackwards() | |
{ | |
// Showing how to walk an array backwards. | |
// Notice that i starts at last index and ends at 0. | |
for (int i = animals.length - 1; i >= 0; i--) | |
{ | |
System.out.print(animals[i] + " "); | |
} | |
} | |
/** | |
* Return true if the array contains the given animal. | |
*/ | |
static boolean containsAnimal(String animal) | |
{ | |
for (int i = 0; i < animals.length; i++) | |
{ | |
if (animal.equals(animals[i])) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Return the sum of the nums array. | |
*/ | |
static int sumNums() | |
{ | |
int total = 0; | |
for (int i = 0; i < nums.length; i++) | |
{ | |
total += nums[i]; | |
// Viewing the progress: | |
System.out.println("Current num = " + nums[i] + " current total = " + total); | |
} | |
return total; | |
} | |
/** | |
* Return the greatest value in the nums array. | |
*/ | |
static int maxNum() | |
{ | |
int greatestSoFar = nums[0]; | |
for (int i = 0; i < nums.length; i++) | |
{ | |
if (nums[i] > greatestSoFar) | |
{ | |
greatestSoFar = nums[i]; | |
System.out.println("Found a larger value: " + nums[i]); | |
} | |
} | |
return greatestSoFar; | |
} | |
/** | |
* Change true to false and false to true in quizAnswers array. | |
*/ | |
static void flipAnswers() | |
{ | |
System.out.println(Arrays.toString(quizAnswers)); | |
for (int i = 0; i < quizAnswers.length; i++) | |
{ | |
quizAnswers[i] = !quizAnswers[i]; | |
} | |
System.out.println(Arrays.toString(quizAnswers)); | |
} | |
/** | |
* Fill the data array with random values and return a reference to it. | |
*/ | |
static int[] generateData() | |
{ | |
for (int i = 0; i < data.length; i++) | |
{ | |
data[i] = (int)(Math.random() * 100); | |
} | |
return data; | |
} | |
/** | |
* Return the number of A grades. | |
*/ | |
static int countAGrades(double[] grades) | |
{ | |
int count = 0; | |
for (int i = 0; i < grades.length; i++) | |
{ | |
if (grades[i] >= 90) | |
{ | |
count++; | |
} | |
} | |
return count; | |
} | |
/** | |
* Return the number of F grades | |
*/ | |
static int countFGrades(double[] grades) | |
{ | |
int count = 0; | |
// "Collection controlled" for loop avoids mentioning array indices | |
// Prefer this style of for loop when iterating over the whole array from beginning | |
// to end and reading the values without modifying them. | |
for (double g : grades) | |
{ | |
if (g < 65) | |
{ | |
count++; | |
} | |
} | |
return count; | |
} | |
} |
Tuesday, November 1, 2022
Arrays Introduction
Today we introduced arrays in Java. You can find the slides and exercises below.
Monday, October 31, 2022
Sub Plans:
Sorry I can't make it in today folks. I'm not feeling very well :(
Please work on the following activities while I am out. They will help you prepare for the quiz tomorrow and for our next unit in programming: arrays.
Logic Problem Set
Answer the following questions in your notebook to review for the Conjunctions Quiz.
Translate the following sentences into logical symbols:
- "All animals are equal, but some animals are more equal than others." - Animal Farm by George Orwell
- "Nowadays people know the price of everything and the value of nothing." - The Picture Of Dorian Gray by Oscar Wilde
- "If we respected only what is inevitable and has a right to be, music and poetry would resound along the streets." - Walden by Henry David Thoreau
- "To produce a mighty book, you must choose a mighty theme. No great and enduring volume can ever be written on the flea, though many there be who have tried it." - Moby Dick by Herman Melville
Prove the following:
- Premises: (P -> Q) ^ (R -> S), -Q ^ -S. Conclusion: -P ^ -R.
- Premises: P ^ ((-Q -> R) ^ (Q -> -P)). Conclusion: R.
Programming Research Task:
Search for the following information online. (Websites only. No videos, please).
Feel free to try out any code you find in BlueJ on the code pad.
- What is an array used for in Java?
- How do you make an array of integers that holds the values 2, 4, 6, 8, and 10?
- How do you make an array of Strings that holds the values "moo", "quack", and "meow"?
- How do you see a single value in an array by its index?
- What number index is "moo" in your String array? What number index is "meow"?
- How do you change what is in an array at a particular slot? Change "quack" to "oink".
- How do you create an empty array of doubles that has space for 500 numbers?
- What is the best way to print an array? What happens when you do the obvious: System.out.println(animalNoiseArray);
Silent Study Hall
When you are finished, please work on something else quietly for the rest of the period. Maybe your Senior Thesis?
Wednesday, October 26, 2022
Turn in Objects Lab
Directions to turn in lab:
- Log in to github.com
- Look for your project in the sidebar
-
Example:
- Drag your folder into the upload files screen and click commit at the bottom.
-
Example:
Monday, October 24, 2022
Objects Lab Challenge Problem
When you are finished with the first two activities, try the small project below for an additional challenge.
You will be making an application that simulates a planet orbiting its sun. Copy the files into their own classes and complete the TODO items in the Planet class
Solar System App
class Planet | |
{ | |
/*# | |
* TODO 1: Add the following instance variables | |
* along with their appropriate data types: | |
* | |
* x, y - decimal values representing the position coordinates | |
* dx, dy - decimal values representing the components of velocity | |
* ddx, ddy - decimal values representing the componets of acceleration | |
* mass - a decimal value used to scale the effect of acceleration | |
* radius - a decimal value used to draw the planet and check for collisions | |
* sun - a Sun object representing the planet's orbital center | |
*/ | |
/*# | |
* TODO 2: Create a constructor for Planet that accepts the following inputs in this order: | |
* x, y, mass, radius, sun | |
* | |
* Set the planet's instance variables to the given inputs of the constructor. | |
* Set the velocity and acceleration to an initial value of zero. | |
*/ | |
/** | |
* Sets the acceleration of the planet. | |
* | |
* This will be called every frame of the animation to simulate | |
* gravitational attraction. | |
*/ | |
void applyGravity() | |
{ | |
/*# | |
* TODO 3: Introduce a local variable called distance which calculates the distance | |
* between the planet and the sun using the distance formula. Research Math.hypot() | |
* for a more convenient way to express the distance formula. | |
*/ | |
/*# | |
* TODO 4: Introduce a local variable called angle which calculates the angle | |
* of rotation in radians between the planet and the sun. Research Math.atan2() | |
* for a convenient way to get the angle using trigonometry. | |
*/ | |
/*# | |
* TODO 5: Now you will calculate the force of gravity. | |
* | |
* Introduce a local variable called gForce to calculate the | |
* gravitational force between the planet and the sun. | |
* | |
* The formula for gravitational force is: | |
* | |
* F = (G * m1 * m2) / distance^2 | |
* | |
* Where: | |
* - m1 is the mass of the first body, i.e. the planet. | |
* - m2 is the mass of the second body, i.e. the sun. | |
* - distance^2 is the squared distance between the two bodies | |
* - and G is the gravitational constant. But in our program, we will let G = 1 | |
* to ignore it for simplicity. | |
*/ | |
/*# | |
* TODO 6: Now you will need to break down gForce into its x and y components. | |
* | |
* Introduce two local variables called gForceX and gForceY. | |
* | |
* We will use trigonometry to calculate these. We already computed the angle | |
* and the magnitude of the force vector, so we can use cos and sin to retrieve | |
* the horizontal and vertical components. | |
* | |
* Set gForceX to gForce multiplied by the cosine of the angle. | |
* Set gForceY to gForce multiplied by the sine of the angle. | |
*/ | |
/*# | |
* TODO 7: Finally, we are going to set the acceleration components ddx and ddy. | |
* | |
* In physics, Force = mass * acceleration. | |
* We can rewrite this as: a = F/m | |
* | |
* Set ddx to gForceX divided by the planet's mass. | |
* Set ddy to gForceY divided by the planet's mass. | |
*/ | |
} | |
/** | |
* Sets the velocity of the planet. | |
* | |
* This method will be used at beginning of simulation to give the | |
* planet an initial velocity. | |
*/ | |
void setVelocity(double dx, double dy) | |
{ | |
/*# | |
* TODO 8: This method simply sets the velocity components | |
* to the values given by the inputs. | |
* | |
* The inputs to this method are named the same as the fields, | |
* so use this.dx and this.dy to distinguish the object's variables | |
* from the method's inputs. | |
*/ | |
} | |
/** | |
* Returns true if the planet collides with the sun and false otherwise. | |
*/ | |
boolean collidesWithSun() | |
{ | |
/*# | |
* TODO 9: Calculate the distance between the planet and the sun. | |
* If that distance is less than the sum of their radii, return true, | |
* otherwise return false. | |
*/ | |
return false; | |
} | |
/** | |
* Calculates and updates the new position, velocity, and acceleration | |
* of the planet. | |
* | |
* This method will be called every frame of the animation to show the planet's motion. | |
*/ | |
void move() | |
{ | |
/*# | |
* TODO 10: Call the applyGravity() method. This will set | |
* the acceleration to what it needs to be for this step of movement. | |
*/ | |
/*# | |
* TODO 11: Now we will change the velocity of the planet. | |
* | |
* First check if this.collidesWithSun(). If so, set dx and dy to zero. | |
* Otherwise increment dx by ddx and increment dy by ddy. | |
*/ | |
/*# | |
* TODO 12: Now we will change the position of the planet. | |
* | |
* Increment x by dx. Increment y by dy. | |
*/ | |
} | |
} |
/*# | |
* Uncomment the code below to enable the application. | |
*/ | |
import javafx.application.Application; | |
import javafx.stage.Stage; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.control.Label; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.paint.Color; | |
import javafx.animation.AnimationTimer; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.input.KeyEvent; | |
import javafx.scene.input.KeyCode; | |
public class SolarSystemApp extends Application | |
{ | |
Canvas canvas; | |
GraphicsContext gc; | |
SolarTimer timer; | |
Planet planet; | |
Sun sun; | |
double mouseX, mouseY; | |
boolean paused; | |
public void start(Stage stage) | |
{ | |
/* | |
canvas = new Canvas(900, 900); | |
gc = canvas.getGraphicsContext2D(); | |
sun = new Sun(450, 450, 20000, 40); | |
initializePlanet(); | |
VBox root = new VBox(canvas); | |
timer = new SolarTimer(); | |
Scene scene = new Scene(root); | |
scene.setOnMouseClicked(this::handleClick); | |
scene.setOnMouseMoved(this::handleMove); | |
scene.setOnKeyPressed(this::handleKey); | |
stage.setScene(scene); | |
stage.show(); | |
unpause(); | |
*/ | |
} | |
/* | |
void pause() | |
{ | |
timer.stop(); | |
paused = true; | |
renderSolarSystem(); | |
} | |
void unpause() | |
{ | |
timer.start(); | |
paused = false; | |
renderSolarSystem(); | |
} | |
void initializePlanet() | |
{ | |
planet = new Planet(250, 350, 50, 10, sun); | |
planet.setVelocity(-2.5, 7); | |
} | |
void handleClick(MouseEvent e) | |
{ | |
if (paused) | |
{ | |
double vectorX = (mouseX - planet.x) / 10; | |
double vectorY = (mouseY - planet.y) / 10; | |
planet.setVelocity(vectorX, vectorY); | |
unpause(); | |
} | |
} | |
void handleMove(MouseEvent e) | |
{ | |
if (paused) | |
{ | |
mouseX = e.getX(); | |
mouseY = e.getY(); | |
renderSolarSystem(); | |
} | |
} | |
void handleKey(KeyEvent e) | |
{ | |
if (!paused && e.getCode() == KeyCode.SPACE) // space bar to pause | |
{ | |
pause(); | |
} | |
else if (e.getCode() == KeyCode.R) // r to reset | |
{ | |
initializePlanet(); | |
unpause(); | |
} | |
} | |
void renderVectorInput() | |
{ | |
gc.save(); | |
gc.setStroke(Color.MAGENTA); | |
gc.setFill(Color.MAGENTA); | |
gc.setLineWidth(2); | |
gc.strokeLine(planet.x, planet.y, mouseX, mouseY); | |
gc.restore(); | |
} | |
void renderVector() | |
{ | |
double vectorX = planet.x + 10 * planet.dx; | |
double vectorY = planet.y + 10 * planet.dy; | |
gc.save(); | |
gc.setStroke(Color.MAGENTA); | |
gc.setFill(Color.MAGENTA); | |
gc.setLineWidth(2); | |
gc.strokeLine(planet.x, planet.y, vectorX, vectorY); | |
gc.restore(); | |
} | |
void renderSun() | |
{ | |
gc.setFill(Color.GOLD); | |
gc.fillOval(sun.x - sun.radius, sun.y - sun.radius, sun.radius * 2, sun.radius * 2); | |
} | |
void renderPlanet() | |
{ | |
gc.setFill(Color.AQUAMARINE); | |
gc.fillOval(planet.x - planet.radius, planet.y - planet.radius, planet.radius * 2, planet.radius * 2); | |
} | |
void renderDataText() | |
{ | |
String xy = "x: " + String.format("%5.3f", planet.x) + ", y: " + String.format("%5.3f", planet.y); | |
String dxdy = "dx: " + String.format("%5.3f", planet.dx) + ", dy: " + String.format("%5.3f", planet.dy); | |
String ddxddy = "ddx: " + String.format("%5.3f", planet.ddx) + ", ddy: " + String.format("%5.3f", planet.ddy); | |
String instructions; | |
if (paused) | |
{ | |
instructions = "PAUSED... Click to set velocity. Press R to restart."; | |
} | |
else | |
{ | |
instructions = "RUNNING... Press SPACE to pause. Press R to restart."; | |
} | |
gc.save(); | |
gc.setLineWidth(2); | |
gc.setFill(Color.AQUAMARINE); | |
gc.fillText(instructions, 5, 15); | |
gc.fillText(xy, 5, 30); | |
gc.fillText(dxdy, 5, 45); | |
gc.fillText(ddxddy, 5, 60); | |
gc.restore(); | |
} | |
void renderSolarSystem() | |
{ | |
gc.setFill(Color.BLACK); | |
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); | |
renderSun(); | |
renderPlanet(); | |
if (paused) | |
{ | |
renderVectorInput(); | |
} | |
else | |
{ | |
renderVector(); | |
} | |
renderDataText(); | |
} | |
*/ | |
class SolarTimer extends AnimationTimer | |
{ | |
public void handle(long time) | |
{ | |
/* | |
planet.move(); | |
renderSolarSystem(); | |
*/ | |
} | |
} | |
} |
class Sun | |
{ | |
/*# | |
* Nothing to do in this class. | |
* | |
* This is an ordinary data class with no behavior. | |
*/ | |
double x, y, mass, radius; | |
Sun(double x, double y, double mass, double radius) | |
{ | |
this.x = x; | |
this.y = y; | |
this.mass = mass; | |
this.radius = radius; | |
} | |
} |
Tuesday, October 18, 2022
Objects Lab
Begin the objects lab. Get the starter code from your Github account after clicking the link below for your class period.
Thursday, October 13, 2022
Object Examples
We created classes that modelled real-world things. This was our first introduction to object-oriented programming.
In this style of programming, related data is bundled together in an object defined from a class. Over the course of the program, the data changes (mutates) through its methods.
Example 1: Radio
Radio class:
class Radio | |
{ | |
int volume; | |
double station; | |
Frequency freq; | |
boolean isOn; | |
enum Frequency | |
{ | |
AM, FM | |
} | |
Radio() | |
{ | |
this.volume = 3; | |
this.station = 100; | |
this.freq = Frequency.FM; | |
this.isOn = false; | |
} | |
void volumeUp() | |
{ | |
if (volume < 10) | |
volume++; | |
} | |
void volumeDown() | |
{ | |
if (volume > 0) | |
volume--; | |
} | |
void tune(double newStation) | |
{ | |
station = newStation; | |
} | |
void togglePower() | |
{ | |
if (isOn == true) | |
{ | |
isOn = false; | |
} | |
else | |
{ | |
isOn = true; | |
} | |
} | |
void toggleFrequency() | |
{ | |
if (freq == Frequency.AM) | |
{ | |
freq = Frequency.FM; | |
} | |
else | |
{ | |
freq = Frequency.AM; | |
} | |
} | |
void play() | |
{ | |
if (isOn == false || volume == 0) | |
{ | |
return; // Exit this method early. | |
} | |
if (station == 101.1 && freq == Frequency.FM) | |
{ | |
System.out.println("It's close to midnight. Something evil's lurking in the dark. Under the moonlight" + | |
" you see a sight that almost stops your heart"); | |
} | |
else if (station == 89.7 && freq == Frequency.FM) | |
{ | |
System.out.println("There is a house way down in New Orleans they call the Rising Sun and it's" + | |
" been the ruin of many a poor boy and God I know I'm one"); | |
} | |
else if (station == 90.9 && freq == Frequency.AM) | |
{ | |
System.out.println("Country roads, take me home to the place I belong. West Virginia, mountain mama" + | |
"Take me home, country roads"); | |
} | |
else | |
{ | |
System.out.println("SSSSSSSSSSSSSSHHSSHHHHSSHHHSSSHHSSSSSSSSHHSSSSS..."); // static noises | |
} | |
} | |
} |
Example 2: Mover object
Mover class:
import java.util.Random; | |
class Mover | |
{ | |
int x; | |
int y; | |
int steps; | |
Random r = new Random(); // r is the name of our random number generator | |
Mover(int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
this.steps = 0; | |
} | |
void randomMove() | |
{ | |
int direction = r.nextInt(4); // random value between 0 and 3. | |
if (direction == 0) | |
{ | |
moveUp(); | |
} | |
if (direction == 1) | |
{ | |
moveDown(); | |
} | |
if (direction == 2) | |
{ | |
moveLeft(); | |
} | |
if (direction == 3) | |
{ | |
moveRight(); | |
} | |
} | |
void moveUp() | |
{ | |
y++; | |
steps++; | |
} | |
void moveDown() | |
{ | |
y--; | |
steps++; | |
} | |
void moveLeft() | |
{ | |
x--; | |
steps++; | |
} | |
void moveRight() | |
{ | |
x++; | |
steps++; | |
} | |
} |
Mover World
World class:
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.input.KeyEvent; | |
import javafx.scene.input.KeyCode; | |
import javafx.animation.AnimationTimer; | |
import javafx.scene.paint.Color; | |
public class World extends Application | |
{ | |
GraphicsContext gc; | |
Mover m; | |
int stepSize = 5; | |
public void start(Stage stage) | |
{ | |
Canvas canvas = new Canvas(500, 500); | |
gc = canvas.getGraphicsContext2D(); | |
m = new Mover(250 / stepSize, 250 / stepSize); | |
VBox container = new VBox(canvas); | |
Scene moverScene = new Scene(container); | |
moverScene.setOnKeyPressed(this::handleKey); | |
stage.setScene(moverScene); | |
stage.show(); | |
WorldTimer timer = new WorldTimer(); | |
timer.start(); | |
} | |
void handleKey(KeyEvent e) | |
{ | |
KeyCode k = e.getCode(); | |
if (k == KeyCode.UP) | |
{ | |
m.moveDown(); // Y values decrease moving upwards on screens | |
} | |
else if (k == KeyCode.RIGHT) | |
{ | |
m.moveRight(); | |
} | |
else if (k == KeyCode.DOWN) | |
{ | |
m.moveUp(); | |
} | |
else if (k == KeyCode.LEFT) | |
{ | |
m.moveLeft(); | |
} | |
else if (k == KeyCode.SPACE) | |
{ | |
m.randomMove(); | |
} | |
} | |
void drawMover(Mover mover, Color color) | |
{ | |
gc.setFill(color); | |
gc.fillOval(mover.x * stepSize, mover.y * stepSize, 10, 10); | |
} | |
void drawSteps() | |
{ | |
gc.setFill(Color.BLACK); | |
gc.fillText("Steps: " + m.steps, 5, 15); | |
} | |
void eraseScreen() | |
{ | |
gc.setFill(Color.WHITE); | |
gc.fillRect(0, 0, 500, 500); | |
} | |
class WorldTimer extends AnimationTimer | |
{ | |
public void handle(long t) | |
{ | |
eraseScreen(); | |
drawSteps(); | |
drawMover(m, Color.RED); | |
} | |
} | |
} |
Wednesday, September 28, 2022
Programming Syntax and Semantics Review
Classwork group activity: Programming Syntax and Semantics Review
Monday, September 26, 2022
Project Euler
More programming challenges are available at projecteuler.net.
Create an account to check your answers and track your progress.
Tuesday, September 20, 2022
Study Guide: Logical Validity
Study Guide: Logical Validity Test Study Guide
Monday, September 19, 2022
Classic Programming Challenges
Problem Set: Classic Programming Challenges
Thursday, Spetember 15, 2022
Methods with While Loops
Code examples:
class LoopMethods | |
{ | |
/** | |
* Prints all integers from 0 to n. | |
*/ | |
static void countTo(int n) | |
{ | |
int counter = 0; | |
while (counter <= n) | |
{ | |
System.out.println(counter); | |
counter++; //increment the variable | |
} | |
} | |
/** | |
* Prints all characters in a range from start to end. | |
*/ | |
static void printChars(int start, int end) | |
{ | |
while (start <= end) | |
{ | |
System.out.println(start + ": " + (char)start); | |
start++; | |
} | |
} | |
/** | |
* Sings an irritating song. | |
*/ | |
static void sing(int verses) | |
{ | |
while (verses > 0) | |
{ | |
System.out.println(verses + " bottles of beer on the wall, " + | |
verses + " bottles of beer..."); | |
verses--; | |
} | |
System.out.println("No more bottles of beer on the wall."); | |
} | |
/** | |
* Prints a horizontal row of stars. | |
*/ | |
static void printStars(int howMany) | |
{ | |
int counter = 1; | |
while (counter <= howMany) | |
{ | |
System.out.print("*"); | |
counter++; | |
} | |
} | |
/** | |
* Prints several rows of stars, increasing number to form a triangle. | |
*/ | |
static void starTriangle(int rows) | |
{ | |
int counter = 1; | |
while (counter <= rows) | |
{ | |
printStars(counter); | |
System.out.println(); //go to a new line | |
counter++; | |
} | |
} | |
/** | |
* Prints all odd numbers from 1 to the given number. | |
*/ | |
static void printOdds(int upto) | |
{ | |
int counter = 1; | |
while (counter <= upto) | |
{ | |
System.out.println(counter); | |
counter += 2; // Increment by 2 | |
} | |
} | |
/** | |
* Return the sum of consecutive whole numbers from 1 to n. | |
* | |
* 1 + 2 + 3 + 4 + ... + n | |
*/ | |
static int sumConsecutive(int n) | |
{ | |
int counter = 1; | |
int total = 0; | |
while (counter <= n) | |
{ | |
total += counter; //Increment by whatever number is on the counter. | |
// System.out.println("Added " + counter + " to the total. Now the total is " + total); | |
counter++; | |
} | |
return total; | |
} | |
} |
Wednesday, September 14, 2022
Logical Validity
Classwork/Homework Activity: Checking Logical Validity
Friday, September 9, 2022
Submit Circuits Lab
Turn in your lab by sharing the google doc of your screenshots.
Tuesday, September 6, 2022
Begin Circuits Lab
Lab Instructions: Logic Gates Lab
Friday, September 2, 2022
Methods Practice Problems
Problem Set: Methods Practice Problems
Wednesday, August 31, 2022
Boolean Algebra Test Study Guide
Classwork/Homework Activity: Boolean Algebra Test Study Guide
Tuesday, August 30, 2022
Static Methods
Code Examples:
class MathExamples | |
{ | |
// The value of PI | |
final static double PI = 3.1415926; | |
static double circleArea(double radius) | |
{ | |
return PI * radius * radius; | |
} | |
static double average(double a, double b) | |
{ | |
return (a + b) / 2; | |
} | |
// Example of overloaded method: | |
// Same name, but different arguments. | |
static double average(double a, double b, double c) | |
{ | |
return (a + b + c)/3; | |
} | |
static double triangleArea(double base, double height) | |
{ | |
return base * height / 2; | |
} | |
// We can use conditionals in methods | |
static double abs(int x) | |
{ | |
if (x >= 0) | |
{ | |
return x; | |
} | |
else | |
{ | |
return x * -1; | |
} | |
} | |
static boolean isEven(int n) | |
{ | |
if (n % 2 == 0) | |
{ | |
return true; | |
} | |
return false; | |
} | |
static String greeting(String name) | |
{ | |
return "Hello there, " + name + "! Have a great day."; | |
} | |
// Use the void keyword when a method does not return a value. | |
static void greeting2(String name) | |
{ | |
System.out.print("Hello there, " + name + "! Have a great day."); | |
} | |
static double square(double x) | |
{ | |
return x * x; | |
} | |
// This method calls other methods to accomplish its objective. | |
static double solveHypotenuse(double a, double b) | |
{ | |
double cSquared = square(a) + square(b); | |
greeting2("Pythagoras"); | |
return Math.sqrt(cSquared); | |
} | |
static String coinFlip() | |
{ | |
if (Math.random() < 0.5) | |
{ | |
return "Heads"; | |
} | |
return "Tails"; | |
} | |
static int rollDice(int sides) | |
{ | |
return (int) (Math.random() * sides) + 1; | |
} | |
// A method can even call itself! | |
static void countDown(int start) | |
{ | |
if (start < 0) | |
{ | |
throw new IllegalArgumentException( | |
"The starting number can't be negative."); | |
} | |
else if (start == 0) | |
{ | |
System.out.println("Zero! Blast off!"); | |
} | |
else | |
{ | |
System.out.println(start); | |
countDown(start - 1); // A recursive call to this method | |
} | |
} | |
} |
Monday, August 29, 2022
Truth Tables
Classwork/Homework Activity: Truth tables exercises
Friday, August 26, 2022
Turn in lab assignments on github
When you have made your github account, click the link below for your class period:
Wednesday, August 17, 2022
Begin Classes and Conditionals Lab
Work with your group to complete the three parts of the lab.
- Instructions: lab-designing-classes-with-conditionals.pdf
Monday, August 15, 2022
Classes with Constructors
Code examples:
// Note: Each class and enum should be written in its own file. | |
class Point | |
{ | |
double x; | |
double y; | |
Point(double x, double y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
} | |
class Time | |
{ | |
int hours; | |
int minutes; | |
int seconds; | |
// Protect integrity of data with exceptions (error messages) | |
Time(int hours, int minutes, int seconds) | |
{ | |
if (hours > 12 || hours < 1) | |
{ | |
throw new IllegalArgumentException("Hours must be between 1 and 12."); | |
} | |
if (minutes > 59 || minutes < 0) | |
{ | |
throw new IllegalArgumentException("Minutes must be between 0 and 59."); | |
} | |
if (hours > 12 || hours < 1) | |
{ | |
throw new IllegalArgumentException("Hours must be between 1 and 12."); | |
} | |
this.hours = hours; | |
this.minutes = minutes; | |
this.seconds = seconds; | |
} | |
} | |
class MovieTicketSale | |
{ | |
String movieName; | |
double price; | |
Time showTime; | |
MovieRating rating; | |
MovieTicketSale(String movieName, double price, Time showtime, MovieRating rating) | |
{ | |
if (price < 0) | |
{ | |
throw new IllegalArgumentException("The price cannot be negative."); | |
} | |
this.movieName = movieName; | |
this.price = price; | |
this.showTime = showTime; | |
this.rating = rating; | |
} | |
} | |
enum MovieRating | |
{ | |
G, PG, PG13, R, NR | |
} | |
class Segment | |
{ | |
Point a; | |
Point b; | |
Segment(Point a, Point b) | |
{ | |
if (a.x == b.x && a.y == b.y) | |
{ | |
throw new IllegalArgumentException("A segment may not have identical endpoints."); | |
} | |
} | |
// Overloaded constructors provide another way to make an object | |
Segment(double x1, double y1, double x2, double y2) | |
{ | |
if (x1 == x2 && y1 == y2) | |
{ | |
throw new IllegalArgumentException("A segment may not have identical endpoints."); | |
} | |
this.a = new Point(x1, y1); | |
this.b = new Point(x2, y2); | |
} | |
} |
Wednesday, August 10, 2022
Data Definitions and Designing Classes
Problem Set: Data Definitions Practice
Tuesday, August 9, 2022
Designing Classes
Code examples:
// Note: Each class and enum should be written in its own file. | |
class Point | |
{ | |
double x; | |
double y; | |
} | |
class Time | |
{ | |
int hours; | |
int minutes; | |
int seconds; | |
} | |
class MovieTicketSale | |
{ | |
String movieName; | |
double price; | |
Time showTime; | |
MovieRating rating; | |
} | |
enum MovieRating | |
{ | |
G, PG, PG13, R, NR | |
} | |
class Segment | |
{ | |
Point a; | |
Point b; | |
} |
Monday, August 8, 2022
Definitions by Genus and Difference
Classwork/Homework Activity: Definitions by Genus and Difference
Thursday, August 4, 2022
Paradox Activity
Group Activity: Classic Paradoxes