Unit 7 Projects
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: APStatsStudent
Description: An APStatsStudent “is-a” Student, however, we add a new field which is the double HomeworkAverage.
Create a constructor using the super keyword that accepts a String, three ints, and a double.
Methods:
Override getAverage() from the Student class so that the student’s average is
.6(testAverage) + .4(hwAverage)
getGrade() returns A, B, C, or F
override toString() to return:
i.e. Craig Sapp has a B in AP Stats
Class: StudyGroup
Description: A StudyGroup “Has-A” APStatsStudent (actually it has 3 AP Stats Students as fields).
Create an initialize constructor (this would accept 3 APStatsStudent object references)
Methods:
double getAverage() – returns the average grade of the three AP Stats Students (Is this overriding?)
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).
You can use this code for your tester class. PLEASE think about what is happening here.
public static void main(String[] args)
{
Student chemStudent = new Student(“Tom Segura”, 80, 90, 30);
System.out.println(chemStudent.getAverage()); //66.666
chemStudent.replaceLowestGradeWithAverage();
System.out.println(chemStudent.getAverage()); //79
APStatsStudent a = new APStatsStudent(“Lebron James”, 80, 90, 100, 62.5);
APStatsStudent b = new APStatsStudent(“Carmelo Anthony”, 70, 75, 80, 75.0);
APStatsStudent c = new APStatsStudent(“Damion Lilliard”, 50, 60, 70, 90.0);
System.out.println(c.getAverage()); //72
System.out.println(c.getGrade()); //C
System.out.println(a);
//Lebron James has a C in AP Stats
StudyGroup g = new StudyGroup(a,b,c);
System.out.println(g.getAverage());
System.out.println(g.nameOfHighestAverage());
System.out.println(g);
}