7 Replies - 550 Views - Last Post: 22 October 2013 - 09:19 AM Rate Topic: -----

#1 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

error with class object

Posted 21 October 2013 - 03:44 PM

I am taking Java for the first time, though I have a background of C++. Because of C++, I understand how Java works pretty well. For my most recent assignment though, I am having trouble understanding how to resolve something to do with my object.

Here is my class:
// This program belongs to Nichole Moore.

/**
    Pet CLASS
		**/

public class PetClass
{
	private String name;
	private String type;
	private int age;

	// Constructor.
	public PetClass(String petName, String petType, int petAge)
	{
		name = petName;
		type = petType;
		age = petAge;
	}

	// Sets.
	public void setName(String petName)
	{
		name = petName;
	}

	public void setType(String petType)
	{
		type = petType;
	}

	public void setAge(int petAge)
	{
		age = petAge;
	}

	// Gets.
	public String getName()
	{
		return name;
	}

	public String getType()
	{
		return type;
	}

	public int getAge()
	{
		return age;
	}
}




Here is the main (still being worked on):
// This program belongs to Nichole Moore.

/**
    Pet CLASS
		**/

import java.util.Scanner; // Scanner class for user input.

public class PetDemo
{
	public static void main(String[] args)
	{
		// Declaration of variables.
		String pName;
		String pType;
		int pAge;

		// Create pet object.
		PetClass pet = new PetClass();

		// Output name and date.
		System.out.println("Nichole Moore		October 17, 2013\n\n");

		// Create a scanner object to read the user's input.
		Scanner keyboard = new Scanner(System.in);

		// Ask the user for pet data.
		System.out.println("Enter the name of your pet: ");
		pName = keyboard.nextLine();
		pet.setName(pName);

		System.out.println("Enter the type of your pet: ");
		pType = keyboard.nextLine();
		pet.setType(pType);

		System.out.println("Enter the age of your pet: ");
		pAge = keyboard.nextInt();
		pet.setAge(pAge);
	}
}{/code]



Here is my error code:
[code]E:\School Stuff\Fall 2013\Intro to Java\Ch 3b\PetDemo.java:19: cannot find symbol
symbol  : constructor PetClass()
location: class PetClass
		PetClass pet = new PetClass();
		               ^
1 error

Tool completed with exit code 1



So far the only way I have figured out to get rid of that error code is by initializing the values in the class through the object, though for this specific assignment the data must be inputted by the user first.

Any advice is very much appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: error with class object

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: error with class object

Posted 21 October 2013 - 03:52 PM

Your PetClass constructor takes three parameters, and you supply none of them
An example of how you should initialize your PetClass would be
PetClass pet = new PetClass("Pet name", "Pet type", 5);


I would also advice you name your class Pet instead of PetClass. No reason to put the Class postfix on all your classes
Was This Post Helpful? 0
  • +
  • -

#3 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: error with class object

Posted 21 October 2013 - 03:57 PM

I agree with you by naming it "Pet" rather than "PetClass" but we are required to name the files by Class and Demo so the teacher can understand the files we send to her.

I realized how to initialize the class like that before, but I need to put data in from the user from the keyboard, so if I initialized it, it would be doing the assignment wrong. :/
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: error with class object

Posted 21 October 2013 - 04:08 PM

View Post4N63L-Z3R0, on 21 October 2013 - 10:57 PM, said:

I agree with you by naming it "Pet" rather than "PetClass" but we are required to name the files by Class and Demo so the teacher can understand the files we send to her.

I realized how to initialize the class like that before, but I need to put data in from the user from the keyboard, so if I initialized it, it would be doing the assignment wrong. ://>

You could either store the input and initialize the pet at a later point, or you could add a default constructor which doesnt require any parameters
Was This Post Helpful? 1
  • +
  • -

#5 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: error with class object

Posted 21 October 2013 - 04:12 PM

I tried initializing it at a later point, but I don't think I was writing it correctly. I tried something like pet.PetClass(pName, pType, pAge) and just got more errors...

How does a default constructor work? Does it have the same purpose as a regular constructor?

This post has been edited by 4N63L-Z3R0: 21 October 2013 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: error with class object

Posted 21 October 2013 - 04:22 PM

pet.PetClass(pName, pType, pAge)


That would be wrong. I've shown you how to create an instance of a PetClass in an earlier post
Was This Post Helpful? 0
  • +
  • -

#7 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: error with class object

Posted 21 October 2013 - 04:26 PM

Yeah I see, sorry. What I meant was how exactly do I assign the data that the user would input into the class? I am trying to do that here:

// Ask the user for pet data.
28
        System.out.println("Enter the name of your pet: ");
29
        pName = keyboard.nextLine();
30
        pet.setName(pName); // here
31
 
32
        System.out.println("Enter the type of your pet: ");
33
        pType = keyboard.nextLine();
34
        pet.setType(pType); // here
35
 
36
        System.out.println("Enter the age of your pet: ");
37
        pAge = keyboard.nextInt(); // and here
38
        pet.setAge(pAge);

Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: error with class object

Posted 22 October 2013 - 09:19 AM

Forget about the pet until you have all of the input stored in variables
It is very straight forward, and I dont know why you keep on trying to use the set methods when you could just initialize the pet later on
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1