User defined class

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1632 Views - Last Post: 04 December 2009 - 07:31 AM Rate Topic: -----

#1 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

User defined class

Posted 03 December 2009 - 06:29 PM

I already created and compiled class Dog w/o errors. when I try to test it in main. I get the error cannot find symbol Dog. I have checked folder and there is Dog.class. What does the error mean?
Dog myDog = new Dog();
Dog ownerDog = new Dog();

Is This A Good Question/Topic? 0
  • +

Replies To: User defined class

#2 pulpCoder   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 25
  • Joined: 02-December 09

Re: User defined class

Posted 03 December 2009 - 06:45 PM

Do you have a constructor for Dog that does not have any parameters? If not then your program will be looking for a method to create an instance of Dog that does not exist.

it would look like this in your Dog class
public Dog(){
	 //anything you need to initialize would go here
}


Was This Post Helpful? 0
  • +
  • -

#3 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 07:02 PM

Well here is my class Dog:
public class Dog
{
	private String owner;
	private String name;
	private String breed;
	int age;
	
	public Dog()
	{
		String owner= "";  
		String name= "";  
		String breed= ""; 
		int age=0; 
	}
	
	public Dog(String newOwner, String newName, String newBreed, int newAge)
	{
		owner = newOwner;
		name = newName;
		breed  = newBreed;
		age = newAge;
	}
	
	public void changeOwner(String newOwner)
	{
		owner = newOwner;
	}
	
	public void changeName(String newName)
	{
		name = newName;
	}
	
	public void changeBreed(String newBreed)
	{
		breed = newBreed;
	}
	
	public void changeAge(int newAge)
	{
		age = newAge;
	}
	
