Tuesday, April 29, 2008

Java Final Exam!

Topics:
  • Recursion
  • ArrayList
  • Strings
  • double vs. int
  • Writing methods.

The coding part will be the math game. You can do it ahead of time and put it on your FTP space. It's more accurate than hand-coding it on paper.

It won't take too long! I would like to spend time in class discussing queues, stacks, and other data structures...

Deadline for final project: Friday.

Assignments

http://mdcjava.blogspot.com/search?q=assignment

Plus blackjack!

Write a blog post about the status of your final project!

Please write a brief blog post about everything that you've done so far towards your final project (picked by you!).

See how well you do on this test.

Remember the different certifications available?

Check out these 10 questions from Sun.

How did you do?

What is going on here?

What is all this about? Comment on your blog.

Also please read the single most important thing you must do to improve your programming career.

Chapter 14



Here is the source code for the examples from class:
http://www.davidcfreer.com/~David/ch14wiley.zip

Chapter 15 Slides



http://www.davidcfreer.com/~David/ch15wiley.zip

Using three IDEs (and Notepad) to create Java programs.

Using simply notepad (and without looking on the Internet) create a program that will keep track of a car's mileage, mph, gas in the tank, and the known sum spent on gas. You will create methods to simulate driving a certain distance. The program must calculate the miles driven and the gas left in the tank. The same method should also allow for the user to enter in the price of gas, which will be kept in a variable.

Download this. Open it with BlueJ.

Create a class to keep track of zoo animals. Use Netbeans to enter in their information. Store the info in an ArrayList when a button is clicked.

Lastly using Eclipse, write a program that teaches arithmetic to your younger brother. The program tests addition and subtraction. In level 1 it tests only addition of numbers less than 10 whose sum is less than 10. In level 2 it tests addition of arbitrary one-digit numbers. In level 3 it tests subtraction of one-digit numbers with a non-negative difference. Generate random problems and get the player input. The player gets up to two tries per problem. Advance from one level to the next when the player has achieved a score of five points.


Use the following class as your main class:
/**
This program runs the math game.
*/
public class MathGameRunner
{
public static void main(String[] args)
{
MathGame game = new MathGame();
game.play();
}
}

Thursday, April 24, 2008

Recursive

Write and test a recursive method/function that returns the maximum among the first n elements of an array.

More recursive fun:

Write and test a recursive method/function that returns the SUM of the squares of the first n positive integers.

Put your code on your blog when you are finished!

GCD

Examine this code for GCD:
public static int gcd(int m, int n)
{
if(m==n) return n;
else if(m < n) return gcd(m,n-m);
else return gcd(m-n,n);
}
//WHY DOES THIS WORK???

Think about them before you give up.

More brain teasers:

  1. Consider a two-player game played on a circular table of unspecified diameter. Each player has an infinite supply of quarters, and take turns placing a quarter on the table such that it is completely on the table and does not overlap with any other quarters already played. A player wins if he makes the last legal move. Which player (if any) has a strategy that will guarantee a win, and what is that strategy?
  2. How do you divide a cake among n people, maximizing fairness?

What do you think?

Blog post about Microsoft

http://moishelettvin.blogspot.com/2006/11/windows-shutdown-crapfest.html

When you are done checking out the article, I'd like you to attempt this test online:

http://www.betterprogrammer.com/

Better "Tower of Hanoi"

public class Hanoi {
public static void main(String [] args)
{
runHanoi(3,'A', 'B', 'C');
}

public static void runHanoi(int n, char x, char y, char z)
{
if(n==1)
System.out.println("Move top disk from peg " + x
+ " to peg " + z);
else
{
runHanoi(n-1,x,z,y);
runHanoi(1,x,y,z);
runHanoi(n-1,y,x,z);
}
}
}

Very strict OO approach

Check this out:

http://binstock.blogspot.com/2008/04/perfecting-oos-small-classes-and-short.html


  • Lots of methods!
  • Longer variable names!
  • No else?

Comment on your blog: what do you think about this approach?

GUI Programming

import javax.swing;
//New Frame
//New Label
//add
//setVisible(true)
//setSize

Here's some source code:
Calculator example.

Remember what we did with connecting Java to the database?

Look at this.

Tuesday, April 22, 2008

sum all the values of an array

