Project 1
Class: Rectangle
Fields: length, width (can be integers)
Constructors: no-args, initialize, copy
methods: accessors, modifiers, getArea, getPerimeter, getLengthOfDiagonal
isSquare // checks to see if the rectangle is a square
quadratize // converts the rectangle into a square with approximately the same area- the closest possible for a square with an integer side
toString// returns a String representation of a “Rectangle” object
Project 2
Class: Fraction
Fields: numerator, denominator
Constructors, no-args (0/1), initialize (2 ints), initialize (1 int), copy
methods:
accessors and modifiers for fields
void reduce()
Fraction add(Fraction other)
Fraction subtract(Fraction other)
Fraction multiply(Fraction other)
Fraction divide(Fraction other)
double toDecimal()
String toString()
boolean equals(Object other)
Project 3
Class: Card
Fields: int rank (1-13), String suit (Hearts, Diamonds, Clubs, Spades)
Constructors: default, initialize, copy
Methods:
accessors, modifiers
boolean equals(Object other)
String toString()
Class: ThreeCardHand
Fields: card1, card2, card3
Constructors: default, initialize
Methods:
accessors, modifiers
boolean isFlush() //all cards same suit
int numberOfSameRank()
boolean isBetterHand(ThreeCardHand other)//returns true if ThreeCardHand calling method is better or equal
1) Three of a Kind (If both have this, the higher card wins)
2) Flush (All flushes are equal)
3) Two of a Kind (All pairs are equal)
Project 4
Coin Class
field: boolean isHeads
constructors: default, initialize
methods: accessor, void flip()
ValuedCoin Class
fields: boolean isHeads
int valueOf // 1,5,10,25
constructors: default, initialize
methods: accessors and modifiers for new field
Player Class
fields: winnings, coin1 (which is a Valued coin)
constructors: default, initialize
methods: accessors, modifiers
HeadsGame Class
fields: player1, player2, pot, decidingCoin
constructors: default, initialize
methods: void play()
A Game will be played between two players. Both players will flip a coin. If both players flip tails, both coins are added to the pot. If one player flips a heads and the other flips a tails, the player who flips the heads will take the pot and the coin of the other player. If both players flip a heads, the deciding coin will be flipped. If it lands heads, player A takes the pot and player B’s coin. If it lands tails, player B takes the pot and player A’s coin.
Project 5
1) Create a Circle class in blueJ
2) Create 3 fields: xCoord, yCoord, and radius (all integers)
3) Create 3 accessor methods that will return the field in a client class (i.e. getRadius())
4) Create 3 modifier methods that will allow you to change a field in a client class (i.e. setRadius())
5) Create a method called getCenter() that returns a string of the center (i.e. (4, -2))
6) Create a method called getDiameter() that returns the diameter of a circle
7) Create a method called getCircumference() that returns the circumference of a circle
8) Create a method called getArea() that returns the area of a circle
** 9) Create a method called getEquation() that returns the equation of the circle in general form as a String.
i.e. If center is (2, -1) and radius = 6, the equation in
standard form is (x – 2)^2 + (y +1)^2 = 36
general form is x^2 – 4x + y^2 + 2y – 31 = 0
Note: Try to get the equation to look like this. (i.e. not x^2 + -4x + y^2 + 2y – 31 = 0)
public class CircleTester // Create a CircleTester class with the following code
{
public static void main (String[] args)
{
Circle c1 = new Circle(5,3,4);
System.out.println("My center is " + c1.getCenter());
System.out.println("My radius is " + c1.getRadius());
System.out.println("My diameter is " + c1.getDiameter());
System.out.println("My circumference is " + c1.getCircumference());
System.out.println("My area is " + c1.getArea());
System.out.println("My equation is " + c1.getEquation());
c1.setXCoord(2);
c1.setYCoord(-5);
c1.setRadius(3);
System.out.println("My center is " + c1.getCenter());
System.out.println("My radius is " + c1.getRadius());
System.out.println("My diameter is " + c1.getDiameter());
System.out.println("My circumference is " + c1.getCircumference());
System.out.println("My area is " + c1.getArea());
System.out.println("My equation is " + c1.getEquation());
}
}
The output should be:
My center is (5, 3)
My radius is 4
My diameter is 8
My circumference is 25.132741228718345
My area is 50.26548245743669
My equation is x^2 - 10x + y^2 - 6y + 18 = 0
My center is (2, -5)
My radius is 3
My diameter is 6
My circumference is 18.84955592153876
My area is 28.274333882308138
My equation is x^2 - 4x + y^2 + 10y + 20 = 0
Project 6: Complex Class
a) Create a class Complex (which represents a complex number a + bi) with two fields of the type double and two constructors described as follows: one that takes in two doubles, and one that takes in one double which should be set to the real part, a.
b) Add a method abs to your class that returns for a complex number.
c) Recall that if a + bi and c + di are two complex numbers, their sum is defined as (a + c) + (b + d)i. Write the Complex class’s add method, which builds and returns the sum of this number and other:
d) Add a toString method to your Complex class that returns a String representation of the number in the form a + bi
e) Test your abs, add, and toString methods.
f) Create a method called multiply which multiplies two Complex objects and returns another Complex object (note: neither of these Complex objects should be changed)
g) Overload the add and multiply method that accepts a double value instead of a Complex object.
Project 7
Class: Student
Fields: String name, int test1, int test2, int test3
Methods: Accessors, modifiers
double getAverage()- returns the average of the three test grades
void replaceLowestGradeWithAverage()- replaces the lowest test grade with the average test grade (rounded to the nearest integer).
String toString() which should return:
i.e. Craig Sapp has a test average of 53.
Class: StudyGroup
Description: A StudyGroup “Has-A” Student (actually it has 3 Students as fields).
Create an initialize constructor (this would accept 3 Student object references)
Methods:
double getAverage() – returns the average grade of the three AP Stats Students
String nameOfHighestAverage() – returns the name of the student in the study group with the highest average
String toString() – returns a String in the following format:
Craig Sapp has a C in AP Stats
Jon Daly has a B in AP Stats
Tiger Woods has a A in AP Stats
Tiger Woods has the highest average in the study group
Note: You can assume one of the students will have the highest average outright (no ties for the highest).
Fields:
int pagesPrinted → an instance variable that tracks how many pages this specific printer has printed.
static int totalPagesPrinted → a static variable that tracks how many total pages all printers combined have printed.
Default constructor: sets pagesPrinted to 0.
void printPages(int pages)
Adds pages to both pagesPrinted and totalPagesPrinted.
Prints a message like:
"Printed 12 pages on this printer."
void printLocalCount()
Prints a message like:
"This printer has printed: 25 pages."
static void printGlobalCount()
Prints a message like:
"Total pages printed by all printers: 67"
Understand the difference between instance variables (object-specific) and static variables (class-wide).
Gain experience updating both in the same method.
Use both static and instance methods in your tester class.
public class PrinterTester
{
public static void main(String[] args)
{
Printer p1 = new Printer();
Printer p2 = new Printer();
p1.printPages(10); // p1 prints 10 pages
p2.printPages(5); // p2 prints 5 pages
p1.printPages(7); // p1 prints 7 more pages
p1.printLocalCount(); // should show 17
p2.printLocalCount(); // should show 5
Printer.printGlobalCount(); // should show 22
}
}
Printed 10 pages on this printer.
Printed 5 pages on this printer.
Printed 7 pages on this printer.
This printer has printed: 17 pages.
This printer has printed: 5 pages.
Total pages printed by all printers: 22