Category Archives: Video

Built-in: Distance Calculation and Magic Numbers

 
 

Write a program Distance_YI.java with these values x1 = 3.0, x2 = 5.0, y1 = 2.5 and y2 = -1.9 and prints the Euclidean distance from the point (x1, y1) to the origin (x2,y2). Your program should display the numbers from each point with exactly the same format, meaning include parenthesis and comma and the distance as one number in a sentence format. This is what it should look like:

“The distance between points (3.0,5.0) and (2.5,-1.9) is 4.8332”


Resources:

Math.sqrt(4) —> 2.0 Math.pow(base,exponent);

Example: Math.pow(2,5) —> 32

Use the following data type and make sure all your code is inside the main method:

double x1 = 3.0;


NOTE: do not use numbers. Use variables. Using numbers is called “MAGIC NUMBERS”
“MAGIC NUMBERS” are consider bad programming practice.

 

In this image and in the video you will see how “constants” are represented and used.

Conditionals: Monty Hall

monty-hall

Assignment:
Game simulation.
In the game show Let’s Make a Deal, a contestant is presented with three doors. Behind one door is a valuable prize, behind the other two are gag gifts. After the contestant chooses a door, the host opens up one of the other two doors (never revealing the prize, of course).

The contestant is then given the opportunity to switch to the other unopened door. Should the contestant do so? Intuitively, it might seem that the contestant’s initial choice door and the other unopened door are equally likely to contain the prize, so there would be no incentive to switch. Write a program MonteHall_YI.java to test this intuition by simulation.

Your program should take an integer command-line argument n, play the game n times using each of the two strategies (switch or don’t switch) and print the chance of success for each strategy.

End the program with a message stating the conclusion: Is a bigger chance of winning if you switch to a different door?

OOD Challenge Exercise

Chapter 1: VN 1.4 Solving a challenge exercise

Exercise 1:
Add a sunset to the single-sun version of Picture. That is: make the sun go
down slowly. Remember: The circle has a method slowMoveVertical
that you can use to do this.

Exercise 2: If you added your sunset to the end of the draw
method (so that the sun goes down automatically when the picture is
drawn), change this now. We now want the sunset in a separate method,
so that we can call draw and see the picture with the sun up, and then
call sunset (a separate method!) to make the sun go down.

You will find this code:
private Square wall;

private Square window;

private Triangle roof;

private Circle sun;

You need to add a line here for the second sun,

For example: private Circle sun2;

Then write the appropriate code for creating the second sun.

Exercise 3: Close the picture project, and open the lab-classes project.
This project is a simplified part of a student database designed to keep track of students in laboratory classes and to print class lists.
Create an object of class Student. You will notice that this time you
are not only prompted for a name of the instance, but also for some
other parameters. Fill them in before clicking Ok. (Remember that
parameters of type String must be written in double-quotes.)

OOD: BallDriver and CollidingBalls Videos

BallDriver Video #1
A short video for Ball and BallDriver – Looking at the geometry behind the relative distance between 2 balls.
Oops: I meant radius.

BallDriver Video #2
Going over the first changes we did to the original code.

BallDriver Video #3
Integrating the geometry into the implementation to have a better way to determine the distance apart from a pair of balls.

BallDriver Video #4
Overloading
A quick look at two constructors for the same class.

BallDriver Video #5
Ball ADT and BallDriver Complete
This is the final version of the BallDriver assignment. The important concept to learn is how accessor methods are used to get information from the input argument which is an object of the Ball ADT. This illustrates ENCAPSULATION since instance fields are private and public methods are needed to get access to their values.

BallDriver Video #6
Last video and final implementation (no color)

BallDriver Video #7
Code for color changing of the balls

BallDriver Video #8
Putting color in the BallDriver assignment.

CollidingBalls Video #9
Overloading Trick

How to make a short video of your animations using QuickTime