Boolean Mutator Method - can I compare to "True"

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3273 Views - Last Post: 29 April 2012 - 07:36 PM Rate Topic: -----

#1 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 09:32 PM

All,
My programming assignment for this week calls for two classes, one a driver class and the other (well I'm not sure what the name of the non-driver class is). This program is about cats and their claws (exciting stuff!!). The driver class is going to ask the user whether the cat has claws with the response being true or false. So below I've posted my non-driver class (will someone tell me what I should refer to that as?), and in my initial variable declarations at the top of the class, I defined declawed as a string and isDeclawed as a boolean. When I try to compile this class I get cannot find symbol; Symbol: variable True

So, my code thinks True is a variable while I intended for it to be a string.
Is there a better way to do what I'm trying to do?
And if not, how can I make this work?

	//*****************************************


	//This method assigns the clawed status.

	public void setDeclawed(String declawed)
	{
		this.declawed=declawed;
	} // end setDeclawed

	//*****************************************

	//This method sets the boolean of clawed status

	public Boolean isDeclawed()
	{
		if (this.declawed.equals(True))
		{
			return true;
		}
		else
		{
			return false;;
		};
	} // end isDeclawed

	//*****************************************


Hah, figured it out after posting. True needed quotes...

Is This A Good Question/Topic? 1
  • +

Replies To: Boolean Mutator Method - can I compare to "True"

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 09:36 PM

you mean

if (this.declawed.equals("True"))
or better
if (this.declawed.equalsIgnoreCase("True"))

in that case


public boolean isDeclawed()   // use boolean no Boolean unless you have a good reason
{
   return this.declawed.equalsIgnoreCase("True");
}


Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 09:37 PM

Quote

in my initial variable declarations at the top of the class, I defined declawed as a string and isDeclawed as a boolean



This is strange. Is there a reason for this?

Quote

Hah, figured it out after posting. True needed quotes...


Yes, for a String literal you need quotes. Plus one for answering your own question - but I'm baffled about the purpose of the String.
Was This Post Helpful? 0
  • +
  • -

#4 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 09:47 PM

Appreciate the quick response as always here...
Thanks for catching the ignore case; that will help when I finish my code and go to test it. As for the boolean variable, my driver class is going to:
  • Create three cats
  • Gather attributes for the cats (name, age, weight, breed and claws)
  • Print the name and age of the cats who are 3 or older and been declawed


For that reason the boolean will be used as a filter for which cats get printed.
I'm working on the driver class now...we'll see what problems I run into there.

View Postjon.kiparsky, on 26 April 2012 - 12:37 AM, said:

Yes, for a String literal you need quotes. Plus one for answering your own question - but I'm baffled about the purpose of the String.

How my program is required to work is the driver class will prompt "Does the cat have claws? True or False:"
I (may be very wrong) was thinking that the user's input would need to be a string if they are replying with true or false. Can I just make it a boolean input?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 09:49 PM

http://docs.oracle.c...asNextBoolean()
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 10:12 PM

Ah, okay. Yes, you can use the nextBoolean() method of the Scanner, but it's generally better to use your own String and process it as you're doing, since the Scanner convenience methods are somewhat fragile. Okay, I understand now. You could also, just for giggles, look at the Boolean wrapper class's second constructor, which takes a String. but of course that's totally useless since it is badly typo-tolerant. (Never expose a user to something that gleefully accepts a bad value and makes a guess at the right result.)

Three suggestions come to mind. First, since you're not going to need that String after it's been processed to a Boolean, consider doing the ask within the method. That saves you keeping that String around, and that makes for a slightly cleaner program. Little housekeeping measures like this add up.


Second, you might want to try to find a more communicative name for this method. If you accept the suggestion above, then this method takes an action, and I would describe the action: askUserIsCatDeclawed() is rather verbose, but it does tell you what's going on. isDeclawed() to me suggests a method returning the already-existing value (this is a common convention for some projects, allowing for stuff like

if (fluffy.isDeclawed())
{
  needHeavyGloves = false;
}




Finally, since this looks like a helper method, it should have private access and it should set the field directly. If this is an isolated method simply for improving program structure (I approve of this) then there's no real benefit to returning a value.

Alternatively, if you want a method that asks a question and process the result to a boolean, and returns that boolean, then maybe you should make it a static method.

public static boolean askUserIsCatDeclawed()  
{
  
   //ask user 
   // validate answer and return result or repeat ask
   
}


This would refactor nicely to a more generic method:



public static boolean askUserTrueOrFalseQuestion(String theQuestion)  
{
   //ask user theQuestion
   // validate answer and return result or repeat ask
}


And then you have the case of asking "is fluffy neutered" covered.

But this may go beyond the scope of your current assignment.

This post has been edited by jon.kiparsky: 25 April 2012 - 10:12 PM

Was This Post Helpful? 1
  • +
  • -

#7 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 10:17 PM

So if I have the user's input set as nextBoolean in my driver class, does my isDeclawed boolean assignment look like this?
	//This method sets the boolean of clawed status

	public Boolean isDeclawed()
	{
		this.isDeclawed=isDeclawed
	} // end isDeclawed


FAST REPLY "edit":
We're all responding so quickly - its great, but this post was in response to pbl's
Was This Post Helpful? 0
  • +
  • -

#8 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 10:25 PM

I figured I'd give the teacher supplied example output for what my program is supposed to do (I'm posting it in code for cleanliness sake). We are currently learning OOP with driver classes and helper classes; mutators, accessors, methods.
Example Run:
Enter the name of Cat 1: Sam
Enter the age of Cat 1: 1
Enter the weight of Cat 1: 5
Enter the breed of Cat 1: fluffy1
Does the cat have claws? True or False?: True

