1

I have to make a program which is supposed to receive an input for pet information and output it in a specific way.
Normally this would be a cake and take 10 minutes, but we just moved into OOP and I am having some trouble figuring out what to put in the mutator on the driver.

Driver:

import java.util.HashSet;
import java.util.Set;

public class JMPets {
    private String petType; 
    private String petName; 
    private int petAge; 
    private double petWeight; 
    boolean isMale; 

    public void setType(String petType)
    {
        this.petType = petType; 
    }
    public void setName(String petName)
    {
        this.petName = petName; 
    }
    public void setAge (int petAge)
    {
        this.petAge = petAge; 
    }
    public void setWeight(double petWeight)
    {
        this.petWeight = petWeight; 
    }
    public String getType()
    {
        return petType; 
    }
    public String getName()
    { 
        return petName; 
    }
    public int getAge()
    { 
        return petAge; 
    }
    public double getWeight()
    {
        return petWeight;         
    }

    public void set(String petType, String petName, int petAge, 
           double petWeight)
    {
        //WHAT DO I PUT HERE

    }

}

import java.util.Scanner;
public class JMUnit6 {


    public static void main(String[] args) {

    JMPets myPet1 = new JMPets(); 
    JMPets myPet2 = new JMPets(); 
    JMPets mypet3 = new JMPets(); 
    Scanner stdIn = new Scanner(System.in); 
    System.out.println("Welcome to the Java Pet Tracker");
    System.out.println("Please enter the type of Pet #1:");
    String petType = stdIn.nextLine(); 

    System.out.println("Please enter the name of Pet #1:"); 
    String petName = stdIn.nextLine(); 

    System.out.println("Please enter the age of " +petName+":");
    int petAge = stdIn.nextInt(); 

    System.out.println("Please enter the weight of "+petName+":");
    double petWeight = stdIn.nextDouble(); 

    System.out.println("Is "+petName+" Male?:");
    boolean isMale = stdIn.nextBoolean(); 

    myPet1.set(petType, petName, petAge, petWeight); 


    System.out.println(myPet1.getType());
    System.out.println(myPet1.getName()); 
    System.out.println(myPet1.getAge());
    System.out.println(myPet1.getWeight()); 



    }//end main
}//end class JMUnit6

The only output I get is null null 0 0.0.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Note: `JMPets` represents a *singular object* - a Pet. So, your class name should preferably not be plural – OneCricketeer Nov 27 '16 at 20:50
  • fixed that thank you – Michael Anthony Judge Nov 27 '16 at 20:56
  • Are you supposed to use the one set method, or your four individual, already defined ones? If the latter, which part is confusing? – OneCricketeer Nov 27 '16 at 21:06
  • This is from the rubric I'm still not super sure on this stuff I am only in 6th week of an intro to programming class(its online and instructor is afk at life.) For the 1st pet, use the default constructor and use proper mutator methods to set all variables. • For the 2nd pet, use a single parameter constructor that accepts type as an argument and use proper mutator methods for all other values. • For the 3rd pet, use a constructor that accepts all values as arguments. – Michael Anthony Judge Nov 27 '16 at 21:11
  • 1
    The below answer is correct. We aren't here to teach the basics, though. If your class isn't doing a sufficient job, then find other resources. https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html Along with that point, the instructions there say to define a *constructor* (see the link). You've defined a *method* named `set`, which is not correct. The second pet needs to be made by `new Pet(petType)`, for example – OneCricketeer Nov 27 '16 at 21:28
  • @cricket_007 your example got cut off – Michael Anthony Judge Nov 27 '16 at 21:40
  • Nope. `new Pet(petType)` was my example. – OneCricketeer Nov 27 '16 at 22:00
  • I have no idea what you are talking about but thanks for the help gonna try to find someone to chat with somewhere else i have too many questions and you seem put off by beginner questions and it doesnt say this is an expert help forum but i will go elsewhere – Michael Anthony Judge Nov 27 '16 at 22:09

2 Answers2

1

Take a look at your setters, you already have an answer there.

this.petType = petType;
this.petName = petName;
this.petAge = petAge;
this.petWeight = petWeight;

You're also probably missing isMale.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • `public void setName(String name) { this.name = name; }` – Michael Anthony Judge Nov 27 '16 at 20:52
  • Thats the example instructor gave us to help out and I realize your identifying itself but its very confusing. – Michael Anthony Judge Nov 27 '16 at 20:54
  • And any recommendations for fixing that i tried renaming the variables from the main and driver so they had separate names and change the = in the driver but just gave me an error for uninitialized variable. – Michael Anthony Judge Nov 27 '16 at 21:03
  • @MichaelAnthonyJudge You misunderstood. This answer is literally what you put at `//WHAT DO I PUT HERE` – OneCricketeer Nov 27 '16 at 21:07
  • Im still lost as hell guys I am very new and like I said online class with an instructor so everything is self taught and there are large holes in my knowledge I honestly have no clue as what to put there do i put print in there restate the same variables idk what mutators even do the books shows us examples of them but does not explain what they are or do and google gives me much more in-depth answers than I am prepared to receive or comprehend. – Michael Anthony Judge Nov 27 '16 at 21:14
  • @MichaelAnthonyJudge - just copy these 4 lines in place of your code comment – OneCricketeer Nov 27 '16 at 21:24
  • Yea i found it @cricket_007 sorry its very frustrating have to do 8-10 hours of work to code a simple program when there isnt really anyone to answer question besides google. – Michael Anthony Judge Nov 27 '16 at 21:25
0

This is an exercise for you to understand constructors and mutators. You can test your implementations with accessors.

This post covers some basics. Java - Using Accessor and Mutator methods

The official tutorial works too, but more importantly - constructors.


As for your comment, here are some guidelines.

For the 1st pet, use the default constructor and use proper mutator methods to set all variables

JMPets myPet1 = new JMPets(); // use the default constructor
System.out.println("Please enter the type of Pet #1:");
String petType = stdIn.nextLine(); 
myPet1.setType(petType); // use proper mutator methods to set all variables

// TODO: stdIn.nextLine for remainder of values. Use the 'individual' set methods

For the 2nd pet, use a single parameter constructor that accepts type as an argument and use proper mutator methods for all other values

(This won't compile until you implement this constructor)

System.out.println("Please enter the type of Pet #2:");
String petType = stdIn.nextLine(); 
JMPets myPet2 = new JMPets(petType); // TODO: Implement this

System.out.println("Please enter the name of Pet #2:"); 
String petName = stdIn.nextLine(); 
myPet2.setName(petName); // use proper mutator methods for all other values


// TODO: stdIn.nextLine for remainder of values. Use the 'individual' set methods

For the 3rd pet, use a constructor that accepts all values as arguments

Again, same problem as before, need a constructor (see very last line).

System.out.println("Please enter the type of Pet #3:");
String petType = stdIn.nextLine(); 

System.out.println("Please enter the name of Pet #3:"); 
String petName = stdIn.nextLine(); 

System.out.println("Please enter the age of " +petName+":");
int petAge = stdIn.nextInt(); 

System.out.println("Please enter the weight of "+petName+":");
double petWeight = stdIn.nextDouble(); 

JMPets mypet3 = new JMPets(petType, petName, petAge, petWeight); 

Now, if you do this all within one method, you'll get errors that variables are already defined.

For example,

String name = "bob";
String name = "sally"; // <--- Error. 'name' already defined. 

Instead, just reassign name = "sally";, no String name is needed the second time

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245