Notes: Make sure to follow all programming guidelines from Handout 4. This progr
ID: 3635082 • Letter: N
Question
Notes:Make sure to follow all programming guidelines from Handout 4.
This program is Football2.java.
Program 8 builds on Program 7, and uses almost the same Sample Input:
Sample Input:
-------------
Football Program -- v. 1.0 Fall 2011
LSU 514 3216 9 0
OSU 587 5016 9 0
STAN 558 4552 9 0
BSU 512 3832 9 0
BAMA 513 3956 8 1
OSU 482 2878 6 3
NoSuchTeam
LSU 5016
OSU 5016
NoSuchTeam
Program 8 is Football2.java. It is similar to Program 7, Football.java,
except that it uses one array of FootballTeam objects instead of five parallel
arrays as was done in Program 7. You should read what the textbook has to
say in Chapter 7 about arrays of objects versus parallel arrays.
One or more solutions for Program 7 will be posted on Thursday, 1 December 2011.
In case you were unable to write Program 7 and get it to work correctly, you
are free to use anything from those solution(s) for Program 7 in writing
Program 8.
There are two classes for this assignment, the main class (public class
Football2, stored in file Football2.java), and a subsidiary class (public
class FootballTeam, stored in file FootballTeam.java).
public class FootballTeam will contain the following:
instance variables:
name, a String
attempts, an int
yards, an int
wins, an int
losses, an int
A constructor
A mutator method for the attempts
A mutator method for the yards
A mutator method for the wins
A mutator method for the losses
An accessor methor for name
An accessor methor for attempts
An accessor methor for yards
An accessor methor for wins
An accessor methor for losses
A toString method, to return a String that starts with " "
and would print out as, for example,
Football Team Information...
Name : OSU
Attempts : 587
Yards : 5016
Wins : 9
Losses : 0
An equals method that checks only the name, and yards. This method
returns "true" if the two names are equal, and the two yards
numbers are equal; otherwise it returns false.
A compareTo method that compares only the names, and yards.
Suppose this method is invoked by football1.compareTo(football2),
where football1 and football2 are each objects of type FootballTeam.
Then this method returns a positive int if the name of football1 is
"greater than" the name of football2, alphabetically, or if the
two names are the same, but the yards of football1 is "greater than"
the yards of football2. The method returns a zero int if the
name and yards of football1 and football2 are equal. It returns a
negative int if the name of football1 is "less than" the name of football2,
or if the names are the same, but the yards of football1 is "less than"
the yards of football2.
These methods can follow the prototypes given in lecture and
in Chapter 7 of the textbook by Horstmann.
public class Football2 is similar to the public class Football from
Programming Assignment 7, except that it will have only one array, which will
be an array of objects of type FootballTeam. The statements at the top of the
methods in pubic class Football2 could be organized as follows.
public static int readDatabase( Scanner scan, String nameSentinel,
FootballTeam[] football)
public static void printDatabase(int numRecords, FootballTeam[] footballArr)
public static void sortDatabase(int numRecords, FootballTeam[] footballArr)
public static int seqSearch(int numRecords, FootballTeam[] footballArr,
FootballTeam footballKey)
However, you may name and organize the parameters and even the method names
anyway you please. You are not allowed to use global (class level)
variables.
readDatabase reads in the team name abbreviation, attempts, yards, wins, and losses,
constructs a FootballTeam object with those instance variables and stores the FootballTeam
object into footballArr[]. Naturally readDatabase has the same defensive
programming that it had in Program 7, checks that the data does not
overflow the array footballArr[], etc.
printDatabase prints the objects that are in the database footballArr[].
printDatabase invokes the toString method of public class FootballTeam to print each
FootballTeam object.
sortDatabase sorts the FootballTeam objects in footballArr[].
sortDatabase invokes the compareTo method of public class FootballTeam in order to
decide whether an object in footballArr[] is less than some other object in
footballArr[]. Because it uses the compareTo method from public class FootballTeam,
sortDatabase in Program 8 will be somewhat simpler than the sortDatabase in
Program 7.
seqSearch searches in footballArr[] for a FootballTeam object that is equal to the
FootballTeam object footballKey. seqSearch will invoke the equals method of
public class FootballTeam to decide whether the object being sought is present in
footballArr[], and if so, what its index value is. seqSearch in Program 8
will be somewhat simpler than the seqSearch in Program 7.
Thorough defensive programming is required in the main class.
In the sentinel loop in the main program, the program should construct a
FootballTeam object with the name abbreviation equal to the name abbreviation
key read in, and an yards equal to the yards number
read in, and any attempts, wins, and losses (such as 0). It will then send this FootballTeam
object to the seqSearch method as the formal parameter
footballKey (or whatever you want to name it).
Program 8 does not use the mutator method or the accessor methods in
public class FootballTeam.
Sample Output
-------------
Football Program -- v. 1.0 Fall 2011
Print the database:
Football Team Information...
Name : LSU
Attempts : 514
Yards : 3216
Wins : 9
Losses : 0
Football Team Information...
Name : OSU
Attempts : 587
Yards : 5016
Wins : 9
Losses : 0
Football Team Information...
Name : STAN
Attempts : 558
Yards : 4552
Wins : 9
Losses : 0
Football Team Information...
Name : BSU
Attempts : 512
Yards : 3832
Wins : 9
Losses : 0
Football Team Information...
Name : BAMA
Attempts : 513
Yards : 3956
Wins : 8
Losses : 1
Football Team Information...
Name : OSU
Attempts : 482
Yards : 2878
Wins : 6
Losses : 3
Sort the database.
Print the database:
Football Team Information...
Name : BAMA
Attempts : 513
Yards : 3956
Wins : 8
Losses : 1
Football Team Information...
Name : BSU
Attempts : 512
Yards : 3832
Wins : 9
Losses : 0
Football Team Information...
Name : LSU
Attempts : 514
Yards : 3216
Wins : 9
Losses : 0
Football Team Information...
Name : OSU
Attempts : 482
Yards : 2878
Wins : 6
Losses : 3
Football Team Information...
Name : OSU
Attempts : 587
Yards : 5016
Wins : 9
Losses : 0
Football Team Information...
Name : STAN
Attempts : 558
Yards : 4552
Wins : 9
Losses : 0
Is there a team with an abbreviation = LSU
and total yards = 5016 in the database,
and if so, what is its number of wins.
No, there is no such team in the database.
Is there a team with an abbreviation = OSU
and total yards = 5016 in the database,
and if so, what is its number of wins.
Yes. The number of wins is 9.
Explanation / Answer
//Football2 .java import java.util.Scanner; public class Football2 { public static int readDatabase( Scanner scan, String nameSentinel,FootballTeam[] football){ String name = ""; Integer Attempts = 0; Integer Yards = 0; Integer Wins = 0; Integer Losses = 0; Integer prop = 0; for(int i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.