Enter the name of Cat 2: Tom
Enter the age of Cat 2: 4
Enter the weight of Cat 2: 5
Enter the breed of Cat 2: fluffy2
Does the cat have claws? True or False?: True

Enter the name of Cat 3: Bob
Enter the age of Cat 3: 5
Enter the weight of Cat 3: 5
Enter the breed of Cat 3: fluffy3
Does the cat have claws? True or False?: False

The Cats over 3 with claws are:

Name: Tom
Age: 4 Years Old



One requirement worth noting is each member variable requires at least one independent accessor and one independent mutator. So I'm making sets and gets for all the attributes.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 10:40 PM

View PostDansellbuddy, on 26 April 2012 - 12:25 AM, said:

One requirement worth noting is each member variable requires at least one independent accessor and one independent mutator. So I'm making sets and gets for all the attributes.



It's always sensible to do it the way the teacher tells you. The customer is always right. However, be aware that this requirement is just to give you practice writing the methods, and not because it's good OO design!

In practice, object-oriented design is largely about encapsulating data, and you should be very wary of giving foreign classes access to read your data, and doubly so when it comes to setting it. If every class has access to every field that it can see, what you have is a big procedural program divided among many files, and this is usually terrible code.

But for now, do it the way the teacher wants.

Also, you should be aware that this is not actually a good example of a mutator. A mutator allows a foreign class to suggest a change to a field. The simplest case simply accepts the suggestion
public void setAge(boolean age)
{
  this.age=age;
}


If this is paired with a public getter, you can save some code and make the field public and take the consequences. (tightly coupled code, spaghetti logic, weird bugs from strange cross-class access)

A more serious sort of mutator will do some sanity checks, at least:

public void setAge(boolean age)
{ if (age >= 0 && age <=969) // 'cause the bible said so
this.age=age;
else
{} // do nothing - maybe throw an exception, if you want to
}[/code]


But really, how often does a person's age get set? Generally, once, when the object is created. Or to take your declawed case, you really want a declaw() method: set declawed to true. Unless you can reclaw the cat, you can't set it to false, right?


What your method does, as I say, is to consult with the user and set a value accordingly. This is somewhat different.

Quote

Does the cat have claws? True or False?: True


Ugh. "Would you like cream in your coffee?" "True".
Was This Post Helpful? 0
  • +
  • -

#10 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: Boolean Mutator Method - can I compare to "True"

Posted 25 April 2012 - 10:56 PM

Yeah, not a big fan of the verbiage of the true or false question.
Well, the age part of my code is the following. So the boolean is only supposed to apply to whether the cat has claws.
	//*****************************************

	//This method assigns the age of the cat.

	public void setAge(int age)
	{
		this.age=age;
	} // end setAge

	//*****************************************

	//This method returns the age of the cat.

	public int getAge()
	{
		return age;
	} // end return age



The thing that I'm sure I'll be back for more on is, I was given some "sample code" to edit and add other code to complete.
public class cat 
{ 

    private String breed;
    boolean declawed;
    private String name;
    private int age;//in years
    private double weight;//in pounds 

    public void setName(String name)
    {
        this.name = name;
    } 

    public void setBreed(String newBreed)
    {
        breed = newBreed; //age and weight are unchanged.
    } 

    public String getBreed( )
    {
        return breed;
    }

    public void set(String name, int age, double weight)
    {
    //put appropriate code here
    }
    //add other accessor/mutator methods as needed.
} 



I'm not yet understanding "public void set(String name, int age, double weight){//put appropriate code here} part. In my version of the code I've set all the variables before that part. Here's the driver class example code:
import java.util.*; 