	public String getOwner()
	{
		return owner;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String getBreed()
	{
		return breed;
	}
	
	public int getAge()
	{
		return age;
	}
	
	public void printDog()
	{
		System.out.print(owner);
		System.out.print(name);
		System.out.print(breed);
		System.out.print(age);
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 08:21 PM

can anyone help me w/ this issue?? I believe my class Dog is ok, but I don't understand the error I am getting.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: User defined class

Posted 03 December 2009 - 08:48 PM

Ummm only one problem I do see, but unrelated to your problem.
public Dog()
	{
		String owner= "";  <--- Don't redefine the variable types, already declared.
		 Should be: owner = "";
		name= "";  
		breed= "";
		age=0;
	}



I ran it and it found Dog okay. Make sure that they are in the same package!
Was This Post Helpful? 0
  • +
  • -

#6 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 09:31 PM

View PostDogstopper, on 3 Dec, 2009 - 07:48 PM, said:

Ummm only one problem I do see, but unrelated to your problem.
public Dog()
	{
		String owner= "";  <--- Don't redefine the variable types, already declared.
		 Should be: owner = "";
		name= "";  
		breed= "";
		age=0;
	}



I ran it and it found Dog okay. Make sure that they are in the same package!


maybe there is still wrong with my main()
here is part of the code for my main
import java.util.*;

public class TestProgDog
{
	static Scanner console = new Scanner(System.in);
	
	public static void main(String[] args)
	{  
		Dog myDog = new Dog();
		Dog ownerDog = new Dog();
		
		String owner;
		String name;
		String breed;
		int age;
		
		System.out.print("Please enter your name: ");
		owner = console.next();
		
		System.out.print("Please enter your dog name: ");
		name = console.next();	
		
		System.out.print("Please enter your dog breed: ");
		owner = console.next();
		
		System.out.print("Please enter your dog age: ");
		age = console.nextInt(); 
		System.out.println(); 
		
		System.out.println("myDog: " );
		myDog.printDog(); 
		System.out.println(); 
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: User defined class

Posted 03 December 2009 - 09:42 PM

These are local variable to your main() method

String owner;
String name;
String breed;
int age;

if you want to change the instance variables of your Dog object you will have to call the setters of Dog with them

myDog.changeOwner(owner);
myDog.changeName(name);
...
before you'll see a difference in

myDog.printDog();
Was This Post Helpful? 0
  • +
  • -

#8 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 10:19 PM

View Postpbl, on 3 Dec, 2009 - 08:42 PM, said:

These are local variable to your main() method

String owner;
String name;
String breed;
int age;

if you want to change the instance variables of your Dog object you will have to call the setters of Dog with them

myDog.changeOwner(owner);
myDog.changeName(name);
...
before you'll see a difference in

myDog.printDog();


I understand what ur saying....but regardless I am still getting the same error I have been getting from the start.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: User defined class

Posted 03 December 2009 - 10:23 PM

Post your updated code
Was This Post Helpful? 0
  • +
  • -

#10 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 10:23 PM

here is my first error:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: User defined class

Posted 03 December 2009 - 10:26 PM

Like ALL of it, even package declarations!
Was This Post Helpful? 0
  • +
  • -

#12 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 10:26 PM

updated code
public class Dog
{
	private String owner;
	private String name;
	private String breed;
	int age;
	
	public Dog()
	{
		owner= "";  
		name= "";  
		breed= ""; 
		age=0; 
	}
	
	public Dog(String newOwner, String newName, String newBreed, int newAge)
	{
		owner = newOwner;
		name = newName;
		breed  = newBreed;
		age = newAge;
	}
	
	public void changeOwner(String newOwner)
	{
		owner = newOwner;
	}
	
	public void changeName(String newName)
	{
		name = newName;
	}
	
	public void changeBreed(String newBreed)
	{
		breed = newBreed;
	}
	
	public void changeAge(int newAge)
	{
		age = newAge;
	}
	
	public String getOwner()
	{
		return owner;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String getBreed()
	{
		return breed;
	}
	
	public int getAge()
	{
		return age;
	}
	
	public void printDog()
	{
		System.out.print(owner);
		System.out.print(name);
		System.out.print(breed);
		System.out.print(age);
	}
}




import java.util.*;

public class TestProgDog
{
	static Scanner console = new Scanner(System.in);
	
	public static void main(String[] args)
	{   
		String owner;
		String name;
		String breed;
		int age;
		
		Dog myDog = new Dog();
		myDog.changeOwner(owner);
		myDog.changeName(name);
		myDog.changeBreed(breed); 
		myDog.changeAge(age);
							 		
		System.out.print("Please enter your name: ");
		owner = console.next();
		
		System.out.print("Please enter your dog name: ");
		name = console.next();	
		
		System.out.print("Please enter your dog breed: ");
		breed = console.next();
		
		System.out.print("Please enter your dog age: ");
		age = console.nextInt(); 
		System.out.println(); 
		
		System.out.println("myDog: " );
		myDog.printDog(); 
		System.out.println(); 
	}
}


Was This Post Helpful? 0
  • +
  • -

#13 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 10:32 PM

View PostDogstopper, on 3 Dec, 2009 - 09:26 PM, said:

Like ALL of it, even package declarations!


I just added import java.util.*; for the class Dog to test ur theory and yes same error unless I more packages that I am not aware of?
Was This Post Helpful? 0
  • +
  • -

#14 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: User defined class

Posted 03 December 2009 - 10:32 PM

		String owner;
		String name;
		String breed;
		int age;
		
		Dog myDog = new Dog();
		myDog.changeOwner(owner);
		myDog.changeName(name);
		myDog.changeBreed(breed); 
		myDog.changeAge(age);
	 

The variables owner, name. breed, age have not been initialized
you cannot call the setters of a class with variables that contain nothing
Was This Post Helpful? 0
  • +
  • -

#15 gemoney   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: User defined class

Posted 03 December 2009 - 11:21 PM

View Postpbl, on 3 Dec, 2009 - 09:32 PM, said:

		String owner;
		String name;
		String breed;
		int age;
		
		Dog myDog = new Dog();
		myDog.changeOwner(owner);
		myDog.changeName(name);
		myDog.changeBreed(breed); 
		myDog.changeAge(age);
	 

The variables owner, name. breed, age have not been initialized
you cannot call the setters of a class with variables that contain nothing


well, I have initialized the variables. The variables contain nothing b/c I have to get use inputs in order to assign it to the variables. I am still getting the same error pointing back to class Dog. I am outta ideas in that and lol...and it is only the beginning of the program. My question is shouldn't the setter still output empty string if nothing is input?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2