Dog myDog = new Dog(); Dog ownerDog = new Dog();
19 Replies - 1632 Views - Last Post: 04 December 2009 - 07:31 AM
#1
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?
Replies To: User defined class
#2
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
it would look like this in your Dog class
public Dog(){
//anything you need to initialize would go here
}
#3
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);
}
}
#4
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.
#5
Re: User defined class
Posted 03 December 2009 - 08:48 PM
Ummm only one problem I do see, but unrelated to your problem.
I ran it and it found Dog okay. Make sure that they are in the same package!
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!
#6
Re: User defined class
Posted 03 December 2009 - 09:31 PM
Dogstopper, on 3 Dec, 2009 - 07:48 PM, said:
Ummm only one problem I do see, but unrelated to your problem.
I ran it and it found Dog okay. Make sure that they are in the same package!
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();
}
}
#7
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();
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();
#8
Re: User defined class
Posted 03 December 2009 - 10:19 PM
pbl, 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();
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.
#11
Re: User defined class
Posted 03 December 2009 - 10:26 PM
Like ALL of it, even package declarations!
#12
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();
}
}
#13
Re: User defined class
Posted 03 December 2009 - 10:32 PM
#14
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
#15
Re: User defined class
Posted 03 December 2009 - 11:21 PM
pbl, 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?

New Topic/Question
Reply



MultiQuote




|