Help with this please youll practice building 3 distinct pr
Help with this please
, you’ll practice building 3 distinct programs called “classes” that each have their own main() driver. In this assignment, you’ll be submitting 3 files instead of one, and these files each represent a standalone reusable unit of logic. While a complete exposure to Class design is beyond the scope of 161, the ability to build small, simple and functional classes naturally arises when combining methods (as discussed in Chapter 1) and related primitive data (as in Chapter 2). Notice too that we’ll leave the “static training wheels” behind: the only thing that will be declared static in this assignment will be the main() function(s), which I will provide for you. Chapter 4 discusses basic class design, so you may want to read that chapter first before getting started. We’ll start by reviewing the three steps to class design first here. Remember to spell classes and methods exactly as indicated here!
Make a new class (one time per class)
Add a data item (one to many times per class)
For example, in a Point class, you might declare a variable integer to manage the x axis.
Add a method (one to many times per class)
Frequently, these methods do some action or return some value. So called “Getters” and “Setters” are methods that provide access to data items, such as getX() or setX(int dx) for use with the variable x above.
Note that you’ll perform these three steps each time you build one of the classes below.
Warm Up With a Small Point1D Class
In this section, you’ll follow the recipe above to construct a simple class that manages a point along a single axis, such as the x or y axis. We can reuse this class later to build even more advanced Points in 2 and 3 dimensions, but we’ll get started with just one data item and two methods here.
Data Items
Declare an integer “x” inside the class definition but outside of any method
This is called a field in Java.
Methods
Declare a method called “getX” that returns the integer value for the variable x.
public int getX(); { //finish this method
Declare a method called “setX” that takes an integer as input and sets the variable x.
public void setX(int nx) { //finish this method
If you’re using BlueJ, then BlueJ will write most of this class for you simply by clicking on the “new class” button and then naming your class “Point1D”. Notice how the variable “x” is declared outside of any method but inside of a class.
Testing Your Code With the Supplied Main()
Included here is a main you should put inside your Point1D class to test it.
public static void main(String[] args) {
Point1D pt = new Point1D();
pt.setX(10);
System.out.println(\"The point\'s x value is: \" + pt.getX());
}
Sample Output for the Point1D Class
The point\'s x value is: 10
Building A Slightly Bigger Class with 2 Data Items – The Point2D Class
We could build this class in multiple ways, and not all techniques would be equivalent with respect to time and efficiency. If you understand code reuse, you may be inclined to build this new class from 2 Point1Ds, and this would promote code reuse. However, to reinforce the concepts we’re practicing here, we’ll build this Point2D class from scratch.
Data Items
Declare an integer x just like in the Point1D class
Declare an integer to track the y axis, just like you did with the x axis.
Methods
Declare a method to get and set the values of x
public int getX() { //…
public void setX(int dx) { //…
Declare a method to get and set the values of y
public int getY()
public void setY(int dy) { //..
Declare a method to describe this Point2D object as a String, as in:
public String toString() { //build a String representation of the point (x,y) and return it
Again, in BlueJ you will have some of this work done for you simply by clicking on the “new class” button – BlueJ starts every class out with some sample code for working with a variable called “x”. Can you extend this code so that it has two variables: an x and a y?
Testing Your Code With the Supplied Main()
Included here is a main you should put inside your Point2D class to test it.
public static void main(String[] args) {
Point2D pt = new Point2D();
pt.setX(5);
pt.setY(2);
System.out.println(\"The 2D point\'s value is: \" + pt.getX());
System.out.println(\"The 2D point\'s y value is: \" + pt.getY());
System.out.println(pt.toString());
}
Sample Output for the Point2D Class
The 2D point\'s value is: 5
The 2D point\'s y value is: 2
Point2D at (5,2)
A Slightly Larger Class With 3 Data Items - The SeaHawksTracker Class
This class is to be used to track the performance of the Seahawks as they experience multiple SuperBowls in their outstanding career. Note that all SuperBowls are not the same, and so we’ll need to store the scores for each of the three times the SeaHawks have been to the SuperBowl in three distinct variables defined inside our SuperBowl class. This is similar to the Point2D class, but instead of the variables “x” and “y” we’ll need a third variable – if we were making a Point3D class, we’d call this variable “z”. Here, well name the variables so they are self-documenting, and so we’ll need names that reflect their intended use here, such as “firstScore”, “secondScore” and “thirdScore”. This class will not only be able to print out the 3 scores by calling a superbowl report method (called toString()), but you will arrange the three values in order by making some helper functions to determine the minimum() and maximum() of the three scores. I’ve included a main to help you test your SeaHawkTracker class, which contains 3 data items and a number of methods defined below. Note that you are not allowed to use Math.max() or Math.min(), and also not allowed to call Arrays.sort() or use any external classes here at all – that is to say, you must build this all from scratch. Chapter 4 discusses basic class design, so you may want to read that if concepts here don’t make sense.
Data Items
Define three integers as fields with the names “firstScore”, “secondScore” and “thirdScore”
Methods
Define 3 getter methods for firstScore, secondScore and thirdScore.
Define 3 helper methods for determining…
The minimum of three integers, called min(a,b,c)
The maximum of three integers, called max(a,b,c)
The middle of three integers, called mid(a,b,c)
One method to report on the games played called toString()
Sample Main
public static void main(String[] args) {
SeaHawksTracker stats = new SeaHawksTracker();
stats.setFirstScore(22);
stats.setSecondScore(11);
stats.setThirdScore(27);
System.out.println(\"---------Min, Mid, & Max----------\");
System.out.println(\"The largest is:\" + stats.max(3,5,1));
System.out.println(\"The middle is:\" + stats.mid(3,5,1));
System.out.println(\"The smallest is:\" + stats.min(3,5,1));
System.out.println(\"\ ---------Report of Scores--------\");
System.out.println(stats.toString());
}
Sample Output for the SeaHawksTracker Class
---------Min, Mid, & Max----------
The largest is:5
The middle is:3
The smallest is:1
---------Report of Scores--------
The first score was:22
The second score was:11
The third score was:27
The least of the SeaHawks\' scores was:11
The middle of the SeaHawks\' scores was:22
The greatest of the SeaHawks\' score was:27
Solution
Point1D.java
public class Point1D
{
// instance variables - replace the example below with your own
private int x;
public static void main(String[] args) {
Point1D pt = new Point1D();
pt.setX(10);
System.out.println(\"The point\'s x value is: \" + pt.getX());
}
public void setX(int nx) {
x = nx; //Sets x to the parameters given.
}
public int getX() {
return x; //Outputs current value of x.
}
}
Point2D.java
public class Point2D
{
// instance variables - replace the example below with your own
private int x;
private int y;
public static void main(String[] args) {
Point2D pt = new Point2D();
pt.setX(5);
pt.setY(2);
System.out.println(\"The 2D point\'s x value is: \" + pt.getX());
System.out.println(\"The 2D point\'s y value is: \" + pt.getY());
System.out.println(pt.toString());
}
public int getX() {
return x; //Outputs current value of x.
}
public void setX(int dx) {
x = dx; //Sets x to the parameters given.
}
public int getY() {
return y; //Outputs current value of y.
}
public void setY(int dy) {
y = dy; //Sets y to the parameters given.
}
public String toString() {
String represent = \"Point 2D at (\" + getX() + \",\" + getY() + \")\";
return represent; //Outputs String for given variables.
}
}
SeaHawksTracker.java
public class SeaHawksTracker
{
// instance variables - replace the example below with your own
private int firstScore;
private int secondScore;
private int thirdScore;
public static void main(String[] args) {
SeaHawksTracker stats = new SeaHawksTracker();
stats.setFirstScore(22);
stats.setSecondScore(11);
stats.setThirdScore(27);
System.out.println(\"---------Min, Mid, & Max----------\");
System.out.println(\"The largest is:\" + stats.max(3,5,1));
System.out.println(\"The middle is:\" + stats.mid(3,5,1));
System.out.println(\"The smallest is:\" + stats.min(3,5,1));
System.out.println(\"\ ---------Report of Scores--------\");
System.out.println(stats.toString());
}
public int getFirstScore() {
return firstScore; //Outputs current value for firstScore.
}
public void setFirstScore(int dx) {
firstScore = dx; //Sets firstScore to the parameters given.
}
public int getSecondScore() {
return secondScore; //Outputs current value for secondScore.
}
public void setSecondScore(int dx) {
secondScore = dx; //Sets secondScore to the parameters given.
}
public int getThirdScore() {
return thirdScore; //Outputs current value for thirdScore.
}
public void setThirdScore(int dx) {
thirdScore = dx; //Sets thirdScore to the parameters given.
}
public int min(int a, int b, int c) {
if (a < b && a < c) {
return a; //Returns int a as minimum.
}
else if (b < a && b < c) {
return b; //Or Returns int b as minimum.
}
return c; //For all else returns int c as minimum.
}
public int max(int a, int b, int c) {
if (a > b && a > c) {
return a; //Returns int a as maximum.
}
else if (b > a && b > c) {
return b; //Or Returns int b as maximum
}
return c; //For all else returns int c as maximum.
}
public int mid(int a, int b, int c) {
if( a > b && a < c || a < b && a > c) {
return a; //Returns int a as medium.
}
if( b > a && b < c || b < a && b > c) {
return b; //Or returns int b as medium.
}
return c; //For all else returns int c as medium.
}
public String toString() {
String report = \"The first score was:\" + getFirstScore() + \"\ The second score was:\" + getSecondScore() + \"\ The third score was:\" + getThirdScore() + \"\ The least of the SeaHawks\' scores was:\" + min(getFirstScore(),getSecondScore(),getThirdScore()) + \"\ The middle of the SeaHawks\' scores was:\" + mid(getFirstScore(),getSecondScore(),getThirdScore()) + \"\ The greatest of the SeaHawks\' score was:\" + max(getFirstScore(),getSecondScore(),getThirdScore());
return report; //String that includes \"get\" and min/max/mid methods.
}
}






