Array Loop is printing a reference instead of a string [duplicate] - java

This question already has answers here:
How to override toString() properly in Java?
(15 answers)
Closed 4 years ago.
I have been provided with an object and several methods to work with it. I am having a tough time printing the string that I am assigning to the variables. At the moment, I am unsure if I assigning new values at all and I am unable to print any values. Previous iterations have only printed references.
Question: Am I assigning new string values? How do I print a string with the given methods?
Here is the code I was provided
public class Team implements Comparable<Team> {
public String toString(String team, int wins) {
return team + ": " + wins;
}
// Data fields
private String name;
private int winCount;
Team() {
name = "Sooners";
winCount = 1;
}
Team(String inputName) {
name = inputName;
winCount = 1;
}
Team(String inputName, int inputWinCount) {
name = inputName;
winCount = inputWinCount;
}
// ----------------------------------------------------
// Getters and Setters
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the winCount
*/
public int getWinCount() {
return winCount;
}
/**
* #param winCount
* the winCount to set
*/
public void setWinCount(int winCount) {
this.winCount = winCount;
}
/**
* Increments the winCount variable by one for this Team
*/
public void incrementWinCount() {
winCount++;
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object.
*
* This method allows you to use the contains method in ArrayList to see
* if any element in an array list has the same name as a specific Team.
*
* #param o
* the other Team being compared to.
*/
#Override
public boolean equals(Object o) {
return name.equals(((Team) o).name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* #param otherTeam
* one team
*/
public boolean sameName(Team otherTeam) {
return name.equals(otherTeam.name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* #param team1
* one team
* #param team2
* the other team
*/
public static boolean sameName(Team team1, Team team2) {
return team1.name.equals(team2.name);
}
/**
* This method allows you to sort an ArrayList of Team items using
* Collections.sort
*
* #param o
* the other Team being compared to.
* #return -1 if this Team item should come first, +1 if this Team item
* should come after the other, and 0 if this Team item is
* equivalent to the other.
*/
#Override
public int compareTo(Team o) {
if (this.winCount < o.winCount) {
return -1;
} else if (this.winCount > o.winCount) {
return 1;
}
return 0;
}
}
Here is my current code
Scanner scnr = new Scanner(System.in);
Random rando = new Random();
String name = "no";
int cycles = 0;
int value = 0;
int match = 0;
ArrayList<Team> teams = new ArrayList<Team>();
Team newTeam = new Team(name,1);
System.out.println("Welcome to the Advanced Sportsball Tracker!");
while (!name.equals("x")) // looping print statement
{ // x loop begins
System.out.println("Which team just won? (x to exit)");
match = 0;
cycles++;
name = scnr.next();
for (Team list : teams)
{
if (list.getName().equals(name)) // compares the name of the team to the input value
{
match++;
}
}
if (match == 0)
{
teams.add(newTeam);
}
}// x loop ends
System.out.print(newTeam.getName());
if (cycles == 1) // prints no data if user immediately exits
{
System.out.println("No data input");
}
if (cycles > 1)
{
System.out.println("Final Tally: "); // loop to print final Talley
for (Team list : teams) // FIXME
{
list.toString(list.getName(),list.getWinCount()); // makes a string out of the string and wincount of the team
}

What was the original code for the toString method they gave you? Did you add the parameters? A better way to do this would be to let the object use it's data fields inside of the method. Passing in the member variables in your for loop is unnecessary, and just bad code. Instead, you want to do something like this:
public String toString() {
return name + ": " + wins;
}
And in your loop, if you want to print the results, simply do this:
System.out.print(list.toString());

Related

Increment map value when meeting conditions

I have hit a brick wall and the revision book isn't much help as it provides flat examples, my revision assignment requires me to declare a local variable of a type to reference a list, Teams, then if should get the list of teams for the key value division, and assign it to the local variable.
It then ask me to increment the number of wins for teamA if it has a higher score than teamB and vice versa, if it is a draw then increment the number of draws.
I have managed to write the method to iterate over the list and an if-else statement to check he given arguments using a .get() method which works
The problem I am having is accessing in the incWon() method in the Team class to change the value of won for each map value. The material though gives quite flat examples of how map values are to be changed which don't really explain how I can change a value using a dynamic input.
any help would be greatly appreciated as if I can get won to work the rest will fall into place.
This is my class
{
private SortedMap<String, Set<Team>> teams;
/**
* Constructor for objects of class LeagueAdmin
*/
public LeagueAdmin()
{
// Create the HashMap
//Map<String, Team> teams = new HashMap<>();
super();
this.teams = new TreeMap<>();
}
public void addTeam(String division, Team team)
{
boolean changed;
if (!this.teams.containsKey(division)) // checks if the key division doesn't contain the value of divsioin
{
HashSet<Team> teamSet = new HashSet<>(); // instantiates a list of objects called Team and assigns them to local variable teamSet
teamSet.add(team); // adds a new team to the list
this.teams.put(division, teamSet);
changed = true;
}
else
{
Set<Team> teamSet = this.teams.get(division); // if list exists already adds new team to existing list
changed = teamSet.add(team);
}
}
public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
{
Set<String> teamKeys = teams.keySet();
for (String eachDivision: teamKeys)
{
if(teamAScore > teamBScore)
{
teams.put(); // updates wins for teamA
// System.out.println(teamA);
}
else if (teamAScore < teamBScore)
{
teams.get(teamB); // updates wins for teamB
// System.out.println(teamB);
}
else
{
// teams.put(); //updates draws for both teams
}
// System.out.println(eachDivision + " teams are " + teams.get(eachDivision));
}
}
}
and this the TEAM class I am have to access to increment values.
public class Team
{
private String name;
private String division;
private int won;
private int drew;
private int lost;
// no need to record points as = 3*won + drew
/**
* Constructor for objects of class Team
*/
public Team(String aName, String aDivision)
{
name = aName;
division = aDivision;
// no need to set won, drew and lost to 0
}
/**
* getter for attribute points
*/
public int getPoints()
{
return 3 * won + drew;
}
/**
* getter for name
*/
public String getName()
{
return name;
}
/**
* getter for division
*/
public String getDivision()
{
return division;
}
/**
* getter for won
*/
public int getWon()
{
return won;
}
/**
* getter for drew
*/
public int getDrew()
{
return drew;
}
/**
* getter for lost
*/
public int getLost()
{
return lost;
}
/**
* increments the number of games won
*/
public void incWon()
{
won = won + 1;
}
/**
* increments the number of games drawn
*/
public void incDrew()
{
drew = drew + 1;
}
/**
* increments the number of games lost
*/
public void incLost()
{
lost = lost + 1;
}
/**
* setter for division
*/
public void setDivision(String aDivision)
{
division = aDivision;
}
public String toString()
{
return ("Team " + name + ", division: " + division + " stats: Won: " + won
+ ", drew: " + drew + ", lost: " + lost + ", points: " + getPoints());
}
}
I think you misunderstood the data structure requirement. You're looping through a list of teams, and never updating the loop variable. I believe the data structure should be something more like this:
private Map<String, Map<String, Team>> teams;
Using the division for the outer map key, and the team name for the inner map key. This separates div1's teamA from div2's teamA
This simplifies your recordResult method to pulling the specific team and updating them accorrdingly
public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
{
Map<String, Team> teamKeys = teams.get(division);
if(teamAScore > teamBScore)
{
teams.get(teamA).incWon(); // updates wins for teamA
}
else if (teamAScore < teamBScore)
{
teams.get(teamB).incWon(); // updates wins for teamB
}
else
{
teams.get(teamA).incDrew();
teams.get(teamB).incDrew();
}
}

Parameters involving arrays (java)

Below I have programs I have worked on regarding an assignment
Card Class:
public class Card
{
// instance variables
private int value;
private String suit;
/**
* parameterized Card constructor -- gets called when an object of
* the Card class is instantiated sending a number as an argument -- it
* determines the value and suit of the card based upon the number received
* #param num a number that gets converted to a value between 1 and 13
* and a suit
*/
public Card(int num)
{
int suitNumber;
value = num % 13;
if (value == 0)
value = 13;
suitNumber = num / 13;
if (suitNumber == 0)
suit = new String("clubs");
else if (suitNumber == 1)
suit = new String("diamonds");
else if (suitNumber == 2)
suit = new String("hearts");
else if (suitNumber == 3)
suit = new String("spades");
else
suit = new String("ERROR");
}
/**
* getValue method -- returns what's stored in the instance variable value
* #return the state of the instance variable value
*/
public int getValue()
{
return value;
}
/**
* getSuit method -- returns what's stored in the instance variable suit
* #return a reference to a String that contains the state of the instance
* variable suit
*/
public String getSuit()
{
return suit;
}
/** equalValue method -- determines if the otherCard's value is the same
* as this card's value
* #param otherCard a reference to the Card object (assumes the object has been
* instantiated) to compare to this Card object
* #return true if the values are equal, false if the values are not equal
*/
public boolean equalValue(Card otherCard)
{
if (this.value == otherCard.value)
return true;
else
return false;
}
/**
* equalSuit method -- determines if the otherCards's suit is the same as
* this card's suit
* #param otherCard a reference to the Card object (assumes the object has been
* instantiated) to compare to this Card object
* #return true if the suits are the same, false if they are not
*/
public boolean equalSuit(Card otherCard)
{
if (this.suit.equals(otherCard.suit))
return true;
else
return false;
}
/**
* getPointValue method - this method returns the point value for the
* card - 10 for a face card, the actual value for cards 1 through 10
* #return the point value of the card
*/
public int getPointValue()
{
if (value > 10)
return 10;
else
return value;
}
/**
* equalCard method -- determines if the otherCard has the same value and
* suit as this card
* #param otherCard a reference to a Card object (assumes the object has been
* instantiated) to compare to this Card object
* #return true if the value and suits are the same, false if they are not
*/
public boolean equalCard(Card otherCard)
{
if (this.equalValue(otherCard) && this.equalSuit(otherCard))
return true;
else
return false;
}
/**
* isAFaceCard method -- determines if this card is a face card
* #return true if this card is a face card, false if it is not
*/
public boolean isAFaceCard()
{
if (value > 10)
return true;
else
return false;
}
/**
* toString method -- this method returns the state of the card object
* #return a reference to a String object that contains the values stored
* in the instance variables
*/
public String toString()
{
if (value == 1)
return (new String("Ace of " + suit));
else if (value == 11)
return (new String("Jack of " + suit));
else if (value == 12)
return (new String("Queen of " + suit));
else if (value == 13)
return (new String("King of " + suit));
else
return (new String(value + " of " + suit));
}
}
Now the trouble I am having is regarding this class
public class Hand
{
// instance variables
private Card[] cards;
private int numCards;
/**
* default Hand constructor --
* allocates an array that is capable of storing at most 10 Card references
* sets the number of cards to 0
*/
public Hand()
{
cards = new Card[10];
numCards = 0;
}
/**
* insertCard method --
* accepts a reference to the Card object to be stored in the next position in the
* cards array - checks to make sure the array isn't full
* #param theCard a reference to the Card object to be stored in the array
*/
public void insertCard(Card theCard)
{
if (numCards < cards.length)
{
cards[numCards] = theCard;
numCards++;
}
}
/**
* getCards method --
* makes a copy of the cards array and returns the address of the copy;
* the size of the array that is returned is based upon the number of
* cards currently in the hand
* #return a reference to the copy of the cards array
*/
public Card [] getCards()
{
Card [] cardsCopy = new Card[cards.length];
for(int i =0; i < cards.length; i++)
{
cardsCopy[i] = cards[i];
}
return cardsCopy;
}
/**
* replaceCard method --
* accepts a reference to a new Card object and the position in which
* the reference should be stored; the position is expected to be in the
* range 0 to (number of cards -1); position is validated to ensure that
* it is in this range
* #param pos the index where the card should be stored
* #param theCard the reference to the Card object to be stored in the array
*/
public void replaceCard(int pos, Card theCard)
{
}
/**
* searchCard method --
* accepts a reference to a Card to search for and determines whether
* or not that card exists in the hand
* #param theCard a reference to the Card to search for
* #return true if the card is found, false otherwise
*/
public boolean searchCard(Card theCard)
{
for(int i = 0; i < numCards; i++)
{
if(theCard.equalCard(cards[i]))
return true;
else
return false;
}
}
/**
* findNumFaceCards method --
* counts the number of face cards (ie. value of 11, 12 or 13) in the hand and
* returns the count
* #return the number of face cards in the hand
*/
public int findNumFaceCards()
{
int count =0;
for(int i =0; i < numCards; i++)
{
if(cards[i].getValue() >= 11)
count ++;
}
return count;
}
/**
* findLowCard method --
* finds and returns the position of the lowest card
* #return the index of the lowest card
*/
public int findLowCard()
{
int lowIndex = 0;
for(int i =0; i < numCards; i++)
{
if(cards[i].getValue() < lowIndex)
{
i = lowIndex;
}
}
return lowIndex;
}
/**
* replaceLowCard method --
* accepts a reference to a Card object and replaces the card
* having the lowest value with the new card
* #param theCard a reference to the Card object that will replace the
* lowest card
*/
public void replaceLowCard (Card theCard)
{
}
/**
* toString method --
* creates and returns a reference to a String with each of the card values
* on a separate line
* #return a reference to a String containing the state of the hand
*/
public String toString()
{
String str = "";
for(int i =0; i < numCards; i++)
str += "Card " + (i + 1) + ": " + cards[i] + "\n";
return str;
}
I am doing the replaceCard, searchCard and replacelowCard methods wrong(syntax errors). Most of these methods have the descriptions that my friend wrote to give me hints on what to write in the body. However, I mostly come up with errors on which code to use in the bodies.
For example, I have not yet learned how to search for a card in a array. I do know that there are two types of searching but I do not know if they are appropriate for this case.
For the replaceLowCard method, I am told that I will need the this.lowCard call to complete it.
And for the replaceCard method, I simply do not know what to do. Any help will be greatly appreciated.
(P.S: for this program, the deck is out of 52 cards).
I think an ArrayList is what you're looking for. It's dynamically sized, so all your insert-remove problems will be gone. Do something like this:
class Hand {
private List<Card> cards;
private int maxCards = 10;
public Hand() {
this.cards = new ArrayList<Card>();
}
}
And then you can do cards.add(...); to insert, cards.remove(...); to remove and so on... You can read more about ArrayLists here.
I hope this gives you a useful hint.

Java PhoneBook using ArrayList

Im trying to make a PhoneBook using array list but I'm not getting the right output here is my code, thank you for any help with this, the output I'm getting now is just a zero when i ask for the size, not seeming to add anyone, that is probably where the problem lies
import java.util.ArrayList;
public class Phonebook implements Directory
{
private ArrayList<Person> book;
public Phonebook ()
{
book = new ArrayList<Person>();
}
/**
* will return the number of entries currently entered in
* the <code>Directory</code>.
* #return - the number of valid entries in the <code>Directory</code>.
**/
public int size()
{
return book.size();
}
/**
* will display the entries currently entered in the <code>Directory</code>.
**/
public void listAll()
{
for(int i = 0; i < book.size(); i++)
{
System.out.println(book.get(i));
}
}
/**
* will add a new record to the <code>Directory</code> in alphabetical order
* if the name is not a duplicate entry. Otherwise no changes will be made.
* #param name - name of individual to be added to the <code>Directory</code>.
* #param number - phone number of the individual to be added.
* #return - true if the entry was added successfully, otherwise false.
**/
public boolean addPerson(String name, String number)
{
Person x = new Person (name, number);
if (checkPerson(name) == -1)
return false;
int index = 0;
while(index < book.size())
{
if((x.getName().compareTo((book.get(index)).getName())) < 0)
{
book.add(x);
return true;
}
index++;
}
return false;
}
public int checkPerson(String name)
{
int lo = 0;
int hi = book.size() - 1;
while(lo <= hi)
{
int half = (lo + hi) / 2;
if(name.equals(book.get(half).getName()))
return half;
if(name.compareTo(book.get(half).getName()) < 0){
hi = half - 1;}
else lo = half + 1;
}
return -1;
}
/**
* will remove an entry from the <code>Directory</code> if the name parameter
* is currently in the <code>Directory</code>. Otherwise no changes
* will be made.
* #param name - individual to be removed from the <code>Directory</code>.
* #return - true if the entry was successfully removed, otherwise false.
**/
public boolean removePerson(String name)
{
if (checkPerson(name) == -1)
return false;
book.remove(checkPerson(name));
return true;
}
/**
* will search the <code>Directory</code> to find out if the name passed in
* is currently in the <code>Directory</code>. If so, it will return the
* phone number associated with this person. Otherwise it will return null.
* #param name - name of individual to look up in the <code>Directory</code>.
* #return - the phone number if the name was found, otherwise null.
**/
public String lookUp(String name)
{
Person n = new Person (name, "999-9999");
int local = checkPerson(n.getName());
if(local == -1)
return null;
return book.get(local).getNumber();
}
/**
* will search the <code>Directory</code> to find out if the phone number
* is currently in the <code>Directory</code>. If so, it will return the
* name associated with this number. Otherwise it will return null.
* #param number - name of individual to look up in the <code>Directory</code>.
* #return - the name of the person if the number was found, otherwise null.
**/
public String lookUpNum(String number)
{
for(int i = 0; i <book.size(); i++)
{
if(number.equals(book.get(i).getNumber()))
return book.get(i).getName();
}
return null;
}
}
/**
* The Person class is a container class to hold the
* name and phone number of an individual. There are methods
* to access the name and number, and modify the name and number.
* Each name is stored in "Last, First" form to facilitate searching
* and sorting of persons. A private helper method is used to be
* sure that names entered in "First Last" form are converted to
* the proper format.
*/
public class Person implements Comparable<Person>
{
private String first;
private String last;
private String name; // Last, First
private String number;
/**
* explicit constructor, will store the first and last
* names, as well as the entire name in Last, First order
*
* #param na is the name of the individual
* #param nu is the phone number of the individual
*/
public Person(String na, String nu)
{
convert(na);
number = nu;
}
/**
* copy constructor, will make an exact copy of the parameter
*
* #param per is the <B>Person</B> to be duplicated
*/
public Person(Person per)
{
first = per.first;
last = per.last;
name = per.name;
number = per.number;
}
/**
* accessor method to return the name of <B>this Person</B>
*
* #return the name of the individual in Last, First order
*/
public String getName()
{
return name;
}
/**
* accessor method to return the phone number of <B>this Person</B>
*
* #return the phone number of the individual
*/
public String getNumber()
{
return number;
}
/**
* modifier method to set a new name for <B>this Person</B>
* The helper method convert() is called to handle the details
*
* #param the new name for the individual
*/
public void setName(String na)
{
convert(na);
}
/**
* modifier method to set a new phone number <B>this Person</B>
* just in case somebody needs to enter witness protection
*
* #param the new phone number for the individual
*/
public void setNumber(String num)
{
number = num;
}
/**
* accessor method that implements the <B>Comparable interface</B>
* based on the name field for <B>this Person</B>
* will return a positive number if <B>this</B> is greater than oth
* zero if <B>this</B> is equal to oth
* and a negative number if <B>this</B> is less than oth
*
* #return negative, zero, or positive int as per Comparable interface
*/
public int compareTo(Person oth)
{
return name.toUpperCase().compareTo(oth.name.toUpperCase());
}
/**
* accessor method to test if the instance data for <B>this Person</B>
* is equal to the instance data for oth
*
* #return true if names and numbers match, false otherwise
*/
public boolean equals(Person oth)
{
return name.toUpperCase().equals(oth.name.toUpperCase()) && number.equals(oth.number);
}
private void convert(String na)
{
if(na.indexOf(" ") == -1)
{
last = na;
first = null;
name = na;
}
else if(na.indexOf(",") != -1)
{
name = na;
first = na.substring(na.indexOf(",") + 2);
last = na.substring(na.indexOf(","));
}
else
{
first = na.substring(0, na.indexOf(" "));
last = na.substring(na.indexOf(" ") + 1);
name = last + ", " + first;
}
}
/**
* accessor method to return the instance data of <B>this Person</B>
* in a formatted String (24 character name field, followed by the number)
*
* #return name in Last, First order followed by the phone number
*/
public String toString()
{
String local = name;
if(name.length() < 8)
local += "\t";
if(name.length() < 16)
local += "\t";
local += "\t" + number;
return local;
}
}
public class client
{
public static void main(String[] args)
{
Phonebook nickBook = new Phonebook();
nickBook.addPerson("name lastname", "321-3256");
System.out.println();
nickBook.listAll();
System.out.println(nickBook.size());
}
}
Your addPerson method won't add the Person if the list is empty, since while (0 < 0) will be false, and the loop won't be entered :
int index = 0;
while(index < book.size())
{
if((x.getName().compareTo((book.get(index)).getName())) < 0)
{
book.add(x);
return true;
}
index++;
}
Beside that problem, book.add(x); will always add the new Person to the end of the List, which is not what you want. You should use book.add(index,x), assuming index is the location in which you wish to add the new Person.
Finally, if the new Person wasn't added inside the while loop, that means this Person should be the last Person on the List, so you have to add it to the end of the List after the loop.
A possible implementation :
public boolean addPerson(String name, String number)
{
Person x = new Person (name, number);
if (checkPerson(name) == -1)
return false;
int index = 0;
while(index < book.size())
{
if((x.getName().compareTo((book.get(index)).getName())) < 0)
{
book.add(index,x);
return true;
}
index++;
}
book.add(x); // this handles both the case of an empty List and the
// case in which the new Person should be the last Person
// on the list
return true;
}
Your function checkPerson is wrong. book.size() is 0 in the beginning and the hi results to -1 which means that it does not enter the loop. Besides that think about your half variable. It is possible that this results in another number than an integer which is not allowed if you are using this variable as an index for a query of the list.
public int checkPerson(String name)
{
int lo = 0;
int hi = book.size() -1;
while(lo <= hi)
{
int half = (lo + hi) / 2;
if(name.equals(book.get(half).getName()))
return half;
if(name.compareTo(book.get(half).getName()) < 0){
hi = half - 1;}
else lo = half + 1;
}
return -1;
}

How to check if user entered capacity is larger than the current capacity?

I'm currently working on a project that works with dogs and the current method I'm working on is supposed to let the user change the kennel capacity. The code below is from the kennel constructor class:
public class Kennel {
private String name;
private ArrayList < Dog > dogs;
private int nextFreeLocation;
private int capacity;
/**
* Creates a kennel with a default size 20
*
* #param maxNoDogs
* The capacity of the kennel
*/
public Kennel() {
this(20);
}
/**
* Create a kennel
*
* #param maxNoDogs
* The capacity of the kennel
*/
public Kennel(int maxNoDogs) {
nextFreeLocation = 0; // no Dogs in collection at start
capacity = maxNoDogs;
dogs = new ArrayList < Dog > (capacity); // set up default. This can
// actually be exceeded
// when using ArrayList but we
// won't allow that
// to happen.
}
/**
* This method sets the value for the name attribute. The purpose of the
* attribute is: The name of the kennel e.g. "DogsRUs"
*
* #param theName
*/
public void setName(String theName) {
name = theName;
}
/**
* Set the size of the kennel
*
* #param capacity
* The max dogs we can house
* #return
*/
public void setCapacity(int capacity) {
if (this.capacity > capacity) {
this.capacity = capacity;
} else
System.out.println("The capacity you entered is lower than the current capacity");
}
/**
* Maximum capacity of the kennels
*
* #return The max size of the kennel
*/
public int getCapacity() {
return capacity;
}
/**
* This method gets the value for the name attribute. The purpose of the
* attribute is: The name of the Kennel e.g. "DogsRUs"
*
* #return String The name of the kennel
*/
public String getName() {
return name;
}
/**
* This method returns the number of dogs in a kennel
*
* #return int Current number of dogs in the kennel
*/
public int getNumOfDogs() {
return nextFreeLocation;
}
/**
* Enables a user to add a Dog to the Kennel
*
* #param theDog
* A new dog to home
*/
public void addDog(Dog theDog) {
if (nextFreeLocation >= capacity) {
System.out.println("Sorry kennel is full - cannot add team");
return;
}
// we add in the position indexed by nextFreeLocation
// This starts at zero
dogs.add(theDog);
// now increment index ready for next one
nextFreeLocation = nextFreeLocation + 1;
}
/**
* Enables a user to delete a Dog from the Kennel.
*
* #param theDog
* The dog to remove
*/
public void removeDog(String who) {
Dog which = null;
// Search for the dog by name
for (Dog d: dogs) {
if (who.equals(d.getName())) {
which = d;
}
}
if (which != null) {
dogs.remove(which); // Requires that Dog has an equals method
System.out.println("removed " + who);
nextFreeLocation = nextFreeLocation - 1;
} else
System.err.println("cannot remove - not in kennel");
}
/**
* #return String showing all the information in the kennel
*/
public String toString() {
StringBuilder sbr = new StringBuilder();
sbr.append("Data in Kennel " + name + " is:");
for (Dog d: dogs) {
sbr.append(d.toString() + "\n");
}
return sbr.toString();
}
/**
* Returns an array of the dogs in the kennels
*
* #return An array of the correct size
*/
public Dog[] obtainAllDogs() {
Dog[] result = new Dog[dogs.size()];
result = dogs.toArray(result);
return result;
}
/**
* Only returns those dogs who like bones
*
* #return An array of dogs of the correct size. If no dogs like bones then
* returns an empty array (size 0)
*/
public List < Dog > obtainDogsWhoLikeBones() {
List < Dog > result = new ArrayList < Dog > (); // changed to List<dog> so can
// freely add data without
// intializing to fixed size
for (Dog d: dogs) {
if (d.getLikesBones()) {
result.add(d);
}
}
return result;
}
public Dog search(String name) {
Dog result = null;
for (Dog d: dogs) {
if (name.equals(d.getName())) {
result = d;
}
}
return result;
}
}
And this method is from the kennel application class:
private void setKennelCapacity() {
System.out.print("Enter max number of dogs: ");
int max = scan.nextInt();
scan.nextLine();
kennel.setCapacity(max);
System.out.println(max + " " + kennel.getCapacity());
}
The result is that the else statement is run, it always says the number entered is too low. It probably a small mistake but I'm inexperienced, thanks for any help.
usercapacity is a local variable. Each time you call setCapacity(int), the variable usercapacity will be initialized to 0 (that variable will always be 0). Instead, create a field variable to store the capacity in
class Kernal {
private int capacity;
public void setCapacity(int newCapacity) {
if(newCapacity < capacity) {
throw new IllegalArgumentException("You cannot enter a value that is lower than the current capacity");
}
capacity = newCapacity;
}
}
Without the IllegalArgumentException (using a simple print statement to notify the user), your method would look like this:
public void setCapacity(int capacity) {
if (this.capacity < capacity) {
this.capacity = capacity;
} else
System.out.println("The capacity you entered is lower than the current capacity");
}

I need to create two more constructors, but Java won't let me

OK, I need to create three constructors as part of a project, one default, one general and one copy. I've managed to create a default constructor, but I can't create either the general or copy constructors because otherwise my code won't compile. Here is the code if anybody knows the answer:
package lab02;
import javax.swing.JOptionPane;
/**
* Stores the personal details of a friend.
*
* #author Keith Francis(11109971)
* #date 4-10-2012
*/
public class Friend {
private String firstName;// stores first name
private String surname;// stores surname
private String address;// stores address
private int age;// stores age in years
private int height;// stores height in cms
private String hairColourString;// stores hiar colour as a string
private boolean colourTrue = false;// hair colour value is not valid
public static final int BLACK = 0;
public static final int BROWN = 1;
public static final int BLONDE = 2;
public static final int RED = 3;
public static final int GREY = 4;
/**
* Default constructor sets everything to 0 or null, depending on type.
*/
public Friend() {
firstName = null;
surname = null;
address = null;
age = 0;
height = 0;
hairColourString = null;
}
/**
* Allows the first name to be edited
*
* #param first
* first name variable
*/
public void setFirstName(String first) {
firstName = first;
}
/**
* Retrieves first name
*
* #return first name to String
*/
public String getFirstName() {
return firstName;
}
/**
* Allows the surname to be edited
*
* #param last
* creates last name variable
*/
public void setSurname(String last) {
surname = last;
}
/**
* Retrieves the surname
*
* #return last name to string
*/
public String getSurname() {
return surname;
}
/**
* Allows the address to be edited
*
* #param place
* where the friend lives
*/
public void setAddress(String place) {
address = place;
}
/**
* Retrieves the address
*
* #return the address of the friend
*/
public String getAddress() {
return address;
}
/**
* Allows the age (in years) to be edited
*
* #param years
* the age in years
*/
public void setAge(int years) {
age = years;
}
/**
* Retrieves the age in years
*
* #return the age in years
*/
public int getAge() {
return age;
}
/**
* Allows the height in centimetres to be edited
*
* #param h
* height in centimetres
*/
public void setHeight(int h) {
height = h;
}
/**
* Retrieves the height in centimetres
*
* #return height in centimetres
*/
public int getHeight() {
return height;
}
/**
*
* #return String of the personal details of the friend
*/
#Override
public String toString() {
return ("First name is: " + firstName + "\nSurname is: " + surname
+ "\nAddress is: " + address + "\nAge is :" + age
+ "\nHeight is: " + height + "\nHair colour is: " + hairColourString);
}
/**
* Uses JOptionPanel to edit the friend's personal details
*/
void inputFriend()
{
//welcome message
JOptionPane.showMessageDialog(null,"Weclome",null,JOptionPane.PLAIN_MESSAGE);
//prompt to enter first name
String name1 = JOptionPane.showInputDialog("Enter the friend's first name.");
//calls setFirstName method
setFirstName(name1);
//prompt user to enter second name
String name2 = JOptionPane.showInputDialog("Enter the friend's surname.");
setSurname(name2);// calls setSurname method
//prompt user to enter address
String thisAddress = JOptionPane.showInputDialog("Enter the friend's address.");
setAddress(thisAddress);//calls setAddress method
//prompt user to enter age in years
String ageString = JOptionPane.showInputDialog("Enter the friend's age in years.");
int i = Integer.parseInt(ageString);
setAge(i);
//prompt user to enter height in centimetres
String heightString = JOptionPane.showInputDialog("Enter the friend's height in cenimetres.");
int j = Integer.parseInt(heightString);
setHeight(j);
//prompt user to enter hair colour
String hairColourInput = JOptionPane.showInputDialog("Select the friend's " +
"hair colour:\n 0 = Black\n1 = Brown\n2 = Blonde\n3 = Red\n4 = Grey");
while(colourTrue != true)//if hair colour is valid
{
if(
hairColourInput.equals("0"))
{ hairColourString = "Black";//hair is black
colourTrue = true;}//entry is valid
else if (hairColourInput.equals("1"))
{ hairColourString = "Brown";//hair is brown
colourTrue = true;}//entry is valid
else if (hairColourInput.equals("2"))
{ hairColourString = "Blonde";//hair is blonde
colourTrue = true;}//entry is valid
else if (hairColourInput.equals("3"))
{ hairColourString = "Red";//hair is red
colourTrue = true;}//entry is valid
else if (hairColourInput.equals("4"))
{ hairColourString = "Grey";//hair is grey
colourTrue = true;}//entry is valid
else {
JOptionPane.showMessageDialog(null,
"The number entered is invalid.", "Error",
JOptionPane.WARNING_MESSAGE);// warns user that entry is
// not valid
hairColourInput = JOptionPane
.showInputDialog("Select the friend's " +
"hair colour:\n 0 = Black\n1 = Brown\n2 = Blonde\n3 = Red\n4 = Grey");
}// user is asked to choose again until they enter a valid number
}
}
/**
*
* #param args
* Calls inputFriend method and prints out the final String using
* JOptionPane
*/
public static void main(String[] args) {
Friend friend = new Friend();
friend.inputFriend();// calls inputFriend method
JOptionPane.showMessageDialog(null, friend.toString()); // prints out details
}
}
Here is my attempt at a copy constructor:
public Friend(Friend aFriend) {
this(aFriend.getFirstName(), aFriend.getSurname(), aFriend.getAddress, aFriend.getAge, aFriend.getHeight);
and my attempt at the general constructor:
public Friend2(){
public static final int BLACK = 0;
public static final int BROWN = 1;
public static final int BLONDE = 2;
public static final int RED = 3;
public static final int GREY = 4;
}
What came up was that a class, interface or enum was expected when I inserted the constructor. Hope that helps.
Right, I've tried the copy constructor like this:
public Friend(Friend f) {
this(f.getFirstName(),f.getSurname(),f.getAddress(),f.getAge(),f.getHeight());
}
But I am getting a message saying that I don't have a suitable constructor.
UPDATE:general and copy constructors are now working. Thanks for your help.
you can overload the constructor like below:
cons1:
public Friend()
{
}
cons2:
public Friend(int arg)
{
}
cons3:
public Friend(String s)
{
}
copy cons:
public Friend(Friend f)
{
}
Your Friend2() constructor is wrong because it is actually a constructor for a Friend2 class. Constructors for a class should all have a method name that is the same as the class name. (constructors declarations look like method declarations named the same as the class but without specify a return type)
Your copy constructor is using this to call a constructor that does not exists. ( this(x,y,z) is calling the 3 argument version of the constructor)
What you want is something that looks like the following:
public class Friend
{
// snip
/**
* Default constructor sets everything to 0 or null, depending on type.
*/
public Friend()
{
firstName = null;
surname = null;
address = null;
age = 0;
height = 0;
hairColourString = null;
}
public Friend(Friend f) {
// copy constructor
}
public Friend(String fName, String sName, String address, int age, int height, String hair) {
// fill in stuff here
}
// snip
}
You need to overload your constructor. You cannot define a method with the same name and parameters. Look here for another reference:
Best way to handle multiple constructors in Java
http://www.java-samples.com/showtutorial.php?tutorialid=284
Overload your constructors, just make sure they have different signatures and it should compile fine. To keep it DRY, have the copy and general constructors use the default one by calling this().

Categories