Monday, October 20, 2008

CoffeeShop and Food classes

10/20/2008 minor project:

Today you will need to make a CoffeeShop class:
//Address
//Manager's name
//An arraylist of food


Food class:
//Price of food
//Description of food
//Quantity
//Make a constructor!
//Create a default constructor!


Within the main:
//The user should be able to make the CoffeeShop...
//The user should be able to enter food into the CoffeeShop (using an arraylist)
//The user to be able to check what's inside the CoffeeShop!

Please upload this minor project to your FTP space ASAP! Also don't forget about your third major project from the book using inheritance!

Wednesday, October 15, 2008

Third Major Project

This is a fairly easy question straight from the book:

P10.6 from page 493

Please have it on the FTP space by next Tuesday 10/21 at midnight.

If you have any other work, please put it online and let me know that you uploaded it.

Write a superclass Worker and subclasses HourlyWorker and SalariedWorker. Every worker has a name and a salary rate. Write a method computePay(int hours) that computes the weekly pay for every worker. An hourly worker gets paid the hourly wage for the actual number of hours worked, if hours is at most 40. If the hourly worker worked more than 40 hours, the excess is paid at time and a half. The salaried worker gets paid the hourly wage for 40 hours, no matter what the actual number of hours is. Supply a test program that uses polymorphism to test these classes and methods.

Use the following class as your tester class: /**
This class tests class Worker and its subclasses.
*/
public class WorkerTester
{
public static void main(String[] args)
{
Worker s = new SalariedWorker(''Sally'', 40);
Worker h = new HourlyWorker(''Harry'', 40);
System.out.println(s.computePay(30));
System.out.println(''Expected: 1600'');
System.out.println(h.computePay(30));
System.out.println(''Expected: 1200'');
System.out.println(s.computePay(50));
System.out.println(''Expected: 1600'');
System.out.println(h.computePay(50));
System.out.println(''Expected: 2200'');
}
}

Thursday, October 9, 2008

Minor Project

For a minor project, I would like everyone to answer Exercise P7.8. and put it on your FTP space by next Tuesday.

Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data
then it computes

Use the following class in your solution:
/**
This class computes the alternating sum
of a set of data values.
*/
public class DataSet
{
/**
Constructs an empty data set.
*/
public DataSet()
{
final int DATA_LENGTH = 100;
data = new double[DATA_LENGTH];
dataSize = 0;
}

/**
Adds a data value to the data set.
@param x a data value
*/
public void add(double x)
{
if (dataSize >= data.length)
{
// make a new array of twice the size
double[] newData = new double[2 * data.length];
// copy over all elements from data to newData
System.arraycopy(data, 0, newData, 0, data.length);
// abandon the old array and store in data
// a reference to the new array
data = newData;
}
data[dataSize] = x;
dataSize++;
}

/**
Gets the alternating sum of the added data.
@return sum the sum of the alternating data or 0 if no data has been added
*/
public double alternatingSum()
{
. . .
}
private double[] data;
private int dataSize;
}

Use the following class as your tester class:
/**
This program calculates an alternating sum.
*/
public class AlternatingSumTester
{
public static void main(String[] args)
{
DataSet data = new DataSet();

data.add(1);
data.add(4);
data.add(9);
data.add(16);
data.add(9);
data.add(7);
data.add(4);
data.add(9);
data.add(11);

double sum = data.alternatingSum();
System.out.println("Alternating Sum = " + sum);
System.out.println("Expected: -2");
}
}

Monday, October 6, 2008

Static method practice in class

  1. Make a static method that takes in two numbers and multiples them and returns the result. Call it multiplier(int x, int y)!
  2. Make a static method that takes in two integers and returns the lower of the two
    call it findMinimum()
    print out the results!
  3. Make a static method, call it dividerOfTwoNumbers(int x, int y){ }
    Ask the user for two numbers. Divide the first number by the second number IF the second number is not zero.
    If they enter a zero, tell them: "You fool! You can't divide by zero!"

Wednesday, October 1, 2008

Test 2 Review

Click here for the test 2 review.

Pseudocode to do Blackjack

What would you need to make a BlackJack program?

What classes? Within those classes, what attributes and methods would you need?

What's the input? What's the output?

Let's start by writing the pseudocode on your blog...

You can win an iPod!

Miami Dade is giving away an iPod!