/**
Computes the sum of a set of data values.
*/
public class DataSet
{
/**
Constructs a DataSet object.
@param values the data values
@param first the first value in the data set
@param last the last value in the data set
*/
public DataSet(int[] values, int first, int last)
{
this.values = values;
this.first = first;
this.last = last;
}

/**
Gets the sum in the set of data values
@return the sum value in the set
*/
public int getSum()
{
if (first > last)
return 0;
DataSet tail = new DataSet(values, first + 1, last);
int tailSum = tail.getSum();
return values[first] + tailSum;
}

private int[] values;
private int first;
private int last;
}

Your final project

I would like for you to think of an interesting problem to solve using Java. It could be console based or done with Netbeans or even an applet coded from scratch!

I'd like for you to think about something you are interested in! If you are into cars maybe you could think of a Java application that helps a dealer. If you like games, perhaps you can come up with an interesting game! Please write your idea on your blog today.

Final due:
MAY 3rd 2008 at 8am!!!!

Question and solution

Using recursion, find the largest element in an array.
public class DataSet
{
public DataSet(int[] values, int first, int last) { . . . }
public int getMaximum() { . . . }
. . .
}
Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element.


Used the following class as your tester class:
import java.util.Random;

/**
A tester class for the recursive maximum.
*/
public class DataSetTester
{
public static void main(String[] args)
{
int[] values = { 1, 10, 100, -1, -10, -100, 100, 0 };
DataSet d = new DataSet(values, 0, values.length - 1);
System.out.println("Maximum: " + d.getMaximum());
System.out.println("Expected: 100");
}
}


//SOLUTION:

/**
Computes the maximum of a set of data values.
*/
public class DataSet
{
/**
Constructs a DataSet object.
@param values the data values
@param first the first value in the data set
@param last the last value in the data set
*/
public DataSet(int[] values, int first, int last)
{
this.values = values;
this.first = first;
this.last = last;
}

/**
Gets the maximum in the set of data values
@return the maximum value in the set
*/
public int getMaximum()
{
if (first > last)
return Integer.MIN_VALUE;
DataSet tail = new DataSet(values, first + 1, last);
int tailMax = tail.getMaximum();
return Math.max(values[first], tailMax);
}

private int[] values;
private int first;
private int last;

Recursion explained graphically with Paint

http://weblogs.java.net/blog/mortazavi/archive/2005/08/recursive_progr.html

Tower of Hanoi!

Put on blog!

Chapter 13: Recursion!

Which way do you see her going?

Click here!

Monday, April 21, 2008

Random?

I found this graphic representation of Java's random function.

http://www.alife.co.uk/nonrandom/

It doesn't appear that random.

Also, this can't be true, can it?

Thursday, April 17, 2008

Interested in Linux?

Check this out.

Check this out!

Here is a great book for algorithm and data structure design:

http://books.google.com/books?q=The+Algorithm+Design+Manual+

Click on the first link. I doubt Google will update THEIR algorithm until you click on the link. ;)

Isn't books.google.com cool?

---

Also I would like for you to check out this tutorial about game design with Java.

After the test...

Today's "minor project" is to create a Jumble game where the user is given a scrambled word and they have to enter in the correct word. You can use Netbeans to create a GUI or use a console based application.

This is what I'm talkin' bout!!!

Show me it works when you are finished.

Read this article and respond to it on your blog.

Read this article and let me know what you think. Good idea?

Funny explanation for inner classes!

Read this to better understand inner classes:
http://www.javaranch.com/campfire/StoryInner.jsp

Tuesday, April 15, 2008

Next Project 6

davidcfreer.com/~David/zuul-better.zip

For the next major project you will have two choices...you can either create a console based Monopoly game (or graphical Monopoly game for major extra credit) or you can edit the code and create an interesting game out of the source code above.

Other creative game ideas are possible as well. Let me know.

DUE next Tuesday.

Monopoly Pseudocode

Start with students writing on their blogs the pseudocode for Monopoly.
Thinking in an object oriented fashion!

What classes would they need?

What methods would they need?

What instance variables?

http://en.wikipedia.org/wiki/Monopoly_(game)

Chapter 9

Polymorphism

What is polymorphism?

Great overview from Sun.

Not related to programming...

but really interesting nonetheless.

Monday, April 14, 2008

A little project I'm working on now...

One of my fav. games :)