public class yournameWeek6_7
{
    public static void main (String[] args)
    {
    cat myCat1 = new cat(); 

    Scanner input = new Scanner(System.in); 

    System.out.println("Enter a cats name:");
    String name = input.next(); 

    System.out.println("Enter cats age:");
    int age = input.nextInt(); 

    System.out.println("Enter cats weight:");
    double weight = input.nextDouble(); 

    System.out.println("Enter cats breed:");
    String breed = input.next(); 

    System.out.println("Is your cat declawed? True or False");
    boolean declawed = input.nextBoolean(); 

    myCat1.set(name, age, weight);
    myCat1.setBreed(breed); 

    System.out.println(myCat1.getName());
    System.out.println(myCat1.getBreed()); 
    }
} 



If I've created set methods in the helper class to set the name age and weight, what does "public void set(String name, int age, double weight)" do for me?

This post has been edited by Dansellbuddy: 25 April 2012 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#11 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: Boolean Mutator Method - can I compare to "True"

Posted 26 April 2012 - 08:34 AM

Okay, now I'm working on the driver class, and I need to make more than just one cat...
So I made the cats in the code below...
But I'm not sure how to be prompted for the other cats' data. Do I use a loop?


import java.util.*;

public class AnsellDanielWeek6Prog
{
	public static void main(String[] args)
	{
		Cat myCat1 = new Cat();
		Cat myCat2 = new Cat();	//Here's where I'm creating the new cats
		Cat myCat3 = new Cat(); //Same here	

		Scanner input=new Scanner(System.in);
                //pretty much everything here on out was teacher supplied. 

		System.out.println("Enter a cats name: ");
		String name = input.next();

		System.out.println("Enter cats age: ");
		int age = input.nextInt();

		System.out.println("Enter cats weight: ");
		double weight = input.nextDouble();

		System.out.println("Enter cats breed: ");
		String breed = input.next();

		System.out.println("Is your cat declawed? True or False: ");
		String declawed = input.next();

		myCat1.set(name, age, weight);
		myCat1.setBreed(breed);

		System.out.println(myCat1.getName());
		System.out.println(myCat1.getBreed());

	} // end main
} 

Was This Post Helpful? 0
  • +
  • -

#12 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Boolean Mutator Method - can I compare to "True"

Posted 26 April 2012 - 08:40 AM

Yes.

You should create an array of Cat objects and construct a for loop to prompt the user for their input. You then set the values to the corresponding object in the array.

Cat[] cats = {new Cat(), new Cat(), New Cat() };

for(int i = 0; i < numberofcats; i++)
{
    String something = someinputfromuser;
  
    cats[i].setSomething(someinputfromuser);
}

Was This Post Helpful? 1
  • +
  • -

#13 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: Boolean Mutator Method - can I compare to "True"

Posted 26 April 2012 - 02:09 PM

I was just looking in my book and we haven't covered arrays yet (that hasn't precluded the teacher from making assignments that covered materiel that we haven't learned yet though.). What would be the less elegant way to do that? Define each cat?
Was This Post Helpful? 0
  • +
  • -

#14 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Boolean Mutator Method - can I compare to "True"

Posted 26 April 2012 - 02:12 PM

Yeah in that case.

However instead of repeating your user input, you could still shove it all into its own method.

cat1 = getCat();
cat2 = getCat();
...

Which at least cuts out on some of the repetitive code.
Was This Post Helpful? 0
  • +
  • -

#15 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: Boolean Mutator Method - can I compare to "True"

Posted 29 April 2012 - 07:25 PM

Off topic, My travels for the week are done (24 hours in a car and two days flying) so now its time to finish my homework.

Things are going pretty good, I'm finally able to get input requests for three cats. Now I'm starting the if problem of which cats are older than 3 and have been declawed. For some reason when I go to compile the driver program, I'm getting a problem with getting my boolean value.

if ((cat1.getAge() >=3) && (cat1.getIsDeclawed = true))

I tested this first the cat1.getAge() by successfully printing a line. When I added the getIsDeclawed I am getting an error that the variable doesn't exist. Here is the code where I created set and get method for the boolean value. I have saved and compiled this helper class.
	//*****************************************

	//This method sets the boolean of clawed status

	public void setIsDeclawed(boolean isDeclawed)
	{
		this.isDeclawed=isDeclawed;
	} // end setIsDeclawed

	//*****************************************

	//This method returns the boolean value of clawed status

	public boolean getIsDeclawed()
	{
		return isDeclawed;
	} // end return boolean



Any thoughts as to why I'm getting this problem?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2