Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: toString method

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default toString method

    ...


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: toString method

    Quote Originally Posted by pelane View Post
    I'm having trouble with this and I don't know what I'm doing wrong..

        public static void main(String[] args) {
     
        	Person person = new Person("Jonda McCord", "123 Edgewood Avenue", "404-555-1212", "jondamccord@yahoo.com");
    		Person student = new Student("Vaneshia Fields", "405 Morris Street", "770-510-1232", "vfields@gmail.com", "Junior");
    		Person employee = new Employee("Chaitanya Sambhara", "70 Broad Street", "404-898-1199", "csambhara@gsu.edu","RCB 1324","50000.00","01-16-2012");
    		Person faculty = new Faculty("Jill Johnson", "91 Park Ave", "678-211-3783", "jjohnson@gsu.edu","RCB 801","22000.00","08-25-2011","1:00-3:00","Professor");
    		Person staff = new Staff("Patricia Howell", "210 Decatur Street", "700-212-1012", "phowell@gsu.edu","RCB 422","10000.00","05-10-2011","Teacher Assistant");
     
    		System.out.println(person.toString());
    		System.out.println(student.toString());
    		System.out.println(employee.toString());
    		System.out.println(faculty.toString());
    		System.out.println(staff.toString());
        }
     
    	public static class Person{
        		public String name;
    			public String address;
    			public String phone;
    			public String email;
    			public Person(String name, String address, String phone, String email) {
    			this.name = name;
    			this.address = address;
    			this.phone = phone;
    			this.email = email;
    			}
        		public String toString() {
    				return ("Name: " + name + "Address: " +adress + "Phone #: " + phone + "Email: " + email);
        		}
        }
     
    	public static class Student extends Person{
    			public final String status;
    			public Student(String name, String address, String phone, String email, String status){
    				super(name, address, phone, email, status);
    				this.status = status;
    			}
    			public String toString(){
    				return (super.toString() + "Status: " + status);
    			}
    	}
     
    	public static class Employee extends Person{
    			public String office;
    			public double salary;
    			public String dateHired;
    			public Employee(String name, String address, String phone, String email, String office, double salary, String dateHired){
    				super(name, address, phone, email, office, salary, dateHired);
    				this.office=office;
    				this.salary=salary;
    				this.dateHired=dateHired;
    			}
    			public String toString(){
    				return (super.toString() + "Office: " +  office +"Salary: " + salary + "Date Hired: "+dateHired);
    			}
    	}
     
    	public static class Faculty extends Employee{
    			public String officeHours;
    			public String rank;
    			public Faculty(String name, String address, String phone, String email, String office, double salary, String dateHired, String officeHours, String rank){
    				super(name, address, phone, email, office, salary, dateHired, officeHours, rank);
    				this.officeHours=officeHours;
    				this.rank=rank;
    			}
    			public String toString(){
    				return (super.toString() + "Office Hours: " + officeHours +"Rank: " + rank);
    			}
    	}
     
    	public static class Staff extends Employee{
    			public String title;
    			public Staff(String name, String address, String phone, String email, String title){
    				super(name, address, phone, email, title);
    				this.title=title;
    			}
    			public String toString(){
    				return (super.toString() + "Title: " + title);
    			}
    	}
    First of all, Person doesn't have a variable called status. So you can't call it in super. Well you can, but it will set something else to the value of status, not what you wanted. Also, the superclass of Staff doesn't have a title.

    I compiled, under a file called Testing.java, and got 8 errors:

     

    Testing.java:7: cannot find symbol
    symbol : constructor Employee(java.lang.String,java.lang.String,java.la ng.String,java.lang.String,java.lang.String,java.l ang.String,java.lang.String)
    location: class Testing.Employee
    Person employee = new Employee("Chaitanya Sambhara", "70 Broad Street", "404-898-1199", "csambhara@gsu.edu","RCB 1324","50000.00","01-16-2012");
    ^
    Testing.java:8: cannot find symbol
    symbol : constructor Faculty(java.lang.String,java.lang.String,java.lan g.String,java.lang.String,java.lang.String,java.la ng.String,java.lang.String,java.lang.String,java.l ang.String)
    location: class Testing.Faculty
    Person faculty = new Faculty("Jill Johnson", "91 Park Ave", "678-211-3783", "jjohnson@gsu.edu","RCB 801","22000.00","08-25-2011","1:00-3:00","Professor");
    ^
    Testing.java:9: cannot find symbol
    symbol : constructor Staff(java.lang.String,java.lang.String,java.lang. String,java.lang.String,java.lang.String,java.lang .String,java.lang.String,java.lang.String)
    location: class Testing.Staff
    Person staff = new Staff("Patricia Howell", "210 Decatur Street", "700-212-1012", "phowell@gsu.edu","RCB 422","10000.00","05-10-2011","Teacher Assistant");
    ^
    Testing.java:30: cannot find symbol
    symbol : variable adress
    location: class Testing.Person
    return ("Name: " + name + "Address: " +adress + "Phone #: " + phone + "Email: " + email);
    ^
    Testing.java:37: cannot find symbol
    symbol : constructor Person(java.lang.String,java.lang.String,java.lang .String,java.lang.String,java.lang.String)
    location: class Testing.Person
    super(name, address, phone, email, status);
    ^
    Testing.java:50: cannot find symbol
    symbol : constructor Person(java.lang.String,java.lang.String,java.lang .String,java.lang.String,java.lang.String,double,j ava.lang.String)
    location: class Testing.Person
    super(name, address, phone, email, office, salary, dateHired);
    ^
    Testing.java:64: cannot find symbol
    symbol : constructor Employee(java.lang.String,java.lang.String,java.la ng.String,java.lang.String,java.lang.String,double ,java.lang.String,java.lang.String,java.lang.Strin g)
    location: class Testing.Employee
    super(name, address, phone, email, office, salary, dateHired, officeHours, rank);
    ^
    Testing.java:76: cannot find symbol
    symbol : constructor Employee(java.lang.String,java.lang.String,java.la ng.String,java.lang.String,java.lang.String)
    location: class Testing.Employee
    super(name, address, phone, email, title);




    You misspelled address for one of those errors.

    Under salary, you put it in quotes so now it's a String.

    You can't go using super unless you're referring directly to the same spot in the parent class.

    For instance,

    public class Point1D
    {
    private int x;
     
    public Point1D()
    {
    setX(0);
    }
     
    public Point1D(int x)
    {
    setX(x);
    }
     
    public void setX(int x)
    {
    this.x = x;
    }
     
    public int getX()
    {
    return x;
    }
     
    public String toString()
    {
    return ("X: " + getX());
    }
    }
     
    public class Point2D extends Point1D
    {
    private int y;
     
    public Point2D()
    {
    super();
    setY(0);
    }
     
    public Point2D(int x, int y)
    {
    super(x);
    setY(y);
    }
     
    public Point2D(int y)
    {
    super();
    setY(y);
    }
     
    public void setY(int y)
    {
    this.y = y;
    }
     
    public int getY()
    {
    return y;
    }
     
    public String toString()
    {
     
    return (super.toString() + " , Y: " + getY());
    }
    }

     
    public class Point3D extends Point2D
    {
    private int z;
     
    public Point3D()
    {
    super();
    setZ(0);
    }
     
    public Point3D(int x, int y, int z)
    {
    super(x,y);
    setZ(z);
    }
     
    public Point3D(int z)
    {
    super();
    setZ(z);
    }
     
    public void setZ(int z)
    {
    this.z = z;
    }
     
    public int getZ()
    {
    return z;
    }
     
    public String toString()
    {
    return (super.toString() + ", Z: " + getZ());
    }
    }

    I've corrected 4 of the 8 errors in your code. Try and see if you can fix the other 4 now.

       public class Testing
       {
          public static void main(String[] args) {
     
             Person person = new Person("Jonda McCord", "123 Edgewood Avenue", "404-555-1212", "jondamccord@yahoo.com");
             Person student = new Student("Vaneshia Fields", "405 Morris Street", "770-510-1232", "vfields@gmail.com", "Junior");
             Person employee = new Employee("Chaitanya Sambhara", "70 Broad Street", "404-898-1199", "csambhara@gsu.edu","RCB 1324",50000.00,"01-16-2012");
             Person faculty = new Faculty("Jill Johnson", "91 Park Ave", "678-211-3783", "jjohnson@gsu.edu","RCB 801",22000.00,"08-25-2011","1:00-3:00","Professor");
             Person staff = new Staff("Patricia Howell", "210 Decatur Street", "700-212-1012", "phowell@gsu.edu","RCB 422",10000.00,"05-10-2011","Teacher Assistant");
     
             System.out.println(person.toString());
             System.out.println(student.toString());
             System.out.println(employee.toString());
             System.out.println(faculty.toString());
             System.out.println(staff.toString());
          }
     
          public static class Person{
             public String name;
             public String address;
             public String phone;
             public String email;
             public Person(String name, String address, String phone, String email) {
                this.name = name;
                this.address = address;
                this.phone = phone;
                this.email = email;
             }
             public String toString() {
                return ("Name: " + name + "Address: " +address + "Phone #: " + phone + "Email: " + email);
             }
          }
     
          public static class Student extends Person{
             public final String status;
             public Student(String name, String address, String phone, String email, String status){
                super(name, address, phone, email);
                this.status = status;
             }
             public String toString(){
                return (super.toString() + "Status: " + status);
             }
          }
     
          public static class Employee extends Person{
             public String office;
             public double salary;
             public String dateHired;
             public Employee(String name, String address, String phone, String email, String office, double salary, String dateHired){
                super(name, address, phone, email, office, salary, dateHired);
                this.office=office;
                this.salary=salary;
                this.dateHired=dateHired;
             }
             public String toString(){
                return (super.toString() + "Office: " + office +"Salary: " + salary + "Date Hired: "+dateHired);
             }
          }
     
          public static class Faculty extends Employee{
             public String officeHours;
             public String rank;
             public Faculty(String name, String address, String phone, String email, String office, double salary, String dateHired, String officeHours, String rank){
                super(name, address, phone, email, office, salary, dateHired, officeHours, rank);
                this.officeHours=officeHours;
                this.rank=rank;
             }
             public String toString(){
                return (super.toString() + "Office Hours: " + officeHours +"Rank: " + rank);
             }
          }
     
          public static class Staff extends Employee{
             public String title;
             public Staff(String name, String address, String phone, String email, String title){
                super(name, address, phone, email, title);
                this.title=title;
             }
             public String toString(){
                return (super.toString() + "Title: " + title);
             }
          } 
     
       }

    Also, any particular reason why status is final?
    Last edited by javapenguin; April 23rd, 2012 at 12:20 AM.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: toString method

    .....

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: toString method

    If status is a constant, then why are you setting it with a parameter?

Similar Threads

  1. [SOLVED] Question about using the toString method.
    By iDizzle in forum Object Oriented Programming
    Replies: 12
    Last Post: April 9th, 2012, 09:24 AM
  2. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  4. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM