PDA

Click to See Complete Forum and Search --> : Cannot Find Symbol Constructor Fun Time!


lonlonmalon
Jun 3rd, 2007, 09:32 AM
Alright, so I am making a program with two parts - the client program GetDogs, and the fun and original one it calls upon, Dog. Basically, GetDogs will create new members of the Dog class and will give it random variables if they are not previously supplied.

Unfortunately, I cannot compile GetDogs.
Here is the code for GetDogs:

import java.io.*;

public class GetDogs {

public static void main(String[] args) {

String newAge;
int age;

BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));
System.out.print("What age would you like Rover the Spaniel to be? ");

newAge = myInput.readLine();
age = Integer.parseInt(newAge);
Dog dog1 = new Dog("Rover", "Spaniel", "Black", "Mediocre", "Emily P.", 3, 4, 9, age);

Dog dog2 = new Dog("Spot", "Border Collie", "Brown", "Great", "Mr. Cop");

System.out.println("How about some doggie details?");

System.out.println(dog1);

System.out.println(dog2);

}


}

And the code for Dog:

public class Dog {

//variables for characteristics

String dogName;

String dogBreed;

String dogColour;

String ownerName;

String health;

int aggression;

int age;

int hunger;





//constructor

public Dog(String dogName, String dogBreed, String dogColour, String health, String ownerName, int aggression, int hunger, int age){

//assign characteristics from parameters

this.dogName = dogName;

this.dogBreed = dogBreed;

this.aggression = aggression;

this.hunger = hunger;

this.age = age;

this.dogColour = dogColour;

this.health = health;

this.ownerName = ownerName;

}



//alternate constructor

public Dog(String dogName, String dogBreed, int age){

//set characteristics from parameters
String newAge;

this.dogName = dogName;

this.dogBreed = dogBreed;

this.dogColour = dogColour;

this.health = health;

this.ownerName = ownerName;

//set other characteristics randomly

this.aggression = (int)(Math.random()*10) + 1;

this.hunger = (int)(Math.random()*10) + 1;


this.age = (int) (Math.random() * 13) + 1;

}



//methods to access characteristics

public void setAggression(int factor){

this.aggression = factor;

}

public void setHunger(int factor){

this.hunger = factor;

}
public String setDogColour() {

return this.dogColour;
}
public String setOwnerName() {

return this.ownerName;
}
public String getHealth() {
return this.health;
}

public String getBreed(){

return this.dogBreed;

}

public String getName(){

return this.dogName;

}

public int getAggression(){

return this.aggression;

}

public int getHunger(){

return this.hunger;

}
public int getAge() {
return this.age;
}



//methods for behaviour or interactions

public void barkFriendly(){

System.out.println("Arf! Arf!");

}

public void barkAngry(){

System.out.println("GRR! RRRFFF!");

}

//method to display info

public String toString(){

String dogData1 = "Name: " + this.dogName + " Breed: " + this.dogBreed + "\n";

String dogData2 = "Aggression factor: " + this.aggression + "\n";

String dogData3 = "Hunger factor: " + this.hunger + "\n";

String dogData4 = "Age: " + this.age + "\n";

String dogData = dogData1 + dogData2 + dogData3 + dogData4;



return dogData;

}
}

When I attempt to compile the GetDogs file, I get these two errors:

cannot find symbol constructor Dog(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,int,int ,int)

and

cannot find symbol constructor Dog(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)

both of which occur where I declare dog1 and dog2. The Dog class-ish-area compiles without errors.

Any help you can offer will be greatly appreciated!

System_Error
Jun 3rd, 2007, 10:42 AM
Your dog constructor takes 5 strings and three integers:

Dog(String dogName, String dogBreed, String dogColour, String health, String ownerName, int aggression, int hunger, int age)


When you create a dog, it must match this constructor and both dog objects you create do not match:


Dog dog1 = new Dog("Rover", "Spaniel", "Black", "Mediocre", "Emily P.", 3, 4, 9, age); //-takes 8 parameters, not 9

Dog dog2 = new Dog("Spot", "Border Collie", "Brown", "Great", "Mr. Cop"); //-takes 8 parameters, not 5

lonlonmalon
Jun 3rd, 2007, 12:16 PM
Sorry, but being a Java newbie, could you explain what I need to do to fix this problem a little more? I am afraid I do not understand completely...

Are you saying to take out some of the bracketed information there?

eranga262154
Jun 4th, 2007, 06:09 AM
Just go through the second code fragment of System Error post. He comment that. In simple word there is no compatibility of arguments you set.

lonlonmalon
Jun 16th, 2007, 10:46 AM
Hi again...Same person, same problem, different code. This time involving super classes and such.

I get the cannot find symbol constructor error, but am unsure as to why (once again). I am realy getting irritable with this error.

This code basically has a client part, a class, and a subclass. The class works perfectly. It's when I get to the subclass that I find problems.

Basically, the user enters a bunch of information on an employee, and the program will spit it back out at them. I am trying to have two separate constructors - one uses the superclass, one doesn't. Somehow, the superclass-using constructor works, but the constructor that doesn't does not.

Here is the main, client program:

Scanner myInput = new Scanner (System.in);

System.out.println("Would you like to build on the information of an employee, or just have worker properties? <e or w>");
String empAndWork = myInput.next();

if (empAndWork == "e")
{
System.out.print("What is the worker's name?");
String name = myInput.next();

System.out.print("What location does the worker work at?");
String location = myInput.next();

System.out.print("What department does the worker work in?");
String department = myInput.next();

System.out.print("What is the worker's ID number?");
int idNumber = myInput.nextInt();

System.out.print("What is the worker's hourly wage?");
double wage = myInput.nextDouble();

System.out.print("What is the worker's average hours worked per week?");
double hours = myInput.nextDouble();

System.out.print("What is the worker's shift type? (Full- or part-time)");
String shiftType = myInput.next();

System.out.print("Does the worker work the day shift or the night shift?");
String dayOrNightShift = myInput.next();

System.out.print("What is the worker's salary?");
int salary = myInput.nextInt();

Worker employee1 = new Worker (hours, wage, name, department, location, shiftType, dayOrNightShift, idNumber, salary);

}
else
{
System.out.print("What is the worker's name?");
String empName = myInput.next();

System.out.print("What is the worker's hourly wage?");
int wage = myInput.nextInt();

System.out.print("What is the worker's average hours worked per week?");
int hours = myInput.nextInt();

Worker employee1 = new Worker (wage, hours, empName);
}



Here is the subclass:


public class Worker extends Employee
{
int hours;
int wage;
String empName;

public Worker(int hours, int wage, String name, String department, String location, String shiftType, String dayOrNightShift, int idNumber, int salary)
{
super(name, department, location, shiftType, dayOrNightShift, idNumber, salary);

this.hours = hours;
this.wage = wage;


}
public Worker(int wage, int hours, String empName)
{

this.hours = hours;
this.wage = wage;
this.empName = empName;

}
public void setHours(int factor)
{
this.hours = factor;
}

public void setWage(int factor)
{
this.wage = factor;
}

public String setEmpName()
{
return this.empName;
}


public int getHours()
{
return this.hours;
}

public int getWage()
{
return this.wage;
}

public String getEmpName()
{
return this.empName;
}


public String toString(){

String employeeData1 = "Name: " + this.name + '\n';
String employeeData2 = "ID Number: " + this.idNumber + '\n';
String employeeData3 = "Salary: " + this.salary + '\n';
String employeeData4 = "Location: " + this.location + '\n';
String employeeData5 = "Department: " + this.department + '\n';
String employeeData6 = "Shift Type: " + this.shiftType + '\n';
String employeeData7 = "Day or Night Shift: " + this.dayOrNightShift + '\n';
String employeeData8 = "Hourly Wage: " + this.wage + '\n';
String employeeData9 = "Hours per Week: " + this.hours + '\n';


String employeeData = employeeData1 + employeeData2 + employeeData3 + employeeData4 + employeeData5 + employeeData6 + employeeData7 + employeeData8 + employeeData9;

return employeeData;
}
}



And here is the superclass, in case you need it:

class Employee {

String name;
String department;
String location;
String shiftType;
String dayOrNightShift;
int idNumber;
int salary;

public Employee(String name, String department, String location, int idNumber)
{
int randomNumber;

this.name = name;
this.department = department;
this.location = location;
this.idNumber = idNumber;

randomNumber = (int)(Math.random()*2) + 1;
if (randomNumber == 1)
this.shiftType = "Full-Time";
else
this.shiftType = "Part-Time";

randomNumber = (int)(Math.random()*2) + 1;
if (randomNumber == 1)
this.dayOrNightShift = "Night";
else
this.dayOrNightShift = "Day";

this.salary = (int)(Math.random()*100000) + 1;

}

public Employee(String name, String department, String location, String shiftType, String dayOrNightShift, int idNumber, int salary)
{
this.name = name;
this.department = department;
this.location = location;
this.shiftType = shiftType;
this.dayOrNightShift = dayOrNightShift;
this.idNumber = idNumber;
this.salary = salary;
}

public void setIdNumber(int factor)
{
this.idNumber = factor;
}

public void setSalary(int factor)
{
this.salary = factor;
}

public String setName()
{
return this.name;
}

public String setDepartment()
{
return this.department;
}

public String setLocation()
{
return this.location;
}

public String setShiftType()
{
return this.shiftType;
}

public String setDayOrNightShift()
{
return this.dayOrNightShift;
}



public int getIdNumber()
{
return this.idNumber;
}

public int getSalary()
{
return this.salary;
}

public String getName()
{
return this.name;
}

public String getDepartment()
{
return this.department;
}

public String getLocation()
{
return this.location;
}

public String getShiftType()
{
return this.shiftType;
}

public String getDayOrNightShift()
{
return this.dayOrNightShift;
}



public String toString()
{
String employeeData1 = "Name: " + this.name + '\n';
String employeeData2 = "ID Number: " + this.idNumber + '\n';
String employeeData3 = "Salary: " + this.salary + '\n';
String employeeData4 = "Location: " + this.location + '\n';
String employeeData5 = "Department: " + this.department + '\n';
String employeeData6 = "Shift Type: " + this.shiftType + '\n';
String employeeData7 = "Day or Night Shift: " + this.dayOrNightShift + '\n';

String employeeData = employeeData1 + employeeData2 + employeeData3 + employeeData4 + employeeData5 + employeeData6 + employeeData7;

return employeeData;
}




}


Thanks for your help (once again).