public class Monopoly {
private Property [] gameboard = new Property[40];
//create new game board

public Monopoly()
{
gameboard[0] = new Property("Go");
gameboard[1] = new Property("Mediterranean");
gameboard[2] = new Property("Community Chest");
gameboard[3] = new Property("Baltic");
gameboard[4] = new Property("Income Tax");
gameboard[5] = new Property("Reading Railroad");
gameboard[6] = new Property("Oriental");
gameboard[7] = new Property("Chance");
gameboard[8] = new Property("Vermont");
gameboard[9] = new Property("Connecticut");
gameboard[10] = new Property("Jail");//all okay
gameboard[11] = new Property("St. Charles");
gameboard[12] = new Property("Electric");
gameboard[13] = new Property("States");
gameboard[14] = new Property("Virginia");
gameboard[15] = new Property("Pennslyvania Railroad");
gameboard[16] = new Property("St. James");
gameboard[17] = new Property("Community Chest");
gameboard[18] = new Property("Tennessee");
gameboard[19] = new Property("New York");
gameboard[20] = new Property("Free Parking");
gameboard[21] = new Property("Kentucky");
gameboard[22] = new Property("Chance");
gameboard[23] = new Property("Indiana");
gameboard[24] = new Property("Illinois");
gameboard[25] = new Property("B&O Railroad");
gameboard[26] = new Property("Atlantic");
gameboard[27] = new Property("Ventnor");
gameboard[28] = new Property("Waterworks");
gameboard[29] = new Property("Marvin Gardens");
gameboard[30] = new Property("Go to jail");
gameboard[31] = new Property("Pacific");
gameboard[32] = new Property("North Carolina");
gameboard[33] = new Property("Community Chest");
gameboard[34] = new Property("Pennslyvania");
gameboard[35] = new Property("Short Line Railroad");
gameboard[36] = new Property("Chance");
gameboard[37] = new Property("Park Place");
gameboard[38] = new Property("Luxury Tax");
gameboard[39] = new Property("Boardwalk");
//all full!
}
public int[] returnOwner()
{
int []i = new int[40];

}

Friday, April 11, 2008

Fifth Major Assignment

The Fifth Major Assignment is straight from the book and relates to inheritance.

Answer Exercises P10.3, P10.5, P10.6 by Monday at 11:59am. (Deadlines are good for focusing your attention.)

Please put each question in a separate folder on your FTP space.

Tuesday, April 8, 2008

ArrayList, LinkedList...intro to Data Structures

Here is the code for the ArrayList examples.

Read about dynamic arrays.

Here is the example for the LinkedList.

Read about linked lists.

Major Assignment Three and Four

Major Assignment 3:
As promised, here's your next major assignment which will be due before midnight on Sunday April 5th 2008. I'm in the process of going over the Blackjack programs tonight.

For this third assignment, I'm giving you a completed class and two partially completed classes for a mail server system.

Use BlueJ for this assignment. Create a new project called mail-system and within it three classes: MailClient, MailServer, MailItem.

Use the code I gave you but fill in the missing behaviors based on the names of the methods. It's quite a bit simpler than the Blackjack program.


Here's the code for the classes.
On blog for Record Keeping
Emailed April 2nd.


Major Assignment 4:
Create a Tic Tac Toe GUI by Sunday at midnight.
+20 points for an Applet online at davidcfreer.com

What are you interested in doing for a final project?

Any ideas?

For fun, try this out!

Check out the source code...pretty cool.

Also about video games, did you know what a "kill screen" is? Funny.

Evaluations Today:

YEAR: 72

IDENTIFICATION NUMBER: 200446993

If programming languages were boats...

Funny post.

Thursday, April 3, 2008

Tuesday, April 1, 2008

What is your feeling about BlueJ as a tool to learn Java?

Good, bad, or negative?

Predicting the future...

Will Java be used much in 10 years ? Why or why not? Try to predict the future...

Create new database, table, and front-end app with Netbeans

Using the steps we just did in class, make a database, table and front end application with Netbeans. It can be about anything you want! Show me and I'll grade it in class.

Creating tables within MySQL

CREATE TABLE address_books (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15), PRIMARY KEY (phone_number));


INSERT INTO address_books (first_name,last_name,phone_number) VALUES ('David','Freer','786-877-4573')";