Create a new class in your Editor, and call it “SeaHawksTracker”. We’ll want to
ID: 3674756 • Letter: C
Question
Create a new class in your Editor, and call it “SeaHawksTracker”. We’ll want to build this new class in steps rather than all at once, so we’ll start with the familiar getters/setters and then move on to the helper methods we need (min, mid, max) that we’ve seen in other homeworks or labs, and then try something new in the toString() function, which will build a string of the scores in ascending order. Note that the toString() function needs to call the min(), mid() and max() function to get full credit.
If this is BlueJ, notice how it provides examples of data and methods for you.
Define some data items inside the class definition but outside of any method so it is a field.
You’ll need 3 distinct integer variables to track the three different superbowl scores that the SeaHawks have earned.
Define your methods
Just like you’ve defined “getters” and “setters” for data items in previous classes for x and for y, you’ll need 3 getters and 3 setters for each of the scores defined in this class.
public int getFirstScore() { //finish this
public void setFirstScore(int sc) //similar getter for the second score
//similar setter for the second score
//similar getter for the third score
//similar setter for the third score
You’ll need a min() and max() method that can determine the smallest and largest of three distinct numbers,
similar to exercises you’ve done in previous labs and homeworks.
public int min(int a, int b, int c) //return the smallest of a,b, and c
public int max(int a, int b, int c) //return the largest of a,b, and c
Next, you’ll need a function that determines the middle element given three integers, called min(). Note that you can build mid() multiple different ways, but the approach that reuses methods you’ve already written is probably the best.
public int mid(int a, int b, int c) { //return the middle of a,b, and c
The last function you’ll be creating is called toString(), and it’s job is to return a string printout of the three variables (firstScore, secondScore, or thirdScore) in ascending order (use of the min(), mid() and max() functions you built previously).
Note that the toString() function needs to call the min(), mid() and max() function to get full credit.
Notes & Hints Consider the code provided and the code BlueJ creates for a great starting point. Don’t wait till the last minute to get started or get help from the QSC or your instructor. Use comments in your code, since it will be graded! Break up all long programs or methods into smaller functions, and call those
Explanation / Answer
import java.util.Scanner;
public class SeaHawksTracker {
int firstScore;
int secondScore;
int thirdScore;
// getter setter for all 3 types of score
public int getFirstScore() {
return firstScore;
}
public void setFirstScore(int firstScore) {
this.firstScore = firstScore;
}
public int getSecondScore() {
return secondScore;
}
public void setSecondScore(int secondScore) {
this.secondScore = secondScore;
}
public int getThirdScore() {
return thirdScore;
}
public void setThirdScore(int thirdScore) {
this.thirdScore = thirdScore;
}
// auxilary methods
public int max(){
int max = firstScore;
if(firstScore > secondScore){
if(firstScore > thirdScore)
max= firstScore;
else
max= thirdScore;
}else{
if(secondScore > thirdScore)
max= secondScore;
else
max= thirdScore;
}
return max;
}
public int min(){
int min = firstScore;
if(firstScore < secondScore){
if(firstScore < thirdScore)
min= firstScore;
else
min= thirdScore;
}else{
if(secondScore < thirdScore)
min= secondScore;
else
min= thirdScore;
}
return min;
}
public int mid(){
int mid;
if (firstScore > secondScore) {
if (secondScore > thirdScore) {
mid = secondScore;
} else if (firstScore > thirdScore) {
mid = thirdScore;
} else {
mid= firstScore;
}
} else {
if (firstScore > thirdScore) {
mid = firstScore;
} else if (secondScore > thirdScore) {
mid= thirdScore;
} else {
mid = secondScore;
}
}
return mid;
}
@Override
public String toString() {
return "max: "+max()+", min: "+min()+", and mid: "+mid();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter three number");
int first = sc.nextInt();
int second = sc.nextInt();
int third = sc.nextInt();
// creating Object of SeaHawksTracker
SeaHawksTracker s = new SeaHawksTracker();
// setting three values
s.setFirstScore(first);
s.setSecondScore(second);
s.setThirdScore(third);
// callint toString
System.out.println(s.toString());
}
}
/*
Output:
Enter three number
12
13
14
max: 14, min: 12, and mid: 13
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.